netmaker/netclient/wireguard/kernel.go

331 lines
7.8 KiB
Go
Raw Normal View History

2021-05-26 00:48:04 +08:00
package wireguard
import (
2021-07-26 02:22:20 +08:00
"fmt"
"io/ioutil"
2021-05-26 00:48:04 +08:00
"log"
"os"
"os/exec"
2021-08-06 11:01:37 +08:00
"strconv"
2021-07-26 02:22:20 +08:00
"strings"
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/netclient/config"
"github.com/gravitl/netmaker/netclient/local"
"github.com/gravitl/netmaker/netclient/server"
2021-05-26 00:48:04 +08:00
"golang.zx2c4.com/wireguard/wgctrl"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
//homedir "github.com/mitchellh/go-homedir"
)
2021-07-26 02:22:20 +08:00
2021-08-03 06:06:26 +08:00
func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig, hasGateway bool, gateways []string) error {
2021-05-26 00:48:04 +08:00
ipExec, err := exec.LookPath("ip")
2021-07-26 02:22:20 +08:00
if err != nil {
2021-05-26 00:48:04 +08:00
return err
}
key, err := wgtypes.ParseKey(privkey)
2021-07-26 02:22:20 +08:00
if err != nil {
return err
}
2021-05-26 00:48:04 +08:00
2021-07-26 02:22:20 +08:00
wgclient, err := wgctrl.New()
if err != nil {
return err
}
2021-08-03 06:06:26 +08:00
modcfg, err := config.ReadConfig(node.Network)
2021-05-26 04:09:49 +08:00
if err != nil {
2021-07-26 02:22:20 +08:00
return err
}
2021-05-26 00:48:04 +08:00
nodecfg := modcfg.Node
servercfg := modcfg.Server
2021-07-26 02:22:20 +08:00
if err != nil {
log.Fatalf("failed to open client: %v", err)
}
defer wgclient.Close()
2021-05-26 00:48:04 +08:00
ifacename := node.Interface
if nodecfg.Interface != "" {
ifacename = nodecfg.Interface
} else if node.Interface != "" {
ifacename = node.Interface
2021-07-26 02:22:20 +08:00
} else {
2021-05-26 00:48:04 +08:00
log.Fatal("no interface to configure")
}
if node.Address == "" {
log.Fatal("no address to configure")
}
2021-07-16 03:14:48 +08:00
nameserver := servercfg.CoreDNSAddr
2021-08-03 06:06:26 +08:00
network := node.Network
2021-07-26 02:22:20 +08:00
if nodecfg.Network != "" {
network = nodecfg.Network
2021-08-03 06:06:26 +08:00
} else if node.Network != "" {
network = node.Network
2021-07-26 02:22:20 +08:00
}
2021-08-03 06:06:26 +08:00
_, delErr := local.RunCmd("ip link delete dev " + ifacename)
_, addLinkErr := local.RunCmd(ipExec + " link add dev " + ifacename + " type wireguard")
_, addErr := local.RunCmd(ipExec + " address add dev " + ifacename + " " + node.Address + "/24")
if delErr != nil {
2021-08-11 01:57:49 +08:00
// pass
2021-05-26 00:48:04 +08:00
}
if addLinkErr != nil {
log.Println(addLinkErr)
2021-05-26 00:48:04 +08:00
}
if addErr != nil {
log.Println(addErr)
2021-05-26 00:48:04 +08:00
}
var nodeport int
2021-08-03 06:06:26 +08:00
nodeport = int(node.ListenPort)
2021-05-26 00:48:04 +08:00
2021-07-26 02:22:20 +08:00
conf := wgtypes.Config{}
2021-08-11 00:15:30 +08:00
if nodecfg.UDPHolePunch == "yes" &&
nodecfg.IsServer == "no" &&
nodecfg.IsIngressGateway != "yes" &&
nodecfg.IsStatic != "yes" {
2021-07-26 02:22:20 +08:00
conf = wgtypes.Config{
PrivateKey: &key,
ReplacePeers: true,
Peers: peers,
}
} else {
conf = wgtypes.Config{
PrivateKey: &key,
ListenPort: &nodeport,
ReplacePeers: true,
Peers: peers,
}
2021-05-26 00:48:04 +08:00
}
_, err = wgclient.Device(ifacename)
2021-07-26 02:22:20 +08:00
if err != nil {
if os.IsNotExist(err) {
fmt.Println("Device does not exist: ")
fmt.Println(err)
} else {
log.Fatalf("Unknown config error: %v", err)
}
}
2021-05-26 00:48:04 +08:00
err = wgclient.ConfigureDevice(ifacename, conf)
if err != nil {
if os.IsNotExist(err) {
fmt.Println("Device does not exist: ")
fmt.Println(err)
} else {
fmt.Printf("This is inconvenient: %v", err)
}
}
//=========DNS Setup==========\\
2021-08-03 06:06:26 +08:00
if nodecfg.DNSOn == "yes" {
2021-06-03 12:55:39 +08:00
_ = local.UpdateDNS(ifacename, network, nameserver)
2021-05-26 00:48:04 +08:00
}
2021-07-26 02:22:20 +08:00
//=========End DNS Setup=======\\
2021-06-02 23:00:10 +08:00
2021-07-26 02:22:20 +08:00
cmdIPLinkUp := &exec.Cmd{
Path: ipExec,
Args: []string{ipExec, "link", "set", "up", "dev", ifacename},
Stdout: os.Stdout,
Stderr: os.Stdout,
}
2021-06-02 23:00:10 +08:00
2021-07-26 02:22:20 +08:00
cmdIPLinkDown := &exec.Cmd{
Path: ipExec,
Args: []string{ipExec, "link", "set", "down", "dev", ifacename},
Stdout: os.Stdout,
Stderr: os.Stdout,
}
err = cmdIPLinkDown.Run()
if nodecfg.PostDown != "" {
2021-05-26 00:48:04 +08:00
runcmds := strings.Split(nodecfg.PostDown, "; ")
err = local.RunCmds(runcmds)
if err != nil {
fmt.Println("Error encountered running PostDown: " + err.Error())
}
}
err = cmdIPLinkUp.Run()
2021-07-26 02:22:20 +08:00
if err != nil {
return err
}
2021-05-26 00:48:04 +08:00
if nodecfg.PostUp != "" {
2021-07-26 02:22:20 +08:00
runcmds := strings.Split(nodecfg.PostUp, "; ")
err = local.RunCmds(runcmds)
2021-05-26 00:48:04 +08:00
if err != nil {
2021-07-26 02:22:20 +08:00
fmt.Println("Error encountered running PostUp: " + err.Error())
}
}
if hasGateway {
for _, gateway := range gateways {
2021-08-03 06:06:26 +08:00
out, err := local.RunCmd(ipExec + " -4 route add " + gateway + " dev " + ifacename)
2021-07-26 02:22:20 +08:00
fmt.Println(string(out))
if err != nil {
fmt.Println("Error encountered adding gateway: " + err.Error())
}
2021-05-26 00:48:04 +08:00
}
}
2021-08-03 06:06:26 +08:00
if node.Address6 != "" && node.IsDualStack == "yes" {
2021-05-26 00:48:04 +08:00
fmt.Println("Adding address: " + node.Address6)
2021-08-03 06:06:26 +08:00
out, err := local.RunCmd(ipExec + " address add dev " + ifacename + " " + node.Address6 + "/64")
2021-07-26 02:22:20 +08:00
if err != nil {
fmt.Println(out)
fmt.Println("Error encountered adding ipv6: " + err.Error())
}
2021-05-26 00:48:04 +08:00
}
2021-06-02 23:00:10 +08:00
2021-05-26 00:48:04 +08:00
return err
}
func SetWGKeyConfig(network string, serveraddr string) error {
2021-07-26 02:22:20 +08:00
cfg, err := config.ReadConfig(network)
if err != nil {
return err
}
2021-07-11 22:18:31 +08:00
2021-08-06 09:26:10 +08:00
node := cfg.Node
2021-05-26 00:48:04 +08:00
privatekey, err := wgtypes.GeneratePrivateKey()
if err != nil {
return err
}
privkeystring := privatekey.String()
publickey := privatekey.PublicKey()
2021-08-03 06:06:26 +08:00
node.PublicKey = publickey.String()
2021-05-26 00:48:04 +08:00
err = StorePrivKey(privkeystring, network)
2021-07-26 02:22:20 +08:00
if err != nil {
return err
}
2021-08-06 08:53:47 +08:00
if node.Action == models.NODE_UPDATE_KEY {
node.Action = models.NODE_NOOP
}
2021-07-26 02:22:20 +08:00
err = config.ModConfig(&node)
if err != nil {
return err
}
2021-05-26 00:48:04 +08:00
2021-07-29 11:08:00 +08:00
err = SetWGConfig(network, false)
2021-07-26 02:22:20 +08:00
if err != nil {
return err
}
2021-05-26 00:48:04 +08:00
2021-07-26 02:22:20 +08:00
return err
}
2021-05-26 00:48:04 +08:00
2021-07-29 11:08:00 +08:00
func SetWGConfig(network string, peerupdate bool) error {
2021-05-26 00:48:04 +08:00
2021-07-26 02:22:20 +08:00
cfg, err := config.ReadConfig(network)
if err != nil {
return err
}
2021-05-26 00:48:04 +08:00
servercfg := cfg.Server
2021-07-26 02:22:20 +08:00
nodecfg := cfg.Node
2021-05-26 00:48:04 +08:00
2021-08-06 09:26:10 +08:00
peers, hasGateway, gateways, err := server.GetPeers(nodecfg.MacAddress, nodecfg.Network, servercfg.GRPCAddress, nodecfg.IsDualStack == "yes", nodecfg.IsIngressGateway == "yes")
2021-07-26 02:22:20 +08:00
if err != nil {
return err
}
2021-05-26 00:48:04 +08:00
privkey, err := RetrievePrivKey(network)
2021-07-26 02:22:20 +08:00
if err != nil {
return err
}
2021-08-03 00:34:25 +08:00
if peerupdate {
2021-08-06 09:26:10 +08:00
SetPeers(nodecfg.Interface, nodecfg.PersistentKeepalive, peers)
2021-07-29 11:08:00 +08:00
} else {
2021-08-06 09:26:10 +08:00
err = InitWireguard(&nodecfg, privkey, peers, hasGateway, gateways)
2021-07-29 11:08:00 +08:00
}
2021-07-26 02:22:20 +08:00
if err != nil {
return err
}
2021-05-26 00:48:04 +08:00
return err
}
2021-07-29 13:03:57 +08:00
func SetPeers(iface string, keepalive int32, peers []wgtypes.PeerConfig) {
2021-08-06 23:55:04 +08:00
2021-07-29 13:03:57 +08:00
client, err := wgctrl.New()
if err != nil {
log.Println("failed to start wgctrl")
return
}
device, err := client.Device(iface)
if err != nil {
log.Println("failed to parse interface")
return
}
2021-08-06 23:55:04 +08:00
devicePeers := device.Peers
if len(devicePeers) > 1 && len(peers) == 0 {
log.Println("no peers pulled")
return
}
2021-07-29 11:08:00 +08:00
for _, peer := range peers {
2021-08-03 06:06:26 +08:00
2021-08-06 23:55:04 +08:00
for _, currentPeer := range devicePeers {
2021-08-03 06:06:26 +08:00
if currentPeer.AllowedIPs[0].String() == peer.AllowedIPs[0].String() &&
2021-08-06 10:35:23 +08:00
currentPeer.PublicKey.String() != peer.PublicKey.String() {
2021-08-06 11:01:37 +08:00
_, err := local.RunCmd("wg set " + iface + " peer " + currentPeer.PublicKey.String() + " remove")
2021-08-03 06:06:26 +08:00
if err != nil {
2021-08-06 11:01:37 +08:00
log.Println("error removing peer", peer.Endpoint.String())
2021-08-03 06:06:26 +08:00
}
2021-07-29 13:03:57 +08:00
}
2021-08-03 06:06:26 +08:00
}
2021-08-06 09:03:00 +08:00
udpendpoint := peer.Endpoint.String()
2021-07-29 13:03:57 +08:00
var allowedips string
var iparr []string
for _, ipaddr := range peer.AllowedIPs {
2021-08-03 06:06:26 +08:00
iparr = append(iparr, ipaddr.String())
2021-07-29 13:03:57 +08:00
}
2021-08-03 06:06:26 +08:00
allowedips = strings.Join(iparr, ",")
2021-08-06 11:01:37 +08:00
keepAliveString := strconv.Itoa(int(keepalive))
if keepAliveString == "0" {
keepAliveString = "5"
}
if peer.Endpoint != nil {
_, err = local.RunCmd("wg set " + iface + " peer " + peer.PublicKey.String() +
" endpoint " + udpendpoint +
" persistent-keepalive " + keepAliveString +
" allowed-ips " + allowedips)
} else {
_, err = local.RunCmd("wg set " + iface + " peer " + peer.PublicKey.String() +
" persistent-keepalive " + keepAliveString +
" allowed-ips " + allowedips)
}
2021-07-29 11:08:00 +08:00
if err != nil {
2021-08-06 11:02:34 +08:00
log.Println("error setting peer", peer.PublicKey.String(), err)
2021-07-29 11:08:00 +08:00
}
}
2021-08-06 23:55:04 +08:00
for _, currentPeer := range devicePeers {
shouldDelete := true
for _, peer := range peers {
if peer.AllowedIPs[0].String() == currentPeer.AllowedIPs[0].String() {
shouldDelete = false
}
}
if shouldDelete {
_, err := local.RunCmd("wg set " + iface + " peer " + currentPeer.PublicKey.String() + " remove")
if err != nil {
log.Println("error removing peer", currentPeer.PublicKey.String())
} else {
log.Println("removed peer " + currentPeer.PublicKey.String())
}
}
}
2021-07-29 11:08:00 +08:00
}
2021-07-26 02:22:20 +08:00
func StorePrivKey(key string, network string) error {
2021-05-26 00:48:04 +08:00
d1 := []byte(key)
2021-07-26 02:22:20 +08:00
err := ioutil.WriteFile("/etc/netclient/wgkey-"+network, d1, 0644)
2021-05-26 00:48:04 +08:00
return err
}
func RetrievePrivKey(network string) (string, error) {
dat, err := ioutil.ReadFile("/etc/netclient/wgkey-" + network)
return string(dat), err
}