mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-06 09:22:42 +08:00
added fwmark
This commit is contained in:
@@ -136,7 +136,7 @@ func ServerJoin(network string, serverID string, privateKey string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = initWireguard(node, privateKey, peers, hasGateway, gateways)
|
err = initWireguard(node, privateKey, peers, hasGateway, gateways, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@@ -70,7 +70,7 @@ func setWGConfig(node models.Node, network string, peerupdate bool) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func initWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig, hasGateway bool, gateways []string) error {
|
func initWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig, hasGateway bool, gateways []string, fwmark int32) error {
|
||||||
|
|
||||||
key, err := wgtypes.ParseKey(privkey)
|
key, err := wgtypes.ParseKey(privkey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -108,7 +108,7 @@ func initWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
|
|||||||
|
|
||||||
if !ncutils.IsKernel() {
|
if !ncutils.IsKernel() {
|
||||||
var newConf string
|
var newConf string
|
||||||
newConf, _ = ncutils.CreateUserSpaceConf(node.Address, key.String(), strconv.FormatInt(int64(node.ListenPort), 10), node.MTU, node.PersistentKeepalive, peers)
|
newConf, _ = ncutils.CreateUserSpaceConf(node.Address, key.String(), strconv.FormatInt(int64(node.ListenPort), 10), node.MTU, fwmark, node.PersistentKeepalive, peers)
|
||||||
confPath := ncutils.GetNetclientPathSpecific() + ifacename + ".conf"
|
confPath := ncutils.GetNetclientPathSpecific() + ifacename + ".conf"
|
||||||
Log("writing wg conf file to: "+confPath, 1)
|
Log("writing wg conf file to: "+confPath, 1)
|
||||||
err = ioutil.WriteFile(confPath, []byte(newConf), 0644)
|
err = ioutil.WriteFile(confPath, []byte(newConf), 0644)
|
||||||
@@ -290,7 +290,7 @@ func setServerPeers(iface string, keepalive int32, peers []wgtypes.PeerConfig) e
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func setWGKeyConfig(node models.Node) error {
|
func setWGConfig(node models.Node) error {
|
||||||
|
|
||||||
node.SetID()
|
node.SetID()
|
||||||
privatekey, err := wgtypes.GeneratePrivateKey()
|
privatekey, err := wgtypes.GeneratePrivateKey()
|
||||||
|
@@ -30,6 +30,7 @@ type ClientConfig struct {
|
|||||||
Daemon string `yaml:"daemon"`
|
Daemon string `yaml:"daemon"`
|
||||||
OperatingSystem string `yaml:"operatingsystem"`
|
OperatingSystem string `yaml:"operatingsystem"`
|
||||||
DebugJoin bool `yaml:"debugjoin"`
|
DebugJoin bool `yaml:"debugjoin"`
|
||||||
|
FWMark int32 `yaml:"fwmark"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServerConfig - struct for dealing with the server information for a netclient
|
// ServerConfig - struct for dealing with the server information for a netclient
|
||||||
|
@@ -2,11 +2,12 @@ package daemon
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gravitl/netmaker/netclient/ncutils"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/gravitl/netmaker/netclient/ncutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const MAC_SERVICE_NAME = "com.gravitl.netclient"
|
const MAC_SERVICE_NAME = "com.gravitl.netclient"
|
||||||
@@ -20,7 +21,7 @@ func SetupMacDaemon(interval string) error {
|
|||||||
binarypath := dir + "/netclient"
|
binarypath := dir + "/netclient"
|
||||||
|
|
||||||
if !ncutils.FileExists("/etc/netclient/netclient") {
|
if !ncutils.FileExists("/etc/netclient/netclient") {
|
||||||
_, err = ncutils.Copy(binarypath, "/etc/netclient/netclient")
|
err = ncutils.Copy(binarypath, "/etc/netclient/netclient")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return err
|
return err
|
||||||
|
@@ -35,7 +35,7 @@ func SetupSystemDDaemon(interval string) error {
|
|||||||
os.Symlink("/etc/netclient/netclient", "/usr/local/bin/netclient")
|
os.Symlink("/etc/netclient/netclient", "/usr/local/bin/netclient")
|
||||||
}
|
}
|
||||||
if !ncutils.FileExists("/etc/netclient/netclient") {
|
if !ncutils.FileExists("/etc/netclient/netclient") {
|
||||||
_, err = ncutils.Copy(binarypath, "/etc/netclient/netclient")
|
err = ncutils.Copy(binarypath, "/etc/netclient/netclient")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return err
|
return err
|
||||||
|
@@ -1,21 +1,11 @@
|
|||||||
package wireguard
|
package wireguard
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"runtime"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gravitl/netmaker/models"
|
|
||||||
"github.com/gravitl/netmaker/netclient/config"
|
|
||||||
"github.com/gravitl/netmaker/netclient/local"
|
|
||||||
"github.com/gravitl/netmaker/netclient/ncutils"
|
"github.com/gravitl/netmaker/netclient/ncutils"
|
||||||
"github.com/gravitl/netmaker/netclient/server"
|
|
||||||
"golang.zx2c4.com/wireguard/wgctrl/internal/wguser"
|
"golang.zx2c4.com/wireguard/wgctrl/internal/wguser"
|
||||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||||
)
|
)
|
||||||
|
@@ -6,7 +6,9 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"math/rand"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"time"
|
||||||
|
|
||||||
nodepb "github.com/gravitl/netmaker/grpc"
|
nodepb "github.com/gravitl/netmaker/grpc"
|
||||||
"github.com/gravitl/netmaker/models"
|
"github.com/gravitl/netmaker/models"
|
||||||
@@ -34,6 +36,13 @@ 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.")
|
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
|
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)
|
err = config.Write(&cfg, cfg.Network)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@@ -2,7 +2,6 @@ package ncutils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"syscall"
|
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -17,7 +16,9 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.zx2c4.com/wireguard/wgctrl"
|
"golang.zx2c4.com/wireguard/wgctrl"
|
||||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
@@ -188,15 +189,19 @@ PersistentKeepAlive = %s
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateUserSpaceConf - creates a user space WireGuard conf
|
// CreateUserSpaceConf - creates a user space WireGuard conf
|
||||||
func CreateUserSpaceConf(address string, privatekey string, listenPort string, mtu int32, perskeepalive int32, peers []wgtypes.PeerConfig) (string, error) {
|
func CreateUserSpaceConf(address string, privatekey string, listenPort string, mtu int32, fwmark int32, perskeepalive int32, peers []wgtypes.PeerConfig) (string, error) {
|
||||||
peersString, err := parsePeers(perskeepalive, peers)
|
peersString, err := parsePeers(perskeepalive, peers)
|
||||||
listenPortString := ""
|
var listenPortString string
|
||||||
|
var fwmarkString string
|
||||||
if mtu <= 0 {
|
if mtu <= 0 {
|
||||||
mtu = 1280
|
mtu = 1280
|
||||||
}
|
}
|
||||||
if listenPort != "" {
|
if listenPort != "" {
|
||||||
listenPortString += "ListenPort = " + listenPort
|
listenPortString += "ListenPort = " + listenPort
|
||||||
}
|
}
|
||||||
|
if fwmark != 0 {
|
||||||
|
fwmarkString += "FWMark = " + strconv.Itoa(int(fwmark))
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -205,6 +210,7 @@ Address = %s
|
|||||||
PrivateKey = %s
|
PrivateKey = %s
|
||||||
MTU = %s
|
MTU = %s
|
||||||
%s
|
%s
|
||||||
|
%s
|
||||||
|
|
||||||
%s
|
%s
|
||||||
|
|
||||||
@@ -213,6 +219,7 @@ MTU = %s
|
|||||||
privatekey,
|
privatekey,
|
||||||
strconv.Itoa(int(mtu)),
|
strconv.Itoa(int(mtu)),
|
||||||
listenPortString,
|
listenPortString,
|
||||||
|
fwmarkString,
|
||||||
peersString)
|
peersString)
|
||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
@@ -342,44 +349,44 @@ func GRPCRequestOpts(isSecure string) grpc.DialOption {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Copy - copies a src file to dest
|
// Copy - copies a src file to dest
|
||||||
func Copy(src, dst string) (int64, error) {
|
func Copy(src, dst string) error {
|
||||||
sourceFileStat, err := os.Stat(src)
|
sourceFileStat, err := os.Stat(src)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !sourceFileStat.Mode().IsRegular() {
|
if !sourceFileStat.Mode().IsRegular() {
|
||||||
return 0, errors.New(src + " is not a regular file")
|
return errors.New(src + " is not a regular file")
|
||||||
}
|
}
|
||||||
|
|
||||||
source, err := os.Open(src)
|
source, err := os.Open(src)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return err
|
||||||
}
|
}
|
||||||
defer source.Close()
|
defer source.Close()
|
||||||
|
|
||||||
destination, err := os.Create(dst)
|
destination, err := os.Create(dst)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return err
|
||||||
}
|
}
|
||||||
defer destination.Close()
|
defer destination.Close()
|
||||||
nBytes, err := io.Copy(destination, source)
|
_, err = io.Copy(destination, source)
|
||||||
err = os.Chmod(dst, 0755)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
return err
|
||||||
}
|
}
|
||||||
return nBytes, err
|
err = os.Chmod(dst, 0755)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunCmd - runs a local command
|
// RunCmd - runs a local command
|
||||||
func RunCmd(command string, printerr bool) (string, error) {
|
func RunCmd(command string, printerr bool) (string, error) {
|
||||||
args := strings.Fields(command)
|
args := strings.Fields(command)
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
cmd := exec.Command(args[0], args[1:]...)
|
cmd := exec.Command(args[0], args[1:]...)
|
||||||
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||||
go func() {
|
go func() {
|
||||||
<- ctx.Done()
|
<-ctx.Done()
|
||||||
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
||||||
}()
|
}()
|
||||||
out, err := cmd.CombinedOutput()
|
out, err := cmd.CombinedOutput()
|
||||||
|
@@ -6,6 +6,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -30,23 +31,23 @@ func SetPeers(iface string, keepalive int32, peers []wgtypes.PeerConfig) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
client, err := wgctrl.New()
|
client, err := wgctrl.New()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ncutils.PrintLog("failed to start wgctrl", 0)
|
ncutils.PrintLog("failed to start wgctrl", 0)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
device, err := client.Device(iface)
|
device, err := client.Device(iface)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ncutils.PrintLog("failed to parse interface", 0)
|
ncutils.PrintLog("failed to parse interface", 0)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
devicePeers = device.Peers
|
devicePeers = device.Peers
|
||||||
}
|
}
|
||||||
if len(devicePeers) > 1 && len(peers) == 0 {
|
if len(devicePeers) > 1 && len(peers) == 0 {
|
||||||
ncutils.PrintLog("no peers pulled", 1)
|
ncutils.PrintLog("no peers pulled", 1)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
PEERS:
|
||||||
for _, peer := range peers {
|
for _, peer := range peers {
|
||||||
|
|
||||||
for _, currentPeer := range devicePeers {
|
for _, currentPeer := range devicePeers {
|
||||||
@@ -56,7 +57,13 @@ func SetPeers(iface string, keepalive int32, peers []wgtypes.PeerConfig) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("error removing peer", peer.Endpoint.String())
|
log.Println("error removing peer", peer.Endpoint.String())
|
||||||
}
|
}
|
||||||
|
} else if currentPeer.PublicKey.String() == peer.PublicKey.String() &&
|
||||||
|
currentPeer.Endpoint.String() == peer.Endpoint.String() &&
|
||||||
|
reflect.DeepEqual(currentPeer.AllowedIPs, peer.AllowedIPs) {
|
||||||
|
|
||||||
|
continue PEERS
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
udpendpoint := peer.Endpoint.String()
|
udpendpoint := peer.Endpoint.String()
|
||||||
var allowedips string
|
var allowedips string
|
||||||
@@ -67,7 +74,7 @@ func SetPeers(iface string, keepalive int32, peers []wgtypes.PeerConfig) error {
|
|||||||
allowedips = strings.Join(iparr, ",")
|
allowedips = strings.Join(iparr, ",")
|
||||||
keepAliveString := strconv.Itoa(int(keepalive))
|
keepAliveString := strconv.Itoa(int(keepalive))
|
||||||
if keepAliveString == "0" {
|
if keepAliveString == "0" {
|
||||||
keepAliveString = "5"
|
keepAliveString = "15"
|
||||||
}
|
}
|
||||||
if peer.Endpoint != nil {
|
if peer.Endpoint != nil {
|
||||||
_, err = ncutils.RunCmd("wg set "+iface+" peer "+peer.PublicKey.String()+
|
_, err = ncutils.RunCmd("wg set "+iface+" peer "+peer.PublicKey.String()+
|
||||||
@@ -119,7 +126,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
fwmark := modcfg.FWMark
|
||||||
nodecfg := modcfg.Node
|
nodecfg := modcfg.Node
|
||||||
servercfg := modcfg.Server
|
servercfg := modcfg.Server
|
||||||
|
|
||||||
@@ -174,9 +181,9 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
|
|||||||
if !ncutils.IsKernel() {
|
if !ncutils.IsKernel() {
|
||||||
var newConf string
|
var newConf string
|
||||||
if node.UDPHolePunch != "yes" {
|
if node.UDPHolePunch != "yes" {
|
||||||
newConf, _ = ncutils.CreateUserSpaceConf(node.Address, key.String(), strconv.FormatInt(int64(node.ListenPort), 10), node.MTU, node.PersistentKeepalive, peers)
|
newConf, _ = ncutils.CreateUserSpaceConf(node.Address, key.String(), strconv.FormatInt(int64(node.ListenPort), 10), node.MTU, fwmark, node.PersistentKeepalive, peers)
|
||||||
} else {
|
} else {
|
||||||
newConf, _ = ncutils.CreateUserSpaceConf(node.Address, key.String(), "", node.MTU, node.PersistentKeepalive, peers)
|
newConf, _ = ncutils.CreateUserSpaceConf(node.Address, key.String(), "", node.MTU, fwmark, node.PersistentKeepalive, peers)
|
||||||
}
|
}
|
||||||
confPath := ncutils.GetNetclientPathSpecific() + ifacename + ".conf"
|
confPath := ncutils.GetNetclientPathSpecific() + ifacename + ".conf"
|
||||||
ncutils.PrintLog("writing wg conf file to: "+confPath, 1)
|
ncutils.PrintLog("writing wg conf file to: "+confPath, 1)
|
||||||
|
Reference in New Issue
Block a user