Skip to content

Commit d948001

Browse files
author
Stefan Bühler
committedAug 14, 2016
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.
1 parent b34cad1 commit d948001

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed
 

‎request.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io"
66
"net"
7+
"strconv"
78
"strings"
89

910
"golang.org/x/net/context"
@@ -54,6 +55,15 @@ func (a *AddrSpec) String() string {
5455
return fmt.Sprintf("%s:%d", a.IP, a.Port)
5556
}
5657

58+
// Address returns a string suitable to dial; prefer returning IP-based
59+
// address, fallback to FQDN
60+
func (a AddrSpec) Address() string {
61+
if 0 != len(a.IP) {
62+
return net.JoinHostPort(a.IP.String(), strconv.Itoa(a.Port))
63+
}
64+
return net.JoinHostPort(a.FQDN, strconv.Itoa(a.Port))
65+
}
66+
5767
// A Request represents request received by a server
5868
type Request struct {
5969
// Protocol version
@@ -158,14 +168,13 @@ func (s *Server) handleConnect(ctx context.Context, conn conn, req *Request) err
158168
}
159169

160170
// Attempt to connect
161-
addr := (&net.TCPAddr{IP: req.realDestAddr.IP, Port: req.realDestAddr.Port}).String()
162171
dial := s.config.Dial
163172
if dial == nil {
164173
dial = func(ctx context.Context, net_, addr string) (net.Conn, error) {
165174
return net.Dial(net_, addr)
166175
}
167176
}
168-
target, err := dial(ctx, "tcp", addr)
177+
target, err := dial(ctx, "tcp", req.realDestAddr.Address())
169178
if err != nil {
170179
msg := err.Error()
171180
resp := hostUnreachable

0 commit comments

Comments
 (0)
Please sign in to comment.