From d948001d942adcb2dd007ef451e139ae35866607 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BChler?= Date: Sun, 14 Aug 2016 12:11:16 +0200 Subject: [PATCH] support not resolving FQDN Although one can put the FQDN into the context and retrieve it later, this makes it more straightforward. Usecase: forwarding to another (SOCKS-)proxy, filtering/routing based on FQDN. --- request.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/request.go b/request.go index e3eebff..6be8435 100644 --- a/request.go +++ b/request.go @@ -4,6 +4,7 @@ import ( "fmt" "io" "net" + "strconv" "strings" "golang.org/x/net/context" @@ -54,6 +55,15 @@ func (a *AddrSpec) String() string { return fmt.Sprintf("%s:%d", a.IP, a.Port) } +// Address returns a string suitable to dial; prefer returning IP-based +// address, fallback to FQDN +func (a AddrSpec) Address() string { + if 0 != len(a.IP) { + return net.JoinHostPort(a.IP.String(), strconv.Itoa(a.Port)) + } + return net.JoinHostPort(a.FQDN, strconv.Itoa(a.Port)) +} + // A Request represents request received by a server type Request struct { // Protocol version @@ -158,14 +168,13 @@ func (s *Server) handleConnect(ctx context.Context, conn conn, req *Request) err } // Attempt to connect - addr := (&net.TCPAddr{IP: req.realDestAddr.IP, Port: req.realDestAddr.Port}).String() dial := s.config.Dial if dial == nil { dial = func(ctx context.Context, net_, addr string) (net.Conn, error) { return net.Dial(net_, addr) } } - target, err := dial(ctx, "tcp", addr) + target, err := dial(ctx, "tcp", req.realDestAddr.Address()) if err != nil { msg := err.Error() resp := hostUnreachable