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.logger
parent
b34cad17d2
commit
d948001d94
13
request.go
13
request.go
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
|
@ -54,6 +55,15 @@ func (a *AddrSpec) String() string {
|
||||||
return fmt.Sprintf("%s:%d", a.IP, a.Port)
|
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
|
// A Request represents request received by a server
|
||||||
type Request struct {
|
type Request struct {
|
||||||
// Protocol version
|
// Protocol version
|
||||||
|
@ -158,14 +168,13 @@ func (s *Server) handleConnect(ctx context.Context, conn conn, req *Request) err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attempt to connect
|
// Attempt to connect
|
||||||
addr := (&net.TCPAddr{IP: req.realDestAddr.IP, Port: req.realDestAddr.Port}).String()
|
|
||||||
dial := s.config.Dial
|
dial := s.config.Dial
|
||||||
if dial == nil {
|
if dial == nil {
|
||||||
dial = func(ctx context.Context, net_, addr string) (net.Conn, error) {
|
dial = func(ctx context.Context, net_, addr string) (net.Conn, error) {
|
||||||
return net.Dial(net_, addr)
|
return net.Dial(net_, addr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
target, err := dial(ctx, "tcp", addr)
|
target, err := dial(ctx, "tcp", req.realDestAddr.Address())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg := err.Error()
|
msg := err.Error()
|
||||||
resp := hostUnreachable
|
resp := hostUnreachable
|
||||||
|
|
Loading…
Reference in New Issue