2017-12-29 04:16:20 +00:00
|
|
|
package main
|
|
|
|
|
2018-06-15 09:30:09 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
2021-07-01 13:04:01 +00:00
|
|
|
"context"
|
2021-05-15 20:55:47 +00:00
|
|
|
"crypto/ed25519"
|
2019-06-11 11:50:01 +00:00
|
|
|
"encoding/hex"
|
2018-06-15 09:30:09 +00:00
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2019-11-10 19:38:35 +00:00
|
|
|
"net"
|
2018-06-15 09:30:09 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2018-12-14 17:49:42 +00:00
|
|
|
"strings"
|
2018-06-15 09:30:09 +00:00
|
|
|
"syscall"
|
2017-12-29 04:16:20 +00:00
|
|
|
|
2018-06-15 09:30:09 +00:00
|
|
|
"golang.org/x/text/encoding/unicode"
|
2018-06-15 08:20:41 +00:00
|
|
|
|
2019-01-27 13:31:43 +00:00
|
|
|
"github.com/gologme/log"
|
2019-06-28 23:32:23 +00:00
|
|
|
gsyslog "github.com/hashicorp/go-syslog"
|
2018-12-17 10:50:57 +00:00
|
|
|
"github.com/hjson/hjson-go"
|
2018-06-15 09:30:09 +00:00
|
|
|
"github.com/kardianos/minwinsvc"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
2017-12-29 04:16:20 +00:00
|
|
|
|
2019-11-10 19:38:35 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
2019-05-19 16:27:48 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/admin"
|
2018-12-08 01:56:04 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/config"
|
2021-06-27 07:18:51 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/defaults"
|
2021-05-23 19:33:28 +00:00
|
|
|
|
2021-05-23 19:42:26 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/core"
|
2021-07-05 18:14:12 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/ipv6rwc"
|
2019-03-28 16:13:14 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/multicast"
|
2019-03-28 00:30:25 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/tuntap"
|
2019-08-10 21:31:22 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/version"
|
2018-06-15 09:30:09 +00:00
|
|
|
)
|
2018-02-16 14:12:44 +00:00
|
|
|
|
2017-12-29 04:16:20 +00:00
|
|
|
type node struct {
|
2021-05-23 19:42:26 +00:00
|
|
|
core core.Core
|
2021-06-02 13:19:32 +00:00
|
|
|
config *config.NodeConfig
|
2021-06-05 19:57:03 +00:00
|
|
|
tuntap *tuntap.TunAdapter
|
|
|
|
multicast *multicast.Multicast
|
|
|
|
admin *admin.AdminSocket
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 13:04:01 +00:00
|
|
|
func readConfig(log *log.Logger, useconf bool, useconffile string, normaliseconf bool) *config.NodeConfig {
|
2018-12-30 12:26:55 +00:00
|
|
|
// Use a configuration file. If -useconf, the configuration will be read
|
|
|
|
// from stdin. If -useconffile, the configuration will be read from the
|
|
|
|
// filesystem.
|
2019-01-14 14:01:38 +00:00
|
|
|
var conf []byte
|
2018-12-30 12:26:55 +00:00
|
|
|
var err error
|
2021-07-01 13:04:01 +00:00
|
|
|
if useconffile != "" {
|
2018-12-30 12:26:55 +00:00
|
|
|
// Read the file from the filesystem
|
2021-07-01 13:04:01 +00:00
|
|
|
conf, err = ioutil.ReadFile(useconffile)
|
2018-12-30 12:26:55 +00:00
|
|
|
} else {
|
|
|
|
// Read the file from stdin.
|
2019-01-14 14:01:38 +00:00
|
|
|
conf, err = ioutil.ReadAll(os.Stdin)
|
2018-12-30 12:26:55 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
// If there's a byte order mark - which Windows 10 is now incredibly fond of
|
|
|
|
// throwing everywhere when it's converting things into UTF-16 for the hell
|
|
|
|
// of it - remove it and decode back down into UTF-8. This is necessary
|
|
|
|
// because hjson doesn't know what to do with UTF-16 and will panic
|
2020-09-27 13:42:46 +00:00
|
|
|
if bytes.Equal(conf[0:2], []byte{0xFF, 0xFE}) ||
|
|
|
|
bytes.Equal(conf[0:2], []byte{0xFE, 0xFF}) {
|
2018-12-30 12:26:55 +00:00
|
|
|
utf := unicode.UTF16(unicode.BigEndian, unicode.UseBOM)
|
|
|
|
decoder := utf.NewDecoder()
|
2019-01-14 14:01:38 +00:00
|
|
|
conf, err = decoder.Bytes(conf)
|
2018-12-30 12:26:55 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Generate a new configuration - this gives us a set of sane defaults -
|
|
|
|
// then parse the configuration we loaded above on top of it. The effect
|
|
|
|
// of this is that any configuration item that is missing from the provided
|
|
|
|
// configuration will use a sane default.
|
2021-06-27 07:18:51 +00:00
|
|
|
cfg := defaults.GenerateConfig()
|
2018-12-30 12:26:55 +00:00
|
|
|
var dat map[string]interface{}
|
2019-01-14 14:01:38 +00:00
|
|
|
if err := hjson.Unmarshal(conf, &dat); err != nil {
|
2018-12-30 12:26:55 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2021-06-05 20:32:04 +00:00
|
|
|
// Check if we have old field names
|
|
|
|
if _, ok := dat["TunnelRouting"]; ok {
|
|
|
|
log.Warnln("WARNING: Tunnel routing is no longer supported")
|
2019-08-07 09:52:19 +00:00
|
|
|
}
|
2021-06-05 20:40:58 +00:00
|
|
|
if old, ok := dat["SigningPrivateKey"]; ok {
|
2021-06-05 20:32:04 +00:00
|
|
|
log.Warnln("WARNING: The \"SigningPrivateKey\" configuration option has been renamed to \"PrivateKey\"")
|
|
|
|
if _, ok := dat["PrivateKey"]; !ok {
|
2021-06-05 20:40:58 +00:00
|
|
|
if privstr, err := hex.DecodeString(old.(string)); err == nil {
|
|
|
|
priv := ed25519.PrivateKey(privstr)
|
|
|
|
pub := priv.Public().(ed25519.PublicKey)
|
|
|
|
dat["PrivateKey"] = hex.EncodeToString(priv[:])
|
|
|
|
dat["PublicKey"] = hex.EncodeToString(pub[:])
|
2021-06-05 20:49:11 +00:00
|
|
|
} else {
|
|
|
|
log.Warnln("WARNING: The \"SigningPrivateKey\" configuration option contains an invalid value and will be ignored")
|
2021-06-05 20:40:58 +00:00
|
|
|
}
|
2019-08-19 09:28:30 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-27 08:33:29 +00:00
|
|
|
if oldmc, ok := dat["MulticastInterfaces"]; ok {
|
|
|
|
if oldmcvals, ok := oldmc.([]interface{}); ok {
|
|
|
|
var newmc []config.MulticastInterfaceConfig
|
|
|
|
for _, oldmcval := range oldmcvals {
|
|
|
|
if str, ok := oldmcval.(string); ok {
|
|
|
|
newmc = append(newmc, config.MulticastInterfaceConfig{
|
2021-06-27 22:24:46 +00:00
|
|
|
Regex: str,
|
|
|
|
Beacon: true,
|
|
|
|
Listen: true,
|
2021-06-27 08:33:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if newmc != nil {
|
2021-06-27 22:24:46 +00:00
|
|
|
if oldport, ok := dat["LinkLocalTCPPort"]; ok {
|
|
|
|
// numbers parse to float64 by default
|
|
|
|
if port, ok := oldport.(float64); ok {
|
|
|
|
for idx := range newmc {
|
|
|
|
newmc[idx].Port = uint16(port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-27 08:33:29 +00:00
|
|
|
dat["MulticastInterfaces"] = newmc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 09:52:19 +00:00
|
|
|
// Sanitise the config
|
2018-12-30 12:26:55 +00:00
|
|
|
confJson, err := json.Marshal(dat)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-06-02 13:40:09 +00:00
|
|
|
if err := json.Unmarshal(confJson, &cfg); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-12-30 12:26:55 +00:00
|
|
|
// Overlay our newly mapped configuration onto the autoconf node config that
|
|
|
|
// we generated above.
|
|
|
|
if err = mapstructure.Decode(dat, &cfg); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
2018-05-27 21:13:37 +00:00
|
|
|
// Generates a new configuration and returns it in HJSON format. This is used
|
|
|
|
// with -genconf.
|
2018-12-02 23:49:48 +00:00
|
|
|
func doGenconf(isjson bool) string {
|
2021-06-27 07:18:51 +00:00
|
|
|
cfg := defaults.GenerateConfig()
|
2018-12-02 23:49:48 +00:00
|
|
|
var bs []byte
|
|
|
|
var err error
|
|
|
|
if isjson {
|
|
|
|
bs, err = json.MarshalIndent(cfg, "", " ")
|
|
|
|
} else {
|
|
|
|
bs, err = hjson.Marshal(cfg)
|
|
|
|
}
|
2018-01-04 22:37:51 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return string(bs)
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
2019-11-30 18:46:29 +00:00
|
|
|
func setLogLevel(loglevel string, logger *log.Logger) {
|
|
|
|
levels := [...]string{"error", "warn", "info", "debug", "trace"}
|
|
|
|
loglevel = strings.ToLower(loglevel)
|
|
|
|
|
|
|
|
contains := func() bool {
|
|
|
|
for _, l := range levels {
|
|
|
|
if l == loglevel {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !contains() { // set default log level
|
|
|
|
logger.Infoln("Loglevel parse failed. Set default level(info)")
|
|
|
|
loglevel = "info"
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, l := range levels {
|
|
|
|
logger.EnableLevel(l)
|
|
|
|
if l == loglevel {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-01 13:04:01 +00:00
|
|
|
type yggArgs struct {
|
|
|
|
genconf bool
|
|
|
|
useconf bool
|
|
|
|
normaliseconf bool
|
|
|
|
confjson bool
|
|
|
|
autoconf bool
|
|
|
|
ver bool
|
|
|
|
getaddr bool
|
|
|
|
getsnet bool
|
2021-09-21 20:19:40 +00:00
|
|
|
useconffile string
|
|
|
|
logto string
|
2021-07-01 13:04:01 +00:00
|
|
|
loglevel string
|
|
|
|
}
|
|
|
|
|
|
|
|
func getArgs() yggArgs {
|
2018-05-27 21:13:37 +00:00
|
|
|
genconf := flag.Bool("genconf", false, "print a new config to stdout")
|
2018-12-02 23:52:57 +00:00
|
|
|
useconf := flag.Bool("useconf", false, "read HJSON/JSON config from stdin")
|
|
|
|
useconffile := flag.String("useconffile", "", "read HJSON/JSON config from specified file path")
|
2018-05-27 21:13:37 +00:00
|
|
|
normaliseconf := flag.Bool("normaliseconf", false, "use in combination with either -useconf or -useconffile, outputs your configuration normalised")
|
2018-12-02 23:49:48 +00:00
|
|
|
confjson := flag.Bool("json", false, "print configuration from -genconf or -normaliseconf as JSON instead of HJSON")
|
2018-05-27 21:13:37 +00:00
|
|
|
autoconf := flag.Bool("autoconf", false, "automatic mode (dynamic IP, peer with IPv6 neighbors)")
|
2019-08-14 19:09:02 +00:00
|
|
|
ver := flag.Bool("version", false, "prints the version of this build")
|
2019-06-28 22:45:04 +00:00
|
|
|
logto := flag.String("logto", "stdout", "file path to log to, \"syslog\" or \"stdout\"")
|
2019-11-10 19:38:35 +00:00
|
|
|
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")
|
2019-11-30 18:46:29 +00:00
|
|
|
loglevel := flag.String("loglevel", "info", "loglevel to enable")
|
2018-01-04 22:37:51 +00:00
|
|
|
flag.Parse()
|
2021-07-01 13:04:01 +00:00
|
|
|
return yggArgs{
|
|
|
|
genconf: *genconf,
|
|
|
|
useconf: *useconf,
|
|
|
|
useconffile: *useconffile,
|
|
|
|
normaliseconf: *normaliseconf,
|
|
|
|
confjson: *confjson,
|
|
|
|
autoconf: *autoconf,
|
|
|
|
ver: *ver,
|
|
|
|
logto: *logto,
|
|
|
|
getaddr: *getaddr,
|
|
|
|
getsnet: *getsnet,
|
|
|
|
loglevel: *loglevel,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The main function is responsible for configuring and starting Yggdrasil.
|
|
|
|
func run(args yggArgs, ctx context.Context, done chan struct{}) {
|
|
|
|
defer close(done)
|
2021-06-05 20:32:04 +00:00
|
|
|
// Create a new logger that logs output to stdout.
|
|
|
|
var logger *log.Logger
|
2021-07-01 13:04:01 +00:00
|
|
|
switch args.logto {
|
2021-06-05 20:32:04 +00:00
|
|
|
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:
|
2021-07-01 13:04:01 +00:00
|
|
|
if logfd, err := os.OpenFile(args.logto, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err == nil {
|
2021-06-05 20:32:04 +00:00
|
|
|
logger = log.New(logfd, "", log.Flags())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if logger == nil {
|
|
|
|
logger = log.New(os.Stdout, "", log.Flags())
|
|
|
|
logger.Warnln("Logging defaulting to stdout")
|
|
|
|
}
|
|
|
|
|
2021-07-01 13:04:01 +00:00
|
|
|
if args.normaliseconf {
|
2021-06-05 20:49:11 +00:00
|
|
|
setLogLevel("error", logger)
|
|
|
|
} else {
|
2021-07-01 13:04:01 +00:00
|
|
|
setLogLevel(args.loglevel, logger)
|
2021-06-05 20:49:11 +00:00
|
|
|
}
|
2021-06-05 20:32:04 +00:00
|
|
|
|
2019-06-11 11:50:01 +00:00
|
|
|
var cfg *config.NodeConfig
|
2018-12-30 12:26:55 +00:00
|
|
|
var err error
|
2018-01-04 22:37:51 +00:00
|
|
|
switch {
|
2021-07-01 13:04:01 +00:00
|
|
|
case args.ver:
|
2019-08-10 21:31:22 +00:00
|
|
|
fmt.Println("Build name:", version.BuildName())
|
|
|
|
fmt.Println("Build version:", version.BuildVersion())
|
2019-08-14 19:09:02 +00:00
|
|
|
return
|
2021-07-01 13:04:01 +00:00
|
|
|
case args.autoconf:
|
2018-05-27 21:13:37 +00:00
|
|
|
// Use an autoconf-generated config, this will give us random keys and
|
|
|
|
// port numbers, and will use an automatically selected TUN/TAP interface.
|
2021-06-27 07:18:51 +00:00
|
|
|
cfg = defaults.GenerateConfig()
|
2021-07-01 13:04:01 +00:00
|
|
|
case args.useconffile != "" || args.useconf:
|
2018-12-30 12:26:55 +00:00
|
|
|
// Read the configuration from either stdin or from the filesystem
|
2021-07-01 13:04:01 +00:00
|
|
|
cfg = readConfig(logger, args.useconf, args.useconffile, args.normaliseconf)
|
2018-05-27 21:13:37 +00:00
|
|
|
// If the -normaliseconf option was specified then remarshal the above
|
|
|
|
// configuration and print it back to stdout. This lets the user update
|
|
|
|
// their configuration file with newly mapped names (like above) or to
|
|
|
|
// convert from plain JSON to commented HJSON.
|
2021-07-01 13:04:01 +00:00
|
|
|
if args.normaliseconf {
|
2018-12-02 23:49:48 +00:00
|
|
|
var bs []byte
|
2021-07-01 13:04:01 +00:00
|
|
|
if args.confjson {
|
2018-12-02 23:49:48 +00:00
|
|
|
bs, err = json.MarshalIndent(cfg, "", " ")
|
|
|
|
} else {
|
|
|
|
bs, err = hjson.Marshal(cfg)
|
|
|
|
}
|
2018-05-23 11:04:27 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fmt.Println(string(bs))
|
|
|
|
return
|
|
|
|
}
|
2021-07-01 13:04:01 +00:00
|
|
|
case args.genconf:
|
2018-05-27 21:13:37 +00:00
|
|
|
// Generate a new configuration and print it to stdout.
|
2021-07-01 13:04:01 +00:00
|
|
|
fmt.Println(doGenconf(args.confjson))
|
2021-07-03 22:27:00 +00:00
|
|
|
return
|
2018-01-04 22:37:51 +00:00
|
|
|
default:
|
2018-05-27 21:13:37 +00:00
|
|
|
// No flags were provided, therefore print the list of flags to stdout.
|
2018-01-04 22:37:51 +00:00
|
|
|
flag.PrintDefaults()
|
|
|
|
}
|
2018-05-27 21:13:37 +00:00
|
|
|
// Have we got a working configuration? If we don't then it probably means
|
|
|
|
// that neither -autoconf, -useconf or -useconffile were set above. Stop
|
|
|
|
// if we don't.
|
2018-01-04 22:37:51 +00:00
|
|
|
if cfg == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-11-10 19:38:35 +00:00
|
|
|
// Have we been asked for the node address yet? If so, print it and then stop.
|
2021-05-23 19:33:28 +00:00
|
|
|
getNodeKey := func() ed25519.PublicKey {
|
2021-06-05 20:32:04 +00:00
|
|
|
if pubkey, err := hex.DecodeString(cfg.PrivateKey); err == nil {
|
|
|
|
return ed25519.PrivateKey(pubkey).Public().(ed25519.PublicKey)
|
2021-05-23 19:33:28 +00:00
|
|
|
}
|
2019-11-11 09:40:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-11-10 19:38:35 +00:00
|
|
|
switch {
|
2021-07-01 13:04:01 +00:00
|
|
|
case args.getaddr:
|
2021-05-23 19:33:28 +00:00
|
|
|
if key := getNodeKey(); key != nil {
|
|
|
|
addr := address.AddrForKey(key)
|
2019-11-11 09:40:25 +00:00
|
|
|
ip := net.IP(addr[:])
|
|
|
|
fmt.Println(ip.String())
|
2019-11-10 19:38:35 +00:00
|
|
|
}
|
|
|
|
return
|
2021-07-01 13:04:01 +00:00
|
|
|
case args.getsnet:
|
2021-05-23 19:33:28 +00:00
|
|
|
if key := getNodeKey(); key != nil {
|
|
|
|
snet := address.SubnetForKey(key)
|
2019-11-11 09:40:25 +00:00
|
|
|
ipnet := net.IPNet{
|
|
|
|
IP: append(snet[:], 0, 0, 0, 0, 0, 0, 0, 0),
|
|
|
|
Mask: net.CIDRMask(len(snet)*8, 128),
|
|
|
|
}
|
2019-11-10 19:38:35 +00:00
|
|
|
fmt.Println(ipnet.String())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
2019-11-30 18:46:29 +00:00
|
|
|
|
2018-05-27 21:13:37 +00:00
|
|
|
// Setup the Yggdrasil node itself. The node{} type includes a Core, so we
|
|
|
|
// don't need to create this manually.
|
2021-06-05 11:00:33 +00:00
|
|
|
n := node{config: cfg}
|
2019-03-29 08:38:09 +00:00
|
|
|
// Now start Yggdrasil - this starts the DHT, router, switch and other core
|
|
|
|
// components needed for Yggdrasil to operate
|
2021-06-02 13:19:32 +00:00
|
|
|
if err = n.core.Start(cfg, logger); err != nil {
|
2019-01-27 13:31:43 +00:00
|
|
|
logger.Errorln("An error occurred during startup")
|
2018-05-27 21:13:37 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2019-06-11 11:50:01 +00:00
|
|
|
// Register the session firewall gatekeeper function
|
2019-10-23 09:44:58 +00:00
|
|
|
// Allocate our modules
|
|
|
|
n.admin = &admin.AdminSocket{}
|
|
|
|
n.multicast = &multicast.Multicast{}
|
|
|
|
n.tuntap = &tuntap.TunAdapter{}
|
2019-05-19 16:27:48 +00:00
|
|
|
// Start the admin socket
|
2021-06-02 13:40:09 +00:00
|
|
|
if err := n.admin.Init(&n.core, cfg, logger, nil); err != nil {
|
2021-06-05 20:32:04 +00:00
|
|
|
logger.Errorln("An error occurred initialising admin socket:", err)
|
2021-06-02 13:40:09 +00:00
|
|
|
} else if err := n.admin.Start(); err != nil {
|
2019-05-19 16:27:48 +00:00
|
|
|
logger.Errorln("An error occurred starting admin socket:", err)
|
|
|
|
}
|
2021-06-05 19:57:03 +00:00
|
|
|
n.admin.SetupAdminHandlers(n.admin)
|
2019-03-28 16:13:14 +00:00
|
|
|
// Start the multicast interface
|
2021-06-02 13:40:09 +00:00
|
|
|
if err := n.multicast.Init(&n.core, cfg, logger, nil); err != nil {
|
2021-06-05 20:32:04 +00:00
|
|
|
logger.Errorln("An error occurred initialising multicast:", err)
|
2021-06-02 13:40:09 +00:00
|
|
|
} else if err := n.multicast.Start(); err != nil {
|
2019-03-28 16:13:14 +00:00
|
|
|
logger.Errorln("An error occurred starting multicast:", err)
|
|
|
|
}
|
2021-06-05 19:57:03 +00:00
|
|
|
n.multicast.SetupAdminHandlers(n.admin)
|
2019-04-20 15:32:27 +00:00
|
|
|
// Start the TUN/TAP interface
|
2021-07-05 18:14:12 +00:00
|
|
|
rwc := ipv6rwc.NewReadWriteCloser(&n.core)
|
|
|
|
if err := n.tuntap.Init(rwc, cfg, logger, nil); err != nil {
|
2021-06-02 13:40:09 +00:00
|
|
|
logger.Errorln("An error occurred initialising TUN/TAP:", err)
|
|
|
|
} else if err := n.tuntap.Start(); err != nil {
|
2021-05-08 16:52:22 +00:00
|
|
|
logger.Errorln("An error occurred starting TUN/TAP:", err)
|
|
|
|
}
|
2021-06-05 19:57:03 +00:00
|
|
|
n.tuntap.SetupAdminHandlers(n.admin)
|
2018-05-27 21:13:37 +00:00
|
|
|
// Make some nice output that tells us what our IPv6 address and subnet are.
|
|
|
|
// This is just logged to stdout for the user.
|
2019-03-29 18:24:57 +00:00
|
|
|
address := n.core.Address()
|
|
|
|
subnet := n.core.Subnet()
|
2021-06-05 20:32:04 +00:00
|
|
|
public := n.core.GetSelf().Key
|
|
|
|
logger.Infof("Your public key is %s", hex.EncodeToString(public[:]))
|
2019-01-27 13:31:43 +00:00
|
|
|
logger.Infof("Your IPv6 address is %s", address.String())
|
|
|
|
logger.Infof("Your IPv6 subnet is %s", subnet.String())
|
2018-05-27 21:13:37 +00:00
|
|
|
// Catch interrupts from the operating system to exit gracefully.
|
2021-07-01 13:04:01 +00:00
|
|
|
<-ctx.Done()
|
2019-07-06 11:04:31 +00:00
|
|
|
// Capture the service being stopped on Windows.
|
2019-07-06 11:17:40 +00:00
|
|
|
minwinsvc.SetOnExit(n.shutdown)
|
2021-05-10 21:47:28 +00:00
|
|
|
n.shutdown()
|
2019-07-06 11:17:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *node) shutdown() {
|
2021-06-02 13:40:09 +00:00
|
|
|
_ = n.admin.Stop()
|
|
|
|
_ = n.multicast.Stop()
|
|
|
|
_ = n.tuntap.Stop()
|
2019-09-18 14:22:17 +00:00
|
|
|
n.core.Stop()
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|
2021-07-01 13:04:01 +00:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
args := getArgs()
|
|
|
|
hup := make(chan os.Signal, 1)
|
2021-07-01 13:54:14 +00:00
|
|
|
//signal.Notify(hup, os.Interrupt, syscall.SIGHUP)
|
2021-07-01 13:04:01 +00:00
|
|
|
term := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(term, os.Interrupt, syscall.SIGTERM)
|
|
|
|
for {
|
|
|
|
done := make(chan struct{})
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
go run(args, ctx, done)
|
|
|
|
select {
|
|
|
|
case <-hup:
|
|
|
|
cancel()
|
|
|
|
<-done
|
|
|
|
case <-term:
|
|
|
|
cancel()
|
|
|
|
<-done
|
|
|
|
return
|
2021-07-03 22:27:00 +00:00
|
|
|
case <-done:
|
|
|
|
return
|
2021-07-01 13:04:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|