Skip to content

Commit

Permalink
Fix IP range search
Browse files Browse the repository at this point in the history
  • Loading branch information
macronut committed Oct 30, 2023
1 parent 8b41804 commit 0b956a4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
8 changes: 5 additions & 3 deletions phantomtcp/phantom.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,20 @@ func (profile *PhantomProfile) GetInterfaceByIP(ip net.IP) *PhantomInterface {
ip4 := ip.To4()
if ip4 != nil {
ip := binary.BigEndian.Uint32(ip4)
index := sort.Search(len(profile.IPv4Ranges), func(i int) bool {
lenRanges := len(profile.IPv4Ranges)
index := sort.Search(lenRanges, func(i int) bool {
return profile.IPv4Ranges[i].End > ip
})
if ip >= profile.IPv4Ranges[index].Start {
if index >= 0 && index < lenRanges && ip >= profile.IPv4Ranges[index].Start {
return profile.IPv4Ranges[index].Interface
}
} else {
lenRanges := len(profile.IPv6Ranges)
ip := binary.BigEndian.Uint64(ip[:16])
index := sort.Search(len(profile.IPv6Ranges), func(i int) bool {
return profile.IPv6Ranges[i].End > ip
})
if ip >= profile.IPv6Ranges[index].Start {
if index >= 0 && index < lenRanges && ip >= profile.IPv6Ranges[index].Start {
return profile.IPv6Ranges[index].Interface
}
}
Expand Down
5 changes: 4 additions & 1 deletion phantomtcp/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ func tcp_redirect(client net.Conn, addr *net.TCPAddr, domain string, header []by

var pface *PhantomInterface = nil
if domain == "" {
pface, ok := DefaultProfile.DomainMap[domain]
var ok bool
pface, ok = DefaultProfile.DomainMap[domain]
if !ok {
pface = DefaultProfile.GetInterfaceByIP(addr.IP)
}
Expand All @@ -301,6 +302,8 @@ func tcp_redirect(client net.Conn, addr *net.TCPAddr, domain string, header []by
pface = DefaultProfile.GetInterface(domain)
}

logPrintln(1, pface)

if pface != nil && (pface.Protocol != 0 || pface.Hint != 0) {
if pface.Hint&HINT_NOTCP != 0 {
time.Sleep(time.Second)
Expand Down
8 changes: 3 additions & 5 deletions phantomtcp/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,13 +363,11 @@ func ModifyAndSendPacket(connInfo *ConnectionInfo, payload []byte, hint uint32,
}
defer f.Close()
fd := int(f.Fd())
level := syscall.IPPROTO_IP
name := syscall.IP_TTL
if network == "ip6:tcp" {
level = syscall.IPPROTO_IPV6
name = syscall.IPV6_UNICAST_HOPS
err = syscall.SetsockoptInt(fd, syscall.IPPROTO_IPV6, syscall.IPV6_UNICAST_HOPS, int(ttl))
} else {
err = syscall.SetsockoptInt(fd, syscall.IPPROTO_IP, syscall.IP_TTL, int(ttl))
}
err = syscall.SetsockoptInt(fd, level, name, int(ttl))
if err != nil {
return err
}
Expand Down

0 comments on commit 0b956a4

Please sign in to comment.