From 3784b6f1af650f4cc278f67c31b730e15365c692 Mon Sep 17 00:00:00 2001 From: Edgaru089 Date: Thu, 18 Jan 2024 03:48:17 +0800 Subject: [PATCH] Initial commit --- .gitignore | 2 + go.mod | 5 ++ go.sum | 2 + main.go | 225 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 234 insertions(+) create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cc5a070 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +wgGen +wgGen.exe diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d5a5476 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module wgGen + +go 1.21.6 + +require github.com/andlabs/ui v0.0.0-20200610043537-70a69d6ae31e // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..43a2db6 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/andlabs/ui v0.0.0-20200610043537-70a69d6ae31e h1:wSQCJiig/QkoUnpvelSPbLiZNWvh2yMqQTQvIQqSUkU= +github.com/andlabs/ui v0.0.0-20200610043537-70a69d6ae31e/go.mod h1:5G2EjwzgZUPnnReoKvPWVneT8APYbyKkihDVAHUi0II= diff --git a/main.go b/main.go new file mode 100644 index 0000000..a9584c6 --- /dev/null +++ b/main.go @@ -0,0 +1,225 @@ +package main + +import ( + "fmt" + "io" + "os/exec" + "runtime" + "strings" + + "github.com/andlabs/ui" +) + +const ConfigBody = `[NetDev] +Name=%s +Kind=wireguard +Description=%s + +[WireGuard] +ListenPort=12345 +PrivateKey=%s + +` +const ConfigPeer = `[WireGuardPeer] +PublicKey=%s +PresharedKey=%s +AllowedIPs=%s +` + +var win *ui.Window +var ( + private, public, psk string + devname string = "wg0" + desc string = "WireGuard tunnel/listener ()" + + peerpublic string + peerip string + peerallowed string = "0.0.0.0/0" +) +var wgexe string + +func init() { + if runtime.GOOS == "windows" { + wgexe = "wg.exe" + } else { + wgexe = "wg" + } +} + +func readStringFromCommands(stdin io.Reader, cmd string, args ...string) (string, error) { + runner := exec.Command(cmd, args...) + runner.Stdin = stdin + genkey_out, err := runner.Output() + if err != nil { + return "", err + } + + // Remove unprintable chars at the end + str := string(genkey_out) + for str[len(str)-1] == '\n' { + str = str[:len(str)-1] + } + + return str, nil +} + +func resetKeys() { + + var err error + private, err = readStringFromCommands(nil, wgexe, "genkey") + if err != nil { + ui.MsgBox(win, "wg genkey error", err.Error()) + return + } + + public, err = readStringFromCommands(strings.NewReader(private), wgexe, "pubkey") + if err != nil { + ui.MsgBox(win, "wg pubkey error", err.Error()) + return + } +} + +func resetPsk() { + var err error + psk, err = readStringFromCommands(strings.NewReader(private), wgexe, "genpsk") + if err != nil { + ui.MsgBox(win, "wg genpsk error", err.Error()) + return + } +} + +func resetConfig() string { + buf := &strings.Builder{} + + fmt.Fprintf(buf, ConfigBody, devname, desc, private) + fmt.Fprintf(buf, ConfigPeer, peerpublic, psk, peerallowed) + if len(peerip) > 0 { + fmt.Fprintf(buf, "Endpoint=%s\n", peerip) + } + + return buf.String() +} + +func setup() { + resetKeys() + resetPsk() + + win = ui.NewWindow("WireGuard Generator", 500, 700, false) + win.OnClosing(func(*ui.Window) bool { + ui.Quit() + return true + }) + ui.OnShouldQuit(func() bool { + win.Destroy() + return true + }) + + muti := ui.NewMultilineEntry() + + vbox := ui.NewVerticalBox() + vbox.SetPadded(true) + + form := ui.NewForm() + form.SetPadded(true) + + entry1 := ui.NewEntry() + entry1.SetReadOnly(true) + entry1.SetText(private) + entry2 := ui.NewEntry() + entry2.SetText(public) + entry2.SetReadOnly(true) + entry3 := ui.NewEntry() + entry3.SetText(psk) + entry3.OnChanged(func(e *ui.Entry) { + psk = e.Text() + muti.SetText(resetConfig()) + }) + + form.Append("Private", entry1, false) + form.Append("Public", entry2, false) + form.Append("PSK", entry3, false) + vbox.Append(form, false) + + hbox_buttons := ui.NewHorizontalBox() + hbox_buttons.SetPadded(true) + + button1 := ui.NewButton("Generate Keys") + button1.OnClicked(func(*ui.Button) { + resetKeys() + entry1.SetText(private) + entry2.SetText(public) + muti.SetText(resetConfig()) + }) + button2 := ui.NewButton("Generate PSK") + button2.OnClicked(func(*ui.Button) { + resetPsk() + entry3.SetText(psk) + muti.SetText(resetConfig()) + }) + hbox_buttons.Append(button1, true) + hbox_buttons.Append(button2, true) + vbox.Append(hbox_buttons, false) + + // Device Name + vbox.Append(ui.NewHorizontalSeparator(), false) + form_dev := ui.NewForm() + form_dev.SetPadded(true) + + dev_entry1 := ui.NewEntry() + dev_entry1.SetText(devname) + dev_entry2 := ui.NewEntry() + dev_entry2.SetText(desc) + dev_entry1.OnChanged(func(e *ui.Entry) { + devname = e.Text() + muti.SetText(resetConfig()) + }) + dev_entry2.OnChanged(func(e *ui.Entry) { + desc = e.Text() + muti.SetText(resetConfig()) + }) + + form_dev.Append("Device name", dev_entry1, false) + form_dev.Append("Description", dev_entry2, false) + vbox.Append(form_dev, false) + + // Peer + vbox.Append(ui.NewHorizontalSeparator(), false) + + form2 := ui.NewForm() + form2.SetPadded(true) + + peer_entry1 := ui.NewEntry() + peer_entry1.SetText(peerpublic) + peer_entry2 := ui.NewEntry() + peer_entry2.SetText(peerip) + peer_entry3 := ui.NewEntry() + peer_entry3.SetText(peerallowed) + peer_entry1.OnChanged(func(e *ui.Entry) { + peerpublic = e.Text() + muti.SetText(resetConfig()) + }) + peer_entry2.OnChanged(func(e *ui.Entry) { + peerip = e.Text() + muti.SetText(resetConfig()) + }) + peer_entry3.OnChanged(func(e *ui.Entry) { + peerallowed = e.Text() + muti.SetText(resetConfig()) + }) + + form2.Append("Peer public", peer_entry1, false) + form2.Append("Endpoint", peer_entry2, false) + form2.Append("Allowed IPs", peer_entry3, false) + vbox.Append(form2, false) + + muti.SetText(resetConfig()) + vbox.Append(muti, true) + + win.SetChild(vbox) + win.SetMargined(true) + win.Show() +} + +func main() { + ui.Main(setup) +}