Browse Source

got netclient ipv6 working

afeiszli 4 years ago
parent
commit
4c0cc042e0
3 changed files with 94 additions and 21 deletions
  1. 2 0
      netclient/config/config.go
  2. 87 16
      netclient/functions/common.go
  3. 5 5
      netclient/main.go

+ 2 - 0
netclient/config/config.go

@@ -35,6 +35,7 @@ type NodeConfig struct {
         RoamingOff bool `yaml:"roamingoff"`
         RoamingOff bool `yaml:"roamingoff"`
         DNSOff bool `yaml:"dnsoff"`
         DNSOff bool `yaml:"dnsoff"`
         IsLocal bool `yaml:"islocal"`
         IsLocal bool `yaml:"islocal"`
+        IsDualStack bool `yaml:"isdualstack"`
         AllowedIPs string `yaml:"allowedips"`
         AllowedIPs string `yaml:"allowedips"`
         LocalRange string `yaml:"localrange"`
         LocalRange string `yaml:"localrange"`
         PostUp string `yaml:"postup"`
         PostUp string `yaml:"postup"`
@@ -45,6 +46,7 @@ type NodeConfig struct {
         PrivateKey string `yaml:"privatekey"`
         PrivateKey string `yaml:"privatekey"`
         Endpoint string `yaml:"endpoint"`
         Endpoint string `yaml:"endpoint"`
         PostChanges string `yaml:"postchanges"`
         PostChanges string `yaml:"postchanges"`
+        IPForwarding string `yaml:"ipforwarding"`
 }
 }
 
 
 //reading in the env file
 //reading in the env file

+ 87 - 16
netclient/functions/common.go

@@ -72,7 +72,7 @@ func GetFreePort(rangestart int32) (int32, error){
         return portno, err
         return portno, err
 }
 }
 
 
