@@ -268,8 +268,14 @@ func (vpn *DefaultVpn) Connect(cfg string) error {
268268 return errors .NewVPNError ("write config" , err )
269269 }
270270
271- output .PrintMsg (fmt .Sprintf ("Calling 'wg-quick up %s'" , cfgPath ), output .MsgOutput )
272- err = console .WgUp (cfgPath , vpn .dirProvider )
271+ // Copy config to /etc/wireguard for wg-quick to use
272+ systemCfgPath , err := vpn .copyConfigToSystem (cfgPath , cfgName )
273+ if err != nil {
274+ return errors .NewVPNError ("copy config to system" , err )
275+ }
276+
277+ output .PrintMsg (fmt .Sprintf ("Calling 'wg-quick up %s'" , systemCfgPath ), output .MsgOutput )
278+ err = console .WgUp (systemCfgPath , vpn .dirProvider )
273279 if err != nil {
274280 return errors .NewVPNError ("connect" , err )
275281 }
@@ -297,12 +303,13 @@ func (vpn *DefaultVpn) Disconnect(cfg string) error {
297303
298304 output .PrintMsg (fmt .Sprintf ("Disconnecting from %s..." , cfg ), output .MsgOutput )
299305
300- cfgDir , err := vpn .dirProvider .GetServersDir ()
306+ wireguardDir , err := vpn .dirProvider .GetWireguardDir ()
301307 if err != nil {
302- return errors .NewConfigError ("get servers directory" , err )
308+ return errors .NewConfigError ("get wireguard directory" , err )
303309 }
304310
305- err = console .WgDown (filepath .Join (cfgDir , cfg + ".conf" ), vpn .dirProvider )
311+ systemCfgPath := filepath .Join (wireguardDir , cfg + ".conf" )
312+ err = console .WgDown (systemCfgPath , vpn .dirProvider )
306313 if err != nil {
307314 return errors .NewVPNError ("disconnect" , err )
308315 }
@@ -417,3 +424,31 @@ func (vpn *DefaultVpn) saveWgConfig(serverName string, configContent string) (st
417424 filePath := filepath .Join (configDir , serverName + ".conf" )
418425 return filePath , os .WriteFile (filePath , []byte (configContent ), 0o600 )
419426}
427+
428+ // copyConfigToSystem copies a config file from user directory to /etc/wireguard
429+ // This requires sudo permissions and is called during connection operations.
430+ func (vpn * DefaultVpn ) copyConfigToSystem (userConfigPath , serverName string ) (string , error ) {
431+ wireguardDir , err := vpn .dirProvider .GetWireguardDir ()
432+ if err != nil {
433+ return "" , fmt .Errorf ("failed to get wireguard directory: %w" , err )
434+ }
435+
436+ // Ensure /etc/wireguard exists with proper permissions
437+ if err := os .MkdirAll (wireguardDir , 0o700 ); err != nil {
438+ return "" , fmt .Errorf ("failed to create wireguard directory: %w" , err )
439+ }
440+
441+ // Read source config
442+ configData , err := os .ReadFile (userConfigPath )
443+ if err != nil {
444+ return "" , fmt .Errorf ("failed to read config: %w" , err )
445+ }
446+
447+ // Write to system location
448+ systemPath := filepath .Join (wireguardDir , serverName + ".conf" )
449+ if err := os .WriteFile (systemPath , configData , 0o600 ); err != nil {
450+ return "" , fmt .Errorf ("failed to write system config: %w" , err )
451+ }
452+
453+ return systemPath , nil
454+ }
0 commit comments