wireguard-gui/main.go

238 lines
4.7 KiB
Go
Raw Normal View History

2022-09-12 11:14:53 +02:00
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
"image/color"
2022-09-12 13:31:42 +02:00
"io/fs"
2022-09-12 11:14:53 +02:00
"log"
"os"
2022-09-12 13:31:42 +02:00
"os/exec"
"path/filepath"
2022-09-12 11:14:53 +02:00
"strings"
2022-09-12 13:31:42 +02:00
"time"
2022-09-12 11:14:53 +02:00
)
2022-09-12 13:31:42 +02:00
type Config struct {
Name string
File string
}
2022-09-12 14:00:24 +02:00
var red color.RGBA
var green color.RGBA
var orange color.RGBA
2022-09-12 11:14:53 +02:00
func main() {
2022-09-12 14:00:24 +02:00
red = color.RGBA{R: 156, G: 21, B: 21}
green = color.RGBA{R: 47, G: 156, B: 17}
orange = color.RGBA{R: 156, G: 106, B: 25}
2022-09-12 11:14:53 +02:00
application := app.New()
window := application.NewWindow("Wireguard GUI (deblan)")
2022-09-12 13:31:42 +02:00
configs := []Config{}
directory := "/etc/wireguard/"
err := filepath.WalkDir(directory, func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
basename := string(info.Name())
if !strings.HasSuffix(basename, ".conf") {
return nil
}
if strings.Contains(strings.ReplaceAll(path, directory, ""), "/") {
return nil
}
2022-09-12 11:14:53 +02:00
2022-09-12 13:31:42 +02:00
configs = append(configs, Config{
Name: strings.ReplaceAll(basename, ".conf", ""),
File: path,
})
2022-09-12 11:14:53 +02:00
2022-09-12 13:31:42 +02:00
return nil
})
2022-09-12 11:14:53 +02:00
if err != nil {
log.Fatalln(err)
}
2022-09-12 13:31:42 +02:00
menu := container.NewAppTabs()
tabs := make([]fyne.Container, len(configs))
for i, config := range configs {
tabs[i] = *createTab(config)
}
for i, config := range configs {
menu.Append(
container.NewTabItem(
config.Name,
&tabs[i],
),
)
}
content := container.New(layout.NewVBoxLayout(), menu)
window.SetContent(content)
window.Resize(fyne.NewSize(900, 400))
window.ShowAndRun()
}
func WgUp(config Config, notice *canvas.Text) {
notice.Hidden = false
notice.Text = fmt.Sprintf("Interface is starting")
2022-09-12 14:00:24 +02:00
notice.Color = orange
2022-09-12 13:31:42 +02:00
notice.Refresh()
exec.Command("wg-quick", "up", config.Name).Output()
notice.Text = fmt.Sprintf("Interface is up")
2022-09-12 14:00:24 +02:00
notice.Color = green
2022-09-12 13:31:42 +02:00
notice.Refresh()
go func() {
time.Sleep(2 * time.Second)
notice.Hidden = true
notice.Refresh()
}()
}
func WgDown(config Config, notice *canvas.Text) {
notice.Refresh()
notice.Hidden = false
notice.Text = fmt.Sprintf("Interface is stopping")
2022-09-12 14:00:24 +02:00
notice.Color = orange
2022-09-12 13:31:42 +02:00
notice.Refresh()
exec.Command("wg-quick", "down", config.Name).Output()
notice.Text = fmt.Sprintf("Interface is down")
2022-09-12 14:00:24 +02:00
notice.Color = green
2022-09-12 13:31:42 +02:00
notice.Refresh()
go func() {
time.Sleep(2 * time.Second)
notice.Hidden = true
notice.Refresh()
}()
}
func WgRestart(config Config, notice *canvas.Text) {
WgDown(config, notice)
WgUp(config, notice)
}
func createTab(config Config) *fyne.Container {
notice := canvas.NewText("", color.White)
notice.TextStyle.Bold = true
r1 := canvas.NewText("", color.Transparent)
r2 := canvas.NewText("", color.Transparent)
r1.TextSize = 5
r2.TextSize = 5
data, err := os.ReadFile(config.File)
if err != nil {
log.Fatalln(err)
}
buttonStart := widget.NewButton("Start", func() {
WgUp(config, notice)
})
buttonStop := widget.NewButton("Stop", func() {
WgDown(config, notice)
})
buttonRestart := widget.NewButton("Restart", func() {
WgRestart(config, notice)
})
2022-09-12 14:00:24 +02:00
buttonStartWrapper := container.NewMax(canvas.NewRectangle(green), buttonStart)
buttonStopWrapper := container.NewMax(canvas.NewRectangle(red), buttonStop)
buttonRestartWrapper := container.NewMax(canvas.NewRectangle(orange), buttonRestart)
2022-09-12 13:31:42 +02:00
top := container.New(
layout.NewVBoxLayout(),
r1,
container.New(
layout.NewHBoxLayout(),
notice,
2022-09-12 14:00:24 +02:00
layout.NewSpacer(),
buttonStartWrapper,
buttonStopWrapper,
buttonRestartWrapper,
2022-09-12 13:31:42 +02:00
),
r2,
)
2022-09-12 11:14:53 +02:00
configuration := string(data)
textareaConfiguration := widget.NewMultiLineEntry()
textareaConfiguration.SetText(configuration)
textareaConfiguration.SetMinRowsVisible(strings.Count(configuration, "\n"))
2022-09-12 13:31:42 +02:00
textareaConfiguration.OnChanged = func(text string) {
textareaConfiguration.SetMinRowsVisible(strings.Count(text, "\n"))
textareaConfiguration.Refresh()
}
2022-09-12 11:14:53 +02:00
form := &widget.Form{
Items: []*widget.FormItem{
{
Text: "Configuration",
Widget: textareaConfiguration,
},
},
OnSubmit: func() {
2022-09-12 13:31:42 +02:00
notice.Hidden = false
configuration := fmt.Sprintf("%s\n", textareaConfiguration.Text)
2022-09-12 14:03:35 +02:00
configuration = strings.TrimSpace(configuration)
textareaConfiguration.Text = configuration
2022-09-12 13:31:42 +02:00
err := os.WriteFile(config.File, []byte(configuration), 600)
2022-09-12 11:14:53 +02:00
if err != nil {
log.Println(err)
2022-09-12 13:31:42 +02:00
notice.Text = fmt.Sprintf("Error while updating: %s", err)
2022-09-12 14:00:24 +02:00
notice.Color = red
2022-09-12 11:14:53 +02:00
notice.Refresh()
} else {
2022-09-12 13:31:42 +02:00
notice.Text = fmt.Sprintf("Configuration updated")
2022-09-12 14:00:24 +02:00
notice.Color = green
2022-09-12 13:31:42 +02:00
go func() {
time.Sleep(2 * time.Second)
notice.Hidden = true
notice.Refresh()
}()
2022-09-12 11:14:53 +02:00
}
notice.Refresh()
},
SubmitText: "Save",
}
2022-09-12 13:31:42 +02:00
content := container.New(
layout.NewVBoxLayout(),
top,
form,
)
2022-09-12 11:14:53 +02:00
2022-09-12 13:31:42 +02:00
return content
2022-09-12 11:14:53 +02:00
}