dsnet/add.go

82 lines
2.0 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-03-03 23:33:48 +01:00
func Add() {
2020-03-05 23:13:09 +01:00
if len(os.Args) <= 2 {
// 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,
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-03-03 22:28:06 +01:00
const peerConf = `[Interface]
2020-03-03 23:30:36 +01:00
Address = {{ .Peer.IP }}
2020-03-03 22:28:06 +01:00
PrivateKey={{ .Peer.PrivateKey.Key }}
2020-03-03 23:30:36 +01:00
DNS = {{ .DsnetConfig.DNS }}
2020-03-02 20:57:52 +01:00
[Peer]
2020-03-03 22:28:06 +01:00
PublicKey={{ .DsnetConfig.PrivateKey.PublicKey.Key }}
2020-03-05 23:18:21 +01:00
PresharedKey={{ .Peer.PresharedKey.Key }}
2020-03-03 22:28:06 +01:00
Endpoint={{ .DsnetConfig.ExternalIP }}:{{ .DsnetConfig.ListenPort }}
2020-03-08 21:48:10 +01:00
AllowedIPs={{ .AllowedIPs }}
2020-03-07 15:36:14 +01:00
PersistentKeepalive={{ .Keepalive }}
2020-03-02 20:57:52 +01:00
`
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-03-03 22:28:06 +01:00
})
check(err)
2020-03-02 20:57:52 +01:00
}