-
-
Notifications
You must be signed in to change notification settings - Fork 545
feat(wireguard): amneziawg implementation #3150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
5ce77db
25c7241
6d6e10e
75bf368
7ea30cd
1f251a5
e75634c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| scratch.txt | ||
| .DS_Store |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| package settings | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/qdm12/gosettings" | ||
| "github.com/qdm12/gotree" | ||
| ) | ||
|
|
||
| type AmneziaWg struct { | ||
| JunkPacketCount uint16 `json:"junk_packet_count"` | ||
| JunkPacketMin uint16 `json:"junk_packet_min"` | ||
| JunkPacketMax uint16 `json:"junk_packet_max"` | ||
| PaddingS1 uint16 `json:"padding_s1"` | ||
| PaddingS2 uint16 `json:"padding_s2"` | ||
| PaddingS3 uint16 `json:"padding_s3"` | ||
| PaddingS4 uint16 `json:"padding_s4"` | ||
| HeaderH1 string `json:"header_h1"` | ||
| HeaderH2 string `json:"header_h2"` | ||
| HeaderH3 string `json:"header_h3"` | ||
| HeaderH4 string `json:"header_h4"` | ||
| InitPacketI1 string `json:"init_packet_i1"` | ||
| InitPacketI2 string `json:"init_packet_i2"` | ||
| InitPacketI3 string `json:"init_packet_i3"` | ||
| InitPacketI4 string `json:"init_packet_i4"` | ||
| InitPacketI5 string `json:"init_packet_i5"` | ||
| } | ||
|
|
||
| func (s AmneziaWg) copy() (copied AmneziaWg) { | ||
| copied.JunkPacketCount = s.JunkPacketCount | ||
| copied.JunkPacketMin = s.JunkPacketMin | ||
| copied.JunkPacketMax = s.JunkPacketMax | ||
| copied.PaddingS1 = s.PaddingS1 | ||
| copied.PaddingS2 = s.PaddingS2 | ||
| copied.PaddingS3 = s.PaddingS3 | ||
| copied.PaddingS4 = s.PaddingS4 | ||
| copied.HeaderH1 = s.HeaderH1 | ||
| copied.HeaderH2 = s.HeaderH2 | ||
| copied.HeaderH3 = s.HeaderH3 | ||
| copied.HeaderH4 = s.HeaderH4 | ||
| copied.InitPacketI1 = s.InitPacketI1 | ||
| copied.InitPacketI2 = s.InitPacketI2 | ||
| copied.InitPacketI3 = s.InitPacketI3 | ||
| copied.InitPacketI4 = s.InitPacketI4 | ||
| copied.InitPacketI5 = s.InitPacketI5 | ||
| return copied | ||
| } | ||
|
|
||
| //nolint:dupl | ||
| func (s *AmneziaWg) overrideWith(other AmneziaWg) { | ||
| s.JunkPacketCount = gosettings.OverrideWithComparable(s.JunkPacketCount, other.JunkPacketCount) | ||
| s.JunkPacketMin = gosettings.OverrideWithComparable(s.JunkPacketMin, other.JunkPacketMin) | ||
| s.JunkPacketMax = gosettings.OverrideWithComparable(s.JunkPacketMax, other.JunkPacketMax) | ||
| s.PaddingS1 = gosettings.OverrideWithComparable(s.PaddingS1, other.PaddingS1) | ||
| s.PaddingS2 = gosettings.OverrideWithComparable(s.PaddingS2, other.PaddingS2) | ||
| s.PaddingS3 = gosettings.OverrideWithComparable(s.PaddingS3, other.PaddingS3) | ||
| s.PaddingS4 = gosettings.OverrideWithComparable(s.PaddingS4, other.PaddingS4) | ||
| s.HeaderH1 = gosettings.OverrideWithComparable(s.HeaderH1, other.HeaderH1) | ||
| s.HeaderH2 = gosettings.OverrideWithComparable(s.HeaderH2, other.HeaderH2) | ||
| s.HeaderH3 = gosettings.OverrideWithComparable(s.HeaderH3, other.HeaderH3) | ||
| s.HeaderH4 = gosettings.OverrideWithComparable(s.HeaderH4, other.HeaderH4) | ||
| s.InitPacketI1 = gosettings.OverrideWithComparable(s.InitPacketI1, other.InitPacketI1) | ||
| s.InitPacketI2 = gosettings.OverrideWithComparable(s.InitPacketI2, other.InitPacketI2) | ||
| s.InitPacketI3 = gosettings.OverrideWithComparable(s.InitPacketI3, other.InitPacketI3) | ||
| s.InitPacketI4 = gosettings.OverrideWithComparable(s.InitPacketI4, other.InitPacketI4) | ||
| s.InitPacketI5 = gosettings.OverrideWithComparable(s.InitPacketI5, other.InitPacketI5) | ||
| } | ||
|
|
||
| func (s AmneziaWg) toLinesNode() (node *gotree.Node) { | ||
| node = gotree.New("Amneziawg parameters:") | ||
|
|
||
| uintFields := []struct { | ||
| key string | ||
| val uint16 | ||
| }{ | ||
| {"jc", s.JunkPacketCount}, | ||
| {"jmin", s.JunkPacketMin}, | ||
| {"jmax", s.JunkPacketMax}, | ||
| {"s1", s.PaddingS1}, | ||
| {"s2", s.PaddingS2}, | ||
| {"s3", s.PaddingS3}, | ||
| {"s4", s.PaddingS4}, | ||
| } | ||
| for _, f := range uintFields { | ||
| if f.val != 0 { | ||
| node.Appendf("%s: %d", f.key, f.val) | ||
| } | ||
| } | ||
| stringFields := []struct { | ||
| key string | ||
| val string | ||
| }{ | ||
| {"h1", s.HeaderH1}, | ||
| {"h2", s.HeaderH2}, | ||
| {"h3", s.HeaderH3}, | ||
| {"h4", s.HeaderH4}, | ||
| {"i1", s.InitPacketI1}, | ||
| {"i2", s.InitPacketI2}, | ||
| {"i3", s.InitPacketI3}, | ||
| {"i4", s.InitPacketI4}, | ||
| {"i5", s.InitPacketI5}, | ||
| } | ||
| for _, f := range stringFields { | ||
| if f.val != "" { | ||
| node.Appendf("%s: %s", f.key, f.val) | ||
| } | ||
| } | ||
|
|
||
| return node | ||
| } | ||
|
|
||
| var ( | ||
| ErrJunkPacketBounds = errors.New("junk packet minimum must be lower than or equal to maximum") | ||
| ErrJunkPacketMinMaxNotSet = errors.New("junk packet min and max must be set when junk packet count is set") | ||
| ErrJunkPacketCountNotSet = errors.New("junk packet count must be set when junk packet min or max is set") | ||
| ) | ||
|
|
||
| func (s AmneziaWg) validate() error { | ||
| switch { | ||
| case s.JunkPacketMax != 0 && s.JunkPacketMin > s.JunkPacketMax: | ||
| return fmt.Errorf("%w: jmin=%d and jmax=%d", | ||
| ErrJunkPacketBounds, s.JunkPacketMin, s.JunkPacketMax) | ||
| case s.JunkPacketCount == 0 && (s.JunkPacketMin != 0 || s.JunkPacketMax != 0): | ||
| return fmt.Errorf("%w: jc=%d and jmin=%d and jmax=%d", | ||
| ErrJunkPacketCountNotSet, s.JunkPacketCount, s.JunkPacketMin, s.JunkPacketMax) | ||
| case s.JunkPacketCount != 0 && (s.JunkPacketMin == 0 || s.JunkPacketMax == 0): | ||
| return fmt.Errorf("%w: jc=%d and jmin=%d and jmax=%d", | ||
| ErrJunkPacketMinMaxNotSet, s.JunkPacketCount, s.JunkPacketMin, s.JunkPacketMax) | ||
| } | ||
|
|
||
| return nil | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,10 +42,12 @@ type Wireguard struct { | |
| // 0 indicating to use PMTUD. | ||
| MTU *uint32 `json:"mtu"` | ||
| // Implementation is the Wireguard implementation to use. | ||
| // It can be "auto", "userspace" or "kernelspace". | ||
| // It can be "auto", "userspace", "kernelspace" or "amneziawg". | ||
| // It defaults to "auto" and cannot be the empty string | ||
| // in the internal state. | ||
| Implementation string `json:"implementation"` | ||
| // AmneziaWG contains obfuscation parameters | ||
| AmneziaWG AmneziaWg `json:"amneziawg"` | ||
| } | ||
|
|
||
| var regexpInterfaceName = regexp.MustCompile(`^[a-zA-Z0-9_]+$`) | ||
|
|
@@ -136,11 +138,16 @@ func (w Wireguard) validate(vpnProvider string, ipv6Supported bool) (err error) | |
| ErrWireguardInterfaceNotValid, w.Interface, regexpInterfaceName) | ||
| } | ||
|
|
||
| validImplementations := []string{"auto", "userspace", "kernelspace"} | ||
| validImplementations := []string{"auto", "userspace", "kernelspace", "amneziawg"} | ||
| if err := validate.IsOneOf(w.Implementation, validImplementations...); err != nil { | ||
| return fmt.Errorf("%w: %w", ErrWireguardImplementationNotValid, err) | ||
| } | ||
|
|
||
| err = w.AmneziaWG.validate() | ||
| if err != nil { | ||
| return fmt.Errorf("amneziawg settings: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
|
|
@@ -154,6 +161,7 @@ func (w *Wireguard) copy() (copied Wireguard) { | |
| Interface: w.Interface, | ||
| MTU: w.MTU, | ||
| Implementation: w.Implementation, | ||
| AmneziaWG: w.AmneziaWG.copy(), | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -167,6 +175,7 @@ func (w *Wireguard) overrideWith(other Wireguard) { | |
| w.Interface = gosettings.OverrideWithComparable(w.Interface, other.Interface) | ||
| w.MTU = gosettings.OverrideWithComparable(w.MTU, other.MTU) | ||
| w.Implementation = gosettings.OverrideWithComparable(w.Implementation, other.Implementation) | ||
| w.AmneziaWG.overrideWith(other.AmneziaWG) | ||
| } | ||
|
|
||
| func (w *Wireguard) setDefaults(vpnProvider string) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should set defaults to the amnesiawg config (maybe
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll set them as zero
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (sorry to reconsider this again) there is a use case for the zero value if the default is Then you need to:
|
||
|
|
@@ -232,7 +241,11 @@ func (w Wireguard) toLinesNode() (node *gotree.Node) { | |
| } | ||
|
|
||
| if w.Implementation != "auto" { | ||
| node.Appendf("Implementation: %s", w.Implementation) | ||
| implNode := node.Appendf("Implementation: %s", w.Implementation) | ||
|
|
||
| if w.Implementation == "amneziawg" { | ||
| implNode.AppendNode(w.AmneziaWG.toLinesNode()) | ||
| } | ||
| } | ||
|
|
||
| return node | ||
|
|
@@ -245,6 +258,51 @@ func (w *Wireguard) read(r *reader.Reader) (err error) { | |
| reader.RetroKeys("WIREGUARD_INTERFACE"), reader.ForceLowercase(false)) | ||
| w.Implementation = r.String("WIREGUARD_IMPLEMENTATION") | ||
|
|
||
| w.AmneziaWG.JunkPacketCount, err = r.Uint16("WIREGUARD_JC") | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| w.AmneziaWG.JunkPacketMin, err = r.Uint16("WIREGUARD_JMIN") | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| w.AmneziaWG.JunkPacketMax, err = r.Uint16("WIREGUARD_JMAX") | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| w.AmneziaWG.PaddingS1, err = r.Uint16("WIREGUARD_S1") | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| w.AmneziaWG.PaddingS2, err = r.Uint16("WIREGUARD_S2") | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| w.AmneziaWG.PaddingS3, err = r.Uint16("WIREGUARD_S3") | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| w.AmneziaWG.PaddingS4, err = r.Uint16("WIREGUARD_S4") | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| w.AmneziaWG.HeaderH1 = r.String("WIREGUARD_H1", reader.ForceLowercase(false)) | ||
| w.AmneziaWG.HeaderH2 = r.String("WIREGUARD_H2", reader.ForceLowercase(false)) | ||
| w.AmneziaWG.HeaderH3 = r.String("WIREGUARD_H3", reader.ForceLowercase(false)) | ||
| w.AmneziaWG.HeaderH4 = r.String("WIREGUARD_H4", reader.ForceLowercase(false)) | ||
| w.AmneziaWG.InitPacketI1 = r.String("WIREGUARD_I1", reader.ForceLowercase(false)) | ||
| w.AmneziaWG.InitPacketI2 = r.String("WIREGUARD_I2", reader.ForceLowercase(false)) | ||
| w.AmneziaWG.InitPacketI3 = r.String("WIREGUARD_I3", reader.ForceLowercase(false)) | ||
| w.AmneziaWG.InitPacketI4 = r.String("WIREGUARD_I4", reader.ForceLowercase(false)) | ||
| w.AmneziaWG.InitPacketI5 = r.String("WIREGUARD_I5", reader.ForceLowercase(false)) | ||
|
Comment on lines
+261
to
+304
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move these to amneziawg.go as its own |
||
|
|
||
| addressStrings := r.CSV("WIREGUARD_ADDRESSES", reader.RetroKeys("WIREGUARD_ADDRESS")) | ||
| // WARNING: do not initialize w.Addresses to an empty slice | ||
| // or the defaults for nordvpn will not work. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry to re-iterate on that, re-reading the code I think it's fine to have these changed to
AMENZIAWG_XYZas long asinternal/configuration/sources/files/reader.gostring cases are changed, please update to useAMNEZIAWG_prefixes for the options 🙏 Thanks!