wireguard.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package conf
  2. import (
  3. "encoding/base64"
  4. "encoding/hex"
  5. "strings"
  6. "github.com/xtls/xray-core/proxy/wireguard"
  7. "google.golang.org/protobuf/proto"
  8. )
  9. type WireGuardPeerConfig struct {
  10. PublicKey string `json:"publicKey"`
  11. PreSharedKey string `json:"preSharedKey"`
  12. Endpoint string `json:"endpoint"`
  13. KeepAlive uint32 `json:"keepAlive"`
  14. AllowedIPs []string `json:"allowedIPs,omitempty"`
  15. }
  16. func (c *WireGuardPeerConfig) Build() (proto.Message, error) {
  17. var err error
  18. config := new(wireguard.PeerConfig)
  19. if c.PublicKey != "" {
  20. config.PublicKey, err = parseWireGuardKey(c.PublicKey)
  21. if err != nil {
  22. return nil, err
  23. }
  24. }
  25. if c.PreSharedKey != "" {
  26. config.PreSharedKey, err = parseWireGuardKey(c.PreSharedKey)
  27. if err != nil {
  28. return nil, err
  29. }
  30. }
  31. config.Endpoint = c.Endpoint
  32. // default 0
  33. config.KeepAlive = c.KeepAlive
  34. if c.AllowedIPs == nil {
  35. config.AllowedIps = []string{"0.0.0.0/0", "::0/0"}
  36. } else {
  37. config.AllowedIps = c.AllowedIPs
  38. }
  39. return config, nil
  40. }
  41. type WireGuardConfig struct {
  42. IsClient bool `json:""`
  43. KernelMode *bool `json:"kernelMode"`
  44. SecretKey string `json:"secretKey"`
  45. Address []string `json:"address"`
  46. Peers []*WireGuardPeerConfig `json:"peers"`
  47. MTU int32 `json:"mtu"`
  48. NumWorkers int32 `json:"workers"`
  49. Reserved []byte `json:"reserved"`
  50. DomainStrategy string `json:"domainStrategy"`
  51. }
  52. func (c *WireGuardConfig) Build() (proto.Message, error) {
  53. config := new(wireguard.DeviceConfig)
  54. var err error
  55. config.SecretKey, err = parseWireGuardKey(c.SecretKey)
  56. if err != nil {
  57. return nil, err
  58. }
  59. if c.Address == nil {
  60. // bogon ips
  61. config.Endpoint = []string{"10.0.0.1", "fd59:7153:2388:b5fd:0000:0000:0000:0001"}
  62. } else {
  63. config.Endpoint = c.Address
  64. }
  65. if c.Peers != nil {
  66. config.Peers = make([]*wireguard.PeerConfig, len(c.Peers))
  67. for i, p := range c.Peers {
  68. msg, err := p.Build()
  69. if err != nil {
  70. return nil, err
  71. }
  72. config.Peers[i] = msg.(*wireguard.PeerConfig)
  73. }
  74. }
  75. if c.MTU == 0 {
  76. config.Mtu = 1420
  77. } else {
  78. config.Mtu = c.MTU
  79. }
  80. // these a fallback code exists in wireguard-go code,
  81. // we don't need to process fallback manually
  82. config.NumWorkers = c.NumWorkers
  83. if len(c.Reserved) != 0 && len(c.Reserved) != 3 {
  84. return nil, newError(`"reserved" should be empty or 3 bytes`)
  85. }
  86. config.Reserved = c.Reserved
  87. switch strings.ToLower(c.DomainStrategy) {
  88. case "forceip", "":
  89. config.DomainStrategy = wireguard.DeviceConfig_FORCE_IP
  90. case "forceipv4":
  91. config.DomainStrategy = wireguard.DeviceConfig_FORCE_IP4
  92. case "forceipv6":
  93. config.DomainStrategy = wireguard.DeviceConfig_FORCE_IP6
  94. case "forceipv4v6":
  95. config.DomainStrategy = wireguard.DeviceConfig_FORCE_IP46
  96. case "forceipv6v4":
  97. config.DomainStrategy = wireguard.DeviceConfig_FORCE_IP64
  98. default:
  99. return nil, newError("unsupported domain strategy: ", c.DomainStrategy)
  100. }
  101. config.IsClient = c.IsClient
  102. if c.KernelMode != nil {
  103. config.KernelMode = *c.KernelMode
  104. if config.KernelMode && !wireguard.KernelTunSupported() {
  105. newError("kernel mode is not supported on your OS or permission is insufficient").AtWarning().WriteToLog()
  106. }
  107. } else {
  108. config.KernelMode = wireguard.KernelTunSupported()
  109. if config.KernelMode {
  110. newError("kernel mode is enabled as it's supported and permission is sufficient").AtDebug().WriteToLog()
  111. }
  112. }
  113. return config, nil
  114. }
  115. func parseWireGuardKey(str string) (string, error) {
  116. var err error
  117. if len(str)%2 == 0 {
  118. _, err = hex.DecodeString(str)
  119. if err == nil {
  120. return str, nil
  121. }
  122. }
  123. var dat []byte
  124. str = strings.TrimSuffix(str, "=")
  125. if strings.ContainsRune(str, '+') || strings.ContainsRune(str, '/') {
  126. dat, err = base64.RawStdEncoding.DecodeString(str)
  127. } else {
  128. dat, err = base64.RawURLEncoding.DecodeString(str)
  129. }
  130. if err == nil {
  131. return hex.EncodeToString(dat), nil
  132. }
  133. return "", newError("failed to deserialize key").Base(err)
  134. }