chore: better parse remote dst

This commit is contained in:
gVisor bot 2023-04-27 02:32:25 +00:00
parent d19543b259
commit 3cd2cd4795

View file

@ -3,6 +3,7 @@ package statistic
import ( import (
"io" "io"
"net" "net"
"net/netip"
"time" "time"
"github.com/Dreamacro/clash/common/atomic" "github.com/Dreamacro/clash/common/atomic"
@ -110,13 +111,24 @@ func (tt *tcpTracker) Upstream() any {
return tt.Conn return tt.Conn
} }
func parseRemoteDestination(addr net.Addr, conn C.Connection) string {
if addr == nil && conn != nil {
return conn.RemoteDestination()
}
if addrPort, err := netip.ParseAddrPort(addr.String()); err == nil && addrPort.Addr().IsValid() {
return addrPort.Addr().String()
} else {
if conn != nil {
return conn.RemoteDestination()
} else {
return ""
}
}
}
func NewTCPTracker(conn C.Conn, manager *Manager, metadata *C.Metadata, rule C.Rule, uploadTotal int64, downloadTotal int64, pushToManager bool) *tcpTracker { func NewTCPTracker(conn C.Conn, manager *Manager, metadata *C.Metadata, rule C.Rule, uploadTotal int64, downloadTotal int64, pushToManager bool) *tcpTracker {
if conn != nil { if conn != nil {
if tcpAddr, ok := conn.RemoteAddr().(*net.TCPAddr); ok && tcpAddr.IP != nil { metadata.RemoteDst = parseRemoteDestination(conn.RemoteAddr(), conn)
metadata.RemoteDst = tcpAddr.IP.String()
} else {
metadata.RemoteDst = conn.RemoteDestination()
}
} }
t := &tcpTracker{ t := &tcpTracker{
@ -190,7 +202,7 @@ func (ut *udpTracker) Close() error {
} }
func NewUDPTracker(conn C.PacketConn, manager *Manager, metadata *C.Metadata, rule C.Rule, uploadTotal int64, downloadTotal int64, pushToManager bool) *udpTracker { func NewUDPTracker(conn C.PacketConn, manager *Manager, metadata *C.Metadata, rule C.Rule, uploadTotal int64, downloadTotal int64, pushToManager bool) *udpTracker {
metadata.RemoteDst = conn.RemoteDestination() metadata.RemoteDst = parseRemoteDestination(nil, conn)
ut := &udpTracker{ ut := &udpTracker{
PacketConn: conn, PacketConn: conn,