wireguard.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package conf
  2. import (
  3. "encoding/base64"
  4. "encoding/hex"
  5. "github.com/golang/protobuf/proto"
  6. "github.com/xtls/xray-core/proxy/wireguard"
  7. )
  8. type WireGuardPeerConfig struct {
  9. PublicKey string `json:"publicKey"`
  10. PreSharedKey string `json:"preSharedKey"`
  11. Endpoint string `json:"endpoint"`
  12. KeepAlive int `json:"keepAlive"`
  13. AllowedIPs []string `json:"allowedIPs,omitempty"`
  14. }
  15. func (c *WireGuardPeerConfig) Build() (proto.Message, error) {
  16. var err error
  17. config := new(wireguard.PeerConfig)
  18. config.PublicKey, err = parseWireGuardKey(c.PublicKey)
  19. if err != nil {
  20. return nil, err
  21. }
  22. if c.PreSharedKey != "" {
  23. config.PreSharedKey, err = parseWireGuardKey(c.PreSharedKey)
  24. if err != nil {
  25. return nil, err
  26. }
  27. } else {
  28. config.PreSharedKey = "0000000000000000000000000000000000000000000000000000000000000000"
  29. }
  30. config.Endpoint = c.Endpoint
  31. // default 0
  32. config.KeepAlive = int32(c.KeepAlive)
  33. if c.AllowedIPs == nil {
  34. config.AllowedIps = []string{"0.0.0.0/0", "::0/0"}
  35. } else {
  36. config.AllowedIps = c.AllowedIPs
  37. }
  38. return config, nil
  39. }
  40. type WireGuardConfig struct {
  41. SecretKey string `json:"secretKey"`
  42. Address []string `json:"address"`
  43. Peers []*WireGuardPeerConfig `json:"peers"`
  44. MTU int `json:"mtu"`
  45. NumWorkers int `json:"workers"`
  46. }
  47. func (c *WireGuardConfig) Build() (proto.Message, error) {
  48. config := new(wireguard.DeviceConfig)
  49. var err error
  50. config.SecretKey, err = parseWireGuardKey(c.SecretKey)
  51. if err != nil {
  52. return nil, err
  53. }
  54. if c.Address == nil {
  55. // bogon ips
  56. config.Endpoint = []string{"10.0.0.1", "fd59:7153:2388:b5fd:0000:0000:0000:0001"}
  57. } else {
  58. config.Endpoint = c.Address
  59. }
  60. if c.Peers != nil {
  61. config.Peers = make([]*wireguard.PeerConfig, len(c.Peers))
  62. for i, p := range c.Peers {
  63. msg, err := p.Build()
  64. if err != nil {
  65. return nil, err
  66. }
  67. config.Peers[i] = msg.(*wireguard.PeerConfig)
  68. }
  69. }
  70. if c.MTU == 0 {
  71. config.Mtu = 1420
  72. } else {
  73. config.Mtu = int32(c.MTU)
  74. }
  75. // these a fallback code exists in github.com/nanoda0523/wireguard-go code,
  76. // we don't need to process fallback manually
  77. config.NumWorkers = int32(c.NumWorkers)
  78. return config, nil
  79. }
  80. func parseWireGuardKey(str string) (string, error) {
  81. if len(str) != 64 {
  82. // may in base64 form
  83. dat, err := base64.StdEncoding.DecodeString(str)
  84. if err != nil {
  85. return "", err
  86. }
  87. if len(dat) != 32 {
  88. return "", newError("key should be 32 bytes: " + str)
  89. }
  90. return hex.EncodeToString(dat), err
  91. } else {
  92. // already hex form
  93. return str, nil
  94. }
  95. }