2017-12-28 22:16:20 -06:00
|
|
|
package main
|
|
|
|
|
2018-06-15 04:30:09 -05:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"regexp"
|
2018-12-14 17:49:42 +00:00
|
|
|
"strings"
|
2018-06-15 04:30:09 -05:00
|
|
|
"syscall"
|
2017-12-28 22:16:20 -06:00
|
|
|
|
2018-06-15 04:30:09 -05:00
|
|
|
"golang.org/x/text/encoding/unicode"
|
2018-06-15 09:20:41 +01:00
|
|
|
|
2018-12-17 10:50:57 +00:00
|
|
|
"github.com/hjson/hjson-go"
|
2018-06-15 04:30:09 -05:00
|
|
|
"github.com/kardianos/minwinsvc"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
2017-12-28 22:16:20 -06:00
|
|
|
|
2018-12-07 19:56:04 -06:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/config"
|
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil"
|
2018-06-15 04:30:09 -05:00
|
|
|
)
|
2018-02-16 14:12:44 +00:00
|
|
|
|
2018-04-19 10:30:40 -04:00
|
|
|
type nodeConfig = config.NodeConfig
|
|
|
|
type Core = yggdrasil.Core
|
2017-12-28 22:16:20 -06:00
|
|
|
|
|
|
|
type node struct {
|
2018-01-04 22:37:51 +00:00
|
|
|
core Core
|
2017-12-28 22:16:20 -06:00
|
|
|
}
|
|
|
|
|
2018-12-30 12:26:55 +00:00
|
|
|
func readConfig(useconf *bool, useconffile *string, normaliseconf *bool) *nodeConfig {
|
|
|
|
// 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
|
|
|
|
if *useconffile != "" {
|
|
|
|
// Read the file from the filesystem
|
2019-01-14 14:01:38 +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
|
2019-01-14 14:01:38 +00:00
|
|
|
if bytes.Compare(conf[0:2], []byte{0xFF, 0xFE}) == 0 ||
|
|
|
|
bytes.Compare(conf[0:2], []byte{0xFE, 0xFF}) == 0 {
|
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.
|
2019-01-14 14:01:38 +00:00
|
|
|
cfg := config.GenerateConfig(false)
|
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)
|
|
|
|
}
|
|
|
|
confJson, err := json.Marshal(dat)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
json.Unmarshal(confJson, &cfg)
|
|
|
|
// For now we will do a little bit to help the user adjust their
|
|
|
|
// configuration to match the new configuration format, as some of the key
|
|
|
|
// names have changed recently.
|
|
|
|
changes := map[string]string{
|
|
|
|
"Multicast": "",
|
|
|
|
"LinkLocal": "MulticastInterfaces",
|
|
|
|
"BoxPub": "EncryptionPublicKey",
|
|
|
|
"BoxPriv": "EncryptionPrivateKey",
|
|
|
|
"SigPub": "SigningPublicKey",
|
|
|
|
"SigPriv": "SigningPrivateKey",
|
|
|
|
"AllowedBoxPubs": "AllowedEncryptionPublicKeys",
|
|
|
|
}
|
|
|
|
// Loop over the mappings aove and see if we have anything to fix.
|
|
|
|
for from, to := range changes {
|
|
|
|
if _, ok := dat[from]; ok {
|
|
|
|
if to == "" {
|
|
|
|
if !*normaliseconf {
|
|
|
|
log.Println("Warning: Deprecated config option", from, "- please remove")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !*normaliseconf {
|
|
|
|
log.Println("Warning: Deprecated config option", from, "- please rename to", to)
|
|
|
|
}
|
|
|
|
// If the configuration file doesn't already contain a line with the
|
|
|
|
// new name then set it to the old value. This makes sure that we
|
|
|
|
// don't overwrite something that was put there intentionally.
|
|
|
|
if _, ok := dat[to]; !ok {
|
|
|
|
dat[to] = dat[from]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check to see if the peers are in a parsable format, if not then default
|
|
|
|
// them to the TCP scheme
|
|
|
|
if peers, ok := dat["Peers"].([]interface{}); ok {
|
|
|
|
for index, peer := range peers {
|
|
|
|
uri := peer.(string)
|
|
|
|
if strings.HasPrefix(uri, "tcp://") || strings.HasPrefix(uri, "socks://") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(uri, "tcp:") {
|
|
|
|
uri = uri[4:]
|
|
|
|
}
|
|
|
|
(dat["Peers"].([]interface{}))[index] = "tcp://" + uri
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Now do the same with the interface peers
|
|
|
|
if interfacepeers, ok := dat["InterfacePeers"].(map[string]interface{}); ok {
|
|
|
|
for intf, peers := range interfacepeers {
|
|
|
|
for index, peer := range peers.([]interface{}) {
|
|
|
|
uri := peer.(string)
|
|
|
|
if strings.HasPrefix(uri, "tcp://") || strings.HasPrefix(uri, "socks://") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(uri, "tcp:") {
|
|
|
|
uri = uri[4:]
|
|
|
|
}
|
|
|
|
((dat["InterfacePeers"].(map[string]interface{}))[intf]).([]interface{})[index] = "tcp://" + uri
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 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 22:13:37 +01: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 {
|
2019-01-01 23:25:20 +00:00
|
|
|
cfg := config.GenerateConfig(false)
|
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-28 22:16:20 -06:00
|
|
|
}
|
|
|
|
|
2018-05-27 22:13:37 +01:00
|
|
|
// The main function is responsible for configuring and starting Yggdrasil.
|
2017-12-28 22:16:20 -06:00
|
|
|
func main() {
|
2018-05-27 22:13:37 +01:00
|
|
|
// Configure the command line parameters.
|
|
|
|
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 22:13:37 +01: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 22:13:37 +01:00
|
|
|
autoconf := flag.Bool("autoconf", false, "automatic mode (dynamic IP, peer with IPv6 neighbors)")
|
2018-12-07 22:20:11 +00:00
|
|
|
version := flag.Bool("version", false, "prints the version of this build")
|
2018-01-04 22:37:51 +00:00
|
|
|
flag.Parse()
|
2018-05-27 22:13:37 +01:00
|
|
|
|
2018-01-04 22:37:51 +00:00
|
|
|
var cfg *nodeConfig
|
2018-12-30 12:26:55 +00:00
|
|
|
var err error
|
2018-01-04 22:37:51 +00:00
|
|
|
switch {
|
2018-12-07 22:20:11 +00:00
|
|
|
case *version:
|
|
|
|
fmt.Println("Build name:", yggdrasil.GetBuildName())
|
|
|
|
fmt.Println("Build version:", yggdrasil.GetBuildVersion())
|
|
|
|
os.Exit(0)
|
2018-01-04 22:37:51 +00:00
|
|
|
case *autoconf:
|
2018-05-27 22:13:37 +01:00
|
|
|
// Use an autoconf-generated config, this will give us random keys and
|
|
|
|
// port numbers, and will use an automatically selected TUN/TAP interface.
|
2019-01-01 23:25:20 +00:00
|
|
|
cfg = config.GenerateConfig(true)
|
2018-05-27 12:31:35 -05:00
|
|
|
case *useconffile != "" || *useconf:
|
2018-12-30 12:26:55 +00:00
|
|
|
// Read the configuration from either stdin or from the filesystem
|
|
|
|
cfg = readConfig(useconf, useconffile, normaliseconf)
|
2018-05-27 22:13:37 +01: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.
|
2018-05-23 12:04:27 +01:00
|
|
|
if *normaliseconf {
|
2018-12-02 23:49:48 +00:00
|
|
|
var bs []byte
|
|
|
|
if *confjson {
|
|
|
|
bs, err = json.MarshalIndent(cfg, "", " ")
|
|
|
|
} else {
|
|
|
|
bs, err = hjson.Marshal(cfg)
|
|
|
|
}
|
2018-05-23 12:04:27 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fmt.Println(string(bs))
|
|
|
|
return
|
|
|
|
}
|
2018-01-04 22:37:51 +00:00
|
|
|
case *genconf:
|
2018-05-27 22:13:37 +01:00
|
|
|
// Generate a new configuration and print it to stdout.
|
2018-12-02 23:49:48 +00:00
|
|
|
fmt.Println(doGenconf(*confjson))
|
2018-01-04 22:37:51 +00:00
|
|
|
default:
|
2018-05-27 22:13:37 +01: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 22:13:37 +01: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
|
|
|
|
}
|
2018-05-27 22:13:37 +01:00
|
|
|
// Create a new logger that logs output to stdout.
|
2018-01-04 22:37:51 +00:00
|
|
|
logger := log.New(os.Stdout, "", log.Flags())
|
2018-05-27 22:13:37 +01:00
|
|
|
// Setup the Yggdrasil node itself. The node{} type includes a Core, so we
|
|
|
|
// don't need to create this manually.
|
2018-01-04 22:37:51 +00:00
|
|
|
n := node{}
|
2018-05-27 22:13:37 +01:00
|
|
|
// Check to see if any multicast interface expressions were provided in the
|
|
|
|
// config. If they were then set them now.
|
|
|
|
for _, ll := range cfg.MulticastInterfaces {
|
|
|
|
ifceExpr, err := regexp.Compile(ll)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
n.core.AddMulticastInterfaceExpr(ifceExpr)
|
|
|
|
}
|
2018-05-29 22:15:54 +01:00
|
|
|
// Now that we have a working configuration, we can now actually start
|
|
|
|
// Yggdrasil. This will start the router, switch, DHT node, TCP and UDP
|
2018-05-27 22:13:37 +01:00
|
|
|
// sockets, TUN/TAP adapter and multicast discovery port.
|
|
|
|
if err := n.core.Start(cfg, logger); err != nil {
|
|
|
|
logger.Println("An error occurred during startup")
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-05-29 22:15:54 +01:00
|
|
|
// Check to see if any allowed encryption keys were provided in the config.
|
|
|
|
// If they were then set them now.
|
|
|
|
for _, pBoxStr := range cfg.AllowedEncryptionPublicKeys {
|
|
|
|
n.core.AddAllowedEncryptionPublicKey(pBoxStr)
|
|
|
|
}
|
2018-05-27 22:13:37 +01:00
|
|
|
// The Stop function ensures that the TUN/TAP adapter is correctly shut down
|
|
|
|
// before the program exits.
|
2018-01-04 22:37:51 +00:00
|
|
|
defer func() {
|
2018-05-27 22:13:37 +01:00
|
|
|
n.core.Stop()
|
2018-01-04 22:37:51 +00:00
|
|
|
}()
|
2018-05-27 22:13:37 +01:00
|
|
|
// Make some nice output that tells us what our IPv6 address and subnet are.
|
|
|
|
// This is just logged to stdout for the user.
|
|
|
|
address := n.core.GetAddress()
|
|
|
|
subnet := n.core.GetSubnet()
|
|
|
|
logger.Printf("Your IPv6 address is %s", address.String())
|
|
|
|
logger.Printf("Your IPv6 subnet is %s", subnet.String())
|
|
|
|
// Catch interrupts from the operating system to exit gracefully.
|
2018-01-04 22:37:51 +00:00
|
|
|
c := make(chan os.Signal, 1)
|
2018-12-29 18:51:51 +00:00
|
|
|
r := make(chan os.Signal, 1)
|
2018-02-18 21:16:47 +00:00
|
|
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
2018-12-29 18:51:51 +00:00
|
|
|
signal.Notify(r, os.Interrupt, syscall.SIGHUP)
|
2018-05-27 22:13:37 +01:00
|
|
|
// Create a function to capture the service being stopped on Windows.
|
2018-02-18 21:16:47 +00:00
|
|
|
winTerminate := func() {
|
2018-02-18 21:32:55 +00:00
|
|
|
c <- os.Interrupt
|
|
|
|
}
|
2018-02-18 21:16:47 +00:00
|
|
|
minwinsvc.SetOnExit(winTerminate)
|
2018-05-27 22:13:37 +01:00
|
|
|
// Wait for the terminate/interrupt signal. Once a signal is received, the
|
|
|
|
// deferred Stop function above will run which will shut down TUN/TAP.
|
2018-12-29 18:51:51 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case _ = <-r:
|
2018-12-30 12:26:55 +00:00
|
|
|
if *useconffile != "" {
|
|
|
|
cfg = readConfig(useconf, useconffile, normaliseconf)
|
|
|
|
n.core.UpdateConfig(cfg)
|
|
|
|
} else {
|
|
|
|
logger.Println("Reloading config at runtime is only possible with -useconffile")
|
|
|
|
}
|
2018-12-29 18:51:51 +00:00
|
|
|
case _ = <-c:
|
|
|
|
goto exit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exit:
|
2017-12-28 22:16:20 -06:00
|
|
|
}
|