wireguard.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package wireguard
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "net/netip"
  7. "strings"
  8. "github.com/xtls/xray-core/common"
  9. "github.com/xtls/xray-core/common/log"
  10. "golang.zx2c4.com/wireguard/device"
  11. )
  12. var wgLogger = &device.Logger{
  13. Verbosef: func(format string, args ...any) {
  14. log.Record(&log.GeneralMessage{
  15. Severity: log.Severity_Debug,
  16. Content: fmt.Sprintf(format, args...),
  17. })
  18. },
  19. Errorf: func(format string, args ...any) {
  20. log.Record(&log.GeneralMessage{
  21. Severity: log.Severity_Error,
  22. Content: fmt.Sprintf(format, args...),
  23. })
  24. },
  25. }
  26. func init() {
  27. common.Must(common.RegisterConfig((*DeviceConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  28. deviceConfig := config.(*DeviceConfig)
  29. if deviceConfig.IsClient {
  30. return New(ctx, deviceConfig)
  31. } else {
  32. return NewServer(ctx, deviceConfig)
  33. }
  34. }))
  35. }
  36. // convert endpoint string to netip.Addr
  37. func parseEndpoints(conf *DeviceConfig) ([]netip.Addr, bool, bool, error) {
  38. var hasIPv4, hasIPv6 bool
  39. endpoints := make([]netip.Addr, len(conf.Endpoint))
  40. for i, str := range conf.Endpoint {
  41. var addr netip.Addr
  42. if strings.Contains(str, "/") {
  43. prefix, err := netip.ParsePrefix(str)
  44. if err != nil {
  45. return nil, false, false, err
  46. }
  47. addr = prefix.Addr()
  48. if prefix.Bits() != addr.BitLen() {
  49. return nil, false, false, errors.New("interface address subnet should be /32 for IPv4 and /128 for IPv6")
  50. }
  51. } else {
  52. var err error
  53. addr, err = netip.ParseAddr(str)
  54. if err != nil {
  55. return nil, false, false, err
  56. }
  57. }
  58. endpoints[i] = addr
  59. if addr.Is4() {
  60. hasIPv4 = true
  61. } else if addr.Is6() {
  62. hasIPv6 = true
  63. }
  64. }
  65. return endpoints, hasIPv4, hasIPv6, nil
  66. }
  67. // serialize the config into an IPC request
  68. func createIPCRequest(conf *DeviceConfig) string {
  69. var request strings.Builder
  70. request.WriteString(fmt.Sprintf("private_key=%s\n", conf.SecretKey))
  71. if !conf.IsClient {
  72. // placeholder, we'll handle actual port listening on Xray
  73. request.WriteString("listen_port=1337\n")
  74. }
  75. for _, peer := range conf.Peers {
  76. if peer.PublicKey != "" {
  77. request.WriteString(fmt.Sprintf("public_key=%s\n", peer.PublicKey))
  78. }
  79. if peer.PreSharedKey != "" {
  80. request.WriteString(fmt.Sprintf("preshared_key=%s\n", peer.PreSharedKey))
  81. }
  82. if peer.Endpoint != "" {
  83. request.WriteString(fmt.Sprintf("endpoint=%s\n", peer.Endpoint))
  84. }
  85. for _, ip := range peer.AllowedIps {
  86. request.WriteString(fmt.Sprintf("allowed_ip=%s\n", ip))
  87. }
  88. if peer.KeepAlive != 0 {
  89. request.WriteString(fmt.Sprintf("persistent_keepalive_interval=%d\n", peer.KeepAlive))
  90. }
  91. }
  92. return request.String()[:request.Len()]
  93. }