|
@@ -16,6 +16,7 @@ import (
|
|
|
"github.com/gravitl/netmaker/netclient/server"
|
|
|
"golang.zx2c4.com/wireguard/wgctrl"
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
+ "gopkg.in/ini.v1"
|
|
|
)
|
|
|
|
|
|
// SetPeers - sets peers on a given WireGuard interface
|
|
@@ -289,3 +290,125 @@ func ApplyConf(node models.Node, ifacename string, confPath string) error {
|
|
|
}
|
|
|
return err
|
|
|
}
|
|
|
+
|
|
|
+// WriteWgConfig - creates a wireguard config file
|
|
|
+func WriteWgConfig(cfg config.ClientConfig, privateKey string, peers []wgtypes.Peer) error {
|
|
|
+ options := ini.LoadOptions{
|
|
|
+ AllowNonUniqueSections: true,
|
|
|
+ AllowShadows: true,
|
|
|
+ }
|
|
|
+ wireguard := ini.Empty(options)
|
|
|
+ wireguard.Section("Interface").Key("PrivateKey").SetValue(privateKey)
|
|
|
+ wireguard.Section("Interface").Key("ListenPort").SetValue(strconv.Itoa(int(cfg.Node.ListenPort)))
|
|
|
+ if cfg.Node.Address != "" {
|
|
|
+ wireguard.Section("Interface").Key("Address").SetValue(cfg.Node.Address)
|
|
|
+ }
|
|
|
+ if cfg.Node.Address6 != "" {
|
|
|
+ wireguard.Section("Interface").Key("Address").SetValue(cfg.Node.Address6)
|
|
|
+ }
|
|
|
+ if cfg.Node.DNSOn == "yes" {
|
|
|
+ wireguard.Section("Interface").Key("DNS").SetValue(cfg.Server.CoreDNSAddr)
|
|
|
+ }
|
|
|
+ if cfg.Node.PostUp != "" {
|
|
|
+ wireguard.Section("Interface").Key("PostUp").SetValue(cfg.Node.PostUp)
|
|
|
+ }
|
|
|
+ if cfg.Node.PostDown != "" {
|
|
|
+ wireguard.Section("Interface").Key("PostDown").SetValue(cfg.Node.PostDown)
|
|
|
+ }
|
|
|
+ for i, peer := range peers {
|
|
|
+ wireguard.SectionWithIndex("Peer", i).Key("PublicKey").SetValue(peer.PublicKey.String())
|
|
|
+ if peer.PresharedKey.String() != "" {
|
|
|
+ wireguard.SectionWithIndex("Peer", i).Key("PreSharedKey").SetValue(peer.PresharedKey.String())
|
|
|
+ }
|
|
|
+ if peer.AllowedIPs != nil {
|
|
|
+ var allowedIPs string
|
|
|
+ for _, ip := range peer.AllowedIPs {
|
|
|
+ allowedIPs = allowedIPs + ", " + ip.String()
|
|
|
+ }
|
|
|
+ wireguard.SectionWithIndex("Peer", i).Key("AllowedIps").SetValue(allowedIPs)
|
|
|
+ }
|
|
|
+ if peer.Endpoint != nil {
|
|
|
+ wireguard.SectionWithIndex("Peer", i).Key("Endpoint").SetValue(peer.Endpoint.String())
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if err := wireguard.SaveTo("/etc/netclient/config" + cfg.Node.Interface + ".conf"); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// UpdateWgPeers - updates the peers of a network
|
|
|
+func UpdateWgPeers(wgInterface string, peers []wgtypes.Peer) error {
|
|
|
+ //update to get path properly
|
|
|
+ file := "/etc/netclient/config/" + wgInterface + ".conf"
|
|
|
+ wireguard, err := ini.ShadowLoad(file)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ for i, peer := range peers {
|
|
|
+ wireguard.SectionWithIndex("Peer", i).Key("PublicKey").SetValue(peer.PublicKey.String())
|
|
|
+ if peer.PresharedKey.String() != "" {
|
|
|
+ wireguard.SectionWithIndex("Peer", i).Key("PreSharedKey").SetValue(peer.PresharedKey.String())
|
|
|
+ }
|
|
|
+ if peer.AllowedIPs != nil {
|
|
|
+ var allowedIPs string
|
|
|
+ for _, ip := range peer.AllowedIPs {
|
|
|
+ allowedIPs = allowedIPs + ", " + ip.String()
|
|
|
+ }
|
|
|
+ wireguard.SectionWithIndex("Peer", i).Key("AllowedIps").SetValue(allowedIPs)
|
|
|
+ }
|
|
|
+ if peer.Endpoint != nil {
|
|
|
+ wireguard.SectionWithIndex("Peer", i).Key("Endpoint").SetValue(peer.Endpoint.String())
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if err := wireguard.SaveTo(file); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// UpdateWgInterface - updates the interface section of a wireguard config file
|
|
|
+func UpdateWgInterface(wgInterface, privateKey, nameserver string, node models.Node) error {
|
|
|
+ //update to get path properly
|
|
|
+ file := "/etc/netclient/config/" + wgInterface + ".conf"
|
|
|
+ wireguard, err := ini.ShadowLoad(file)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ wireguard.Section("Interface").Key("PrivateKey").SetValue(privateKey)
|
|
|
+ wireguard.Section("Interface").Key("ListenPort").SetValue(strconv.Itoa(int(node.ListenPort)))
|
|
|
+ if node.Address != "" {
|
|
|
+ wireguard.Section("Interface").Key("Address").SetValue(node.Address)
|
|
|
+ }
|
|
|
+ if node.Address6 != "" {
|
|
|
+ wireguard.Section("Interface").Key("Address").SetValue(node.Address6)
|
|
|
+ }
|
|
|
+ if node.DNSOn == "yes" {
|
|
|
+ wireguard.Section("Interface").Key("DNS").SetValue(nameserver)
|
|
|
+ }
|
|
|
+ if node.PostUp != "" {
|
|
|
+ wireguard.Section("Interface").Key("PostUp").SetValue(node.PostUp)
|
|
|
+ }
|
|
|
+ if node.PostDown != "" {
|
|
|
+ wireguard.Section("Interface").Key("PostDown").SetValue(node.PostDown)
|
|
|
+ }
|
|
|
+ if err := wireguard.SaveTo(file); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// UpdatePrivateKey - updates the private key of a wireguard config file
|
|
|
+func UpdatePrivateKey(wgInterface, privateKey string) error {
|
|
|
+ //update to get path properly
|
|
|
+ file := "/etc/netclient/config/" + wgInterface + ".conf"
|
|
|
+ wireguard, err := ini.ShadowLoad(file)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ wireguard.Section("Interface").Key("PrivateKey").SetValue(privateKey)
|
|
|
+ if err := wireguard.SaveTo(file); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|