dsnet/add.go

68 lines
1.7 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-03 22:28:06 +01:00
"os"
2020-03-05 21:43:31 +01:00
"fmt"
2020-03-03 22:28:06 +01:00
"text/template"
2020-03-02 20:13:47 +01:00
)
2020-03-03 23:33:48 +01:00
func Add() {
// TODO maybe accept flags to avoid prompt and allow programmatic use
// TODO accept existing pubkey
conf := MustLoadDsnetConfig()
2020-03-03 23:33:48 +01:00
hostname := MustPromptString("Hostname (unique)", true)
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) {
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 }}
PresharedKey={{ .Peer.PresharedKey.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 }}
PresharedKey={{ .DsnetConfig.PresharedKey.Key }}
Endpoint={{ .DsnetConfig.ExternalIP }}:{{ .DsnetConfig.ListenPort }}
#AllowedIPs=0.0.0.0/0
AllowedIPs={{ .DsnetConfig.Network }}
2020-03-02 20:57:52 +01:00
PersistentKeepalive=21
`
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,
})
check(err)
2020-03-02 20:57:52 +01:00
}