resolve.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package wireguard
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "encoding/hex"
  6. "net/netip"
  7. "github.com/sagernet/sing-box/adapter"
  8. "github.com/sagernet/sing-box/option"
  9. "github.com/sagernet/sing-dns"
  10. E "github.com/sagernet/sing/common/exceptions"
  11. M "github.com/sagernet/sing/common/metadata"
  12. )
  13. type PeerConfig struct {
  14. destination M.Socksaddr
  15. domainStrategy dns.DomainStrategy
  16. Endpoint netip.AddrPort
  17. PublicKey string
  18. PreSharedKey string
  19. AllowedIPs []string
  20. Reserved [3]uint8
  21. }
  22. func (c PeerConfig) GenerateIpcLines() string {
  23. ipcLines := "\npublic_key=" + c.PublicKey
  24. ipcLines += "\nendpoint=" + c.Endpoint.String()
  25. if c.PreSharedKey != "" {
  26. ipcLines += "\npreshared_key=" + c.PreSharedKey
  27. }
  28. for _, allowedIP := range c.AllowedIPs {
  29. ipcLines += "\nallowed_ip=" + allowedIP
  30. }
  31. return ipcLines
  32. }
  33. func ParsePeers(options option.WireGuardOutboundOptions) ([]PeerConfig, error) {
  34. var peers []PeerConfig
  35. if len(options.Peers) > 0 {
  36. for peerIndex, rawPeer := range options.Peers {
  37. peer := PeerConfig{
  38. AllowedIPs: rawPeer.AllowedIPs,
  39. }
  40. destination := rawPeer.ServerOptions.Build()
  41. if destination.IsFqdn() {
  42. peer.destination = destination
  43. peer.domainStrategy = dns.DomainStrategy(options.DomainStrategy)
  44. } else {
  45. peer.Endpoint = destination.AddrPort()
  46. }
  47. {
  48. bytes, err := base64.StdEncoding.DecodeString(rawPeer.PublicKey)
  49. if err != nil {
  50. return nil, E.Cause(err, "decode public key for peer ", peerIndex)
  51. }
  52. peer.PublicKey = hex.EncodeToString(bytes)
  53. }
  54. if rawPeer.PreSharedKey != "" {
  55. bytes, err := base64.StdEncoding.DecodeString(rawPeer.PreSharedKey)
  56. if err != nil {
  57. return nil, E.Cause(err, "decode pre shared key for peer ", peerIndex)
  58. }
  59. peer.PreSharedKey = hex.EncodeToString(bytes)
  60. }
  61. if len(rawPeer.AllowedIPs) == 0 {
  62. return nil, E.New("missing allowed_ips for peer ", peerIndex)
  63. }
  64. if len(rawPeer.Reserved) > 0 {
  65. if len(rawPeer.Reserved) != 3 {
  66. return nil, E.New("invalid reserved value for peer ", peerIndex, ", required 3 bytes, got ", len(peer.Reserved))
  67. }
  68. copy(peer.Reserved[:], options.Reserved)
  69. }
  70. peers = append(peers, peer)
  71. }
  72. } else {
  73. peer := PeerConfig{}
  74. var (
  75. addressHas4 bool
  76. addressHas6 bool
  77. )
  78. for _, localAddress := range options.LocalAddress {
  79. if localAddress.Addr().Is4() {
  80. addressHas4 = true
  81. } else {
  82. addressHas6 = true
  83. }
  84. }
  85. if addressHas4 {
  86. peer.AllowedIPs = append(peer.AllowedIPs, netip.PrefixFrom(netip.IPv4Unspecified(), 0).String())
  87. }
  88. if addressHas6 {
  89. peer.AllowedIPs = append(peer.AllowedIPs, netip.PrefixFrom(netip.IPv6Unspecified(), 0).String())
  90. }
  91. destination := options.ServerOptions.Build()
  92. if destination.IsFqdn() {
  93. peer.destination = destination
  94. peer.domainStrategy = dns.DomainStrategy(options.DomainStrategy)
  95. } else {
  96. peer.Endpoint = destination.AddrPort()
  97. }
  98. {
  99. bytes, err := base64.StdEncoding.DecodeString(options.PeerPublicKey)
  100. if err != nil {
  101. return nil, E.Cause(err, "decode peer public key")
  102. }
  103. peer.PublicKey = hex.EncodeToString(bytes)
  104. }
  105. if options.PreSharedKey != "" {
  106. bytes, err := base64.StdEncoding.DecodeString(options.PreSharedKey)
  107. if err != nil {
  108. return nil, E.Cause(err, "decode pre shared key")
  109. }
  110. peer.PreSharedKey = hex.EncodeToString(bytes)
  111. }
  112. if len(options.Reserved) > 0 {
  113. if len(options.Reserved) != 3 {
  114. return nil, E.New("invalid reserved value, required 3 bytes, got ", len(peer.Reserved))
  115. }
  116. copy(peer.Reserved[:], options.Reserved)
  117. }
  118. peers = append(peers, peer)
  119. }
  120. return peers, nil
  121. }
  122. func ResolvePeers(ctx context.Context, router adapter.Router, peers []PeerConfig) error {
  123. for peerIndex, peer := range peers {
  124. if peer.Endpoint.IsValid() {
  125. continue
  126. }
  127. destinationAddresses, err := router.Lookup(ctx, peer.destination.Fqdn, peer.domainStrategy)
  128. if err != nil {
  129. if len(peers) == 1 {
  130. return E.Cause(err, "resolve endpoint domain")
  131. } else {
  132. return E.Cause(err, "resolve endpoint domain for peer ", peerIndex)
  133. }
  134. }
  135. if len(destinationAddresses) == 0 {
  136. return E.New("no addresses found for endpoint domain: ", peer.destination.Fqdn)
  137. }
  138. peers[peerIndex].Endpoint = netip.AddrPortFrom(destinationAddresses[0], peer.destination.Port)
  139. }
  140. return nil
  141. }