2021-03-26 00:17:52 +08:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2021-05-26 00:48:04 +08:00
|
|
|
//"github.com/davecgh/go-spew/spew"
|
2021-07-26 02:22:20 +08:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2021-05-26 00:48:04 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2021-07-26 02:22:20 +08:00
|
|
|
"os"
|
|
|
|
|
2021-05-26 00:48:04 +08:00
|
|
|
nodepb "github.com/gravitl/netmaker/grpc"
|
2021-05-26 04:09:49 +08:00
|
|
|
"github.com/gravitl/netmaker/models"
|
2021-07-26 02:22:20 +08:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"gopkg.in/yaml.v3"
|
2021-03-26 00:17:52 +08:00
|
|
|
)
|
2021-07-26 02:22:20 +08:00
|
|
|
|
2021-05-26 04:09:49 +08:00
|
|
|
type GlobalConfig struct {
|
2021-07-11 22:18:31 +08:00
|
|
|
GRPCWireGuard string `yaml:"grpcwg"`
|
2021-07-26 02:22:20 +08:00
|
|
|
Client models.IntClient
|
2021-05-26 04:09:49 +08:00
|
|
|
}
|
2021-03-26 00:17:52 +08:00
|
|
|
|
|
|
|
type ClientConfig struct {
|
2021-07-26 02:22:20 +08:00
|
|
|
Server ServerConfig `yaml:"server"`
|
|
|
|
Node NodeConfig `yaml:"node"`
|
|
|
|
Network string `yaml:"network"`
|
|
|
|
Daemon string `yaml:"daemon"`
|
|
|
|
OperatingSystem string `yaml:"operatingsystem"`
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
type ServerConfig struct {
|
2021-07-26 02:22:20 +08:00
|
|
|
CoreDNSAddr string `yaml:"corednsaddr"`
|
|
|
|
GRPCAddress string `yaml:"grpcaddress"`
|
|
|
|
APIAddress string `yaml:"apiaddress"`
|
|
|
|
AccessKey string `yaml:"accesskey"`
|
|
|
|
GRPCSSL string `yaml:"grpcssl"`
|
|
|
|
GRPCWireGuard string `yaml:"grpcwg"`
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
|
2021-05-30 01:22:18 +08:00
|
|
|
type ListConfig struct {
|
2021-07-26 02:22:20 +08:00
|
|
|
Name string `yaml:"name"`
|
|
|
|
Interface string `yaml:"interface"`
|
|
|
|
PrivateIPv4 string `yaml:"wgaddress"`
|
|
|
|
PrivateIPv6 string `yaml:"wgaddress6"`
|
|
|
|
PublicEndpoint string `yaml:"endpoint"`
|
2021-05-30 01:22:18 +08:00
|
|
|
}
|
|
|
|
|
2021-03-26 00:17:52 +08:00
|
|
|
type NodeConfig struct {
|
2021-07-26 02:22:20 +08:00
|
|
|
Name string `yaml:"name"`
|
|
|
|
Interface string `yaml:"interface"`
|
|
|
|
Network string `yaml:"network"`
|
|
|
|
Password string `yaml:"password"`
|
|
|
|
MacAddress string `yaml:"macaddress"`
|
|
|
|
LocalAddress string `yaml:"localaddress"`
|
|
|
|
WGAddress string `yaml:"wgaddress"`
|
|
|
|
WGAddress6 string `yaml:"wgaddress6"`
|
|
|
|
Roaming string `yaml:"roaming"`
|
|
|
|
DNS string `yaml:"dns"`
|
|
|
|
IsLocal string `yaml:"islocal"`
|
|
|
|
IsDualStack string `yaml:"isdualstack"`
|
|
|
|
IsIngressGateway string `yaml:"isingressgateway"`
|
|
|
|
LocalRange string `yaml:"localrange"`
|
|
|
|
PostUp string `yaml:"postup"`
|
|
|
|
PostDown string `yaml:"postdown"`
|
|
|
|
Port int32 `yaml:"port"`
|
|
|
|
KeepAlive int32 `yaml:"keepalive"`
|
|
|
|
PublicKey string `yaml:"publickey"`
|
|
|
|
ServerPubKey string `yaml:"serverpubkey"`
|
|
|
|
PrivateKey string `yaml:"privatekey"`
|
|
|
|
Endpoint string `yaml:"endpoint"`
|
|
|
|
PostChanges string `yaml:"postchanges"`
|
|
|
|
StaticIP string `yaml:"staticip"`
|
|
|
|
StaticPubKey string `yaml:"staticpubkey"`
|
|
|
|
IPForwarding string `yaml:"ipforwarding"`
|
|
|
|
UDPHolePunch string `yaml:"udpholepunch"`
|
2021-07-26 04:05:31 +08:00
|
|
|
SaveConfig string `yaml:"saveconfig"`
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//reading in the env file
|
2021-07-26 02:22:20 +08:00
|
|
|
func Write(config *ClientConfig, network string) error {
|
2021-04-05 23:06:30 +08:00
|
|
|
if network == "" {
|
|
|
|
err := errors.New("No network provided. Exiting.")
|
|
|
|
return err
|
|
|
|
}
|
2021-07-26 02:22:20 +08:00
|
|
|
_, err := os.Stat("/etc/netclient")
|
2021-03-26 10:29:36 +08:00
|
|
|
if os.IsNotExist(err) {
|
2021-07-26 02:22:20 +08:00
|
|
|
os.Mkdir("/etc/netclient", 744)
|
2021-03-26 10:29:36 +08:00
|
|
|
} else if err != nil {
|
2021-07-26 02:22:20 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-03-26 10:29:36 +08:00
|
|
|
home := "/etc/netclient"
|
|
|
|
|
2021-07-26 02:22:20 +08:00
|
|
|
file := fmt.Sprintf(home + "/netconfig-" + network)
|
|
|
|
f, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
|
|
|
|
defer f.Close()
|
2021-03-26 00:17:52 +08:00
|
|
|
|
2021-05-26 00:48:04 +08:00
|
|
|
err = yaml.NewEncoder(f).Encode(config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
2021-07-26 02:22:20 +08:00
|
|
|
return err
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
2021-07-26 02:22:20 +08:00
|
|
|
|
2021-05-26 04:09:49 +08:00
|
|
|
//reading in the env file
|
2021-07-26 02:22:20 +08:00
|
|
|
func WriteGlobal(config *GlobalConfig) error {
|
|
|
|
_, err := os.Stat("/etc/netclient")
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
os.Mkdir("/etc/netclient", 744)
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
home := "/etc/netclient"
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
file := fmt.Sprintf(home + "/netconfig-global-001")
|
|
|
|
f, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
err = yaml.NewEncoder(f).Encode(config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return err
|
2021-05-26 04:09:49 +08:00
|
|
|
}
|
2021-07-26 02:22:20 +08:00
|
|
|
func WriteServer(server string, accesskey string, network string) error {
|
|
|
|
if network == "" {
|
|
|
|
err := errors.New("No network provided. Exiting.")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nofile := false
|
|
|
|
//home, err := homedir.Dir()
|
|
|
|
_, err := os.Stat("/etc/netclient")
|
2021-03-26 10:29:36 +08:00
|
|
|
if os.IsNotExist(err) {
|
2021-07-26 02:22:20 +08:00
|
|
|
os.Mkdir("/etc/netclient", 744)
|
|
|
|
} else if err != nil {
|
2021-03-26 10:29:36 +08:00
|
|
|
fmt.Println("couldnt find or create /etc/netclient")
|
2021-07-26 02:22:20 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
home := "/etc/netclient"
|
2021-03-26 10:29:36 +08:00
|
|
|
|
2021-04-05 23:06:30 +08:00
|
|
|
file := fmt.Sprintf(home + "/netconfig-" + network)
|
2021-07-26 02:22:20 +08:00
|
|
|
//f, err := os.Open(file)
|
|
|
|
f, err := os.OpenFile(file, os.O_CREATE|os.O_RDWR, 0666)
|
2021-03-26 00:17:52 +08:00
|
|
|
//f, err := ioutil.ReadFile(file)
|
2021-07-26 02:22:20 +08:00
|
|
|
if err != nil {
|
2021-04-05 23:06:30 +08:00
|
|
|
fmt.Println("couldnt open netconfig-" + network)
|
2021-03-26 00:17:52 +08:00
|
|
|
fmt.Println(err)
|
2021-07-26 02:22:20 +08:00
|
|
|
nofile = true
|
2021-03-26 00:17:52 +08:00
|
|
|
//err = nil
|
|
|
|
return err
|
2021-07-26 02:22:20 +08:00
|
|
|
}
|
|
|
|
defer f.Close()
|
2021-03-26 00:17:52 +08:00
|
|
|
|
|
|
|
//cfg := &ClientConfig{}
|
|
|
|
var cfg ClientConfig
|
|
|
|
|
2021-07-26 02:22:20 +08:00
|
|
|
if !nofile {
|
2021-04-05 23:06:30 +08:00
|
|
|
fmt.Println("Writing to existing config file at " + home + "/netconfig-" + network)
|
2021-07-26 02:22:20 +08:00
|
|
|
decoder := yaml.NewDecoder(f)
|
|
|
|
err = decoder.Decode(&cfg)
|
2021-03-26 00:17:52 +08:00
|
|
|
//err = yaml.Unmarshal(f, &cfg)
|
2021-07-26 02:22:20 +08:00
|
|
|
if err != nil {
|
2021-03-26 00:17:52 +08:00
|
|
|
//fmt.Println(err)
|
2021-07-26 02:22:20 +08:00
|
|
|
//return err
|
|
|
|
}
|
2021-03-26 00:17:52 +08:00
|
|
|
f.Close()
|
|
|
|
f, err = os.OpenFile(file, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0666)
|
2021-07-26 02:22:20 +08:00
|
|
|
if err != nil {
|
2021-03-26 10:29:36 +08:00
|
|
|
fmt.Println("couldnt open netconfig")
|
2021-03-26 00:17:52 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
nofile = true
|
|
|
|
//err = nil
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2021-07-26 02:22:20 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("trouble opening file")
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2021-03-26 00:17:52 +08:00
|
|
|
|
2021-05-26 04:09:49 +08:00
|
|
|
cfg.Server.GRPCAddress = server
|
2021-03-26 00:17:52 +08:00
|
|
|
cfg.Server.AccessKey = accesskey
|
|
|
|
|
|
|
|
err = yaml.NewEncoder(f).Encode(cfg)
|
|
|
|
//_, err = yaml.Marshal(f, &cfg)
|
|
|
|
if err != nil {
|
2021-07-26 02:22:20 +08:00
|
|
|
fmt.Println("trouble encoding file")
|
|
|
|
return err
|
|
|
|
}
|
2021-03-26 00:17:52 +08:00
|
|
|
} else {
|
2021-07-26 02:22:20 +08:00
|
|
|
fmt.Println("Creating new config file at " + home + "/netconfig-" + network)
|
2021-03-26 00:17:52 +08:00
|
|
|
|
2021-07-26 02:22:20 +08:00
|
|
|
cfg.Server.GRPCAddress = server
|
|
|
|
cfg.Server.AccessKey = accesskey
|
2021-03-26 00:17:52 +08:00
|
|
|
|
2021-07-26 02:22:20 +08:00
|
|
|
newf, err := os.Create(home + "/netconfig-" + network)
|
|
|
|
err = yaml.NewEncoder(newf).Encode(cfg)
|
|
|
|
defer newf.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-03-26 00:17:52 +08:00
|
|
|
|
2021-07-26 02:22:20 +08:00
|
|
|
return err
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
|
2021-07-26 02:22:20 +08:00
|
|
|
func (config *ClientConfig) ReadConfig() {
|
2021-03-26 00:17:52 +08:00
|
|
|
|
|
|
|
nofile := false
|
2021-03-26 10:29:36 +08:00
|
|
|
//home, err := homedir.Dir()
|
|
|
|
home := "/etc/netclient"
|
2021-04-05 23:06:30 +08:00
|
|
|
file := fmt.Sprintf(home + "/netconfig-" + config.Network)
|
2021-03-26 00:17:52 +08:00
|
|
|
//f, err := os.Open(file)
|
2021-07-26 02:22:20 +08:00
|
|
|
f, err := os.OpenFile(file, os.O_RDONLY, 0666)
|
2021-03-26 00:17:52 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("trouble opening file")
|
|
|
|
fmt.Println(err)
|
|
|
|
nofile = true
|
2021-03-26 10:29:36 +08:00
|
|
|
//fmt.Println("Could not access " + home + "/.netconfig, proceeding...")
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
//var cfg ClientConfig
|
|
|
|
|
|
|
|
if !nofile {
|
|
|
|
decoder := yaml.NewDecoder(f)
|
|
|
|
err = decoder.Decode(&config)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("no config or invalid")
|
|
|
|
fmt.Println(err)
|
|
|
|
log.Fatal(err)
|
|
|
|
} else {
|
|
|
|
//config = cfg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-26 02:22:20 +08:00
|
|
|
func ModGlobalConfig(cfg models.IntClient) error {
|
|
|
|
var modconfig GlobalConfig
|
|
|
|
var err error
|
|
|
|
if FileExists("/etc/netclient/netconfig-global-001") {
|
|
|
|
useconfig, err := ReadGlobalConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
modconfig = *useconfig
|
|
|
|
}
|
|
|
|
if cfg.ServerWGPort != "" {
|
|
|
|
modconfig.Client.ServerWGPort = cfg.ServerWGPort
|
|
|
|
}
|
|
|
|
if cfg.ServerGRPCPort != "" {
|
|
|
|
modconfig.Client.ServerGRPCPort = cfg.ServerGRPCPort
|
|
|
|
}
|
|
|
|
if cfg.ServerAPIPort != "" {
|
|
|
|
modconfig.Client.ServerAPIPort = cfg.ServerAPIPort
|
|
|
|
}
|
|
|
|
if cfg.PublicKey != "" {
|
|
|
|
modconfig.Client.PublicKey = cfg.PublicKey
|
|
|
|
}
|
|
|
|
if cfg.PrivateKey != "" {
|
|
|
|
modconfig.Client.PrivateKey = cfg.PrivateKey
|
|
|
|
}
|
|
|
|
if cfg.ServerPublicEndpoint != "" {
|
|
|
|
modconfig.Client.ServerPublicEndpoint = cfg.ServerPublicEndpoint
|
|
|
|
}
|
|
|
|
if cfg.ServerPrivateAddress != "" {
|
|
|
|
modconfig.Client.ServerPrivateAddress = cfg.ServerPrivateAddress
|
|
|
|
}
|
|
|
|
if cfg.Address != "" {
|
|
|
|
modconfig.Client.Address = cfg.Address
|
|
|
|
}
|
|
|
|
if cfg.Address6 != "" {
|
|
|
|
modconfig.Client.Address6 = cfg.Address6
|
|
|
|
}
|
|
|
|
if cfg.Network != "" {
|
|
|
|
modconfig.Client.Network = cfg.Network
|
|
|
|
}
|
|
|
|
if cfg.ServerKey != "" {
|
|
|
|
modconfig.Client.ServerKey = cfg.ServerKey
|
|
|
|
}
|
|
|
|
if cfg.AccessKey != "" {
|
|
|
|
modconfig.Client.AccessKey = cfg.AccessKey
|
|
|
|
}
|
|
|
|
if cfg.ClientID != "" {
|
|
|
|
modconfig.Client.ClientID = cfg.ClientID
|
|
|
|
}
|
|
|
|
err = WriteGlobal(&modconfig)
|
|
|
|
return err
|
2021-05-26 04:09:49 +08:00
|
|
|
}
|
|
|
|
|
2021-07-26 02:22:20 +08:00
|
|
|
func ModConfig(node *nodepb.Node) error {
|
|
|
|
network := node.Nodenetwork
|
|
|
|
if network == "" {
|
|
|
|
return errors.New("No Network Provided")
|
|
|
|
}
|
2021-05-26 00:48:04 +08:00
|
|
|
var modconfig ClientConfig
|
|
|
|
var err error
|
2021-07-26 02:22:20 +08:00
|
|
|
if FileExists("/etc/netclient/netconfig-" + network) {
|
2021-05-26 00:48:04 +08:00
|
|
|
useconfig, err := ReadConfig(network)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
modconfig = *useconfig
|
|
|
|
}
|
2021-07-26 02:22:20 +08:00
|
|
|
nodecfg := modconfig.Node
|
|
|
|
if node.Name != "" {
|
|
|
|
nodecfg.Name = node.Name
|
|
|
|
}
|
|
|
|
if node.Interface != "" {
|
|
|
|
nodecfg.Interface = node.Interface
|
|
|
|
}
|
|
|
|
if node.Nodenetwork != "" {
|
|
|
|
nodecfg.Network = node.Nodenetwork
|
|
|
|
}
|
|
|
|
if node.Macaddress != "" {
|
|
|
|
nodecfg.MacAddress = node.Macaddress
|
|
|
|
}
|
|
|
|
if node.Localaddress != "" {
|
|
|
|
nodecfg.LocalAddress = node.Localaddress
|
|
|
|
}
|
|
|
|
if node.Postup != "" {
|
|
|
|
nodecfg.PostUp = node.Postup
|
|
|
|
}
|
|
|
|
if node.Postdown != "" {
|
|
|
|
nodecfg.PostDown = node.Postdown
|
|
|
|
}
|
|
|
|
if node.Listenport != 0 {
|
|
|
|
nodecfg.Port = node.Listenport
|
|
|
|
}
|
|
|
|
if node.Keepalive != 0 {
|
|
|
|
nodecfg.KeepAlive = node.Keepalive
|
|
|
|
}
|
|
|
|
if node.Publickey != "" {
|
|
|
|
nodecfg.PublicKey = node.Publickey
|
|
|
|
}
|
|
|
|
if node.Endpoint != "" {
|
|
|
|
nodecfg.Endpoint = node.Endpoint
|
|
|
|
}
|
|
|
|
if node.Password != "" {
|
|
|
|
nodecfg.Password = node.Password
|
|
|
|
}
|
|
|
|
if node.Address != "" {
|
|
|
|
nodecfg.WGAddress = node.Address
|
|
|
|
}
|
|
|
|
if node.Address6 != "" {
|
|
|
|
nodecfg.WGAddress6 = node.Address6
|
|
|
|
}
|
|
|
|
if node.Postchanges != "" {
|
|
|
|
nodecfg.PostChanges = node.Postchanges
|
|
|
|
}
|
|
|
|
if node.Dnsoff == true {
|
2021-05-26 00:48:04 +08:00
|
|
|
nodecfg.DNS = "off"
|
2021-07-26 02:22:20 +08:00
|
|
|
}
|
|
|
|
if node.Isdualstack == true {
|
|
|
|
nodecfg.IsDualStack = "yes"
|
|
|
|
}
|
|
|
|
if node.Udpholepunch != "" {
|
|
|
|
nodecfg.UDPHolePunch = node.Udpholepunch
|
|
|
|
}
|
2021-07-26 04:05:31 +08:00
|
|
|
if node.Saveconfig != "" {
|
|
|
|
nodecfg.SaveConfig = node.Saveconfig
|
|
|
|
}
|
2021-05-26 00:48:04 +08:00
|
|
|
if node.Isingressgateway {
|
|
|
|
nodecfg.IsIngressGateway = "yes"
|
|
|
|
} else {
|
2021-07-26 02:22:20 +08:00
|
|
|
nodecfg.IsIngressGateway = "no"
|
|
|
|
}
|
|
|
|
if node.Localrange != "" && node.Islocal {
|
|
|
|
nodecfg.IsLocal = "yes"
|
|
|
|
nodecfg.LocalRange = node.Localrange
|
|
|
|
}
|
|
|
|
modconfig.Node = nodecfg
|
|
|
|
err = Write(&modconfig, network)
|
|
|
|
return err
|
2021-05-26 00:48:04 +08:00
|
|
|
}
|
|
|
|
|
2021-07-26 02:22:20 +08:00
|
|
|
func GetCLIConfig(c *cli.Context) (ClientConfig, error) {
|
2021-05-26 00:48:04 +08:00
|
|
|
var cfg ClientConfig
|
|
|
|
if c.String("token") != "" {
|
2021-07-26 02:22:20 +08:00
|
|
|
tokenbytes, err := base64.StdEncoding.DecodeString(c.String("token"))
|
|
|
|
if err != nil {
|
2021-05-26 00:48:04 +08:00
|
|
|
log.Println("error decoding token")
|
|
|
|
return cfg, err
|
2021-07-26 02:22:20 +08:00
|
|
|
}
|
2021-07-11 12:49:31 +08:00
|
|
|
var accesstoken models.AccessToken
|
|
|
|
if err := json.Unmarshal(tokenbytes, &accesstoken); err != nil {
|
2021-07-26 02:22:20 +08:00
|
|
|
log.Println("error converting token json to object", tokenbytes)
|
2021-07-11 12:49:31 +08:00
|
|
|
return cfg, err
|
2021-06-02 23:13:47 +08:00
|
|
|
}
|
2021-07-11 12:49:31 +08:00
|
|
|
|
|
|
|
if accesstoken.ServerConfig.APIConnString != "" {
|
|
|
|
cfg.Server.APIAddress = accesstoken.ServerConfig.APIConnString
|
|
|
|
} else {
|
|
|
|
cfg.Server.APIAddress = accesstoken.ServerConfig.APIHost
|
|
|
|
if accesstoken.ServerConfig.APIPort != "" {
|
|
|
|
cfg.Server.APIAddress = cfg.Server.APIAddress + ":" + accesstoken.ServerConfig.APIPort
|
|
|
|
}
|
|
|
|
}
|
2021-07-26 02:22:20 +08:00
|
|
|
if accesstoken.ServerConfig.GRPCConnString != "" {
|
|
|
|
cfg.Server.GRPCAddress = accesstoken.ServerConfig.GRPCConnString
|
|
|
|
} else {
|
|
|
|
cfg.Server.GRPCAddress = accesstoken.ServerConfig.GRPCHost
|
|
|
|
if accesstoken.ServerConfig.GRPCPort != "" {
|
|
|
|
cfg.Server.GRPCAddress = cfg.Server.GRPCAddress + ":" + accesstoken.ServerConfig.GRPCPort
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cfg.Network = accesstoken.ClientConfig.Network
|
|
|
|
cfg.Node.Network = accesstoken.ClientConfig.Network
|
|
|
|
cfg.Server.AccessKey = accesstoken.ClientConfig.Key
|
2021-07-11 12:49:31 +08:00
|
|
|
cfg.Node.LocalRange = accesstoken.ClientConfig.LocalRange
|
|
|
|
cfg.Server.GRPCSSL = accesstoken.ServerConfig.GRPCSSL
|
|
|
|
cfg.Server.GRPCWireGuard = accesstoken.WG.GRPCWireGuard
|
2021-07-16 03:14:48 +08:00
|
|
|
cfg.Server.CoreDNSAddr = accesstoken.ServerConfig.CoreDNSAddr
|
2021-05-26 04:09:49 +08:00
|
|
|
if c.String("grpcserver") != "" {
|
|
|
|
cfg.Server.GRPCAddress = c.String("grpcserver")
|
2021-05-26 00:48:04 +08:00
|
|
|
}
|
2021-07-26 02:22:20 +08:00
|
|
|
if c.String("apiserver") != "" {
|
|
|
|
cfg.Server.APIAddress = c.String("apiserver")
|
|
|
|
}
|
2021-05-26 00:48:04 +08:00
|
|
|
if c.String("key") != "" {
|
|
|
|
cfg.Server.AccessKey = c.String("key")
|
|
|
|
}
|
|
|
|
if c.String("network") != "all" {
|
|
|
|
cfg.Network = c.String("network")
|
|
|
|
cfg.Node.Network = c.String("network")
|
|
|
|
}
|
|
|
|
if c.String("localrange") != "" {
|
|
|
|
cfg.Node.LocalRange = c.String("localrange")
|
|
|
|
}
|
2021-07-26 02:22:20 +08:00
|
|
|
if c.String("grpcssl") != "" {
|
|
|
|
cfg.Server.GRPCSSL = c.String("grpcssl")
|
|
|
|
}
|
|
|
|
if c.String("corednsaddr") != "" {
|
|
|
|
cfg.Server.CoreDNSAddr = c.String("corednsaddr")
|
|
|
|
}
|
|
|
|
if c.String("grpcwg") != "" {
|
|
|
|
cfg.Server.GRPCWireGuard = c.String("grpcwg")
|
|
|
|
}
|
2021-07-11 12:49:31 +08:00
|
|
|
|
2021-05-26 00:48:04 +08:00
|
|
|
} else {
|
2021-05-26 04:09:49 +08:00
|
|
|
cfg.Server.GRPCAddress = c.String("grpcserver")
|
|
|
|
cfg.Server.APIAddress = c.String("apiserver")
|
2021-05-26 00:48:04 +08:00
|
|
|
cfg.Server.AccessKey = c.String("key")
|
2021-07-26 02:22:20 +08:00
|
|
|
cfg.Network = c.String("network")
|
|
|
|
cfg.Node.Network = c.String("network")
|
|
|
|
cfg.Node.LocalRange = c.String("localrange")
|
|
|
|
cfg.Server.GRPCWireGuard = c.String("grpcwg")
|
|
|
|
cfg.Server.GRPCSSL = c.String("grpcssl")
|
|
|
|
cfg.Server.CoreDNSAddr = c.String("corednsaddr")
|
2021-05-26 00:48:04 +08:00
|
|
|
}
|
|
|
|
cfg.Node.Name = c.String("name")
|
|
|
|
cfg.Node.Interface = c.String("interface")
|
|
|
|
cfg.Node.Password = c.String("password")
|
|
|
|
cfg.Node.MacAddress = c.String("macaddress")
|
|
|
|
cfg.Node.LocalAddress = c.String("localaddress")
|
|
|
|
cfg.Node.WGAddress = c.String("address")
|
|
|
|
cfg.Node.WGAddress6 = c.String("addressIPV6")
|
2021-06-02 23:00:10 +08:00
|
|
|
cfg.Node.Roaming = c.String("roaming")
|
|
|
|
cfg.Node.DNS = c.String("dns")
|
|
|
|
cfg.Node.IsLocal = c.String("islocal")
|
|
|
|
cfg.Node.IsDualStack = c.String("isdualstack")
|
|
|
|
cfg.Node.PostUp = c.String("postup")
|
|
|
|
cfg.Node.PostDown = c.String("postdown")
|
|
|
|
cfg.Node.Port = int32(c.Int("port"))
|
|
|
|
cfg.Node.KeepAlive = int32(c.Int("keepalive"))
|
|
|
|
cfg.Node.PublicKey = c.String("publickey")
|
|
|
|
cfg.Node.PrivateKey = c.String("privatekey")
|
|
|
|
cfg.Node.Endpoint = c.String("endpoint")
|
|
|
|
cfg.Node.IPForwarding = c.String("ipforwarding")
|
|
|
|
cfg.OperatingSystem = c.String("operatingsystem")
|
|
|
|
cfg.Daemon = c.String("daemon")
|
2021-07-26 02:22:20 +08:00
|
|
|
cfg.Node.UDPHolePunch = c.String("udpholepunch")
|
2021-05-26 00:48:04 +08:00
|
|
|
|
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
2021-07-26 02:22:20 +08:00
|
|
|
func GetCLIConfigRegister(c *cli.Context) (GlobalConfig, error) {
|
2021-05-30 01:22:18 +08:00
|
|
|
var cfg GlobalConfig
|
2021-06-10 14:31:55 +08:00
|
|
|
if c.String("token") != "" {
|
|
|
|
tokenbytes, err := base64.StdEncoding.DecodeString(c.String("token"))
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error decoding token")
|
|
|
|
return cfg, err
|
|
|
|
}
|
2021-07-26 02:22:20 +08:00
|
|
|
var accesstoken models.AccessToken
|
|
|
|
if err := json.Unmarshal(tokenbytes, &accesstoken); err != nil {
|
|
|
|
log.Println("error converting token json to object", tokenbytes)
|
|
|
|
return cfg, err
|
|
|
|
}
|
2021-07-11 22:18:31 +08:00
|
|
|
cfg.GRPCWireGuard = accesstoken.WG.GRPCWireGuard
|
2021-07-11 12:49:31 +08:00
|
|
|
cfg.Client.ServerPrivateAddress = accesstoken.WG.GRPCWGAddress
|
|
|
|
cfg.Client.ServerGRPCPort = accesstoken.WG.GRPCWGPort
|
2021-06-10 14:31:55 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error decoding token grpcserver")
|
|
|
|
return cfg, err
|
|
|
|
}
|
2021-07-26 02:22:20 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error decoding token apiserver")
|
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
if accesstoken.ServerConfig.APIConnString != "" {
|
|
|
|
cfg.Client.ServerPublicEndpoint = accesstoken.ServerConfig.APIConnString
|
|
|
|
} else {
|
|
|
|
cfg.Client.ServerPublicEndpoint = accesstoken.ServerConfig.APIHost
|
|
|
|
if accesstoken.ServerConfig.APIPort != "" {
|
|
|
|
cfg.Client.ServerAPIPort = accesstoken.ServerConfig.APIPort
|
|
|
|
}
|
|
|
|
}
|
2021-07-11 12:49:31 +08:00
|
|
|
cfg.Client.ServerWGPort = accesstoken.WG.GRPCWGPort
|
|
|
|
cfg.Client.ServerKey = accesstoken.ClientConfig.Key
|
2021-07-26 02:22:20 +08:00
|
|
|
cfg.Client.ServerKey = accesstoken.WG.GRPCWGPubKey
|
2021-05-26 04:09:49 +08:00
|
|
|
|
2021-07-26 02:22:20 +08:00
|
|
|
if c.String("grpcserver") != "" {
|
|
|
|
cfg.Client.ServerPrivateAddress = c.String("grpcserver")
|
|
|
|
}
|
|
|
|
if c.String("apiserver") != "" {
|
|
|
|
cfg.Client.ServerPublicEndpoint = c.String("apiserver")
|
|
|
|
}
|
|
|
|
if c.String("pubkey") != "" {
|
|
|
|
cfg.Client.ServerKey = c.String("pubkey")
|
|
|
|
}
|
|
|
|
if c.String("network") != "all" {
|
|
|
|
cfg.Client.Network = c.String("network")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cfg.Client.ServerPrivateAddress = c.String("grpcserver")
|
|
|
|
cfg.Client.ServerPublicEndpoint = c.String("apiserver")
|
|
|
|
cfg.Client.ServerKey = c.String("key")
|
|
|
|
cfg.Client.Network = c.String("network")
|
|
|
|
}
|
|
|
|
cfg.Client.Address = c.String("address")
|
|
|
|
cfg.Client.Address6 = c.String("addressIPV6")
|
|
|
|
cfg.Client.PublicKey = c.String("pubkey")
|
|
|
|
cfg.Client.PrivateKey = c.String("privkey")
|
|
|
|
|
|
|
|
return cfg, nil
|
|
|
|
}
|
2021-05-26 00:48:04 +08:00
|
|
|
|
2021-04-05 23:06:30 +08:00
|
|
|
func ReadConfig(network string) (*ClientConfig, error) {
|
2021-07-26 02:22:20 +08:00
|
|
|
if network == "" {
|
|
|
|
err := errors.New("No network provided. Exiting.")
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-26 00:17:52 +08:00
|
|
|
nofile := false
|
2021-03-26 10:29:36 +08:00
|
|
|
home := "/etc/netclient"
|
2021-04-05 23:06:30 +08:00
|
|
|
file := fmt.Sprintf(home + "/netconfig-" + network)
|
2021-03-26 00:17:52 +08:00
|
|
|
f, err := os.Open(file)
|
2021-05-26 00:48:04 +08:00
|
|
|
|
2021-03-26 00:17:52 +08:00
|
|
|
if err != nil {
|
|
|
|
nofile = true
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
var cfg ClientConfig
|
|
|
|
|
|
|
|
if !nofile {
|
|
|
|
decoder := yaml.NewDecoder(f)
|
|
|
|
err = decoder.Decode(&cfg)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("trouble decoding file")
|
2021-04-05 23:06:30 +08:00
|
|
|
return nil, err
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
}
|
2021-04-05 23:06:30 +08:00
|
|
|
return &cfg, err
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
2021-05-26 00:48:04 +08:00
|
|
|
|
2021-05-26 04:09:49 +08:00
|
|
|
func ReadGlobalConfig() (*GlobalConfig, error) {
|
2021-07-26 02:22:20 +08:00
|
|
|
nofile := false
|
|
|
|
home := "/etc/netclient"
|
|
|
|
file := fmt.Sprintf(home + "/netconfig-global-001")
|
|
|
|
f, err := os.Open(file)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
nofile = true
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
var cfg GlobalConfig
|
|
|
|
|
|
|
|
if !nofile {
|
|
|
|
decoder := yaml.NewDecoder(f)
|
|
|
|
err = decoder.Decode(&cfg)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("trouble decoding file")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &cfg, err
|
2021-05-26 04:09:49 +08:00
|
|
|
}
|
|
|
|
|
2021-05-26 00:48:04 +08:00
|
|
|
func FileExists(f string) bool {
|
2021-07-26 02:22:20 +08:00
|
|
|
info, err := os.Stat(f)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return !info.IsDir()
|
2021-05-26 00:48:04 +08:00
|
|
|
}
|