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"
|
|
|
|
"text/template"
|
2020-03-02 20:13:47 +01:00
|
|
|
)
|
|
|
|
|
2020-03-02 20:03:00 +01:00
|
|
|
func Add(hostname string, owner string, description string) { //, publicKey string) {
|
2020-03-02 04:08:28 +01:00
|
|
|
conf := MustLoadDsnetConfig()
|
|
|
|
|
|
|
|
privateKey := GenerateJSONPrivateKey()
|
|
|
|
publicKey := privateKey.PublicKey()
|
|
|
|
|
2020-03-02 21:11:33 +01:00
|
|
|
IP := conf.MustAllocateIP()
|
2020-03-02 19:44:19 +01:00
|
|
|
|
2020-03-02 04:08:28 +01:00
|
|
|
peer := PeerConfig{
|
2020-03-02 20:03:00 +01:00
|
|
|
Owner: owner,
|
|
|
|
Hostname: hostname,
|
|
|
|
Description: description,
|
|
|
|
PublicKey: publicKey,
|
2020-03-02 23:31:52 +01:00
|
|
|
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-02 04:08:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
conf.MustAddPeer(peer)
|
2020-03-03 22:28:06 +01:00
|
|
|
PrintPeerCfg(peer, conf)
|
2020-03-02 04:08:28 +01:00
|
|
|
conf.MustSave()
|
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
|
|
|
}
|