Browse Source

fixing client issues for uspace, fwmark, vpn

afeiszli 3 years ago
parent
commit
801919ab0d

+ 39 - 0
docker/Dockerfile-netclient-doks

@@ -0,0 +1,39 @@
+FROM debian:buster as builder
+# add glib support daemon manager
+
+RUN apt update -y && apt install -y wget bash gcc musl-dev openssl golang git build-essential libmnl-dev iptables
+
+RUN wget -O go.tgz https://dl.google.com/go/go1.17.1.linux-amd64.tar.gz
+
+RUN tar -C /usr/local -xzf go.tgz
+
+WORKDIR /usr/local/go/src
+
+RUN chmod +x make.bash
+
+RUN ./make.bash
+
+ENV PATH="/usr/local/go/bin:$PATH"
+
+ENV GOPATH=/opt/go/
+
+ENV PATH=$PATH:$GOPATH/bin
+
+WORKDIR /app
+
+COPY . .
+
+ENV GO111MODULE=auto
+
+RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 /usr/local/go/bin/go build -ldflags="-w -s" -o netclient-app netclient/main.go
+
+FROM debian:buster
+
+WORKDIR /root/
+
+RUN apt update -y && apt install -y bash curl wget traceroute procps dnsutils iptables openresolv iproute2
+COPY --from=builder /app/netclient-app ./netclient
+COPY --from=builder /app/scripts/netclient.sh .
+RUN chmod 0755 netclient && chmod 0755 netclient.sh
+
+ENTRYPOINT ["/bin/sh", "./netclient.sh"]

+ 57 - 0
docker/Dockerfile-netclient-doks-uspace

@@ -0,0 +1,57 @@
+FROM debian:buster as builder
+# add glib support daemon manager
+
+RUN apt update -y && apt install -y wget bash gcc musl-dev openssl golang git build-essential libmnl-dev iptables
+
+RUN wget -O go.tgz https://dl.google.com/go/go1.17.1.linux-amd64.tar.gz
+
+RUN tar -C /usr/local -xzf go.tgz
+
+WORKDIR /usr/local/go/src
+
+RUN chmod +x make.bash
+
+RUN ./make.bash
+
+ENV PATH="/usr/local/go/bin:$PATH"
+
+ENV GOPATH=/opt/go/
+
+ENV PATH=$PATH:$GOPATH/bin
+
+WORKDIR /app
+
+COPY . .
+
+ENV GO111MODULE=auto
+
+RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 /usr/local/go/bin/go build -ldflags="-w -s" -o netclient-app netclient/main.go
+
+WORKDIR /root/
+
+RUN git clone https://git.zx2c4.com/wireguard-go && \
+    cd wireguard-go && \
+    make && \
+    make install
+
+ENV WITH_WGQUICK=yes
+RUN git clone https://git.zx2c4.com/wireguard-tools && \
+    cd wireguard-tools && \
+    cd src && \
+    make && \
+    make install
+
+
+FROM debian:buster
+
+WORKDIR /root/
+
+RUN apt update -y && apt install -y bash curl wget traceroute procps dnsutils iptables openresolv iproute2
+COPY --from=builder /usr/bin/wireguard-go /usr/bin/wg* /usr/bin/
+COPY --from=builder /app/netclient-app ./netclient
+COPY --from=builder /app/scripts/netclient.sh .
+RUN chmod 0755 netclient && chmod 0755 netclient.sh
+
+ENV WG_QUICK_USERSPACE_IMPLEMENTATION=wireguard-go
+
+ENTRYPOINT ["/bin/sh", "./netclient.sh"]

+ 1 - 1
logic/server.go

@@ -136,7 +136,7 @@ func ServerJoin(network string, serverID string, privateKey string) error {
 		return err
 	}
 
-	err = initWireguard(node, privateKey, peers, hasGateway, gateways, 0)
+	err = initWireguard(node, privateKey, peers, hasGateway, gateways)
 	if err != nil {
 		return err
 	}

+ 3 - 3
logic/wireguard.go

