inbound_amneziawg.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "strings"
  7. "gorm.io/gorm"
  8. "github.com/mhsanaei/3x-ui/v3/internal/amneziawg"
  9. "github.com/mhsanaei/3x-ui/v3/internal/amneziawgnet"
  10. "github.com/mhsanaei/3x-ui/v3/internal/database"
  11. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  12. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  13. wgutil "github.com/mhsanaei/3x-ui/v3/internal/util/wireguard"
  14. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  15. )
  16. // DesiredAmneziaWGInstances derives the AmneziaWG interfaces this panel
  17. // should be running: one instance per enabled local AmneziaWG inbound,
  18. // serving only the peers of clients that are both enabled in the inbound
  19. // settings and not depletion-disabled in client_traffics. That is the same
  20. // effective peer set buildInboundForLocalRuntime pushes on interactive edits,
  21. // so the reconcile job and the push path agree on one fingerprint — see
  22. // DesiredMtprotoInstances, which this mirrors exactly.
  23. func (s *InboundService) DesiredAmneziaWGInstances() ([]amneziawg.Instance, error) {
  24. db := database.GetDB()
  25. var inbounds []*model.Inbound
  26. err := db.Model(model.Inbound{}).
  27. Where("protocol = ? AND enable = ? AND node_id IS NULL", model.AmneziaWG, true).
  28. Find(&inbounds).Error
  29. if err != nil {
  30. return nil, err
  31. }
  32. if len(inbounds) == 0 {
  33. return nil, nil
  34. }
  35. ids := make([]int, 0, len(inbounds))
  36. for _, ib := range inbounds {
  37. ids = append(ids, ib.Id)
  38. }
  39. var disabledRows []xray.ClientTraffic
  40. err = db.Model(xray.ClientTraffic{}).
  41. Where("inbound_id IN ? AND enable = ?", ids, false).
  42. Select("inbound_id", "email").
  43. Find(&disabledRows).Error
  44. if err != nil {
  45. return nil, err
  46. }
  47. disabled := make(map[int]map[string]struct{}, len(disabledRows))
  48. for _, row := range disabledRows {
  49. if disabled[row.InboundId] == nil {
  50. disabled[row.InboundId] = map[string]struct{}{}
  51. }
  52. disabled[row.InboundId][row.Email] = struct{}{}
  53. }
  54. instances := make([]amneziawg.Instance, 0, len(inbounds))
  55. for _, ib := range inbounds {
  56. inst, ok := amneziawg.InstanceFromInbound(ib)
  57. if !ok {
  58. continue
  59. }
  60. if off := disabled[ib.Id]; len(off) > 0 {
  61. kept := make([]amneziawg.Peer, 0, len(inst.Peers))
  62. for _, p := range inst.Peers {
  63. if _, skip := off[p.Email]; !skip {
  64. kept = append(kept, p)
  65. }
  66. }
  67. inst.Peers = kept
  68. }
  69. if len(inst.Peers) == 0 {
  70. continue
  71. }
  72. instances = append(instances, inst)
  73. }
  74. return instances, nil
  75. }
  76. // applyLocalAmneziaWG pushes a single local AmneziaWG inbound's current peer
  77. // set to its interface right after a client edit commits, so an add,
  78. // removal, re-key or enable-toggle takes effect immediately instead of
  79. // waiting up to 10s for the reconcile job. It re-reads the inbound so it sees
  80. // the committed settings, filters depleted clients exactly like the
  81. // reconcile job, and is a no-op for node-owned or non-AmneziaWG inbounds.
  82. // Failures are logged and swallowed: the reconcile job is the backstop.
  83. // Mirrors applyLocalMtproto.
  84. func (s *InboundService) applyLocalAmneziaWG(inboundId int) {
  85. inbound, err := s.GetInbound(inboundId)
  86. if err != nil || inbound == nil || inbound.Protocol != model.AmneziaWG || inbound.NodeID != nil {
  87. return
  88. }
  89. rt, err := s.runtimeFor(inbound)
  90. if err != nil {
  91. return
  92. }
  93. payload := inbound
  94. if inbound.Enable {
  95. if built, bErr := s.buildInboundForLocalRuntime(database.GetDB(), inbound); bErr == nil {
  96. payload = built
  97. }
  98. }
  99. if err := rt.UpdateInbound(context.Background(), inbound, payload); err != nil {
  100. logger.Debugf("amneziawg: immediate apply failed for inbound %d: %v", inboundId, err)
  101. }
  102. }
  103. // defaultAmneziaWGServer builds a fresh server block: a random AmneziaWG 3.1
  104. // obfuscation set, the default tunnel subnet/DNS, and a freshly generated
  105. // keypair.
  106. func defaultAmneziaWGServer() (*amneziawg.ServerSettings, error) {
  107. obf := amneziawg.GenerateObfuscation31()
  108. server := &amneziawg.ServerSettings{
  109. SubnetIP: "10.8.1.0",
  110. SubnetCIDR: 24,
  111. PrimaryDNS: "8.8.8.8",
  112. SecondaryDNS: "8.8.4.4",
  113. Jc: obf.Jc,
  114. Jmin: obf.Jmin,
  115. Jmax: obf.Jmax,
  116. S1: obf.S1,
  117. S2: obf.S2,
  118. S3: obf.S3,
  119. S4: obf.S4,
  120. H1: obf.H1,
  121. H2: obf.H2,
  122. H3: obf.H3,
  123. H4: obf.H4,
  124. I1: obf.I1,
  125. HeaderProtectionKey: obf.HeaderProtectionKey,
  126. ContentPaddingAddition: obf.ContentPaddingAddition,
  127. RekeyAfterTime: obf.RekeyAfterTime,
  128. RekeyTimeout: obf.RekeyTimeout,
  129. RejectAfterTime: obf.RejectAfterTime,
  130. KeepaliveTimeout: obf.KeepaliveTimeout,
  131. MaxHandshakeAttempts: obf.MaxHandshakeAttempts,
  132. RandomTrailers: obf.RandomTrailers,
  133. DisableCookies: obf.DisableCookies,
  134. }
  135. if err := fillAmneziaWGServerKeys(server); err != nil {
  136. return nil, err
  137. }
  138. return server, nil
  139. }
  140. // fillAmneziaWGServerKeys generates a real WireGuard-compatible keypair for
  141. // the server block when one is missing.
  142. func fillAmneziaWGServerKeys(server *amneziawg.ServerSettings) error {
  143. priv, pub, err := wgutil.GenerateWireguardKeypair()
  144. if err != nil {
  145. return fmt.Errorf("amneziawg: generate server keypair: %w", err)
  146. }
  147. server.PrivateKey = priv
  148. server.PublicKey = pub
  149. return nil
  150. }
  151. // normalizeAmneziaWGSettings ensures an AmneziaWG inbound's settings have a
  152. // valid server block, generating one (fresh obfuscation params + keypair) on
  153. // first save and validating a manually-edited one so a bad entry can't bring
  154. // the interface down on the next apply. A no-op for every other protocol.
  155. func (s *InboundService) normalizeAmneziaWGSettings(inbound *model.Inbound) error {
  156. if inbound.Protocol != model.AmneziaWG {
  157. return nil
  158. }
  159. trimmed := strings.TrimSpace(inbound.Settings)
  160. if trimmed == "" || trimmed == "null" || trimmed == "{}" {
  161. server, err := defaultAmneziaWGServer()
  162. if err != nil {
  163. return err
  164. }
  165. settings := amneziawg.InboundSettings{Server: server, Clients: []model.Client{}}
  166. bs, err := json.MarshalIndent(settings, "", " ")
  167. if err != nil {
  168. return err
  169. }
  170. inbound.Settings = string(bs)
  171. return nil
  172. }
  173. var parsed amneziawg.InboundSettings
  174. if err := json.Unmarshal([]byte(inbound.Settings), &parsed); err != nil {
  175. return fmt.Errorf("amneziawg: invalid settings: %w", err)
  176. }
  177. if parsed.Server == nil {
  178. server, err := defaultAmneziaWGServer()
  179. if err != nil {
  180. return err
  181. }
  182. parsed.Server = server
  183. } else if parsed.Server.PrivateKey == "" {
  184. if err := fillAmneziaWGServerKeys(parsed.Server); err != nil {
  185. return err
  186. }
  187. }
  188. parsed.Server.HeaderProtectionKey = strings.TrimSpace(parsed.Server.HeaderProtectionKey)
  189. for _, f := range []*string{
  190. &parsed.Server.ContentPaddingAddition, &parsed.Server.RekeyAfterTime,
  191. &parsed.Server.RekeyTimeout, &parsed.Server.RejectAfterTime,
  192. &parsed.Server.KeepaliveTimeout, &parsed.Server.MaxHandshakeAttempts,
  193. } {
  194. *f = amneziawg.CanonicalizeUintRange(*f)
  195. }
  196. if err := amneziawg.ValidateObfuscation(parsed.Server.Obfuscation()); err != nil {
  197. return fmt.Errorf("amneziawg: %w", err)
  198. }
  199. if err := amneziawg.ValidateIPv6Subnet(parsed.Server.IPv6Enabled, parsed.Server.IPv6Subnet); err != nil {
  200. return fmt.Errorf("amneziawg: %w", err)
  201. }
  202. if err := amneziawg.ValidateSubnetIPv4(parsed.Server.SubnetIP, parsed.Server.SubnetCIDR); err != nil {
  203. return fmt.Errorf("amneziawg: %w", err)
  204. }
  205. if err := amneziawg.ValidateInterfaceName(parsed.Server.ExternalInterface); err != nil {
  206. return fmt.Errorf("amneziawg: externalInterface: %w", err)
  207. }
  208. if err := amneziawg.ValidateInterfaceName(parsed.Server.IPv6ExternalInterface); err != nil {
  209. return fmt.Errorf("amneziawg: ipv6ExternalInterface: %w", err)
  210. }
  211. if err := amneziawg.ValidateConfigValue("privateKey", parsed.Server.PrivateKey); err != nil {
  212. return fmt.Errorf("amneziawg: %w", err)
  213. }
  214. if err := amneziawg.ValidateConfigValue("publicKey", parsed.Server.PublicKey); err != nil {
  215. return fmt.Errorf("amneziawg: %w", err)
  216. }
  217. signaturePackets := []struct{ field, v string }{
  218. {"i1", parsed.Server.I1},
  219. {"i2", parsed.Server.I2},
  220. {"i3", parsed.Server.I3},
  221. {"i4", parsed.Server.I4},
  222. {"i5", parsed.Server.I5},
  223. }
  224. for _, sp := range signaturePackets {
  225. if err := amneziawg.ValidateConfigValue(sp.field, sp.v); err != nil {
  226. return fmt.Errorf("amneziawg: %w", err)
  227. }
  228. }
  229. portCtx, err := s.loadPortConflictContext(database.GetDB())
  230. if err != nil {
  231. return err
  232. }
  233. for i := range parsed.Clients {
  234. c := &parsed.Clients[i]
  235. if hit := s.checkForwardedPortsConflict(portCtx, c.ForwardedPorts); hit != "" {
  236. return fmt.Errorf("amneziawg: client %q forwardedPorts collides with %s", c.Email, hit)
  237. }
  238. if err := amneziawg.ValidateConfigValue("email", c.Email); err != nil {
  239. return fmt.Errorf("amneziawg: %w", err)
  240. }
  241. if err := amneziawg.ValidateConfigValue("publicKey", c.PublicKey); err != nil {
  242. return fmt.Errorf("amneziawg: client %q: %w", c.Email, err)
  243. }
  244. if err := amneziawg.ValidateConfigValue("preSharedKey", c.PreSharedKey); err != nil {
  245. return fmt.Errorf("amneziawg: client %q: %w", c.Email, err)
  246. }
  247. // AllowedIPs lands verbatim in a rendered [Peer] block, so a newline here
  248. // re-opens an [Interface] section whose PostUp runs as root once the
  249. // downloaded config is applied (client app, or awg-quick directly).
  250. normalized, err := normalizeWireguardAllowedIPs(c.AllowedIPs)
  251. if err != nil {
  252. return fmt.Errorf("amneziawg: client %q: %w", c.Email, err)
  253. }
  254. c.AllowedIPs = normalized
  255. }
  256. bs, err := json.MarshalIndent(parsed, "", " ")
  257. if err != nil {
  258. return err
  259. }
  260. inbound.Settings = string(bs)
  261. return nil
  262. }
  263. // portConflictContext caches the state checkForwardedPortsConflict needs —
  264. // the panel's own port and this host's enabled inbound ports — so validating
  265. // N clients in one save (normalizeAmneziaWGSettings, or a bulk client add)
  266. // costs one query total instead of N. Load it once with
  267. // loadPortConflictContext and pass it to every checkForwardedPortsConflict
  268. // call in that batch.
  269. type portConflictContext struct {
  270. webPort int
  271. inbounds []*model.Inbound
  272. }
  273. // loadPortConflictContext loads the panel's own port and every enabled
  274. // inbound hosted on THIS panel (node_id IS NULL) — an inbound hosted on a
  275. // different node listens on that node's own host, never this one, so it can
  276. // never collide with a DNAT rule this process installs.
  277. func (s *InboundService) loadPortConflictContext(db *gorm.DB) (portConflictContext, error) {
  278. var ctx portConflictContext
  279. if webPort, err := (&SettingService{}).GetPort(); err == nil {
  280. ctx.webPort = webPort
  281. }
  282. err := db.Model(model.Inbound{}).
  283. Where("enable = ? AND node_id IS NULL", true).
  284. Find(&ctx.inbounds).Error
  285. return ctx, err
  286. }
  287. // checkForwardedPortsConflict reports whether a client's ForwardedPorts spec
  288. // exceeds the cap, covers the panel's own web port, one of this host's own
  289. // enabled inbound listen ports, or an AmneziaWG inbound's own phantom SOCKS5
  290. // relay port (SOCKSPortForInbound -- never a real inbounds row, so the loop
  291. // below can't see it any other way). A collision on the SOCKS5 port would
  292. // let a port-forward listener race Xray's own relay for the bind and, if it
  293. // wins, take down that inbound's entire relay rather than just one forward.
  294. // Returns a human-readable description of the first collision found, or ""
  295. // when there is none.
  296. func (s *InboundService) checkForwardedPortsConflict(ctx portConflictContext, forwardedPorts string) string {
  297. if forwardedPorts == "" {
  298. return ""
  299. }
  300. if amneziawg.ExceedsForwardedPortsCap(forwardedPorts) {
  301. return fmt.Sprintf("more than %d forwarded ports", amneziawg.MaxForwardedPorts)
  302. }
  303. if ctx.webPort > 0 && amneziawg.ForwardedPortsInclude(forwardedPorts, ctx.webPort) {
  304. return fmt.Sprintf("the panel's own port (%d)", ctx.webPort)
  305. }
  306. for _, ib := range ctx.inbounds {
  307. if amneziawg.ForwardedPortsInclude(forwardedPorts, ib.Port) {
  308. name := ib.Remark
  309. if name == "" {
  310. name = ib.Tag
  311. }
  312. return fmt.Sprintf("inbound '%s' (#%d, port %d)", name, ib.Id, ib.Port)
  313. }
  314. if ib.Protocol != model.AmneziaWG {
  315. continue
  316. }
  317. socksPort := amneziawgnet.SOCKSPortForInbound(ib.Id)
  318. if amneziawg.ForwardedPortsInclude(forwardedPorts, socksPort) {
  319. name := ib.Remark
  320. if name == "" {
  321. name = ib.Tag
  322. }
  323. return fmt.Sprintf("inbound '%s' (#%d)'s own SOCKS5 relay port (%d)", name, ib.Id, socksPort)
  324. }
  325. }
  326. return ""
  327. }
  328. // GetAmneziaWGDiagnostics returns a live diagnostics snapshot for inbound
  329. // id: interface up/down, listen port, and per-client handshake/traffic
  330. // state, read entirely from data amneziawgnet.Manager already tracks --
  331. // gathering it can never itself change anything. Returns an error only
  332. // when id doesn't name an AmneziaWG inbound at all; an inbound that simply
  333. // isn't running right now (disabled, no enabled clients, or reconcile
  334. // hasn't caught up yet) comes back as amneziawgnet.Diagnostics{}
  335. // (Running=false), not an error, since that's a normal state an admin
  336. // might specifically be checking for.
  337. func (s *InboundService) GetAmneziaWGDiagnostics(id int) (amneziawgnet.Diagnostics, error) {
  338. inbound, err := s.GetInbound(id)
  339. if err != nil {
  340. return amneziawgnet.Diagnostics{}, err
  341. }
  342. if inbound.Protocol != model.AmneziaWG {
  343. return amneziawgnet.Diagnostics{}, fmt.Errorf("inbound %d is not an AmneziaWG inbound", id)
  344. }
  345. inst, ok := amneziawg.InstanceFromInbound(inbound)
  346. if !ok {
  347. return amneziawgnet.Diagnostics{}, nil
  348. }
  349. return amneziawgnet.Diagnose(inst.Id, inst.Peers), nil
  350. }