macronut / phantomsocks

A cross-platform proxy client/server for Linux/Windows/macOS
GNU Lesser General Public License v3.0
453 stars 18 forks source link

Panic when DNS for interface is set and the requested domain name can't be resolved. #65

Closed rakino closed 1 year ago

rakino commented 1 year ago
2023-10-04 01:04:46 Redirect: 127.0.0.1:48488 ->  443 &{ udp://127.0.0.1:53 0 0 0 0 65535 6 127.0.0.1:9909}
2023-10-04 01:04:46  []
2023-10-04 01:04:46 Redirect: 127.0.0.1:48486 ->  443 &{ udp://127.0.0.1:53 0 0 0 0 65535 6 127.0.0.1:9909}
2023-10-04 01:04:46 panic: invalid argument to Intn
2023-10-04 01:04:46
2023-10-04 01:04:46 goroutine 19 [running]:
2023-10-04 01:04:46 math/rand.(*Rand).Intn(0x7e90e0, 0xc000010018)
2023-10-04 01:04:46     /gnu/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-go-1.17.13/lib/go/src/math/rand/rand.go:168 +0x65
2023-10-04 01:04:46 math/rand.Intn(...)
2023-10-04 01:04:46     /gnu/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-go-1.17.13/lib/go/src/math/rand/rand.go:337
2023-10-04 01:04:46 github.com/macronut/phantomsocks/phantomtcp.(*PhantomInterface).ProxyHandshake(0xc000026600, {0x7f5c80, 0xc000094028}, 0x0, {0x0, 0x0}, 0x1bb)
2023-10-04 01:04:46     /tmp/guix-build-phantomsocks-0.0.0-20230829023258-013a0002abe2.drv-0/src/github.com/macronut/phantomsocks/phantomtcp/tcp.go:604 +0x7cf
2023-10-04 01:04:46 github.com/macronut/phantomsocks/phantomtcp.(*PhantomInterface).Dial(0xc000026600, {0x0, 0x0}, 0x6, {0xc0000a6600, 0x205, 0x5b4})
2023-10-04 01:04:46     /tmp/guix-build-phantomsocks-0.0.0-20230829023258-013a0002abe2.drv-0/src/github.com/macronut/phantomsocks/phantomtcp/tcp.go:191 +0x11d7
2023-10-04 01:04:46 github.com/macronut/phantomsocks/phantomtcp.tcp_redirect({0x7f5c80, 0xc000094010}, 0xc0000920c0, {0x0, 0x0}, {0x0, 0x0, 0x0})
2023-10-04 01:04:46     /tmp/guix-build-phantomsocks-0.0.0-20230829023258-013a0002abe2.drv-0/src/github.com/macronut/phantomsocks/phantomtcp/proxy.go:323 +0x5d7
2023-10-04 01:04:46 github.com/macronut/phantomsocks/phantomtcp.SocksProxy({0x7f5c80, 0xc000094010})
2023-10-04 01:04:46     /tmp/guix-build-phantomsocks-0.0.0-20230829023258-013a0002abe2.drv-0/src/github.com/macronut/phantomsocks/phantomtcp/proxy.go:120 +0xada
2023-10-04 01:04:46 created by main.ListenAndServe
2023-10-04 01:04:46     /tmp/guix-build-phantomsocks-0.0.0-20230829023258-013a0002abe2.drv-0/src/github.com/macronut/phantomsocks/main.go:81 +0x3d9

The panic can be avoided with the following change:

diff --git a/phantomtcp/tcp.go b/phantomtcp/tcp.go
index 300aeb3..aa97cb5 100755
--- a/phantomtcp/tcp.go
+++ b/phantomtcp/tcp.go
@@ -600,7 +600,7 @@ func (server *PhantomInterface) ProxyHandshake(conn net.Conn, synpacket *Connect
            if server.DNS != "" {
                _, ips := NSLookup(host, server.Hint, server.DNS)
                logPrintln(1, host, ips)
-               if ips != nil {
+               if len(ips) != 0 {
                    ip := ips[rand.Intn(len(ips))]
                    ip4 := ip.To4()
                    if ip4 != nil {
Redirect: 127.0.0.1:57166 -> this.name.does.not.exist 443 &{ udp://127.0.0.1:53 0 0 0 0 65535 6 127.0.0.1:9909}
this.name.does.not.exist []

However, another issue reveals when I'm accessing https://1.1.1.1/, and I'm not sure where I should change:

Redirect: 127.0.0.1:46328 ->  443 &{ udp://127.0.0.1:53 0 0 0 0 65535 6 127.0.0.1:9909}
 []
macronut commented 1 year ago

https://github.com/macronut/phantomsocks/commit/8b0ac27fc450d5c03c8bad3a2c43beac93c2041e