hub_linux.go 937 B

1234567891011121314151617181920212223242526272829303132333435
  1. //go:build linux
  2. // +build linux
  3. package udp
  4. import (
  5. "syscall"
  6. "golang.org/x/sys/unix"
  7. "github.com/v2fly/v2ray-core/v5/common/net"
  8. )
  9. func RetrieveOriginalDest(oob []byte) net.Destination {
  10. msgs, err := syscall.ParseSocketControlMessage(oob)
  11. if err != nil {
  12. return net.Destination{}
  13. }
  14. for _, msg := range msgs {
  15. if msg.Header.Level == syscall.SOL_IP && msg.Header.Type == syscall.IP_RECVORIGDSTADDR {
  16. ip := net.IPAddress(msg.Data[4:8])
  17. port := net.PortFromBytes(msg.Data[2:4])
  18. return net.UDPDestination(ip, port)
  19. } else if msg.Header.Level == syscall.SOL_IPV6 && msg.Header.Type == unix.IPV6_RECVORIGDSTADDR {
  20. ip := net.IPAddress(msg.Data[8:24])
  21. port := net.PortFromBytes(msg.Data[2:4])
  22. return net.UDPDestination(ip, port)
  23. }
  24. }
  25. return net.Destination{}
  26. }
  27. func ReadUDPMsg(conn *net.UDPConn, payload []byte, oob []byte) (int, int, int, *net.UDPAddr, error) {
  28. return conn.ReadMsgUDP(payload, oob)
  29. }