Support custom tunnels path via settings file (no gui yet)

This commit is contained in:
Tien Tran 2024-10-19 12:31:36 +11:00
parent c8b27fdab8
commit 7c85400b69
5 changed files with 32 additions and 12 deletions

View File

@ -15,10 +15,10 @@ import (
)
const (
Version = "1.1.0"
Repo = "https://github.com/UnnoTed/wireguird"
TunnelsPath = "/etc/wireguard/"
IconPath = "/opt/wireguird/Icon/"
Version = "1.1.0"
Repo = "https://github.com/UnnoTed/wireguird"
DefaultTunnelsPath = "/etc/wireguard/"
IconPath = "/opt/wireguird/Icon/"
)
var (
@ -31,6 +31,7 @@ var (
header *gtk.HeaderBar
wgc *wgctrl.Client
updateTicker *time.Ticker
TunnelsPath string
)
func Create(app *gtk.Application, b *gtk.Builder, w *gtk.ApplicationWindow, ind *appindicator.Indicator) error {

View File

@ -229,7 +229,7 @@ func (t *Tunnels) Create() error {
t.icons[d.Name].SetFromPixbuf(gray.GetPixbuf())
})
c := exec.Command("wg-quick", "down", d.Name)
c := exec.Command("wg-quick", "down", TunnelsPath+d.Name+".conf")
output, err := c.Output()
if err != nil {
es := string(err.(*exec.ExitError).Stderr)
@ -283,7 +283,7 @@ func (t *Tunnels) Create() error {
}
// connect to a tunnel
c := exec.Command("wg-quick", "up", name)
c := exec.Command("wg-quick", "up", TunnelsPath+name+".conf")
output, err := c.Output()
if err != nil {
es := string(err.(*exec.ExitError).Stderr)

10
main.go
View File

@ -4,6 +4,7 @@ package main
import (
"os"
"strings"
"github.com/UnnoTed/go-appindicator"
"github.com/UnnoTed/horizontal"
@ -21,6 +22,15 @@ func main() {
log.Logger = log.Output(horizontal.ConsoleWriter{Out: os.Stderr})
log.Info().Uint("major", gtk.GetMajorVersion()).Uint("minor", gtk.GetMinorVersion()).Uint("micro", gtk.GetMicroVersion()).Msg("GTK Version")
if gui.Settings.TunnelsPath == "" {
gui.TunnelsPath = gui.DefaultTunnelsPath
} else {
gui.TunnelsPath = gui.Settings.TunnelsPath
}
if !strings.HasSuffix(gui.TunnelsPath, "/") {
gui.TunnelsPath += "/"
}
if err := gui.Settings.Load(); err != nil {
log.Error().Err(err).Msg("error initial settings load")
}

View File

@ -23,7 +23,7 @@ cp -r ./Icon/ "$opt_w_dir"
echo "wireguird: building deb package..."
touch "$opt_w_dir""wireguird.settings"
echo '{"MultipleTunnels":false,"StartOnTray":false,"CheckUpdates":false,"TunnelsPath":"/etc/wireguard","Debug":false}' > "$opt_w_dir""wireguird.settings"
if [ ! -d "./build/" ]; then
mkdir ./build/

View File

@ -3,18 +3,19 @@ package settings
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/ungerik/go-dry"
)
const FilePath = "./wireguird.settings"
type Settings struct {
MultipleTunnels bool
StartOnTray bool
CheckUpdates bool
TunnelsPath string
Debug bool
}
@ -23,11 +24,19 @@ var (
checkUpdates *bool
debug *bool
tray *bool
filePath string
)
func (s *Settings) Init() error {
log.Debug().Msg("Settings init")
exePath, err := os.Executable()
if err != nil {
return err
}
filePath = filepath.Join(filepath.Dir(exePath), "wireguird.settings")
if err := s.Load(); err != nil {
return err
}
@ -48,7 +57,7 @@ func (s *Settings) Save() error {
return err
}
if err := ioutil.WriteFile(FilePath, data, 0660); err != nil {
if err := ioutil.WriteFile(filePath, data, 0666); err != nil {
return err
}
@ -58,12 +67,12 @@ func (s *Settings) Save() error {
func (s *Settings) Load() error {
log.Debug().Msg("loading settings")
if !dry.FileExists(FilePath) {
if !dry.FileExists(filePath) {
log.Debug().Msg("settings file doesnt exist")
return nil
}
data, err := ioutil.ReadFile(FilePath)
data, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}