From 1bd215600cdc2ef682620f97c4394e56e32a738f Mon Sep 17 00:00:00 2001 From: Callan Bryant Date: Mon, 2 Mar 2020 03:08:28 +0000 Subject: [PATCH] working incomplete adding of peers. Now sleep. --- add.go | 20 +++++++++++++++++++- cmd/dsnet.go | 4 ++-- types.go | 11 +++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/add.go b/add.go index cea2c1e..5c98057 100644 --- a/add.go +++ b/add.go @@ -1,4 +1,22 @@ package dsnet -func Add(hostname string, owner string, description string, publicKey string) { +func Add(hostname string, owner string, description string) {//, publicKey string) { + conf := MustLoadDsnetConfig() + + privateKey := GenerateJSONPrivateKey() + presharedKey := GenerateJSONKey() + publicKey := privateKey.PublicKey() + + peer := PeerConfig{ + Owner: owner, + Hostname: hostname, + Description: description, + PublicKey: publicKey, + PresharedKey: presharedKey, + // TODO Endpoint: + // TODO pick an available IP AllowedIPs + } + + conf.MustAddPeer(peer) + conf.MustSave() } diff --git a/cmd/dsnet.go b/cmd/dsnet.go index 40962b7..15482b7 100644 --- a/cmd/dsnet.go +++ b/cmd/dsnet.go @@ -24,11 +24,11 @@ func main() { hostname := dsnet.MustPromptString("Hostname (unique)", true) owner := dsnet.MustPromptString("owner", true) description := dsnet.MustPromptString("Description", true) - publicKey := dsnet.MustPromptString("PublicKey (optional)", false) + //publicKey := dsnet.MustPromptString("PublicKey (optional)", false) dsnet.ConfirmOrAbort("\nDo you want to add the above configuration?") - dsnet.Add(hostname, owner, description, publicKey) + dsnet.Add(hostname, owner, description)//, publicKey) case "up": diff --git a/types.go b/types.go index 3aeabd7..159aee7 100644 --- a/types.go +++ b/types.go @@ -76,6 +76,11 @@ func (conf *DsnetConfig) MustSave() { check(err) } +func (conf *DsnetConfig) MustAddPeer(peer PeerConfig) { + // TODO validate PeerConfig!!! + conf.Peers = append(conf.Peers, peer) +} + type Dsnet struct { Name string PrivateKey wgtypes.Key @@ -111,6 +116,12 @@ func (k JSONKey) MarshalJSON() ([]byte, error) { return []byte("\"" + k.Key.String() + "\""), nil } +func (k JSONKey) PublicKey() JSONKey { + return JSONKey{ + Key: k.Key.PublicKey(), + } +} + func (k *JSONKey) UnmarshalJSON(b []byte) error { b64Key := strings.Trim(string(b), "\"") key, err := wgtypes.ParseKey(b64Key)