Fix connection owner lookup on legacy Android

This commit is contained in:
世界
2026-10-23 19:57:03 +08:00
parent af288b53e6
commit 40bc411e8d
5 changed files with 121 additions and 79 deletions
+89 -48
View File
@@ -4,10 +4,10 @@ import (
"bufio"
"encoding/binary"
"encoding/hex"
"fmt"
"net"
"net/netip"
"os"
"slices"
"strconv"
"strings"
"unsafe"
@@ -16,9 +16,11 @@ import (
)
var (
netIndexOfLocal = -1
netIndexOfUid = -1
nativeEndian binary.ByteOrder
netIndexOfLocal = -1
netIndexOfRemote = -1
netIndexOfUid = -1
netIndexOfInode = -1
nativeEndian binary.ByteOrder
)
func init() {
@@ -30,62 +32,97 @@ func init() {
}
}
func ResolveSocketByProcSearch(network string, source, _ netip.AddrPort) int32 {
if netIndexOfLocal < 0 || netIndexOfUid < 0 {
func ResolveSocketByProcSearch(network string, source netip.AddrPort, destination netip.AddrPort) int32 {
if netIndexOfLocal < 0 || netIndexOfRemote < 0 || netIndexOfUid < 0 || netIndexOfInode < 0 {
return -1
}
path := "/proc/net/"
if network == N.NetworkTCP {
path += "tcp"
} else {
path += "udp"
path := "/proc/net/" + network
if source.Addr().Is4() {
uid := searchSocketTable(path, network, source, destination)
if uid != -1 {
return uid
}
// java.net sockets on Android are always dual-stack AF_INET6, so their IPv4 connections are only listed in the IPv6 table.
source = netip.AddrPortFrom(netip.AddrFrom16(source.Addr().As16()), source.Port())
if destination.Addr().Is4() {
destination = netip.AddrPortFrom(netip.AddrFrom16(destination.Addr().As16()), destination.Port())
}
}
return searchSocketTable(path+"6", network, source, destination)
}
if source.Addr().Is6() {
path += "6"
}
sIP := source.Addr().AsSlice()
if len(sIP) == 0 {
return -1
}
var bytes [2]byte
binary.BigEndian.PutUint16(bytes[:], source.Port())
local := fmt.Sprintf("%s:%s", hex.EncodeToString(nativeEndianIP(sIP)), hex.EncodeToString(bytes[:]))
func searchSocketTable(path string, network string, source netip.AddrPort, destination netip.AddrPort) int32 {
file, err := os.Open(path)
if err != nil {
return -1
}
defer file.Close()
reader := bufio.NewReader(file)
for {
row, _, err := reader.ReadLine()
if err != nil {
return -1
}
fields := strings.Fields(string(row))
if len(fields) <= netIndexOfLocal || len(fields) <= netIndexOfUid {
continue
}
if strings.EqualFold(local, fields[netIndexOfLocal]) {
uid, err := strconv.Atoi(fields[netIndexOfUid])
if err != nil {
return -1
local := formatSocketAddress(source)
var remote string
if destination.IsValid() {
remote = formatSocketAddress(destination)
}
var unspecifiedAddresses []string
if network == N.NetworkUDP {
if source.Addr().Is4() {
unspecifiedAddresses = append(unspecifiedAddresses, formatSocketAddress(netip.AddrPortFrom(netip.IPv4Unspecified(), source.Port())))
} else {
unspecifiedAddresses = append(unspecifiedAddresses, formatSocketAddress(netip.AddrPortFrom(netip.IPv6Unspecified(), source.Port())))
if source.Addr().Is4In6() {
unspecifiedAddresses = append(unspecifiedAddresses, formatSocketAddress(netip.AddrPortFrom(netip.AddrFrom16(netip.IPv4Unspecified().As16()), source.Port())))
}
return int32(uid)
}
}
minFields := max(netIndexOfLocal, netIndexOfRemote, netIndexOfUid, netIndexOfInode) + 1
var (
row []byte
rank int
bestRank int
uid int
bestUID int32 = -1
)
reader := bufio.NewReader(file)
for {
row, _, err = reader.ReadLine()
if err != nil {
return bestUID
}
fields := strings.Fields(string(row))
if len(fields) < minFields || fields[netIndexOfInode] == "0" && fields[netIndexOfUid] == "0" {
continue
}
localAddress := fields[netIndexOfLocal]
switch {
case strings.EqualFold(localAddress, local) && (remote == "" || strings.EqualFold(fields[netIndexOfRemote], remote)):
rank = 3
case network != N.NetworkUDP:
continue
case strings.EqualFold(localAddress, local):
rank = 2
case slices.ContainsFunc(unspecifiedAddresses, func(address string) bool { return strings.EqualFold(localAddress, address) }):
rank = 1
default:
continue
}
if rank <= bestRank {
continue
}
uid, err = strconv.Atoi(fields[netIndexOfUid])
if err != nil {
continue
}
if rank == 3 {
return int32(uid)
}
bestRank = rank
bestUID = int32(uid)
}
}
func formatSocketAddress(address netip.AddrPort) string {
var portBytes [2]byte
binary.BigEndian.PutUint16(portBytes[:], address.Port())
return hex.EncodeToString(nativeEndianIP(address.Addr().AsSlice())) + ":" + hex.EncodeToString(portBytes[:])
}
func nativeEndianIP(ip net.IP) []byte {
@@ -141,8 +178,12 @@ func init() {
tmWhen = true
case "local_address":
netIndexOfLocal = idx + offset
case "rem_address":
netIndexOfRemote = idx + offset
case "uid":
netIndexOfUid = idx + offset
case "inode":
netIndexOfInode = idx + offset
}
}
}