@@ -41,15 +41,17 @@ type WGUSPConfigurer struct {
4141 device * device.Device
4242 deviceName string
4343 activityRecorder * bind.ActivityRecorder
44+ amneziaConfig AmneziaConfig
4445
4546 uapiListener net.Listener
4647}
4748
48- func NewUSPConfigurer (device * device.Device , deviceName string , activityRecorder * bind.ActivityRecorder ) * WGUSPConfigurer {
49+ func NewUSPConfigurer (device * device.Device , deviceName string , activityRecorder * bind.ActivityRecorder , amneziaConfig AmneziaConfig ) * WGUSPConfigurer {
4950 wgCfg := & WGUSPConfigurer {
5051 device : device ,
5152 deviceName : deviceName ,
5253 activityRecorder : activityRecorder ,
54+ amneziaConfig : amneziaConfig ,
5355 }
5456 wgCfg .startUAPI ()
5557 return wgCfg
@@ -69,7 +71,7 @@ func (c *WGUSPConfigurer) ConfigureInterface(privateKey string, port int) error
6971 ListenPort : & port ,
7072 }
7173
72- return c .device .IpcSet (toWgUserspaceString (config ))
74+ return c .device .IpcSet (c . toWgUserspaceString (config ))
7375}
7476
7577func (c * WGUSPConfigurer ) UpdatePeer (peerKey string , allowedIps []netip.Prefix , keepAlive time.Duration , endpoint * net.UDPAddr , preSharedKey * wgtypes.Key ) error {
@@ -91,7 +93,7 @@ func (c *WGUSPConfigurer) UpdatePeer(peerKey string, allowedIps []netip.Prefix,
9193 Peers : []wgtypes.PeerConfig {peer },
9294 }
9395
94- if ipcErr := c .device .IpcSet (toWgUserspaceString (config )); ipcErr != nil {
96+ if ipcErr := c .device .IpcSet (c . toWgUserspaceString (config )); ipcErr != nil {
9597 return ipcErr
9698 }
9799
@@ -145,7 +147,7 @@ func (c *WGUSPConfigurer) RemoveEndpointAddress(peerKey string) error {
145147 config := wgtypes.Config {
146148 Peers : []wgtypes.PeerConfig {peer },
147149 }
148- if ipcErr := c .device .IpcSet (toWgUserspaceString (config )); ipcErr != nil {
150+ if ipcErr := c .device .IpcSet (c . toWgUserspaceString (config )); ipcErr != nil {
149151 return fmt .Errorf ("failed to remove peer: %s" , ipcErr )
150152 }
151153
@@ -160,7 +162,7 @@ func (c *WGUSPConfigurer) RemoveEndpointAddress(peerKey string) error {
160162 Peers : []wgtypes.PeerConfig {peer },
161163 }
162164
163- if err := c .device .IpcSet (toWgUserspaceString (config )); err != nil {
165+ if err := c .device .IpcSet (c . toWgUserspaceString (config )); err != nil {
164166 return fmt .Errorf ("remove endpoint address: %w" , err )
165167 }
166168
@@ -181,7 +183,7 @@ func (c *WGUSPConfigurer) RemovePeer(peerKey string) error {
181183 config := wgtypes.Config {
182184 Peers : []wgtypes.PeerConfig {peer },
183185 }
184- ipcErr := c .device .IpcSet (toWgUserspaceString (config ))
186+ ipcErr := c .device .IpcSet (c . toWgUserspaceString (config ))
185187
186188 c .activityRecorder .Remove (peerKey )
187189 return ipcErr
@@ -208,7 +210,7 @@ func (c *WGUSPConfigurer) AddAllowedIP(peerKey string, allowedIP netip.Prefix) e
208210 Peers : []wgtypes.PeerConfig {peer },
209211 }
210212
211- return c .device .IpcSet (toWgUserspaceString (config ))
213+ return c .device .IpcSet (c . toWgUserspaceString (config ))
212214}
213215
214216func (c * WGUSPConfigurer ) RemoveAllowedIP (peerKey string , allowedIP netip.Prefix ) error {
@@ -273,7 +275,7 @@ func (c *WGUSPConfigurer) RemoveAllowedIP(peerKey string, allowedIP netip.Prefix
273275 config := wgtypes.Config {
274276 Peers : []wgtypes.PeerConfig {peer },
275277 }
276- return c .device .IpcSet (toWgUserspaceString (config ))
278+ return c .device .IpcSet (c . toWgUserspaceString (config ))
277279}
278280
279281func (c * WGUSPConfigurer ) FullStats () (* Stats , error ) {
@@ -399,23 +401,59 @@ func parseTransfers(ipc string) (map[string]WGStats, error) {
399401 return stats , nil
400402}
401403
402- func toWgUserspaceString (wgCfg wgtypes.Config ) string {
404+ func ( c * WGUSPConfigurer ) toWgUserspaceString (wgCfg wgtypes.Config ) string {
403405 var sb strings.Builder
404406 if wgCfg .PrivateKey != nil {
405407 hexKey := hex .EncodeToString (wgCfg .PrivateKey [:])
406408 sb .WriteString (fmt .Sprintf ("private_key=%s\n " , hexKey ))
407409
408- // write amneziawg settings here
409- // sorry for hardcode
410- sb .WriteString ("jc=8\n " )
411- sb .WriteString ("jmin=50\n " )
412- sb .WriteString ("jmax=1000\n " )
413- sb .WriteString ("s1=30\n " )
414- sb .WriteString ("s2=32\n " )
415- sb .WriteString ("h1=567134433\n " )
416- sb .WriteString ("h2=1497534042\n " )
417- sb .WriteString ("h3=862417541\n " )
418- sb .WriteString ("h4=1695024432\n " )
410+ // Write AmneziaWG settings only if config is not empty
411+ // If nil or empty, acts as standard WireGuard
412+ if ! c .amneziaConfig .IsEmpty () {
413+
414+ if val := c .amneziaConfig .GetJc (); val > 0 {
415+ sb .WriteString (fmt .Sprintf ("jc=%d\n " , val ))
416+ }
417+ if val := c .amneziaConfig .GetJmin (); val > 0 {
418+ sb .WriteString (fmt .Sprintf ("jmin=%d\n " , val ))
419+ }
420+ if val := c .amneziaConfig .GetJmax (); val > 0 {
421+ sb .WriteString (fmt .Sprintf ("jmax=%d\n " , val ))
422+ }
423+ if val := c .amneziaConfig .GetS1 (); val > 0 {
424+ sb .WriteString (fmt .Sprintf ("s1=%d\n " , val ))
425+ }
426+ if val := c .amneziaConfig .GetS2 (); val > 0 {
427+ sb .WriteString (fmt .Sprintf ("s2=%d\n " , val ))
428+ }
429+ if val := c .amneziaConfig .GetH1 (); val > 0 {
430+ sb .WriteString (fmt .Sprintf ("h1=%d\n " , val ))
431+ }
432+ if val := c .amneziaConfig .GetH2 (); val > 0 {
433+ sb .WriteString (fmt .Sprintf ("h2=%d\n " , val ))
434+ }
435+ if val := c .amneziaConfig .GetH3 (); val > 0 {
436+ sb .WriteString (fmt .Sprintf ("h3=%d\n " , val ))
437+ }
438+ if val := c .amneziaConfig .GetH4 (); val > 0 {
439+ sb .WriteString (fmt .Sprintf ("h4=%d\n " , val ))
440+ }
441+ if val := c .amneziaConfig .GetI1 (); val != "" {
442+ sb .WriteString (fmt .Sprintf ("i1=%s\n " , val ))
443+ }
444+ if val := c .amneziaConfig .GetI2 (); val != "" {
445+ sb .WriteString (fmt .Sprintf ("i2=%s\n " , val ))
446+ }
447+ if val := c .amneziaConfig .GetI3 (); val != "" {
448+ sb .WriteString (fmt .Sprintf ("i3=%s\n " , val ))
449+ }
450+ if val := c .amneziaConfig .GetI4 (); val != "" {
451+ sb .WriteString (fmt .Sprintf ("i4=%s\n " , val ))
452+ }
453+ if val := c .amneziaConfig .GetI5 (); val != "" {
454+ sb .WriteString (fmt .Sprintf ("i5=%s\n " , val ))
455+ }
456+ }
419457 }
420458
421459 if wgCfg .ListenPort != nil {
0 commit comments