Skip to content

Commit

Permalink
Fix Socks5 local domain name resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
macronut committed Oct 31, 2023
1 parent 0b956a4 commit 8b0ac27
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
4 changes: 3 additions & 1 deletion phantomtcp/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,14 @@ func tcp_redirect(client net.Conn, addr *net.TCPAddr, domain string, header []by
return
}
domain = Nose[index]
addr.IP = nil
case VirtualAddrPrefix:
index := int(binary.BigEndian.Uint16(addr.IP[2:4]))
if index >= len(Nose) {
return
}
domain = Nose[index]
addr.IP = nil
}
}
port = addr.Port
Expand Down Expand Up @@ -320,7 +322,7 @@ func tcp_redirect(client net.Conn, addr *net.TCPAddr, domain string, header []by
header = b[:n]
}

if header[0] == 0x16 {
if addr.IP == nil && header[0] == 0x16 {
offset, length := GetSNI(header)
if length > 0 {
_domain := string(header[offset : offset+length])
Expand Down
33 changes: 17 additions & 16 deletions phantomtcp/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,28 +597,29 @@ func (server *PhantomInterface) ProxyHandshake(conn net.Conn, synpacket *Connect
return proxy_err
}

if server.DNS != "" {
ip := net.ParseIP(host)
if ip == nil && server.DNS != "" {
_, ips := NSLookup(host, server.Hint, server.DNS)
logPrintln(1, host, ips)
if ips != nil {
ip := ips[rand.Intn(len(ips))]
ip4 := ip.To4()
if ip4 != nil {
copy(b[:], []byte{0x05, 0x01, 0x00, 0x01})
copy(b[4:], ip4[:4])
binary.BigEndian.PutUint16(b[8:], uint16(port))
n, err = conn.Write(b[:10])
} else {
copy(b[:], []byte{0x05, 0x01, 0x00, 0x04})
copy(b[4:], ip[:16])
binary.BigEndian.PutUint16(b[20:], uint16(port))
n, err = conn.Write(b[:22])
}
host = ""
ip = ips[rand.Intn(len(ips))]
}
}

if host != "" {
if ip != nil {
ip4 := ip.To4()
if ip4 != nil {
copy(b[:], []byte{0x05, 0x01, 0x00, 0x01})
copy(b[4:], ip4[:4])
binary.BigEndian.PutUint16(b[8:], uint16(port))
n, err = conn.Write(b[:10])
} else {
copy(b[:], []byte{0x05, 0x01, 0x00, 0x04})
copy(b[4:], ip[:16])
binary.BigEndian.PutUint16(b[20:], uint16(port))
n, err = conn.Write(b[:22])
}
} else {
copy(b[:], []byte{0x05, 0x01, 0x00, 0x03})
hostLen := len(host)
b[4] = byte(hostLen)
Expand Down

0 comments on commit 8b0ac27

Please sign in to comment.