types.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Package amneziawg holds the AmneziaWG protocol's shared, DB-backed shapes
  2. // (Instance, Peer, Obfuscation31, ServerSettings/InboundSettings) and the
  3. // pure functions that derive an Instance from a stored inbound row. It has
  4. // no OS dependency of its own: internal/amneziawgnet embeds amneziawg-go
  5. // over a gVisor netstack and owns the actual running interfaces, one
  6. // Manager-managed Device per desired Instance -- see that package's Manager
  7. // for the reconcile-on-tick lifecycle (modeled on internal/mtproto's own
  8. // Manager), and instance.go's own doc comment for how this package's role
  9. // narrowed to protocol-shape-only after the kernel-module (DKMS) + awg-quick
  10. // architecture this fork originally shipped was retired.
  11. package amneziawg
  12. import "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  13. // Obfuscation31 is an AmneziaWG 3.1 obfuscation parameter set (junk packets,
  14. // padding, magic headers, the five CPS signature-packet slots, and the 3.x
  15. // header-protection/content-padding/timing/boolean fields). The same values
  16. // must be applied on both ends of a tunnel, so the server stores them and
  17. // every client config inherits them verbatim.
  18. type Obfuscation31 struct {
  19. Jc int `json:"jc"`
  20. Jmin int `json:"jmin"`
  21. Jmax int `json:"jmax"`
  22. S1 int `json:"s1"`
  23. S2 int `json:"s2"`
  24. S3 int `json:"s3"`
  25. S4 int `json:"s4"`
  26. H1 string `json:"h1"`
  27. H2 string `json:"h2"`
  28. H3 string `json:"h3"`
  29. H4 string `json:"h4"`
  30. // I1-I5 are the real protocol's five CPS signature-packet slots
  31. // (confirmed against amneziawg-go v3.0.3's device/uapi.go: "i1"
  32. // through "i5" are five independent UAPI setters, device.ipackets[0..4],
  33. // all parsed via the identical newObfChain grammar).
  34. I1 string `json:"i1,omitempty"`
  35. I2 string `json:"i2,omitempty"`
  36. I3 string `json:"i3,omitempty"`
  37. I4 string `json:"i4,omitempty"`
  38. I5 string `json:"i5,omitempty"`
  39. // HeaderProtectionKey is a base64 32-byte key shared by both ends; the
  40. // ranges/booleans below are 3.x-only and optional.
  41. HeaderProtectionKey string `json:"headerProtectionKey,omitempty"`
  42. ContentPaddingAddition string `json:"contentPaddingAddition,omitempty"`
  43. RekeyAfterTime string `json:"rekeyAfterTime,omitempty"`
  44. RekeyTimeout string `json:"rekeyTimeout,omitempty"`
  45. RejectAfterTime string `json:"rejectAfterTime,omitempty"`
  46. KeepaliveTimeout string `json:"keepaliveTimeout,omitempty"`
  47. MaxHandshakeAttempts string `json:"maxHandshakeAttempts,omitempty"`
  48. RandomTrailers bool `json:"randomTrailers,omitempty"`
  49. DisableCookies bool `json:"disableCookies,omitempty"`
  50. }
  51. // Peer is one desired AmneziaWG peer: a client device the interface accepts.
  52. // Email attributes traffic and online status back to the owning client, the
  53. // same role SecretEntry.Name plays for mtproto.
  54. type Peer struct {
  55. Email string
  56. PublicKey string
  57. PresharedKey string
  58. AllowedIPs []string
  59. // ForwardedPorts is a raw, user-supplied port list ("80, 443, 8000-8100")
  60. // forwarded to this peer's tunnel address by internal/amneziawgnet's
  61. // PortForwardSet listener supervisor. Empty means no port-forwarding.
  62. ForwardedPorts string
  63. }
  64. // Instance is the desired runtime configuration of one AmneziaWG inbound: a
  65. // single interface (e.g. awg1) with a set of peers, mirroring how one mtproto
  66. // inbound maps to one mtg process (internal/mtproto.Instance).
  67. type Instance struct {
  68. Id int
  69. Tag string
  70. InterfaceName string
  71. ListenPort int
  72. PrivateKey string
  73. PublicKey string
  74. // Address holds the interface's own tunnel address(es), e.g. "10.8.1.1/24".
  75. // Carries both the IPv4 and (when enabled) IPv6 server address.
  76. Address []string
  77. MTU int
  78. // Obfuscation carries the full AmneziaWG 3.1 parameter set, including
  79. // the 3.x header-protection/content-padding/timing/boolean fields (see
  80. // Obfuscation31's own doc comment) -- amneziawgnet.DeviceOptions is
  81. // what actually consumes it when building the embedded Device's UAPI
  82. // config.
  83. Obfuscation Obfuscation31
  84. Peers []Peer
  85. // ExternalInterface named the host NIC PostUp/PostDown NAT rules
  86. // attached to under the retired kernel-module architecture. Also the
  87. // fallback host NIC internal/amneziawgnet's IPv6-address-alias
  88. // mechanism (desiredV6Aliases) uses when IPv6ExternalInterface is left
  89. // blank.
  90. ExternalInterface string
  91. // IPv6Enabled/IPv6ExternalInterface gate internal/amneziawgnet's
  92. // IPv6-address-alias mechanism (desiredV6Aliases,
  93. // internal/web/service/xray.go's injectAmneziawgV6Egress): each peer
  94. // with an IPv6 AllowedIPs entry gets that address aliased onto this
  95. // host NIC (ip -6 addr add) and a dedicated Xray freedom outbound bound
  96. // to it, giving that peer's own outbound connections a distinct public
  97. // source identity. Narrower in scope than these identically-named
  98. // fields' role under the retired kernel-module architecture, which used
  99. // per-peer NDP-proxy entries (ip -6 neigh add proxy) to also support
  100. // unsolicited inbound connections toward the peer -- that capability is
  101. // the separate, not-yet-built Phase 3.6 (port-forwarding).
  102. IPv6Enabled bool
  103. IPv6ExternalInterface string
  104. // RouteThroughXray gated the kernel-module architecture's opt-in
  105. // TPROXY-into-Xray bridge. The embedded path (internal/amneziawgnet)
  106. // has no equivalent opt-in at all -- every peer's traffic already goes
  107. // through Xray's own SOCKS5 inbound unconditionally, since there's no
  108. // other way for decapsulated gVisor traffic to reach the real internet
  109. // -- so this field is now vestigial: read from existing stored settings
  110. // for backward compatibility, but not acted on by anything. Slated for
  111. // removal alongside the frontend toggle in a follow-up.
  112. RouteThroughXray bool
  113. }
  114. // ServerSettings is the "server" block of an AmneziaWG inbound's Settings
  115. // JSON: the interface-level configuration shared by every client/peer. The
  116. // listen port is deliberately not duplicated here — it lives on the inbound
  117. // row itself (Inbound.Port), like every other protocol.
  118. type ServerSettings struct {
  119. PrivateKey string `json:"privateKey"`
  120. PublicKey string `json:"publicKey"`
  121. SubnetIP string `json:"subnetIp"`
  122. SubnetCIDR int `json:"subnetCidr"`
  123. MTU int `json:"mtu,omitempty"`
  124. // PrimaryDNS/SecondaryDNS seed client configs' DNS line. Blank is
  125. // meaningful, so no omitempty: a dropped key resurrects frontend defaults.
  126. PrimaryDNS string `json:"primaryDns"`
  127. SecondaryDNS string `json:"secondaryDns"`
  128. // ExternalInterface, IPv6Enabled, and IPv6ExternalInterface are live
  129. // again as of Phase 3.5 -- see the matching fields on Instance for what
  130. // they gate (internal/amneziawgnet's IPv6-address-alias mechanism).
  131. // IPv6Subnet was never actually vestigial either: InstanceFromInbound
  132. // already consumes it (via serverAddressV6) to build the server's own
  133. // tunnel address, same as always. Only RouteThroughXray, below, remains
  134. // genuinely vestigial as of the hard cutover to the embedded path
  135. // (internal/amneziawgnet) -- read from existing stored settings for
  136. // backward compatibility, but not acted on by anything.
  137. ExternalInterface string `json:"externalInterface,omitempty"`
  138. IPv6Enabled bool `json:"ipv6Enabled,omitempty"`
  139. IPv6Subnet string `json:"ipv6Subnet,omitempty"`
  140. IPv6ExternalInterface string `json:"ipv6ExternalInterface,omitempty"`
  141. RouteThroughXray bool `json:"routeThroughXray,omitempty"`
  142. // Obfuscation31's fields, repeated flat (not embedded) rather than
  143. // nested under their own key: encoding/json would happily inline an
  144. // embedded Obfuscation31 the same way, but the frontend's Go->Zod/TS
  145. // generator (tools/openapigen) does not — it emits a genuinely nested
  146. // `obfuscation31` object, which would silently diverge from the real
  147. // wire JSON. See Obfuscation() below for the manager-facing conversion.
  148. Jc int `json:"jc"`
  149. Jmin int `json:"jmin"`
  150. Jmax int `json:"jmax"`
  151. S1 int `json:"s1"`
  152. S2 int `json:"s2"`
  153. S3 int `json:"s3"`
  154. S4 int `json:"s4"`
  155. H1 string `json:"h1"`
  156. H2 string `json:"h2"`
  157. H3 string `json:"h3"`
  158. H4 string `json:"h4"`
  159. I1 string `json:"i1,omitempty"`
  160. I2 string `json:"i2,omitempty"`
  161. I3 string `json:"i3,omitempty"`
  162. I4 string `json:"i4,omitempty"`
  163. I5 string `json:"i5,omitempty"`
  164. // HeaderProtectionKey and ContentPaddingAddition are AmneziaWG 3.0
  165. // fields, flat and top-level for the same tools/openapigen reason as
  166. // the block above; Obfuscation() below folds them back into
  167. // Obfuscation31's own identically named fields.
  168. // HeaderProtectionKey is a base64 32-byte key; empty (the default)
  169. // disables AWG 3.0 header protection. A non-empty value requires
  170. // every one of S1-S4 above to be >= 12 -- ValidateObfuscation
  171. // enforces this at save time, not just at IpcSet time.
  172. // ContentPaddingAddition is a "low-high" range or bare integer, the
  173. // same grammar and uint32 cap as H1-H4.
  174. HeaderProtectionKey string `json:"headerProtectionKey,omitempty"`
  175. ContentPaddingAddition string `json:"contentPaddingAddition,omitempty"`
  176. // RekeyAfterTime/RekeyTimeout/RejectAfterTime/KeepaliveTimeout/
  177. // MaxHandshakeAttempts mirror Instance's identically named fields --
  178. // see that type's own doc comment for the grammar/width/real-default
  179. // details. Flat and top-level for the same tools/openapigen reason as
  180. // the rest of this struct.
  181. RekeyAfterTime string `json:"rekeyAfterTime,omitempty"`
  182. RekeyTimeout string `json:"rekeyTimeout,omitempty"`
  183. RejectAfterTime string `json:"rejectAfterTime,omitempty"`
  184. KeepaliveTimeout string `json:"keepaliveTimeout,omitempty"`
  185. MaxHandshakeAttempts string `json:"maxHandshakeAttempts,omitempty"`
  186. // RandomTrailers/DisableCookies mirror Instance's identically named
  187. // AmneziaWG 3.1 fields -- see that type's own doc comment for the real
  188. // protocol/interop details. Both real bool fields (not omitempty):
  189. // buildUAPIConfig always emits both lines explicitly so the
  190. // reconfigure-in-place diff correctly notices a true->false edit, not
  191. // just false->true.
  192. RandomTrailers bool `json:"randomTrailers"`
  193. DisableCookies bool `json:"disableCookies"`
  194. }
  195. // Obfuscation extracts the Obfuscation31 parameter set from a ServerSettings
  196. // block, for callers (the Manager, ValidateObfuscation) that want the
  197. // grouped type rather than the flat wire fields.
  198. func (s ServerSettings) Obfuscation() Obfuscation31 {
  199. return Obfuscation31{
  200. Jc: s.Jc, Jmin: s.Jmin, Jmax: s.Jmax,
  201. S1: s.S1, S2: s.S2, S3: s.S3, S4: s.S4,
  202. H1: s.H1, H2: s.H2, H3: s.H3, H4: s.H4,
  203. I1: s.I1, I2: s.I2, I3: s.I3, I4: s.I4, I5: s.I5,
  204. HeaderProtectionKey: s.HeaderProtectionKey,
  205. ContentPaddingAddition: s.ContentPaddingAddition,
  206. RekeyAfterTime: s.RekeyAfterTime,
  207. RekeyTimeout: s.RekeyTimeout,
  208. RejectAfterTime: s.RejectAfterTime,
  209. KeepaliveTimeout: s.KeepaliveTimeout,
  210. MaxHandshakeAttempts: s.MaxHandshakeAttempts,
  211. RandomTrailers: s.RandomTrailers,
  212. DisableCookies: s.DisableCookies,
  213. }
  214. }
  215. // InboundSettings is the full Settings JSON shape stored on an AmneziaWG
  216. // inbound row: one server block plus the usual generic client list, so bulk
  217. // operations, the QR modal and subscriptions all come from the same shared
  218. // infrastructure every other protocol uses.
  219. type InboundSettings struct {
  220. Server *ServerSettings `json:"server"`
  221. Clients []model.Client `json:"clients"`
  222. }