2017-12-29 04:16:20 +00:00
|
|
|
package main
|
|
|
|
|
2018-05-27 01:40:19 +00:00
|
|
|
import "encoding/json"
|
2017-12-29 04:16:20 +00:00
|
|
|
import "encoding/hex"
|
|
|
|
import "flag"
|
|
|
|
import "fmt"
|
|
|
|
import "io/ioutil"
|
|
|
|
import "os"
|
|
|
|
import "os/signal"
|
2018-02-18 21:16:47 +00:00
|
|
|
import "syscall"
|
2017-12-29 04:16:20 +00:00
|
|
|
import "time"
|
2018-01-09 08:08:54 +00:00
|
|
|
import "regexp"
|
2018-03-07 19:41:56 +00:00
|
|
|
import "math/rand"
|
2017-12-29 04:16:20 +00:00
|
|
|
import "log"
|
|
|
|
|
2018-04-19 14:30:40 +00:00
|
|
|
import "yggdrasil"
|
|
|
|
import "yggdrasil/config"
|
2017-12-29 04:16:20 +00:00
|
|
|
|
2018-02-18 21:16:47 +00:00
|
|
|
import "github.com/kardianos/minwinsvc"
|
2018-05-09 12:54:31 +00:00
|
|
|
import "github.com/neilalexander/hjson-go"
|
|
|
|
import "github.com/mitchellh/mapstructure"
|
2018-02-16 14:12:44 +00:00
|
|
|
|
2018-04-19 14:30:40 +00:00
|
|
|
type nodeConfig = config.NodeConfig
|
|
|
|
type Core = yggdrasil.Core
|
2017-12-29 04:16:20 +00:00
|
|
|
|
|
|
|
type node struct {
|
2018-01-04 22:37:51 +00:00
|
|
|
core Core
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 21:13:37 +00:00
|
|
|
// Generates default configuration. This is used when outputting the -genconf
|
|
|
|
// parameter and also when using -autoconf. The isAutoconf flag is used to
|
|
|
|
// determine whether the operating system should select a free port by itself
|
|
|
|
// (which guarantees that there will not be a conflict with any other services)
|
|
|
|
// or whether to generate a random port number. The only side effect of setting
|
|
|
|
// isAutoconf is that the TCP and UDP ports will likely end up with different
|
|
|
|
// port numbers.
|
2018-03-07 19:41:56 +00:00
|
|
|
func generateConfig(isAutoconf bool) *nodeConfig {
|
2018-05-27 21:13:37 +00:00
|
|
|
// Create a new core.
|
2018-01-04 22:37:51 +00:00
|
|
|
core := Core{}
|
2018-05-27 21:13:37 +00:00
|
|
|
// Generate encryption keys.
|
|
|
|
bpub, bpriv := core.NewEncryptionKeys()
|
|
|
|
spub, spriv := core.NewSigningKeys()
|
|
|
|
// Create a node configuration and populate it.
|
2018-01-04 22:37:51 +00:00
|
|
|
cfg := nodeConfig{}
|
2018-03-07 19:41:56 +00:00
|
|
|
if isAutoconf {
|
|
|
|
cfg.Listen = "[::]:0"
|
|
|
|
} else {
|
|
|
|
r1 := rand.New(rand.NewSource(time.Now().UnixNano()))
|
2018-03-10 19:58:48 +00:00
|
|
|
cfg.Listen = fmt.Sprintf("[::]:%d", r1.Intn(65534-32768)+32768)
|
2018-03-07 19:41:56 +00:00
|
|
|
}
|
2018-02-11 21:45:44 +00:00
|
|
|
cfg.AdminListen = "[::1]:9001"
|
2018-05-23 10:28:20 +00:00
|
|
|
cfg.EncryptionPublicKey = hex.EncodeToString(bpub[:])
|
|
|
|
cfg.EncryptionPrivateKey = hex.EncodeToString(bpriv[:])
|
|
|
|
cfg.SigningPublicKey = hex.EncodeToString(spub[:])
|
|
|
|
cfg.SigningPrivateKey = hex.EncodeToString(spriv[:])
|
2018-01-04 22:37:51 +00:00
|
|
|
cfg.Peers = []string{}
|
2018-05-23 10:28:20 +00:00
|
|
|
cfg.AllowedEncryptionPublicKeys = []string{}
|
2018-05-27 01:40:19 +00:00
|
|
|
cfg.MulticastInterfaces = []string{".*"}
|
2018-05-27 21:36:36 +00:00
|
|
|
cfg.IfName = core.GetTUNDefaultIfName()
|
|
|
|
cfg.IfMTU = core.GetTUNDefaultIfMTU()
|
|
|
|
cfg.IfTAPMode = core.GetTUNDefaultIfTAPMode()
|
2018-04-19 14:30:40 +00:00
|
|
|
|
2018-01-04 22:37:51 +00:00
|
|
|
return &cfg
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 21:13:37 +00:00
|
|
|
// Generates a new configuration and returns it in HJSON format. This is used
|
|
|
|
// with -genconf.
|
2017-12-29 04:16:20 +00:00
|
|
|
func doGenconf() string {
|
2018-03-07 19:41:56 +00:00
|
|
|
cfg := generateConfig(false)
|
2018-05-09 12:54:31 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-05-27 21:13:37 +00:00
|
|
|
// The main function is responsible for configuring and starting Yggdrasil.
|
2017-12-29 04:16:20 +00:00
|
|
|
func main() {
|
2018-05-27 21:13:37 +00:00
|
|
|
// Configure the command line parameters.
|
|
|
|
pprof := flag.Bool("pprof", false, "Run pprof, see http://localhost:6060/debug/pprof/")
|
|
|
|
genconf := flag.Bool("genconf", false, "print a new config to stdout")
|
|
|
|
useconf := flag.Bool("useconf", false, "read config from stdin")
|
|
|
|
useconffile := flag.String("useconffile", "", "read config from specified file path")
|
|
|
|
normaliseconf := flag.Bool("normaliseconf", false, "use in combination with either -useconf or -useconffile, outputs your configuration normalised")
|
|
|
|
autoconf := flag.Bool("autoconf", false, "automatic mode (dynamic IP, peer with IPv6 neighbors)")
|
2018-01-04 22:37:51 +00:00
|
|
|
flag.Parse()
|
2018-05-27 21:13:37 +00:00
|
|
|
|
2018-01-04 22:37:51 +00:00
|
|
|
var cfg *nodeConfig
|
|
|
|
switch {
|
|
|
|
case *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.
|
2018-03-07 19:41:56 +00:00
|
|
|
cfg = generateConfig(true)
|
2018-05-27 17:31:35 +00:00
|
|
|
case *useconffile != "" || *useconf:
|
2018-05-27 21:13:37 +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.
|
2018-02-16 14:12:44 +00:00
|
|
|
var config []byte
|
|
|
|
var err error
|
2018-05-27 17:31:35 +00:00
|
|
|
if *useconffile != "" {
|
2018-05-27 21:13:37 +00:00
|
|
|
// Read the file from the filesystem
|
2018-05-27 17:31:35 +00:00
|
|
|
config, err = ioutil.ReadFile(*useconffile)
|
|
|
|
} else {
|
2018-05-27 21:13:37 +00:00
|
|
|
// Read the file from stdin.
|
2018-05-27 17:31:35 +00:00
|
|
|
config, err = ioutil.ReadAll(os.Stdin)
|
|
|
|
}
|
2018-01-04 22:37:51 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-05-27 21:13:37 +00:00
|
|
|
// 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.
|
2018-03-07 19:41:56 +00:00
|
|
|
cfg = generateConfig(false)
|
2018-05-09 12:54:31 +00:00
|
|
|
var dat map[string]interface{}
|
|
|
|
if err := hjson.Unmarshal(config, &dat); err != nil {
|
2018-05-09 13:03:28 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2018-05-27 01:40:19 +00:00
|
|
|
confJson, err := json.Marshal(dat)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
json.Unmarshal(confJson, &cfg)
|
2018-05-23 11:04:27 +00:00
|
|
|
// For now we will do a little bit to help the user adjust their
|
2018-05-27 21:13:37 +00:00
|
|
|
// configuration to match the new configuration format, as some of the key
|
|
|
|
// names have changed recently.
|
2018-05-23 11:04:27 +00:00
|
|
|
changes := map[string]string{
|
2018-05-26 20:50:47 +00:00
|
|
|
"Multicast": "",
|
|
|
|
"LinkLocal": "MulticastInterfaces",
|
|
|
|
"BoxPub": "EncryptionPublicKey",
|
|
|
|
"BoxPriv": "EncryptionPrivateKey",
|
|
|
|
"SigPub": "SigningPublicKey",
|
|
|
|
"SigPriv": "SigningPrivateKey",
|
2018-05-23 11:32:26 +00:00
|
|
|
"AllowedBoxPubs": "AllowedEncryptionPublicKeys",
|
2018-05-23 11:04:27 +00:00
|
|
|
}
|
2018-05-27 21:13:37 +00:00
|
|
|
// Loop over the mappings aove and see if we have anything to fix.
|
2018-05-23 11:04:27 +00:00
|
|
|
for from, to := range changes {
|
2018-05-23 17:53:44 +00:00
|
|
|
if _, ok := dat[from]; ok {
|
|
|
|
if to == "" {
|
2018-05-23 11:04:27 +00:00
|
|
|
if !*normaliseconf {
|
2018-05-23 17:53:44 +00:00
|
|
|
log.Println("Warning: Deprecated config option", from, "- please remove")
|
2018-05-23 11:04:27 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !*normaliseconf {
|
2018-05-23 17:53:44 +00:00
|
|
|
log.Println("Warning: Deprecated config option", from, "- please rename to", to)
|
2018-05-23 11:04:27 +00:00
|
|
|
}
|
2018-05-27 21:13:37 +00:00
|
|
|
// 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.
|
2018-05-23 11:04:27 +00:00
|
|
|
if _, ok := dat[to]; !ok {
|
|
|
|
dat[to] = dat[from]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-27 21:13:37 +00:00
|
|
|
// Overlay our newly mapped configuration onto the autoconf node config that
|
|
|
|
// we generated above.
|
2018-05-09 12:54:31 +00:00
|
|
|
if err = mapstructure.Decode(dat, &cfg); err != nil {
|
2018-01-04 22:37:51 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
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.
|
2018-05-23 11:04:27 +00:00
|
|
|
if *normaliseconf {
|
|
|
|
bs, err := hjson.Marshal(cfg)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fmt.Println(string(bs))
|
|
|
|
return
|
|
|
|
}
|
2018-01-04 22:37:51 +00:00
|
|
|
case *genconf:
|
2018-05-27 21:13:37 +00:00
|
|
|
// Generate a new configuration and print it to stdout.
|
2018-01-04 22:37:51 +00:00
|
|
|
fmt.Println(doGenconf())
|
|
|
|
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
|
|
|
|
}
|
2018-05-27 21:13:37 +00: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 21:13:37 +00:00
|
|
|
// If the -pprof flag was provided then start the pprof service on port 6060.
|
2018-01-04 22:37:51 +00:00
|
|
|
if *pprof {
|
2018-05-27 22:22:50 +00:00
|
|
|
if err := yggdrasil.StartProfiler(logger); err != nil {
|
|
|
|
logger.Println(err)
|
|
|
|
}
|
2018-01-04 22:37:51 +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.
|
2018-01-04 22:37:51 +00:00
|
|
|
n := node{}
|
2018-05-27 21:13:37 +00: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 21:15:54 +00: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 21:13:37 +00: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 21:15:54 +00: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 21:13:37 +00:00
|
|
|
// If any static peers were provided in the configuration above then we should
|
|
|
|
// configure them. The loop ensures that disconnected peers will eventually
|
|
|
|
// be reconnected with.
|
|
|
|
go func() {
|
|
|
|
if len(cfg.Peers) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
for _, p := range cfg.Peers {
|
|
|
|
n.core.AddPeer(p)
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Minute)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
// 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 21:13:37 +00:00
|
|
|
n.core.Stop()
|
2018-01-04 22:37:51 +00:00
|
|
|
}()
|
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.
|
|
|
|
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-02-18 21:16:47 +00:00
|
|
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
2018-05-27 21:13:37 +00: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 21:13:37 +00: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-01-04 22:37:51 +00:00
|
|
|
<-c
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|