2022-07-15 00:42:02 +00:00
|
|
|
package redir
|
|
|
|
|
|
|
|
import (
|
2023-02-05 06:38:34 +00:00
|
|
|
"encoding/binary"
|
2022-07-15 00:42:02 +00:00
|
|
|
"net"
|
|
|
|
"net/netip"
|
2022-08-16 10:19:48 +00:00
|
|
|
"os"
|
2022-07-15 00:42:02 +00:00
|
|
|
"syscall"
|
|
|
|
|
2022-08-16 10:19:48 +00:00
|
|
|
"github.com/sagernet/sing/common"
|
|
|
|
"github.com/sagernet/sing/common/control"
|
2022-07-15 00:42:02 +00:00
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetOriginalDestination(conn net.Conn) (destination netip.AddrPort, err error) {
|
2022-08-16 10:19:48 +00:00
|
|
|
syscallConn, ok := common.Cast[syscall.Conn](conn)
|
|
|
|
if !ok {
|
|
|
|
return netip.AddrPort{}, os.ErrInvalid
|
2022-07-15 00:42:02 +00:00
|
|
|
}
|
2022-08-16 10:19:48 +00:00
|
|
|
err = control.Conn(syscallConn, func(fd uintptr) error {
|
|
|
|
const SO_ORIGINAL_DST = 80
|
|
|
|
if conn.RemoteAddr().(*net.TCPAddr).IP.To4() != nil {
|
|
|
|
raw, err := syscall.GetsockoptIPv6Mreq(int(fd), syscall.IPPROTO_IP, SO_ORIGINAL_DST)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
destination = netip.AddrPortFrom(M.AddrFromIP(raw.Multiaddr[4:8]), uint16(raw.Multiaddr[2])<<8+uint16(raw.Multiaddr[3]))
|
|
|
|
} else {
|
|
|
|
raw, err := syscall.GetsockoptIPv6MTUInfo(int(fd), syscall.IPPROTO_IPV6, SO_ORIGINAL_DST)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-05 06:38:34 +00:00
|
|
|
var port [2]byte
|
|
|
|
binary.BigEndian.PutUint16(port[:], raw.Addr.Port)
|
|
|
|
destination = netip.AddrPortFrom(M.AddrFromIP(raw.Addr.Addr[:]), binary.LittleEndian.Uint16(port[:]))
|
2022-07-15 00:42:02 +00:00
|
|
|
}
|
2022-08-16 10:19:48 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return
|
2022-07-15 00:42:02 +00:00
|
|
|
}
|