From e3a5e4f3b79c934ab20b2aebe40e839a172c60af Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sun, 10 Nov 2019 19:38:35 +0000 Subject: [PATCH 1/2] Add -address and -subnet flag for getting address/subnet out of config --- cmd/yggdrasil/main.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/cmd/yggdrasil/main.go b/cmd/yggdrasil/main.go index 91cea9a7..6158fd9d 100644 --- a/cmd/yggdrasil/main.go +++ b/cmd/yggdrasil/main.go @@ -2,11 +2,13 @@ package main import ( "bytes" + "crypto/sha512" "encoding/hex" "encoding/json" "flag" "fmt" "io/ioutil" + "net" "os" "os/signal" "strings" @@ -20,6 +22,7 @@ import ( "github.com/kardianos/minwinsvc" "github.com/mitchellh/mapstructure" + "github.com/yggdrasil-network/yggdrasil-go/src/address" "github.com/yggdrasil-network/yggdrasil-go/src/admin" "github.com/yggdrasil-network/yggdrasil-go/src/config" "github.com/yggdrasil-network/yggdrasil-go/src/crypto" @@ -142,6 +145,8 @@ func main() { ver := flag.Bool("version", false, "prints the version of this build") logging := flag.String("logging", "info,warn,error", "comma-separated list of logging levels to enable") logto := flag.String("logto", "stdout", "file path to log to, \"syslog\" or \"stdout\"") + getaddr := flag.Bool("address", false, "returns the IPv6 address as derived from the supplied configuration") + getsnet := flag.Bool("subnet", false, "returns the IPv6 subnet as derived from the supplied configuration") flag.Parse() var cfg *config.NodeConfig @@ -188,6 +193,26 @@ func main() { if cfg == nil { return } + // Have we been asked for the node address yet? If so, print it and then stop. + switch { + case *getaddr: + if pubkey, err := hex.DecodeString(cfg.EncryptionPublicKey); err == nil { + nodeid := sha512.Sum512(pubkey) + fromnodeid := address.AddrForNodeID((*crypto.NodeID)(&nodeid)) + fmt.Println(net.IP(fromnodeid[:]).String()) + } + return + case *getsnet: + if pubkey, err := hex.DecodeString(cfg.EncryptionPublicKey); err == nil { + nodeid := sha512.Sum512(pubkey) + fromnodeid := address.SubnetForNodeID((*crypto.NodeID)(&nodeid)) + subnet := append(fromnodeid[:], 0, 0, 0, 0, 0, 0, 0, 0) + ipnet := net.IPNet{IP: subnet, Mask: net.CIDRMask(64, 128)} + fmt.Println(ipnet.String()) + } + return + default: + } // Create a new logger that logs output to stdout. var logger *log.Logger switch *logto { From e310a25e5987a2400bbdbb765b14bb1967ca02e2 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Mon, 11 Nov 2019 09:40:25 +0000 Subject: [PATCH 2/2] Use crypto.GetNodeID instead of sha512 directly --- cmd/yggdrasil/main.go | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/cmd/yggdrasil/main.go b/cmd/yggdrasil/main.go index 6158fd9d..981c6efc 100644 --- a/cmd/yggdrasil/main.go +++ b/cmd/yggdrasil/main.go @@ -2,7 +2,6 @@ package main import ( "bytes" - "crypto/sha512" "encoding/hex" "encoding/json" "flag" @@ -194,20 +193,29 @@ func main() { return } // Have we been asked for the node address yet? If so, print it and then stop. + getNodeID := func() *crypto.NodeID { + if pubkey, err := hex.DecodeString(cfg.EncryptionPublicKey); err == nil { + var box crypto.BoxPubKey + copy(box[:], pubkey[:]) + return crypto.GetNodeID(&box) + } + return nil + } switch { case *getaddr: - if pubkey, err := hex.DecodeString(cfg.EncryptionPublicKey); err == nil { - nodeid := sha512.Sum512(pubkey) - fromnodeid := address.AddrForNodeID((*crypto.NodeID)(&nodeid)) - fmt.Println(net.IP(fromnodeid[:]).String()) + if nodeid := getNodeID(); nodeid != nil { + addr := *address.AddrForNodeID(nodeid) + ip := net.IP(addr[:]) + fmt.Println(ip.String()) } return case *getsnet: - if pubkey, err := hex.DecodeString(cfg.EncryptionPublicKey); err == nil { - nodeid := sha512.Sum512(pubkey) - fromnodeid := address.SubnetForNodeID((*crypto.NodeID)(&nodeid)) - subnet := append(fromnodeid[:], 0, 0, 0, 0, 0, 0, 0, 0) - ipnet := net.IPNet{IP: subnet, Mask: net.CIDRMask(64, 128)} + if nodeid := getNodeID(); nodeid != nil { + snet := *address.SubnetForNodeID(nodeid) + ipnet := net.IPNet{ + IP: append(snet[:], 0, 0, 0, 0, 0, 0, 0, 0), + Mask: net.CIDRMask(len(snet)*8, 128), + } fmt.Println(ipnet.String()) } return