dsnet/add.go

116 lines
3.3 KiB
Go
Raw Normal View History

2020-03-02 01:45:14 +01:00
package dsnet
2020-03-02 20:13:47 +01:00
import (
2020-03-05 21:43:31 +01:00
"fmt"
2020-03-05 21:46:28 +01:00
"os"
2020-03-08 21:48:10 +01:00
"strings"
2020-03-03 22:28:06 +01:00
"text/template"
2020-03-07 22:57:00 +01:00
"time"
2020-03-02 20:13:47 +01:00
)
2020-10-19 01:04:05 +02:00
const wgQuickPeerConf = `[Interface]
Address = {{ .Peer.IP }}/22
2020-10-19 01:04:05 +02:00
PrivateKey={{ .Peer.PrivateKey.Key }}
{{- if .DsnetConfig.DNS }}
DNS = {{ .DsnetConfig.DNS }}
{{ end }}
[Peer]
PublicKey={{ .DsnetConfig.PrivateKey.PublicKey.Key }}
PresharedKey={{ .Peer.PresharedKey.Key }}
Endpoint={{ .DsnetConfig.ExternalIP }}:{{ .DsnetConfig.ListenPort }}
AllowedIPs={{ .AllowedIPs }}
PersistentKeepalive={{ .Keepalive }}
`
const vyattaPeerConf = `configure
2020-10-19 18:52:54 +02:00
set interfaces wireguard wg0 address {{ .Peer.IP }}/{{ .Cidrmask }}
set interfaces wireguard wg0 route-allowed-ips true
set interfaces wireguard wg0 private-key {{ .Peer.PrivateKey.Key }}
2020-10-19 01:04:05 +02:00
{{- if .DsnetConfig.DNS }}
#set service dns forwarding name-server {{ .DsnetConfig.DNS }}
{{ end }}
2020-10-19 18:52:54 +02:00
set interfaces wireguard wg0 peer {{ .DsnetConfig.PrivateKey.PublicKey.Key }} endpoint {{ .DsnetConfig.ExternalIP }}:{{ .DsnetConfig.ListenPort }}
2020-10-19 04:17:06 +02:00
set interfaces wireguard wg0 peer {{ .DsnetConfig.PrivateKey.PublicKey.Key }} allowed-ips {{ .AllowedIPs }}
set interfaces wireguard wg0 peer {{ .DsnetConfig.PrivateKey.PublicKey.Key }} persistent-keepalive {{ .Keepalive }}
2020-10-19 20:25:56 +02:00
set interfaces wireguard wg0 peer {{ .DsnetConfig.PrivateKey.PublicKey.Key }} preshared-key {{ .Peer.PresharedKey.Key }}
2020-10-19 01:04:05 +02:00
commit; save
`
2020-03-03 23:33:48 +01:00
func Add() {
2020-03-14 15:51:49 +01:00
if len(os.Args) != 3 {
2020-03-05 23:13:09 +01:00
// TODO non-red
ExitFail("Hostname argument required: dsnet add <hostname>")
}
// TODO maybe accept flags to avoid prompt and allow programmatic use?
2020-03-03 23:33:48 +01:00
// TODO accept existing pubkey
conf := MustLoadDsnetConfig()
2020-03-05 23:13:09 +01:00
hostname := os.Args[2]
2020-03-03 23:33:48 +01:00
owner := MustPromptString("owner", true)
description := MustPromptString("Description", true)
//publicKey := MustPromptString("PublicKey (optional)", false)
ConfirmOrAbort("\nDo you want to add the above configuration?")
2020-03-05 21:43:31 +01:00
// newline (not on stdout) to separate config
fmt.Fprintln(os.Stderr)
privateKey := GenerateJSONPrivateKey()
publicKey := privateKey.PublicKey()
2020-03-02 21:11:33 +01:00
IP := conf.MustAllocateIP()
2020-03-02 19:44:19 +01:00
peer := PeerConfig{
2020-03-02 20:03:00 +01:00
Owner: owner,
Hostname: hostname,
Description: description,
2020-03-19 21:12:42 +01:00
Added: time.Now(),
2020-03-02 20:03:00 +01:00
PublicKey: publicKey,
PrivateKey: privateKey, // omitted from server config JSON!
2020-03-02 22:36:41 +01:00
PresharedKey: GenerateJSONKey(),
2020-03-03 23:30:36 +01:00
IP: IP,
2020-03-04 20:46:39 +01:00
Networks: []JSONIPNet{},
}
conf.MustAddPeer(peer)
2020-03-03 22:28:06 +01:00
PrintPeerCfg(peer, conf)
conf.MustSave()
2020-03-05 21:43:31 +01:00
ConfigureDevice(conf)
2020-03-02 01:45:14 +01:00
}
2020-03-02 20:57:52 +01:00
2020-03-03 22:28:06 +01:00
func PrintPeerCfg(peer PeerConfig, conf *DsnetConfig) {
2020-03-08 21:48:10 +01:00
allowedIPsStr := make([]string, len(conf.Networks)+1)
allowedIPsStr[0] = conf.Network.String()
for i, net := range conf.Networks {
allowedIPsStr[i+1] = net.String()
}
2020-10-19 01:04:05 +02:00
var peerConf string
2020-03-02 20:57:52 +01:00
2020-10-19 01:04:05 +02:00
switch os.Getenv("DSNET_OUTPUT") {
// https://manpages.debian.org/unstable/wireguard-tools/wg-quick.8.en.html
case "", "wg-quick":
peerConf = wgQuickPeerConf
// https://github.com/WireGuard/wireguard-vyatta-ubnt/
case "vyatta":
peerConf = vyattaPeerConf
default:
ExitFail("Unrecognised DSNET_OUTPUT type")
}
2020-03-03 22:28:06 +01:00
2020-10-19 01:27:39 +02:00
cidrmask, _ := conf.Network.IPNet.Mask.Size()
2020-03-03 22:28:06 +01:00
t := template.Must(template.New("peerConf").Parse(peerConf))
err := t.Execute(os.Stdout, map[string]interface{}{
"Peer": peer,
"DsnetConfig": conf,
2020-03-07 15:36:14 +01:00
"Keepalive": time.Duration(KEEPALIVE).Seconds(),
2020-03-08 21:48:10 +01:00
"AllowedIPs": strings.Join(allowedIPsStr, ","),
2020-10-19 01:27:39 +02:00
"Cidrmask": cidrmask,
2020-03-03 22:28:06 +01:00
})
check(err)
2020-03-02 20:57:52 +01:00
}