sockopt_linux.go 1012 B

123456789101112131415161718192021222324252627282930313233343536
  1. // +build linux
  2. package tcp
  3. import (
  4. "syscall"
  5. "v2ray.com/core/app/log"
  6. "v2ray.com/core/common/errors"
  7. "v2ray.com/core/common/net"
  8. "v2ray.com/core/transport/internet"
  9. )
  10. const SO_ORIGINAL_DST = 80
  11. func GetOriginalDestination(conn internet.Connection) net.Destination {
  12. tcpConn, ok := conn.(internet.SysFd)
  13. if !ok {
  14. log.Trace(errors.New("failed to get sys fd").Path("Transport", "Internet", "TCP"))
  15. return net.Destination{}
  16. }
  17. fd, err := tcpConn.SysFd()
  18. if err != nil {
  19. log.Trace(errors.New("failed to get original destination").Base(err).Path("Transport", "Internet", "TCP"))
  20. return net.Destination{}
  21. }
  22. addr, err := syscall.GetsockoptIPv6Mreq(fd, syscall.IPPROTO_IP, SO_ORIGINAL_DST)
  23. if err != nil {
  24. log.Trace(errors.New("failed to call getsockopt").Base(err).Path("Transport", "Internet", "TCP"))
  25. return net.Destination{}
  26. }
  27. ip := net.IPAddress(addr.Multiaddr[4:8])
  28. port := uint16(addr.Multiaddr[2])<<8 + uint16(addr.Multiaddr[3])
  29. return net.TCPDestination(ip, net.Port(port))
  30. }