wireguard.go 2.7 KB

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