Skip to content

Commit ef9f217

Browse files
committed
updated resolver to be context aware
1 parent bac6b52 commit ef9f217

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

cmd/main.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"os"
1212
"strings"
1313

14-
"github.com/haxii/socks5"
1514
"github.com/lanrat/stargate"
1615
)
1716

@@ -28,8 +27,6 @@ var (
2827
var (
2928
// l is the logger instance used throughout the application
3029
l = log.New(os.Stderr, "", log.LstdFlags)
31-
// resolver is the DNS resolver instance for SOCKS5 connections
32-
resolver socks5.NameResolver
3330
// version is the application version string, set at build time
3431
version = "dev"
3532
)
@@ -122,11 +119,6 @@ func main() {
122119
*listenAddr = ":" + *listenAddr
123120
}
124121

125-
// prep network aware resolver
126-
resolver = &DNSResolver{
127-
network: getCIDRNetwork(parsedNetwork),
128-
}
129-
130122
// run subnet proxy server
131123
l.Printf("Starting subnet egress proxy %s\n", *listenAddr)
132124
err = runRandomSubnetProxy(*listenAddr, parsedNetwork, *subnetBits)
@@ -146,12 +138,3 @@ func v(format string, a ...any) {
146138
func showVersion() string {
147139
return fmt.Sprintf("Version: %s", version)
148140
}
149-
150-
// getCIDRNetwork returns "ip4" for IPv4 addresses or "ip6" for IPv6 addresses.
151-
// This is used for DNS resolution context.
152-
func getCIDRNetwork(prefix netip.Prefix) string {
153-
if prefix.Addr().Is4() {
154-
return "ip4"
155-
}
156-
return "ip6"
157-
}

cmd/socks.go

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func runRandomSubnetProxy(listenAddr string, parsedNetwork netip.Prefix, cidrSiz
3636

3737
conf := &socks5.Config{
3838
Logger: l,
39-
Resolver: resolver,
39+
Resolver: NewDNSResolver(getCIDRNetwork(parsedNetwork)),
4040
Dial: ipItr.Dial,
4141
BindIP: net.ParseIP(host),
4242
BindPort: port,
@@ -69,19 +69,46 @@ func runRandomSubnetProxy(listenAddr string, parsedNetwork netip.Prefix, cidrSiz
6969
// It ensures that domain names are resolved to the same IP family (IPv4 or IPv6)
7070
// as the proxy's egress IP.
7171
type DNSResolver struct {
72-
network string
72+
network string
73+
resolver net.Resolver
74+
}
75+
76+
func NewDNSResolver(network string) *DNSResolver {
77+
d := &DNSResolver{
78+
network: network,
79+
}
80+
return d
7381
}
7482

7583
// Resolve resolves a domain name to an IP address using the system DNS resolver.
7684
// It ensures the resolved IP is in the same address family (IPv4 or IPv6) as specified
7785
// by the network field, which helps maintain consistency with the proxy's egress IP.
78-
// TODO use context for name resolution
79-
func (d DNSResolver) Resolve(ctx context.Context, name string) (context.Context, net.IP, error) {
80-
//v("resolving %q: %q", d.network, name)
81-
addr, err := net.ResolveIPAddr(d.network, name)
86+
func (d *DNSResolver) Resolve(ctx context.Context, name string) (context.Context, net.IP, error) {
87+
addrs, err := d.resolver.LookupIPAddr(ctx, name)
8288
if err != nil {
8389
return ctx, nil, err
8490
}
85-
v("resolved %q to %q", name, addr.IP.String())
86-
return ctx, addr.IP, err
91+
92+
// Filter addresses by the desired IP family
93+
for _, addr := range addrs {
94+
if d.network == "ip4" && addr.IP.To4() != nil {
95+
v("resolved %q to %q", name, addr.IP.String())
96+
return ctx, addr.IP, nil
97+
}
98+
if d.network == "ip6" && addr.IP.To4() == nil && addr.IP.To16() != nil {
99+
v("resolved %q to %q", name, addr.IP.String())
100+
return ctx, addr.IP, nil
101+
}
102+
}
103+
104+
return ctx, nil, &net.AddrError{Err: "no suitable address found", Addr: name}
105+
}
106+
107+
// getCIDRNetwork returns "ip4" for IPv4 addresses or "ip6" for IPv6 addresses.
108+
// This is used for DNS resolution context.
109+
func getCIDRNetwork(prefix netip.Prefix) string {
110+
if prefix.Addr().Is4() {
111+
return "ip4"
112+
}
113+
return "ip6"
87114
}

0 commit comments

Comments
 (0)