Skip to content

Commit

Permalink
Add a simple retry to re-resolve the DNS host if the IP address has c…
Browse files Browse the repository at this point in the history
…hanged in DNS for non-TLS connections
  • Loading branch information
berglh committed Aug 25, 2020
1 parent 69cade9 commit eac32a1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
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

0 comments on commit eac32a1

Please sign in to comment.