5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-25 12:41:35 +00:00

Ensure PublicKey option is unused, map old config options

This commit is contained in:
Neil Alexander 2021-06-05 21:32:04 +01:00
parent 99973b2757
commit ea15eeee7e
5 changed files with 51 additions and 55 deletions

View File

@ -40,7 +40,7 @@ type node struct {
admin *admin.AdminSocket admin *admin.AdminSocket
} }
func readConfig(useconf *bool, useconffile *string, normaliseconf *bool) *config.NodeConfig { func readConfig(log *log.Logger, useconf *bool, useconffile *string, normaliseconf *bool) *config.NodeConfig {
// Use a configuration file. If -useconf, the configuration will be read // Use a configuration file. If -useconf, the configuration will be read
// from stdin. If -useconffile, the configuration will be read from the // from stdin. If -useconffile, the configuration will be read from the
// filesystem. // filesystem.
@ -78,27 +78,21 @@ func readConfig(useconf *bool, useconffile *string, normaliseconf *bool) *config
if err := hjson.Unmarshal(conf, &dat); err != nil { if err := hjson.Unmarshal(conf, &dat); err != nil {
panic(err) panic(err)
} }
// Check for fields that have changed type recently, e.g. the Listen config // Check if we have old field names
// option is now a []string rather than a string if _, ok := dat["TunnelRouting"]; ok {
if listen, ok := dat["Listen"].(string); ok { log.Warnln("WARNING: Tunnel routing is no longer supported")
dat["Listen"] = []string{listen}
} }
if tunnelrouting, ok := dat["TunnelRouting"].(map[string]interface{}); ok { if _, ok := dat["SigningPrivateKey"]; ok {
if c, ok := tunnelrouting["IPv4Sources"]; ok { log.Warnln("WARNING: The configuration file is out of date, please take a backup and then use -normaliseconf")
delete(tunnelrouting, "IPv4Sources") log.Warnln("WARNING: The \"SigningPrivateKey\" configuration option has been renamed to \"PrivateKey\"")
tunnelrouting["IPv4LocalSubnets"] = c if _, ok := dat["PrivateKey"]; !ok {
dat["PrivateKey"] = dat["SigningPrivateKey"]
} }
if c, ok := tunnelrouting["IPv6Sources"]; ok {
delete(tunnelrouting, "IPv6Sources")
tunnelrouting["IPv6LocalSubnets"] = c
} }
if c, ok := tunnelrouting["IPv4Destinations"]; ok { if _, ok := dat["SigningPublicKey"]; ok {
delete(tunnelrouting, "IPv4Destinations") log.Warnln("WARNING: The \"SigningPrivateKey\" configuration option has been renamed to \"PrivateKey\"")
tunnelrouting["IPv4RemoteSubnets"] = c if _, ok := dat["PublicKey"]; !ok {
} dat["PublicKey"] = dat["SigningPublicKey"]
if c, ok := tunnelrouting["IPv6Destinations"]; ok {
delete(tunnelrouting, "IPv6Destinations")
tunnelrouting["IPv6RemoteSubnets"] = c
} }
} }
// Sanitise the config // Sanitise the config
@ -176,6 +170,27 @@ func main() {
loglevel := flag.String("loglevel", "info", "loglevel to enable") loglevel := flag.String("loglevel", "info", "loglevel to enable")
flag.Parse() flag.Parse()
// Create a new logger that logs output to stdout.
var logger *log.Logger
switch *logto {
case "stdout":
logger = log.New(os.Stdout, "", log.Flags())
case "syslog":
if syslogger, err := gsyslog.NewLogger(gsyslog.LOG_NOTICE, "DAEMON", version.BuildName()); err == nil {
logger = log.New(syslogger, "", log.Flags())
}
default:
if logfd, err := os.OpenFile(*logto, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err == nil {
logger = log.New(logfd, "", log.Flags())
}
}
if logger == nil {
logger = log.New(os.Stdout, "", log.Flags())
logger.Warnln("Logging defaulting to stdout")
}
setLogLevel(*loglevel, logger)
var cfg *config.NodeConfig var cfg *config.NodeConfig
var err error var err error
switch { switch {
@ -189,7 +204,7 @@ func main() {
cfg = config.GenerateConfig() cfg = config.GenerateConfig()
case *useconffile != "" || *useconf: case *useconffile != "" || *useconf:
// Read the configuration from either stdin or from the filesystem // Read the configuration from either stdin or from the filesystem
cfg = readConfig(useconf, useconffile, normaliseconf) cfg = readConfig(logger, useconf, useconffile, normaliseconf)
// If the -normaliseconf option was specified then remarshal the above // If the -normaliseconf option was specified then remarshal the above
// configuration and print it back to stdout. This lets the user update // configuration and print it back to stdout. This lets the user update
// their configuration file with newly mapped names (like above) or to // their configuration file with newly mapped names (like above) or to
@ -222,8 +237,8 @@ func main() {
} }
// Have we been asked for the node address yet? If so, print it and then stop. // Have we been asked for the node address yet? If so, print it and then stop.
getNodeKey := func() ed25519.PublicKey { getNodeKey := func() ed25519.PublicKey {
if pubkey, err := hex.DecodeString(cfg.PublicKey); err == nil { if pubkey, err := hex.DecodeString(cfg.PrivateKey); err == nil {
return ed25519.PublicKey(pubkey) return ed25519.PrivateKey(pubkey).Public().(ed25519.PublicKey)
} }
return nil return nil
} }
@ -247,26 +262,6 @@ func main() {
return return
default: default:
} }
// Create a new logger that logs output to stdout.
var logger *log.Logger
switch *logto {
case "stdout":
logger = log.New(os.Stdout, "", log.Flags())
case "syslog":
if syslogger, err := gsyslog.NewLogger(gsyslog.LOG_NOTICE, "DAEMON", version.BuildName()); err == nil {
logger = log.New(syslogger, "", log.Flags())
}
default:
if logfd, err := os.OpenFile(*logto, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err == nil {
logger = log.New(logfd, "", log.Flags())
}
}
if logger == nil {
logger = log.New(os.Stdout, "", log.Flags())
logger.Warnln("Logging defaulting to stdout")
}
setLogLevel(*loglevel, logger)
// Setup the Yggdrasil node itself. The node{} type includes a Core, so we // Setup the Yggdrasil node itself. The node{} type includes a Core, so we
// don't need to create this manually. // don't need to create this manually.
@ -285,14 +280,14 @@ func main() {
n.tuntap.SetSessionGatekeeper(n.sessionFirewall) n.tuntap.SetSessionGatekeeper(n.sessionFirewall)
// Start the admin socket // Start the admin socket
if err := n.admin.Init(&n.core, cfg, logger, nil); err != nil { if err := n.admin.Init(&n.core, cfg, logger, nil); err != nil {
logger.Errorln("An error occured initialising admin socket:", err) logger.Errorln("An error occurred initialising admin socket:", err)
} else if err := n.admin.Start(); err != nil { } else if err := n.admin.Start(); err != nil {
logger.Errorln("An error occurred starting admin socket:", err) logger.Errorln("An error occurred starting admin socket:", err)
} }
n.admin.SetupAdminHandlers(n.admin) n.admin.SetupAdminHandlers(n.admin)
// Start the multicast interface // Start the multicast interface
if err := n.multicast.Init(&n.core, cfg, logger, nil); err != nil { if err := n.multicast.Init(&n.core, cfg, logger, nil); err != nil {
logger.Errorln("An error occured initialising multicast:", err) logger.Errorln("An error occurred initialising multicast:", err)
} else if err := n.multicast.Start(); err != nil { } else if err := n.multicast.Start(); err != nil {
logger.Errorln("An error occurred starting multicast:", err) logger.Errorln("An error occurred starting multicast:", err)
} }
@ -308,6 +303,8 @@ func main() {
// This is just logged to stdout for the user. // This is just logged to stdout for the user.
address := n.core.Address() address := n.core.Address()
subnet := n.core.Subnet() subnet := n.core.Subnet()
public := n.core.GetSelf().Key
logger.Infof("Your public key is %s", hex.EncodeToString(public[:]))
logger.Infof("Your IPv6 address is %s", address.String()) logger.Infof("Your IPv6 address is %s", address.String())
logger.Infof("Your IPv6 subnet is %s", subnet.String()) logger.Infof("Your IPv6 subnet is %s", subnet.String())
// Catch interrupts from the operating system to exit gracefully. // Catch interrupts from the operating system to exit gracefully.

View File

@ -47,8 +47,11 @@ func run() int {
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [options] command [key=value] [key=value] ...\n\n", os.Args[0]) fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [options] command [key=value] [key=value] ...\n\n", os.Args[0])
fmt.Println("Options:") fmt.Println("Options:")
flag.PrintDefaults() flag.PrintDefaults()
fmt.Println("\nPlease note that options must always specified BEFORE the command\non the command line or they will be ignored.\n") // nolint:govet fmt.Println()
fmt.Println("Commands:\n - Use \"list\" for a list of available commands\n") // nolint:govet fmt.Println("Please note that options must always specified BEFORE the command\non the command line or they will be ignored.")
fmt.Println()
fmt.Println("Commands:\n - Use \"list\" for a list of available commands")
fmt.Println()
fmt.Println("Examples:") fmt.Println("Examples:")
fmt.Println(" - ", os.Args[0], "list") fmt.Println(" - ", os.Args[0], "list")
fmt.Println(" - ", os.Args[0], "getPeers") fmt.Println(" - ", os.Args[0], "getPeers")

View File

@ -305,11 +305,7 @@ func (m *Multicast) _announce() {
if a, err := net.ResolveTCPAddr("tcp6", lladdr); err == nil { if a, err := net.ResolveTCPAddr("tcp6", lladdr); err == nil {
a.Zone = "" a.Zone = ""
destAddr.Zone = iface.Name destAddr.Zone = iface.Name
key, err := hex.DecodeString(m.config.PublicKey) msg := append([]byte(nil), m.core.GetSelf().Key...)
if err != nil {
panic(err)
}
msg := append([]byte(nil), key...)
msg = append(msg, a.String()...) msg = append(msg, a.String()...)
_, _ = m.sock.WriteTo(msg, nil, destAddr) _, _ = m.sock.WriteTo(msg, nil, destAddr)
} }

View File

@ -48,7 +48,7 @@ func (tun *TunAdapter) read() {
copy(srcSubnet[:], bs[8:]) copy(srcSubnet[:], bs[8:])
copy(dstSubnet[:], bs[24:]) copy(dstSubnet[:], bs[24:])
if srcAddr != tun.addr && srcSubnet != tun.subnet { if srcAddr != tun.addr && srcSubnet != tun.subnet {
continue // Wrong soruce address continue // Wrong source address
} }
bs = buf[begin-1 : end] bs = buf[begin-1 : end]
bs[0] = typeSessionTraffic bs[0] = typeSessionTraffic

View File

@ -90,7 +90,7 @@ func (tun *TunAdapter) setupAddress(addr string) error {
ar.ifra_prefixmask.sin6_len = uint8(unsafe.Sizeof(ar.ifra_prefixmask)) ar.ifra_prefixmask.sin6_len = uint8(unsafe.Sizeof(ar.ifra_prefixmask))
b := make([]byte, 16) b := make([]byte, 16)
binary.LittleEndian.PutUint16(b, uint16(0xFE00)) binary.LittleEndian.PutUint16(b, uint16(0xFE00))
ar.ifra_prefixmask.sin6_addr[0] = uint16(binary.BigEndian.Uint16(b)) ar.ifra_prefixmask.sin6_addr[0] = binary.BigEndian.Uint16(b)
ar.ifra_addr.sin6_len = uint8(unsafe.Sizeof(ar.ifra_addr)) ar.ifra_addr.sin6_len = uint8(unsafe.Sizeof(ar.ifra_addr))
ar.ifra_addr.sin6_family = unix.AF_INET6 ar.ifra_addr.sin6_family = unix.AF_INET6
@ -99,7 +99,7 @@ func (tun *TunAdapter) setupAddress(addr string) error {
addr, _ := strconv.ParseUint(parts[i], 16, 16) addr, _ := strconv.ParseUint(parts[i], 16, 16)
b := make([]byte, 16) b := make([]byte, 16)
binary.LittleEndian.PutUint16(b, uint16(addr)) binary.LittleEndian.PutUint16(b, uint16(addr))
ar.ifra_addr.sin6_addr[i] = uint16(binary.BigEndian.Uint16(b)) ar.ifra_addr.sin6_addr[i] = binary.BigEndian.Uint16(b)
} }
ar.ifra_flags |= darwin_IN6_IFF_NODAD ar.ifra_flags |= darwin_IN6_IFF_NODAD