226 lines
4.5 KiB
Go
226 lines
4.5 KiB
Go
|
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)
|
||
|
}
|