This commit is contained in:
Adam Ierymenko 2019-09-22 22:25:55 -07:00
commit 536bc59abb
No known key found for this signature in database
GPG key ID: C8877CF2D7A5D7F3
5 changed files with 390 additions and 55 deletions

View file

@ -14,7 +14,11 @@
package zerotier
import (
"encoding/json"
"io/ioutil"
rand "math/rand"
"net"
"os"
"runtime"
)
@ -41,33 +45,48 @@ type LocalConfigSettings struct {
// LocalConfig is the local.conf file and stores local settings for the node.
type LocalConfig struct {
Physical map[string]LocalConfigPhysicalPathConfiguration
Virtual map[Address]LocalConfigVirtualAddressConfiguration
Physical map[string]*LocalConfigPhysicalPathConfiguration
Virtual map[Address]*LocalConfigVirtualAddressConfiguration
Network map[NetworkID]*NetworkLocalSettings
Settings LocalConfigSettings
}
// NewLocalConfig creates a new local.conf file with defaults
func NewLocalConfig() *LocalConfig {
lc := &LocalConfig{
Physical: make(map[string]LocalConfigPhysicalPathConfiguration),
Virtual: make(map[Address]LocalConfigVirtualAddressConfiguration),
Settings: LocalConfigSettings{
PrimaryPort: 9993,
SecondaryPort: 0,
TertiaryPort: 0,
PortMappingEnabled: true,
MuiltipathMode: 0,
},
// Read this local config from a file, initializing to defaults if the file does not exist
func (lc *LocalConfig) Read(p string) error {
if lc.Physical == nil {
lc.Physical = make(map[string]*LocalConfigPhysicalPathConfiguration)
lc.Virtual = make(map[Address]*LocalConfigVirtualAddressConfiguration)
lc.Network = make(map[NetworkID]*NetworkLocalSettings)
lc.Settings.PrimaryPort = 9993
lc.Settings.SecondaryPort = 16384 + (rand.Int() % 16384)
lc.Settings.TertiaryPort = 32768 + (rand.Int() % 16384)
lc.Settings.PortMappingEnabled = true
lc.Settings.MuiltipathMode = 0
switch runtime.GOOS {
case "darwin":
lc.Settings.InterfacePrefixBlacklist = []string{"utun", "tun", "tap", "feth", "lo", "zt"}
case "linux":
lc.Settings.InterfacePrefixBlacklist = []string{"tun", "tap", "lo", "zt"}
case "freebsd", "openbsd", "netbsd", "illumos", "solaris", "dragonfly":
lc.Settings.InterfacePrefixBlacklist = []string{"tun", "tap", "zt"}
case "android":
lc.Settings.InterfacePrefixBlacklist = []string{"tun", "tap"}
}
}
switch runtime.GOOS {
case "darwin":
lc.Settings.InterfacePrefixBlacklist = []string{"utun", "tun", "tap", "feth", "lo", "zt"}
case "linux":
lc.Settings.InterfacePrefixBlacklist = []string{"tun", "tap", "lo", "zt"}
case "freebsd", "openbsd", "netbsd", "illumos", "solaris", "dragonfly":
lc.Settings.InterfacePrefixBlacklist = []string{"tun", "tap", "zt"}
case "android":
lc.Settings.InterfacePrefixBlacklist = []string{"tun", "tap"}
data, err := ioutil.ReadFile(p)
if err != nil && err != os.ErrNotExist {
return err
}
return lc
return json.Unmarshal(data, lc)
}
// Write this local config to a file
func (lc *LocalConfig) Write(p string) error {
data, err := json.MarshalIndent(lc, "", "\t")
if err != nil {
return err
}
return ioutil.WriteFile(p, data, 0644)
}