Move "-logto" and "-loglevel" parameters to configuration file

This commit is contained in:
beryll1um 2022-04-19 07:47:49 +02:00
parent 6c4778bb67
commit 0c4b21353f
3 changed files with 33 additions and 36 deletions

View file

@ -43,7 +43,7 @@ type node struct {
admin *admin.AdminSocket admin *admin.AdminSocket
} }
func readConfig(log *log.Logger, useconf bool, useconffile string, normaliseconf bool) *config.NodeConfig { func readConfig(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.
@ -83,10 +83,10 @@ func readConfig(log *log.Logger, useconf bool, useconffile string, normaliseconf
} }
// Check if we have old field names // Check if we have old field names
if _, ok := dat["TunnelRouting"]; ok { if _, ok := dat["TunnelRouting"]; ok {
log.Warnln("WARNING: Tunnel routing is no longer supported") fmt.Println("WARNING: Tunnel routing is no longer supported")
} }
if old, ok := dat["SigningPrivateKey"]; ok { if old, ok := dat["SigningPrivateKey"]; ok {
log.Warnln("WARNING: The \"SigningPrivateKey\" configuration option has been renamed to \"PrivateKey\"") fmt.Println("WARNING: The \"SigningPrivateKey\" configuration option has been renamed to \"PrivateKey\"")
if _, ok := dat["PrivateKey"]; !ok { if _, ok := dat["PrivateKey"]; !ok {
if privstr, err := hex.DecodeString(old.(string)); err == nil { if privstr, err := hex.DecodeString(old.(string)); err == nil {
priv := ed25519.PrivateKey(privstr) priv := ed25519.PrivateKey(privstr)
@ -94,7 +94,7 @@ func readConfig(log *log.Logger, useconf bool, useconffile string, normaliseconf
dat["PrivateKey"] = hex.EncodeToString(priv[:]) dat["PrivateKey"] = hex.EncodeToString(priv[:])
dat["PublicKey"] = hex.EncodeToString(pub[:]) dat["PublicKey"] = hex.EncodeToString(pub[:])
} else { } else {
log.Warnln("WARNING: The \"SigningPrivateKey\" configuration option contains an invalid value and will be ignored") fmt.Println("WARNING: The \"SigningPrivateKey\" configuration option contains an invalid value and will be ignored")
} }
} }
} }
@ -192,8 +192,6 @@ type yggArgs struct {
getaddr bool getaddr bool
getsnet bool getsnet bool
useconffile string useconffile string
logto string
loglevel string
} }
func getArgs() yggArgs { func getArgs() yggArgs {
@ -204,10 +202,8 @@ func getArgs() yggArgs {
confjson := flag.Bool("json", false, "print configuration from -genconf or -normaliseconf as JSON instead of HJSON") confjson := flag.Bool("json", false, "print configuration from -genconf or -normaliseconf as JSON instead of HJSON")
autoconf := flag.Bool("autoconf", false, "automatic mode (dynamic IP, peer with IPv6 neighbors)") autoconf := flag.Bool("autoconf", false, "automatic mode (dynamic IP, peer with IPv6 neighbors)")
ver := flag.Bool("version", false, "prints the version of this build") ver := flag.Bool("version", false, "prints the version of this build")
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") 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") getsnet := flag.Bool("subnet", false, "returns the IPv6 subnet as derived from the supplied configuration")
loglevel := flag.String("loglevel", "info", "loglevel to enable")
flag.Parse() flag.Parse()
return yggArgs{ return yggArgs{
genconf: *genconf, genconf: *genconf,
@ -217,41 +213,14 @@ func getArgs() yggArgs {
confjson: *confjson, confjson: *confjson,
autoconf: *autoconf, autoconf: *autoconf,
ver: *ver, ver: *ver,
logto: *logto,
getaddr: *getaddr, getaddr: *getaddr,
getsnet: *getsnet, getsnet: *getsnet,
loglevel: *loglevel,
} }
} }
// The main function is responsible for configuring and starting Yggdrasil. // The main function is responsible for configuring and starting Yggdrasil.
func run(args yggArgs, ctx context.Context, done chan struct{}) { func run(args yggArgs, ctx context.Context, done chan struct{}) {
defer close(done) defer close(done)
// Create a new logger that logs output to stdout.
var logger *log.Logger
switch args.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(args.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")
}
if args.normaliseconf {
setLogLevel("error", logger)
} else {
setLogLevel(args.loglevel, logger)
}
var cfg *config.NodeConfig var cfg *config.NodeConfig
var err error var err error
switch { switch {
@ -265,7 +234,7 @@ func run(args yggArgs, ctx context.Context, done chan struct{}) {
cfg = defaults.GenerateConfig() cfg = defaults.GenerateConfig()
case args.useconffile != "" || args.useconf: case args.useconffile != "" || args.useconf:
// Read the configuration from either stdin or from the filesystem // Read the configuration from either stdin or from the filesystem
cfg = readConfig(logger, args.useconf, args.useconffile, args.normaliseconf) cfg = readConfig(args.useconf, args.useconffile, args.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
@ -297,6 +266,30 @@ func run(args yggArgs, ctx context.Context, done chan struct{}) {
if cfg == nil { if cfg == nil {
return return
} }
// Create a new logger that logs output to stdout.
var logger *log.Logger
switch cfg.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(cfg.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")
}
if args.normaliseconf {
setLogLevel("error", logger)
} else {
setLogLevel(cfg.LogLevel, logger)
}
// 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.PrivateKey); err == nil { if pubkey, err := hex.DecodeString(cfg.PrivateKey); err == nil {

View file

@ -27,6 +27,8 @@ import (
// supply one of these structs to the Yggdrasil core when starting a node. // supply one of these structs to the Yggdrasil core when starting a node.
type NodeConfig struct { type NodeConfig struct {
sync.RWMutex `json:"-"` sync.RWMutex `json:"-"`
LogTo string `comment:"Your file path to log to, \"syslog\" or \"stdout\". It will override run argument provided configuration."`
LogLevel string `comment:"Your log level, \"error\", \"warn\", \"info\", \"debug\", \"trace\"."`
Peers []string `comment:"List of connection strings for outbound peer connections in URI format,\ne.g. tls://a.b.c.d:e or socks://a.b.c.d:e/f.g.h.i:j. These connections\nwill obey the operating system routing table, therefore you should\nuse this section when you may connect via different interfaces."` Peers []string `comment:"List of connection strings for outbound peer connections in URI format,\ne.g. tls://a.b.c.d:e or socks://a.b.c.d:e/f.g.h.i:j. These connections\nwill obey the operating system routing table, therefore you should\nuse this section when you may connect via different interfaces."`
InterfacePeers map[string][]string `comment:"List of connection strings for outbound peer connections in URI format,\narranged by source interface, e.g. { \"eth0\": [ tls://a.b.c.d:e ] }.\nNote that SOCKS peerings will NOT be affected by this option and should\ngo in the \"Peers\" section instead."` InterfacePeers map[string][]string `comment:"List of connection strings for outbound peer connections in URI format,\narranged by source interface, e.g. { \"eth0\": [ tls://a.b.c.d:e ] }.\nNote that SOCKS peerings will NOT be affected by this option and should\ngo in the \"Peers\" section instead."`
Listen []string `comment:"Listen addresses for incoming connections. You will need to add\nlisteners in order to accept incoming peerings from non-local nodes.\nMulticast peer discovery will work regardless of any listeners set\nhere. Each listener should be specified in URI format as above, e.g.\ntls://0.0.0.0:0 or tls://[::]:0 to listen on all interfaces."` Listen []string `comment:"Listen addresses for incoming connections. You will need to add\nlisteners in order to accept incoming peerings from non-local nodes.\nMulticast peer discovery will work regardless of any listeners set\nhere. Each listener should be specified in URI format as above, e.g.\ntls://0.0.0.0:0 or tls://[::]:0 to listen on all interfaces."`

View file

@ -30,6 +30,8 @@ func GenerateConfig() *config.NodeConfig {
// Create a node configuration and populate it. // Create a node configuration and populate it.
cfg := new(config.NodeConfig) cfg := new(config.NodeConfig)
cfg.NewKeys() cfg.NewKeys()
cfg.LogTo = "stdout"
cfg.LogLevel = "info"
cfg.Listen = []string{} cfg.Listen = []string{}
cfg.AdminListen = GetDefaults().DefaultAdminListen cfg.AdminListen = GetDefaults().DefaultAdminListen
cfg.Peers = []string{} cfg.Peers = []string{}