netclient working

This commit is contained in:
afeiszli 2021-06-02 15:15:23 +00:00
commit f757b9e2c8
6 changed files with 83 additions and 36 deletions

View file

@ -413,22 +413,22 @@ func GetCLIConfig(c *cli.Context) (ClientConfig, error){
cfg.Node.Password = c.String("password") cfg.Node.Password = c.String("password")
cfg.Node.MacAddress = c.String("macaddress") cfg.Node.MacAddress = c.String("macaddress")
cfg.Node.LocalAddress = c.String("localaddress") cfg.Node.LocalAddress = c.String("localaddress")
cfg.Node.LocalRange = c.String("localrange")
cfg.Node.WGAddress = c.String("address") cfg.Node.WGAddress = c.String("address")
cfg.Node.WGAddress6 = c.String("addressIPV6") cfg.Node.WGAddress6 = c.String("addressIPV6")
cfg.Node.Roaming = c.String("") cfg.Node.Roaming = c.String("roaming")
cfg.Node.DNS = c.String("") cfg.Node.DNS = c.String("dns")
cfg.Node.IsLocal = c.String("") cfg.Node.IsLocal = c.String("islocal")
cfg.Node.IsDualStack = c.String("") cfg.Node.IsDualStack = c.String("isdualstack")
cfg.Node.IsIngressGateway = c.String("") cfg.Node.PostUp = c.String("postup")
cfg.Node.PostUp = c.String("") cfg.Node.PostDown = c.String("postdown")
cfg.Node.PostDown = c.String("") cfg.Node.Port = int32(c.Int("port"))
cfg.Node.Port = int32(c.Int("")) cfg.Node.KeepAlive = int32(c.Int("keepalive"))
cfg.Node.KeepAlive = int32(c.Int("")) cfg.Node.PublicKey = c.String("publickey")
cfg.Node.PublicKey = c.String("") cfg.Node.PrivateKey = c.String("privatekey")
cfg.Node.PrivateKey = c.String("") cfg.Node.Endpoint = c.String("endpoint")
cfg.Node.Endpoint = c.String("") cfg.Node.IPForwarding = c.String("ipforwarding")
cfg.Node.IPForwarding = c.String("") cfg.OperatingSystem = c.String("operatingsystem")
cfg.Daemon = c.String("daemon")
return cfg, nil return cfg, nil
} }
@ -539,4 +539,3 @@ func FileExists(f string) bool {
} }
return !info.IsDir() return !info.IsDir()
} }

View file

@ -309,15 +309,17 @@ func LeaveNetwork(network string) error {
) )
if err != nil { if err != nil {
log.Printf("Encountered error deleting node: %v", err) log.Printf("Encountered error deleting node: %v", err)
fmt.Println(err) log.Println(err)
} else { } else {
fmt.Println("delete node " + node.MacAddress + "from remote server on network " + node.Network) log.Println("Removed machine from " + node.Network + " network on remote server")
} }
} }
} }
err = local.WipeLocal(network) err = local.WipeLocal(network)
if err != nil { if err != nil {
log.Printf("Unable to wipe local config: %v", err) log.Printf("Unable to wipe local config: %v", err)
} else {
log.Println("Removed " + node.Network + " network locally")
} }
if cfg.Daemon != "off" { if cfg.Daemon != "off" {
err = local.RemoveSystemDServices(network) err = local.RemoveSystemDServices(network)
@ -336,13 +338,13 @@ func DeleteInterface(ifacename string, postdown string) error{
} }
err = cmdIPLinkDel.Run() err = cmdIPLinkDel.Run()
if err != nil { if err != nil {
fmt.Println(err) log.Println(err)
} }
if postdown != "" { if postdown != "" {
runcmds := strings.Split(postdown, "; ") runcmds := strings.Split(postdown, "; ")
err = local.RunCmds(runcmds) err = local.RunCmds(runcmds)
if err != nil { if err != nil {
fmt.Println("Error encountered running PostDown: " + err.Error()) log.Println("Error encountered running PostDown: " + err.Error())
} }
} }
return err return err
@ -367,9 +369,9 @@ func List() error{
PublicEndpoint: cfg.Node.Endpoint, PublicEndpoint: cfg.Node.Endpoint,
} }
jsoncfg, _ := json.Marshal(listconfig) jsoncfg, _ := json.Marshal(listconfig)
fmt.Println(network + ": " + string(jsoncfg)) log.Println(network + ": " + string(jsoncfg))
} else { } else {
fmt.Println(network + ": Could not retrieve network configuration.") log.Println(network + ": Could not retrieve network configuration.")
} }
} }
return nil return nil

