client_wireguard.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package service
  2. import (
  3. "net/netip"
  4. "strconv"
  5. "strings"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  7. "github.com/mhsanaei/3x-ui/v3/internal/util/common"
  8. wgutil "github.com/mhsanaei/3x-ui/v3/internal/util/wireguard"
  9. )
  10. const defaultWireguardBase = "10.0.0.0/24"
  11. func keepAliveStr(seconds int) string {
  12. if seconds <= 0 {
  13. return ""
  14. }
  15. return strconv.Itoa(seconds)
  16. }
  17. func wireguardHostAddr(s string) netip.Addr {
  18. s = strings.TrimSpace(s)
  19. if s == "" {
  20. return netip.Addr{}
  21. }
  22. if p, err := netip.ParsePrefix(s); err == nil {
  23. return p.Addr()
  24. }
  25. if a, err := netip.ParseAddr(s); err == nil {
  26. return a
  27. }
  28. return netip.Addr{}
  29. }
  30. // allocateWireguardAddress returns the first free /32 host address in base that
  31. // is not already present in used. The server holds the first host (.1), so
  32. // allocation starts at the second host (.2).
  33. func allocateWireguardAddress(used []string, base string) (string, error) {
  34. if base == "" {
  35. base = defaultWireguardBase
  36. }
  37. prefix, err := netip.ParsePrefix(base)
  38. if err != nil {
  39. return "", err
  40. }
  41. taken := make(map[netip.Addr]struct{}, len(used))
  42. for _, u := range used {
  43. if a := wireguardHostAddr(u); a.IsValid() {
  44. taken[a] = struct{}{}
  45. }
  46. }
  47. addr := prefix.Masked().Addr().Next().Next()
  48. for prefix.Contains(addr) {
  49. if _, ok := taken[addr]; !ok {
  50. return addr.String() + "/32", nil
  51. }
  52. addr = addr.Next()
  53. }
  54. return "", common.NewError("wireguard: no free address available in", base)
  55. }
  56. // defaultWireguardClients fills in blank WireGuard credentials for newly added
  57. // clients: a generated keypair when none was provided, a derived public key when
  58. // only a private key was given, and a unique tunnel address allocated from the
  59. // inbound's subnet. It mutates both the typed clients and the parallel raw client
  60. // maps that get persisted into the inbound settings. Existing values are never
  61. // overwritten, so editing a client never rotates its keys.
  62. func defaultWireguardClients(existing, clients []model.Client, interfaceClients []any) error {
  63. used := make([]string, 0)
  64. for i := range existing {
  65. used = append(used, existing[i].AllowedIPs...)
  66. }
  67. for i := range clients {
  68. c := &clients[i]
  69. if c.PrivateKey == "" && c.PublicKey == "" {
  70. priv, pub, err := wgutil.GenerateWireguardKeypair()
  71. if err != nil {
  72. return err
  73. }
  74. c.PrivateKey = priv
  75. c.PublicKey = pub
  76. } else if c.PublicKey == "" && c.PrivateKey != "" {
  77. pub, err := wgutil.PublicKeyFromPrivate(c.PrivateKey)
  78. if err != nil {
  79. return err
  80. }
  81. c.PublicKey = pub
  82. }
  83. if len(c.AllowedIPs) == 0 {
  84. addr, err := allocateWireguardAddress(used, defaultWireguardBase)
  85. if err != nil {
  86. return err
  87. }
  88. c.AllowedIPs = []string{addr}
  89. }
  90. used = append(used, c.AllowedIPs...)
  91. if i < len(interfaceClients) {
  92. if m, ok := interfaceClients[i].(map[string]any); ok {
  93. m["privateKey"] = c.PrivateKey
  94. m["publicKey"] = c.PublicKey
  95. m["allowedIPs"] = c.AllowedIPs
  96. if c.PreSharedKey != "" {
  97. m["preSharedKey"] = c.PreSharedKey
  98. }
  99. if c.KeepAlive > 0 {
  100. m["keepAlive"] = c.KeepAlive
  101. }
  102. interfaceClients[i] = m
  103. }
  104. }
  105. }
  106. return nil
  107. }