@@ -47,7 +47,7 @@ func RemoveConf(iface string, printlog bool) error {
 
 // Private Functions
 
-func initWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig, hasGateway bool, gateways []string, fwmark int32) error {
+func initWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig, hasGateway bool, gateways []string) error {
 
 	key, err := wgtypes.ParseKey(privkey)
 	if err != nil {
@@ -85,7 +85,7 @@ func initWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
 
 	if !ncutils.IsKernel() {
 		var newConf string
-		newConf, _ = ncutils.CreateUserSpaceConf(node.Address, key.String(), strconv.FormatInt(int64(node.ListenPort), 10), node.MTU, fwmark, node.PersistentKeepalive, peers)
+		newConf, _ = ncutils.CreateUserSpaceConf(node.Address, key.String(), strconv.FormatInt(int64(node.ListenPort), 10), node.MTU, node.PersistentKeepalive, peers)
 		confPath := ncutils.GetNetclientPathSpecific() + ifacename + ".conf"
 		Log("writing wg conf file to: "+confPath, 1)
 		err = ioutil.WriteFile(confPath, []byte(newConf), 0644)
@@ -293,7 +293,7 @@ func setWGConfig(node models.Node, network string, peerupdate bool) error {
 		err = setServerPeers(iface, node.PersistentKeepalive, peers)
 		Log("updated peers on server "+node.Name, 2)
 	} else {
-		err = initWireguard(&node, privkey, peers, hasGateway, gateways, 0)
+		err = initWireguard(&node, privkey, peers, hasGateway, gateways)
 		Log("finished setting wg config on server "+node.Name, 3)
 	}
 	return err

+ 4 - 0
netclient/command/commands.go

@@ -48,6 +48,10 @@ func Join(cfg config.ClientConfig, privateKey string) error {
 		} else {
 			ncutils.PrintLog("success", 0)
 		}
+		if strings.Contains(err.Error(), "ALREADY_INSTALLED") {
+			ncutils.PrintLog(err.Error(), 0)
+			err = nil
+		}
 		return err
 	}
 	ncutils.PrintLog("joined "+cfg.Network, 1)

+ 1 - 1
netclient/config/config.go

@@ -31,7 +31,6 @@ type ClientConfig struct {
 	Daemon          string         `yaml:"daemon"`
 	OperatingSystem string         `yaml:"operatingsystem"`
 	DebugJoin       bool           `yaml:"debugjoin"`
-	FWMark          int32          `yaml:"fwmark"`
 }
 
 // ServerConfig - struct for dealing with the server information for a netclient
@@ -342,6 +341,7 @@ func GetCLIConfig(c *cli.Context) (ClientConfig, string, error) {
 	cfg.Node.Roaming = c.String("roaming")
 	cfg.Node.DNSOn = c.String("dnson")
 	cfg.Node.IsLocal = c.String("islocal")
+	cfg.Node.IsStatic = c.String("isstatic")
 	cfg.Node.IsDualStack = c.String("isdualstack")
 	cfg.Node.PostUp = c.String("postup")
 	cfg.Node.PostDown = c.String("postdown")

+ 1 - 8
netclient/functions/join.go

@@ -6,9 +6,7 @@ import (
 	"errors"
 	"fmt"
 	"log"
-	"math/rand"
 	"os/exec"
-	"time"
 
 	nodepb "github.com/gravitl/netmaker/grpc"
 	"github.com/gravitl/netmaker/models"
@@ -35,12 +33,6 @@ func JoinNetwork(cfg config.ClientConfig, privateKey string) error {
 			err := errors.New("ALREADY_INSTALLED. Netclient appears to already be installed for " + cfg.Network + ". To re-install, please remove by executing 'sudo netclient leave -n " + cfg.Network + "'. Then re-run the install command.")
 			return err
 		}
-		if cfg.FWMark == 0 {
-			rand.Seed(time.Now().UnixNano())
-			var min int32 = 1000
-			var max int32 = 9999
-			cfg.FWMark = rand.Int31n(max-min) + min
-		}
 
 		err = config.Write(&cfg, cfg.Network)
 		if err != nil {
@@ -111,6 +103,7 @@ func JoinNetwork(cfg config.ClientConfig, privateKey string) error {
 		MacAddress:          cfg.Node.MacAddress,
 		AccessKey:           cfg.Server.AccessKey,
 		IsStatic:            cfg.Node.IsStatic,
+		Roaming:             cfg.Node.Roaming,
 		Network:             cfg.Network,
 		ListenPort:          cfg.Node.ListenPort,
 		PostUp:              cfg.Node.PostUp,

+ 7 - 0
netclient/main.go

@@ -105,6 +105,13 @@ func main() {
 			Value:   "",
 			Usage:   "Local address for machine. Can be used in place of Endpoint for machines on the same LAN.",
 		},
+		&cli.StringFlag{
+			Name:    "isstatic",
+			Aliases: []string{"st"},
+			EnvVars: []string{"NETCLIENT_IS_STATIC"},
+			Value:   "",
+			Usage:   "Indicates if client is static by default (will not change addresses automatically).",
+		},
 		&cli.StringFlag{
 			Name:    "address",
 			Aliases: []string{"a"},

+ 1 - 7
netclient/ncutils/netclientutils_darwin.go

@@ -28,19 +28,15 @@ func RunCmdFormatted(command string, printerr bool) (string, error) {
 }
 
 // CreateUserSpaceConf - creates a user space WireGuard conf
-func CreateUserSpaceConf(address string, privatekey string, listenPort string, mtu int32, fwmark int32, perskeepalive int32, peers []wgtypes.PeerConfig) (string, error) {
+func CreateUserSpaceConf(address string, privatekey string, listenPort string, mtu int32, perskeepalive int32, peers []wgtypes.PeerConfig) (string, error) {
 	peersString, err := parsePeers(perskeepalive, peers)
 	var listenPortString string
-	var fwmarkString string
 	if mtu <= 0 {
 		mtu = 1280
 	}
 	if listenPort != "" {
 		listenPortString += "ListenPort = " + listenPort
 	}
-	if fwmark != 0 {
-		fwmarkString += "FWMark = " + strconv.Itoa(int(fwmark))
-	}
 	if err != nil {
 		return "", err
 	}
@@ -49,7 +45,6 @@ Address = %s
 PrivateKey = %s
 MTU = %s
 %s
-%s
 
 %s
 
@@ -58,7 +53,6 @@ MTU = %s
 		privatekey,
 		strconv.Itoa(int(mtu)),
 		listenPortString,
-		fwmarkString,
 		peersString)
 	return config, nil
 }

+ 1 - 7
netclient/ncutils/netclientutils_freebsd.go

@@ -36,19 +36,15 @@ func RunCmd(command string, printerr bool) (string, error) {
 }
 
 // CreateUserSpaceConf - creates a user space WireGuard conf
-func CreateUserSpaceConf(address string, privatekey string, listenPort string, mtu int32, fwmark int32, perskeepalive int32, peers []wgtypes.PeerConfig) (string, error) {
+func CreateUserSpaceConf(address string, privatekey string, listenPort string, mtu int32, perskeepalive int32, peers []wgtypes.PeerConfig) (string, error) {
 	peersString, err := parsePeers(perskeepalive, peers)
 	var listenPortString string
-	var fwmarkString string
 	if mtu <= 0 {
 		mtu = 1280
 	}
 	if listenPort != "" {
 		listenPortString += "ListenPort = " + listenPort
 	}
-	if fwmark != 0 {
-		fwmarkString += "FWMark = " + strconv.Itoa(int(fwmark))
-	}
 	if err != nil {
 		return "", err
 	}
@@ -57,7 +53,6 @@ Address = %s
 PrivateKey = %s
 MTU = %s
 %s
-%s
 
 %s
 
@@ -66,7 +61,6 @@ MTU = %s
 		privatekey,
 		strconv.Itoa(int(mtu)),
 		listenPortString,
-		fwmarkString,
 		peersString)
 	return config, nil
 }

+ 1 - 7
netclient/ncutils/netclientutils_linux.go

@@ -28,19 +28,15 @@ func RunCmdFormatted(command string, printerr bool) (string, error) {
 }
 
 // CreateUserSpaceConf - creates a user space WireGuard conf
-func CreateUserSpaceConf(address string, privatekey string, listenPort string, mtu int32, fwmark int32, perskeepalive int32, peers []wgtypes.PeerConfig) (string, error) {
+func CreateUserSpaceConf(address string, privatekey string, listenPort string, mtu int32, perskeepalive int32, peers []wgtypes.PeerConfig) (string, error) {
 	peersString, err := parsePeers(perskeepalive, peers)
 	var listenPortString string
-	var fwmarkString string
 	if mtu <= 0 {
 		mtu = 1280
 	}
 	if listenPort != "" {
 		listenPortString += "ListenPort = " + listenPort
 	}
-	if fwmark != 0 {
-		fwmarkString += "FWMark = " + strconv.Itoa(int(fwmark))
-	}
 	if err != nil {
 		return "", err
 	}
@@ -49,7 +45,6 @@ Address = %s
 PrivateKey = %s
 MTU = %s
 %s
-%s
 
 %s
 
@@ -58,7 +53,6 @@ MTU = %s
 		privatekey,
 		strconv.Itoa(int(mtu)),
 		listenPortString,
-		fwmarkString,
 		peersString)
 	return config, nil
 }

+ 1 - 1
netclient/ncutils/netclientutils_windows.go

@@ -44,7 +44,7 @@ func RunCmdFormatted(command string, printerr bool) (string, error) {
 }
 
 // CreateUserSpaceConf - creates a user space WireGuard conf
-func CreateUserSpaceConf(address string, privatekey string, listenPort string, mtu int32, fwmark int32, perskeepalive int32, peers []wgtypes.PeerConfig) (string, error) {
+func CreateUserSpaceConf(address string, privatekey string, listenPort string, mtu int32, perskeepalive int32, peers []wgtypes.PeerConfig) (string, error) {
 	peersString, err := parsePeers(perskeepalive, peers)
 	var listenPortString string
 	if mtu <= 0 {

+ 3 - 6
netclient/wireguard/common.go

@@ -119,8 +119,6 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
 	if err != nil {
 		return err
 	}
-	fwmarkint32 := modcfg.FWMark
-	fwmarkint := int(fwmarkint32)
 	nodecfg := modcfg.Node
 	servercfg := modcfg.Server
 
@@ -168,7 +166,6 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
 		conf = wgtypes.Config{
 			PrivateKey:   &key,
 			ListenPort:   &nodeport,
-			FirewallMark: &fwmarkint,
 			ReplacePeers: true,
 			Peers:        peers,
 		}
@@ -176,9 +173,9 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
 	if !ncutils.IsKernel() {
 		var newConf string
 		if node.UDPHolePunch != "yes" {
-			newConf, _ = ncutils.CreateUserSpaceConf(node.Address, key.String(), strconv.FormatInt(int64(node.ListenPort), 10), node.MTU, fwmarkint32, node.PersistentKeepalive, peers)
+			newConf, _ = ncutils.CreateUserSpaceConf(node.Address, key.String(), strconv.FormatInt(int64(node.ListenPort), 10), node.MTU, node.PersistentKeepalive, peers)
 		} else {
-			newConf, _ = ncutils.CreateUserSpaceConf(node.Address, key.String(), "", node.MTU, fwmarkint32, node.PersistentKeepalive, peers)
+			newConf, _ = ncutils.CreateUserSpaceConf(node.Address, key.String(), "", node.MTU, node.PersistentKeepalive, peers)
 		}
 		confPath := ncutils.GetNetclientPathSpecific() + ifacename + ".conf"
 		ncutils.PrintLog("writing wg conf file to: "+confPath, 1)
@@ -325,7 +322,7 @@ func SetWGConfig(network string, peerupdate bool) error {
 	if err != nil {
 		return err
 	}
-	if peerupdate && !ncutils.IsFreeBSD() {
+	if peerupdate && !ncutils.IsFreeBSD() && !(ncutils.IsLinux() && !ncutils.IsKernel()) {
 		var iface string
 		iface = nodecfg.Interface
 		if ncutils.IsMac() {

+ 2 - 0
scripts/netclient.sh

@@ -1,4 +1,5 @@
 #!/bin/sh
+
 echo "[netclient] joining network"
 
 if [ -z "${SLEEP}" ]; then
@@ -11,6 +12,7 @@ if [ "$TOKEN" != "" ]; then
 fi
 
 /root/netclient join $TOKEN_CMD -daemon off -dnson no
+if [ $? -ne 0 ]; then { echo "Failed to join, quitting." ; exit 1; } fi
 
 echo "[netclient] Starting netclient checkin"
 # loop and call checkin -n all