View file

@ -6,6 +6,8 @@ import (
"context" "context"
"log" "log"
"net" "net"
"math/rand"
"time"
"github.com/gravitl/netmaker/netclient/config" "github.com/gravitl/netmaker/netclient/config"
"github.com/gravitl/netmaker/netclient/wireguard" "github.com/gravitl/netmaker/netclient/wireguard"
"github.com/gravitl/netmaker/netclient/server" "github.com/gravitl/netmaker/netclient/server"
@ -35,9 +37,12 @@ func JoinNetwork(cfg config.ClientConfig) error {
return err return err
} }
defer wgclient.Close() defer wgclient.Close()
if cfg.Node.Network == "" {
return errors.New("no network provided")
}
if cfg.Node.LocalRange != "" { if cfg.Node.LocalRange != "" {
if cfg.Node.LocalAddress == "" { if cfg.Node.LocalAddress == "" {
log.Println("local vpn, getting local address from range: " + cfg.Node.LocalRange)
ifaces, err := net.Interfaces() ifaces, err := net.Interfaces()
if err != nil { if err != nil {
return err return err
@ -90,6 +95,9 @@ func JoinNetwork(cfg config.ClientConfig) error {
cfg.Node.LocalAddress = local cfg.Node.LocalAddress = local
} }
} }
if cfg.Node.Password == "" {
cfg.Node.Password = GenPass()
}
if cfg.Node.Endpoint == "" { if cfg.Node.Endpoint == "" {
if cfg.Node.IsLocal == "yes" && cfg.Node.LocalAddress != "" { if cfg.Node.IsLocal == "yes" && cfg.Node.LocalAddress != "" {
cfg.Node.Endpoint = cfg.Node.LocalAddress cfg.Node.Endpoint = cfg.Node.LocalAddress
@ -124,7 +132,12 @@ func JoinNetwork(cfg config.ClientConfig) error {
cfg.Node.MacAddress = macs[0] cfg.Node.MacAddress = macs[0]
} }
} }
if cfg.Node.Port == 0 {
cfg.Node.Port, err = GetFreePort(51821)
if err != nil {
fmt.Printf("Error retrieving port: %v", err)
}
}
var wcclient nodepb.NodeServiceClient var wcclient nodepb.NodeServiceClient
var requestOpts grpc.DialOption var requestOpts grpc.DialOption
requestOpts = grpc.WithInsecure() requestOpts = grpc.WithInsecure()
@ -214,3 +227,20 @@ func JoinNetwork(cfg config.ClientConfig) error {
return err return err
} }
//generate an access key value
func GenPass() string {
var seededRand *rand.Rand = rand.New(
rand.NewSource(time.Now().UnixNano()))
length := 16
charset := "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}

View file

@ -43,12 +43,13 @@ func Register(cfg config.GlobalConfig) error {
body := bytes.NewBuffer(jsonbytes) body := bytes.NewBuffer(jsonbytes)
publicaddress := cfg.Client.ServerPublicEndpoint + ":" + cfg.Client.ServerAPIPort publicaddress := cfg.Client.ServerPublicEndpoint + ":" + cfg.Client.ServerAPIPort
log.Println("registering to http://"+publicaddress+"/api/client/register")
res, err := http.Post("http://"+publicaddress+"/api/intclient/register","application/json",body) res, err := http.Post("http://"+publicaddress+"/api/intclient/register","application/json",body)
if err != nil { if err != nil {
log.Println("Failed to register to http://"+publicaddress+"/api/client/register")
return err return err
} }
if res.StatusCode != http.StatusOK { if res.StatusCode != http.StatusOK {
log.Println("Failed to register to http://"+publicaddress+"/api/client/register")
return errors.New("request to server failed: " + res.Status) return errors.New("request to server failed: " + res.Status)
} }
bodyBytes, err := ioutil.ReadAll(res.Body) bodyBytes, err := ioutil.ReadAll(res.Body)
@ -69,7 +70,7 @@ func Register(cfg config.GlobalConfig) error {
if err != nil { if err != nil {
return err return err
} }
log.Println("registered netclient to " + cfg.Client.ServerPrivateAddress)
return err return err
} }

View file

@ -134,7 +134,6 @@ OnCalendar=*:*:0/30
WantedBy=timers.target WantedBy=timers.target
` `
servicebytes := []byte(systemservice) servicebytes := []byte(systemservice)
timerbytes := []byte(systemtimer) timerbytes := []byte(systemtimer)
@ -253,9 +252,13 @@ func RemoveSystemDServices(network string) error {
log.Println(err) log.Println(err)
} }
if fullremove { if fullremove {
if FileExists("/etc/systemd/system/netclient@.service") {
err = os.Remove("/etc/systemd/system/netclient@.service") err = os.Remove("/etc/systemd/system/netclient@.service")
} }
}
if FileExists("/etc/systemd/system/netclient-"+network+".timer") {
err = os.Remove("/etc/systemd/system/netclient-"+network+".timer") err = os.Remove("/etc/systemd/system/netclient-"+network+".timer")
}
if err != nil { if err != nil {
log.Println("Error removing file. Please investigate.") log.Println("Error removing file. Please investigate.")
log.Println(err) log.Println(err)
@ -284,9 +287,15 @@ func WipeLocal(network string) error{
//home, err := homedir.Dir() //home, err := homedir.Dir()
home := "/etc/netclient" home := "/etc/netclient"
if FileExists(home + "/netconfig-" + network) {
_ = os.Remove(home + "/netconfig-" + network) _ = os.Remove(home + "/netconfig-" + network)
}
if FileExists(home + "/nettoken-" + network) {
_ = os.Remove(home + "/nettoken-" + network) _ = os.Remove(home + "/nettoken-" + network)
}
if FileExists(home + "/wgkey-" + network) {
_ = os.Remove(home + "/wgkey-" + network) _ = os.Remove(home + "/wgkey-" + network)
}
ipExec, err := exec.LookPath("ip") ipExec, err := exec.LookPath("ip")

View file

@ -25,6 +25,8 @@ import (
//homedir "github.com/mitchellh/go-homedir" //homedir "github.com/mitchellh/go-homedir"
) )
func InitGRPCWireguard(client models.IntClient) error { func InitGRPCWireguard(client models.IntClient) error {
//spew.Dump(client)
key, err := wgtypes.ParseKey(client.PrivateKey) key, err := wgtypes.ParseKey(client.PrivateKey)
if err != nil { if err != nil {
return err return err
@ -142,6 +144,8 @@ func InitGRPCWireguard(client models.IntClient) error {
func InitWireguard(node *nodepb.Node, privkey string, peers []wgtypes.PeerConfig, hasGateway bool, gateways []string) error { func InitWireguard(node *nodepb.Node, privkey string, peers []wgtypes.PeerConfig, hasGateway bool, gateways []string) error {
//spew.Dump(node)
//spew.Dump(peers)
ipExec, err := exec.LookPath("ip") ipExec, err := exec.LookPath("ip")
if err != nil { if err != nil {
return err return err
@ -246,7 +250,6 @@ func InitWireguard(node *nodepb.Node, privkey string, peers []wgtypes.PeerConfig
} }
} }
err = wgclient.ConfigureDevice(ifacename, conf) err = wgclient.ConfigureDevice(ifacename, conf)
if err != nil { if err != nil {
@ -284,12 +287,14 @@ func InitWireguard(node *nodepb.Node, privkey string, peers []wgtypes.PeerConfig
} }
//=========End DNS Setup=======\\ //=========End DNS Setup=======\\
cmdIPLinkUp := &exec.Cmd { cmdIPLinkUp := &exec.Cmd {
Path: ipExec, Path: ipExec,
Args: []string{ ipExec, "link", "set", "up", "dev", ifacename}, Args: []string{ ipExec, "link", "set", "up", "dev", ifacename},
Stdout: os.Stdout, Stdout: os.Stdout,
Stderr: os.Stdout, Stderr: os.Stdout,
} }
cmdIPLinkDown := &exec.Cmd { cmdIPLinkDown := &exec.Cmd {
Path: ipExec, Path: ipExec,
Args: []string{ ipExec, "link", "set", "down", "dev", ifacename}, Args: []string{ ipExec, "link", "set", "down", "dev", ifacename},
@ -334,6 +339,7 @@ func InitWireguard(node *nodepb.Node, privkey string, peers []wgtypes.PeerConfig
fmt.Println("Error encountered adding ipv6: " + err.Error()) fmt.Println("Error encountered adding ipv6: " + err.Error())
} }
} }
return err return err
} }