fix: inline GetConnFD into GetOriginalDstAddr

This commit is contained in:
SunBK201 2025-12-09 16:32:39 +08:00
parent f67658ae86
commit dfe1999616

View File

@ -32,11 +32,18 @@ func Connect(addr string, mark int) (target net.Conn, err error) {
// GetOriginalDstAddr retrieves the original destination address of the redirected connection.
func GetOriginalDstAddr(conn net.Conn) (addr string, err error) {
fd, err := GetConnFD(conn)
if err != nil {
return "", fmt.Errorf("GetConnFD: %v", err)
tcpConn, ok := conn.(*net.TCPConn)
if !ok {
return "", errors.New("GetConnFD connection is not *net.TCPConn")
}
raw, err := unix.GetsockoptIPv6Mreq(fd, unix.SOL_IP, unix.SO_ORIGINAL_DST)
file, err := tcpConn.File()
if err != nil {
return "", fmt.Errorf("tcpConn.File: %v", err)
}
defer file.Close()
raw, err := unix.GetsockoptIPv6Mreq(int(file.Fd()), unix.SOL_IP, unix.SO_ORIGINAL_DST)
if err != nil {
return "", fmt.Errorf("unix.GetsockoptIPv6Mreq: %v", err)
}
@ -45,17 +52,3 @@ func GetOriginalDstAddr(conn net.Conn) (addr string, err error) {
port := uint16(raw.Multiaddr[2])<<8 + uint16(raw.Multiaddr[3])
return fmt.Sprintf("%s:%d", ip.String(), port), nil
}
func GetConnFD(conn net.Conn) (fd int, err error) {
tcpConn, ok := conn.(*net.TCPConn)
if !ok {
return 0, errors.New("GetConnFD connection is not *net.TCPConn")
}
file, err := tcpConn.File()
if err != nil {
return 0, fmt.Errorf("tcpConn.File: %v", err)
}
defer file.Close()
return int(file.Fd()), nil
}