Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a simple retry to re-resolve the DNS host if the IP address has changed [ Feedback Required ] #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/tcp-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func main() {
logger.Info("Unwrapping TLS")
p = proxy.NewTLSUnwrapped(conn, laddr, raddr, *remoteAddr)
} else {
p = proxy.New(conn, laddr, raddr)
p = proxy.New(conn, laddr, raddr, *remoteAddr)
}

p.Matcher = matcher
Expand Down
33 changes: 24 additions & 9 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Proxy struct {
errsig chan bool
tlsUnwrapp bool
tlsAddress string
fqdnAddress string

Matcher func([]byte)
Replacer func([]byte) []byte
Expand All @@ -28,22 +29,23 @@ type Proxy struct {

// New - Create a new Proxy instance. Takes over local connection passed in,
// and closes it when finished.
func New(lconn *net.TCPConn, laddr, raddr *net.TCPAddr) *Proxy {
func New(lconn *net.TCPConn, laddr, raddr *net.TCPAddr, addr string) *Proxy {
return &Proxy{
lconn: lconn,
laddr: laddr,
raddr: raddr,
erred: false,
errsig: make(chan bool),
Log: NullLogger{},
lconn: lconn,
laddr: laddr,
raddr: raddr,
fqdnAddress: addr,
erred: false,
errsig: make(chan bool),
Log: NullLogger{},
}
}

// NewTLSUnwrapped - Create a new Proxy instance with a remote TLS server for
// which we want to unwrap the TLS to be able to connect without encryption
// locally
func NewTLSUnwrapped(lconn *net.TCPConn, laddr, raddr *net.TCPAddr, addr string) *Proxy {
p := New(lconn, laddr, raddr)
p := New(lconn, laddr, raddr, addr)
p.tlsUnwrapp = true
p.tlsAddress = addr
return p
Expand All @@ -64,10 +66,23 @@ func (p *Proxy) Start() {
} else {
p.rconn, err = net.DialTCP("tcp", nil, p.raddr)
}
if err != nil {
if err != nil && !p.tlsUnwrapp {
p.Log.Warn("Remote connection failed: %s, retry DNS resolution", err)
p.raddr, err = net.ResolveTCPAddr("tcp", p.fqdnAddress)
if err != nil {
p.Log.Warn("Remote connection failed: %s", err)
return
}
p.rconn, err = net.DialTCP("tcp", nil, p.raddr)
if err != nil {
p.Log.Warn("Remote connection failed: %s", err)
return
}
} else if err != nil && p.tlsUnwrapp {
p.Log.Warn("Remote connection failed: %s", err)
return
}

defer p.rconn.Close()

//nagles?
Expand Down