working incomplete adding of peers. Now sleep.

This commit is contained in:
Callan Bryant 2020-03-02 03:08:28 +00:00
parent c419dfed48
commit 1bd215600c
No known key found for this signature in database
GPG Key ID: C31FA9DF3ACBFFAA
3 changed files with 32 additions and 3 deletions

20
add.go
View File

@ -1,4 +1,22 @@
package dsnet 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()
} }

View File

@ -24,11 +24,11 @@ func main() {
hostname := dsnet.MustPromptString("Hostname (unique)", true) hostname := dsnet.MustPromptString("Hostname (unique)", true)
owner := dsnet.MustPromptString("owner", true) owner := dsnet.MustPromptString("owner", true)
description := dsnet.MustPromptString("Description", 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.ConfirmOrAbort("\nDo you want to add the above configuration?")
dsnet.Add(hostname, owner, description, publicKey) dsnet.Add(hostname, owner, description)//, publicKey)
case "up": case "up":

View File

@ -76,6 +76,11 @@ func (conf *DsnetConfig) MustSave() {
check(err) check(err)
} }
func (conf *DsnetConfig) MustAddPeer(peer PeerConfig) {
// TODO validate PeerConfig!!!
conf.Peers = append(conf.Peers, peer)
}
type Dsnet struct { type Dsnet struct {
Name string Name string
PrivateKey wgtypes.Key PrivateKey wgtypes.Key
@ -111,6 +116,12 @@ func (k JSONKey) MarshalJSON() ([]byte, error) {
return []byte("\"" + k.Key.String() + "\""), nil return []byte("\"" + k.Key.String() + "\""), nil
} }
func (k JSONKey) PublicKey() JSONKey {
return JSONKey{
Key: k.Key.PublicKey(),
}
}
func (k *JSONKey) UnmarshalJSON(b []byte) error { func (k *JSONKey) UnmarshalJSON(b []byte) error {
b64Key := strings.Trim(string(b), "\"") b64Key := strings.Trim(string(b), "\"")
key, err := wgtypes.ParseKey(b64Key) key, err := wgtypes.ParseKey(b64Key)