-func Install(accesskey string, password string, server string, network string, noauto bool, accesstoken string,  inputname string, pubip string, dnsoff bool) error {
+func Install(accesskey string, password string, server string, network string, noauto bool, accesstoken string,  inputname string, pubip string, dnsoff bool, ipforward string) error {
 
 
 	tserver := ""
 	tserver := ""
 	tnetwork := ""
 	tnetwork := ""
@@ -144,6 +144,7 @@ func Install(accesskey string, password string, server string, network string, n
 	fmt.Println("SERVER SETTINGS:")
 	fmt.Println("SERVER SETTINGS:")
 
 
 	nodecfg.DNSOff = dnsoff
 	nodecfg.DNSOff = dnsoff
+	nodecfg.IPForwarding = ipforward
 
 
 	if server == "" {
 	if server == "" {
 		if servercfg.Address == "" && tserver == "" {
 		if servercfg.Address == "" && tserver == "" {
@@ -410,6 +411,7 @@ func Install(accesskey string, password string, server string, network string, n
        fmt.Println("NODE RECIEVED SETTINGS: ")
        fmt.Println("NODE RECIEVED SETTINGS: ")
        fmt.Println("     Password: " + node.Password)
        fmt.Println("     Password: " + node.Password)
        fmt.Println("     WG Address: " + node.Address)
        fmt.Println("     WG Address: " + node.Address)
+       fmt.Println("     WG ipv6 Address: " + node.Address6)
        fmt.Println("     Network: " + node.Nodenetwork)
        fmt.Println("     Network: " + node.Nodenetwork)
        fmt.Println("     Public  Endpoint: " + node.Endpoint)
        fmt.Println("     Public  Endpoint: " + node.Endpoint)
        fmt.Println("     Local Address: " + node.Localaddress)
        fmt.Println("     Local Address: " + node.Localaddress)
@@ -422,8 +424,12 @@ func Install(accesskey string, password string, server string, network string, n
        fmt.Println("     Public Key: " + node.Publickey)
        fmt.Println("     Public Key: " + node.Publickey)
        fmt.Println("     Mac Address: " + node.Macaddress)
        fmt.Println("     Mac Address: " + node.Macaddress)
        fmt.Println("     Is Local?: " + strconv.FormatBool(node.Islocal))
        fmt.Println("     Is Local?: " + strconv.FormatBool(node.Islocal))
+       fmt.Println("     Is Dual Stack?: " + strconv.FormatBool(node.Isdualstack))
        fmt.Println("     Local Range: " + node.Localrange)
        fmt.Println("     Local Range: " + node.Localrange)
 
 
+       if node.Dnsoff==true && !nodecfg.DNSOff {
+		nodecfg.DNSOff = true
+	}
 	if !islocal && node.Islocal && node.Localrange != "" {
 	if !islocal && node.Islocal && node.Localrange != "" {
 		fmt.Println("Resetting local settings for local network.")
 		fmt.Println("Resetting local settings for local network.")
 		node.Localaddress, err = getLocalIP(node.Localrange)
 		node.Localaddress, err = getLocalIP(node.Localrange)
@@ -448,7 +454,7 @@ func Install(accesskey string, password string, server string, network string, n
 		}
 		}
 	}
 	}
 
 
-	peers, hasGateway, gateways, err := getPeers(node.Macaddress, network, server)
+	peers, hasGateway, gateways, err := getPeers(node.Macaddress, network, server, node.Isdualstack)
 
 
 	if err != nil {
 	if err != nil {
                 return err
                 return err
@@ -594,12 +600,19 @@ func modConfig(node *nodepb.Node) error{
         if node.Address != ""{
         if node.Address != ""{
                 nodecfg.WGAddress = node.Address
                 nodecfg.WGAddress = node.Address
         }
         }
-        if node.Address != ""{
-                nodecfg.WGAddress = node.Address
+        if node.Address6 != ""{
+                nodecfg.WGAddress6 = node.Address6
         }
         }
         if node.Postchanges != "" {
         if node.Postchanges != "" {
                 nodecfg.PostChanges = node.Postchanges
                 nodecfg.PostChanges = node.Postchanges
         }
         }
+        if node.Dnsoff == true {
+                nodecfg.DNSOff = node.Dnsoff
+        }
+        if node.Isdualstack == true {
+                nodecfg.IsDualStack = true
+        }
+
         if node.Localrange != "" && node.Islocal {
         if node.Localrange != "" && node.Islocal {
                 nodecfg.IsLocal = true
                 nodecfg.IsLocal = true
                 nodecfg.LocalRange = node.Localrange
                 nodecfg.LocalRange = node.Localrange
@@ -647,6 +660,7 @@ func initWireguard(node *nodepb.Node, privkey string, peers []wgtypes.PeerConfig
 
 
 
 
 	nodecfg := modcfg.Node
 	nodecfg := modcfg.Node
+	servercfg := modcfg.Server
 	fmt.Println("beginning local WG config")
 	fmt.Println("beginning local WG config")
 
 
 
 
@@ -668,7 +682,14 @@ func initWireguard(node *nodepb.Node, privkey string, peers []wgtypes.PeerConfig
 	if node.Address == "" {
 	if node.Address == "" {
 		log.Fatal("no address to configure")
 		log.Fatal("no address to configure")
 	}
 	}
-
+	nameserver := servercfg.Address
+	nameserver = strings.Split(nameserver, ":")[0]
+	network := node.Nodenetwork
+        if nodecfg.Network != "" {
+                network = nodecfg.Network
+        } else if node.Nodenetwork != "" {
+                network = node.Nodenetwork
+        }
         cmdIPDevLinkAdd := &exec.Cmd {
         cmdIPDevLinkAdd := &exec.Cmd {
                 Path: ipExec,
                 Path: ipExec,
                 Args: []string{ ipExec, "link", "add", "dev", ifacename, "type",  "wireguard" },
                 Args: []string{ ipExec, "link", "add", "dev", ifacename, "type",  "wireguard" },
@@ -742,6 +763,33 @@ func initWireguard(node *nodepb.Node, privkey string, peers []wgtypes.PeerConfig
 			fmt.Printf("This is inconvenient: %v", err)
 			fmt.Printf("This is inconvenient: %v", err)
 		}
 		}
 	}
 	}
+
+	//=========DNS Setup==========\\
+	if nodecfg.DNSOff != true {
+
+	        _, err := exec.LookPath("resolvectl")
+		if err != nil {
+			fmt.Println(err)
+			fmt.Println("WARNING: resolvectl not present. Unable to set dns. Install resolvectl or run manually.")
+		} else {
+			_, err = exec.Command("resolvectl", "domain", ifacename, "~"+network).Output()
+			if err != nil {
+				fmt.Println(err)
+				fmt.Println("WARNING: Error encountered setting dns. Aborted setting dns.")
+			} else {
+				_, err = exec.Command("resolvectl", "default-route", ifacename, "false").Output()
+				if err != nil {
+	                                fmt.Println(err)
+	                                fmt.Println("WARNING: Error encountered setting dns. Aborted setting dns.")
+				} else {
+					_, err = exec.Command("resolvectl", "dns", ifacename, nameserver).Output()
+					fmt.Println(err)
+				}
+			}
+		}
+	}
+        //=========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},
@@ -783,6 +831,14 @@ func initWireguard(node *nodepb.Node, privkey string, peers []wgtypes.PeerConfig
                         fmt.Println("Error encountered adding gateway: " + err.Error())
                         fmt.Println("Error encountered adding gateway: " + err.Error())
                 }
                 }
 	}
 	}
+	/*
+        if (node.Address6 != "" && node.Isdualstack) {
+                _, err := exec.Command(ipExec,"-6","route","add",node.Address6,"dev",ifacename).Output()
+                if err != nil {
+                        fmt.Println("Error encountered adding ipv6: " + err.Error())
+                }
+        }
+	*/
 	}
 	}
 	return err
 	return err
 }
 }
@@ -876,7 +932,7 @@ func setWGConfig(network string) error {
         nodecfg := cfg.Node
         nodecfg := cfg.Node
         node := getNode(network)
         node := getNode(network)
 
 
-	peers, hasGateway, gateways, err := getPeers(node.Macaddress, nodecfg.Network, servercfg.Address)
+	peers, hasGateway, gateways, err := getPeers(node.Macaddress, nodecfg.Network, servercfg.Address, node.Isdualstack)
         if err != nil {
         if err != nil {
                 return err
                 return err
         }
         }
@@ -961,12 +1017,20 @@ func CheckIn(network string) error {
 	setupcheck := true
 	setupcheck := true
 	ipchange := false
 	ipchange := false
 
 
-	if !nodecfg.DNSOff {
-		vals := strings.Split(servercfg.Address, ":")
-		server := vals[0]
-		err = SetDNS(server)
-		if err != nil {
-                        fmt.Printf("Error encountered setting dns: %v", err)
+	if !(nodecfg.IPForwarding == "off") {
+		out, err := exec.Command("sysctl", "net.ipv4.ip_forward").Output()
+                 if err != nil {
+	                 fmt.Println(err)
+			 fmt.Println("WARNING: Error encountered setting ip forwarding. This can break functionality.")
+                 } else {
+                         s := strings.Fields(string(out))
+                         if s[2] != "1" {
+				_, err = exec.Command("sysctl", "-w", "net.ipv4.ip_forward=1").Output()
+				if err != nil {
+					fmt.Println(err)
+					fmt.Println("WARNING: Error encountered setting ip forwarding. You may want to investigate this.")
+				}
+			}
 		}
 		}
 	}
 	}
 
 
@@ -1238,6 +1302,7 @@ func getNode(network string) nodepb.Node {
 	node.Nodenetwork = nodecfg.Network
 	node.Nodenetwork = nodecfg.Network
 	node.Localaddress = nodecfg.LocalAddress
 	node.Localaddress = nodecfg.LocalAddress
 	node.Address = nodecfg.WGAddress
 	node.Address = nodecfg.WGAddress
+	node.Address6 = nodecfg.WGAddress6
 	node.Listenport = nodecfg.Port
 	node.Listenport = nodecfg.Port
 	node.Keepalive = nodecfg.KeepAlive
 	node.Keepalive = nodecfg.KeepAlive
 	node.Postup = nodecfg.PostUp
 	node.Postup = nodecfg.PostUp
@@ -1246,9 +1311,8 @@ func getNode(network string) nodepb.Node {
 	node.Macaddress = nodecfg.MacAddress
 	node.Macaddress = nodecfg.MacAddress
 	node.Endpoint = nodecfg.Endpoint
 	node.Endpoint = nodecfg.Endpoint
 	node.Password = nodecfg.Password
 	node.Password = nodecfg.Password
-
-	//spew.Dump(node)
-
+	node.Dnsoff = nodecfg.DNSOff
+	node.Isdualstack = nodecfg.IsDualStack
         return node
         return node
 }
 }
 
 
@@ -1389,7 +1453,7 @@ func DeleteInterface(ifacename string, postdown string) error{
         return err
         return err
 }
 }
 
 
-func getPeers(macaddress string, network string, server string) ([]wgtypes.PeerConfig, bool, []string, error) {
+func getPeers(macaddress string, network string, server string, dualstack bool) ([]wgtypes.PeerConfig, bool, []string, error) {
         //need to  implement checkin on server side
         //need to  implement checkin on server side
 	hasGateway := false
 	hasGateway := false
 	var gateways []string
 	var gateways []string
@@ -1488,6 +1552,13 @@ func getPeers(macaddress string, network string, server string) ([]wgtypes.PeerC
 				allowedips = append(allowedips, *ipnet)
 				allowedips = append(allowedips, *ipnet)
 			}
 			}
 		}
 		}
+                if res.Peers.Address6 != "" && dualstack {
+			var addr6 = net.IPNet{
+	                        IP: net.ParseIP(res.Peers.Address6),
+	                        Mask: net.CIDRMask(128, 128),
+	                }
+                        allowedips = append(allowedips, addr6)
+                }
 		if keepalive != 0 {
 		if keepalive != 0 {
 		peer = wgtypes.PeerConfig{
 		peer = wgtypes.PeerConfig{
 			PublicKey: pubkey,
 			PublicKey: pubkey,

+ 5 - 5
netclient/main.go

@@ -39,10 +39,10 @@ func main() {
 	tname := flag.String("name", "noname", "give the node a name at runtime")
 	tname := flag.String("name", "noname", "give the node a name at runtime")
 	tserver := flag.String("s", "localhost:50051", "The location (including port) of the remote gRPC server.")
 	tserver := flag.String("s", "localhost:50051", "The location (including port) of the remote gRPC server.")
 	tnetwork := flag.String("n", "nonetwork", "The node network you are attempting to join.")
 	tnetwork := flag.String("n", "nonetwork", "The node network you are attempting to join.")
-	tdnsoff := flag.Bool("dnsoff", false, "No dns mode. If true, netclient will not alter system dns.")
+	tdnsoff := flag.Bool("dnsoff", false, "DNS Mode. If true, netclient will not alter system dns. false by default.")
 	tpublicip := flag.String("ip4", "nopubip", "The node network you are attempting to join.")
 	tpublicip := flag.String("ip4", "nopubip", "The node network you are attempting to join.")
 	tnoauto := flag.Bool("na", false, "No auto mode. If true, netmclient will not be installed as a system service and you will have to retrieve updates manually via checkin command.")
 	tnoauto := flag.Bool("na", false, "No auto mode. If true, netmclient will not be installed as a system service and you will have to retrieve updates manually via checkin command.")
-	tnoforward := flag.Bool("nf", false, "No Forward mode. If true, netclient will not check for IP forwarding. This may break functionality")
+	tipforward := flag.String("nf", "on", "No Forward mode. If true, netclient will not check for IP forwarding. This may break functionality")
 	command := flag.String("c", "required", "The command to run")
 	command := flag.String("c", "required", "The command to run")
 
 
 
 
@@ -91,7 +91,7 @@ func main() {
                                 fmt.Println("Required, '-n'. No network provided. Exiting.")
                                 fmt.Println("Required, '-n'. No network provided. Exiting.")
                                 os.Exit(1)
                                 os.Exit(1)
                         }
                         }
-
+			/*
 			if !*tnoforward {
 			if !*tnoforward {
 				forward := exec.Command("sysctl", "net.ipv4.ip_forward")
 				forward := exec.Command("sysctl", "net.ipv4.ip_forward")
 				out, err := forward.Output()
 				out, err := forward.Output()
@@ -108,9 +108,9 @@ func main() {
 					log.Fatal("It is recommended to enable IP Forwarding. Current status is: " +  s[2] + ", but should be 1. if you would like to run without IP Forwarding, re-run with flag '-nf true'")
 					log.Fatal("It is recommended to enable IP Forwarding. Current status is: " +  s[2] + ", but should be 1. if you would like to run without IP Forwarding, re-run with flag '-nf true'")
 				}
 				}
 			}
 			}
-
+			*/
 			fmt.Println("Beginning agent installation.")
 			fmt.Println("Beginning agent installation.")
-			err := functions.Install(*taccesskey, *tpassword, *tserver, *tnetwork, *tnoauto, *taccesstoken, *tname, *tpublicip, *tdnsoff)
+			err := functions.Install(*taccesskey, *tpassword, *tserver, *tnetwork, *tnoauto, *taccesstoken, *tname, *tpublicip, *tdnsoff, *tipforward)
 			if err != nil {
 			if err != nil {
 				fmt.Println("Error encountered while installing.")
 				fmt.Println("Error encountered while installing.")
 				if !strings.Contains(err.Error(), "ALREADY_INSTALLED") {
 				if !strings.Contains(err.Error(), "ALREADY_INSTALLED") {