destination.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package singbridge
  2. import (
  3. M "github.com/sagernet/sing/common/metadata"
  4. N "github.com/sagernet/sing/common/network"
  5. "github.com/xtls/xray-core/common/net"
  6. )
  7. func ToNetwork(network string) net.Network {
  8. switch N.NetworkName(network) {
  9. case N.NetworkTCP:
  10. return net.Network_TCP
  11. case N.NetworkUDP:
  12. return net.Network_UDP
  13. default:
  14. return net.Network_Unknown
  15. }
  16. }
  17. func ToDestination(socksaddr M.Socksaddr, network net.Network) net.Destination {
  18. // IsFqdn() implicitly checks if the domain name is valid
  19. if socksaddr.IsFqdn() {
  20. return net.Destination{
  21. Network: network,
  22. Address: net.DomainAddress(socksaddr.Fqdn),
  23. Port: net.Port(socksaddr.Port),
  24. }
  25. }
  26. // IsIP() implicitly checks if the IP address is valid
  27. if socksaddr.IsIP() {
  28. return net.Destination{
  29. Network: network,
  30. Address: net.IPAddress(socksaddr.Addr.AsSlice()),
  31. Port: net.Port(socksaddr.Port),
  32. }
  33. }
  34. return net.Destination{}
  35. }
  36. func ToSocksaddr(destination net.Destination) M.Socksaddr {
  37. var addr M.Socksaddr
  38. switch destination.Address.Family() {
  39. case net.AddressFamilyDomain:
  40. addr.Fqdn = destination.Address.Domain()
  41. default:
  42. addr.Addr = M.AddrFromIP(destination.Address.IP())
  43. }
  44. addr.Port = uint16(destination.Port)
  45. return addr
  46. }