fixed udp ports?

This commit is contained in:
worker-9 2021-08-05 22:35:23 -04:00
parent 27de1580e9
commit 01e9d1efb4
3 changed files with 51 additions and 165 deletions

View file

@ -203,13 +203,13 @@ func Pull(network string, manual bool) (*models.Node, error) {
log.Println("could not delete old interface", cfg.Node.Interface) log.Println("could not delete old interface", cfg.Node.Interface)
} }
} }
resNode.PullChanges = "no"
if err = config.ModConfig(&resNode); err != nil { if err = config.ModConfig(&resNode); err != nil {
return nil, err return nil, err
} }
if err = wireguard.SetWGConfig(network, false); err != nil { if err = wireguard.SetWGConfig(network, false); err != nil {
return nil, err return nil, err
} }
resNode.PullChanges = "no"
nodeData, err := json.Marshal(&resNode) nodeData, err := json.Marshal(&resNode)
if err != nil { if err != nil {
return &resNode, err return &resNode, err

View file

@ -1,20 +1,20 @@
package functions package functions
import ( import (
"time"
"os"
"net"
"log"
"io/ioutil"
"bytes" "bytes"
"encoding/json"
"errors"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"time"
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/netclient/config" "github.com/gravitl/netmaker/netclient/config"
"github.com/gravitl/netmaker/netclient/local" "github.com/gravitl/netmaker/netclient/local"
"github.com/gravitl/netmaker/netclient/wireguard" // "github.com/davecgh/go-spew/spew"
"github.com/gravitl/netmaker/models"
"encoding/json"
"net/http"
"errors"
// "github.com/davecgh/go-spew/spew"
) )
func Register(cfg config.GlobalConfig) error { func Register(cfg config.GlobalConfig) error {
@ -44,13 +44,13 @@ func Register(cfg config.GlobalConfig) error {
body := bytes.NewBuffer(jsonbytes) body := bytes.NewBuffer(jsonbytes)
publicaddress := net.JoinHostPort(cfg.Client.ServerPublicEndpoint, cfg.Client.ServerAPIPort) publicaddress := net.JoinHostPort(cfg.Client.ServerPublicEndpoint, cfg.Client.ServerAPIPort)
res, err := http.Post("http://"+publicaddress+"/api/intclient/register","application/json",body) res, err := http.Post("http://"+publicaddress+"/api/intclient/register", "application/json", body)
if err != nil { if err != nil {
log.Println("Failed to register to http://"+publicaddress+"/api/client/register") log.Println("Failed to register to http://" + publicaddress + "/api/client/register")
return err return err
} }
if res.StatusCode != http.StatusOK { if res.StatusCode != http.StatusOK {
log.Println("Failed to register to http://"+publicaddress+"/api/client/register") log.Println("Failed to register to http://" + publicaddress + "/api/client/register")
return errors.New("request to server failed: " + res.Status) return errors.New("request to server failed: " + res.Status)
} }
bodyBytes, err := ioutil.ReadAll(res.Body) bodyBytes, err := ioutil.ReadAll(res.Body)
@ -67,25 +67,25 @@ func Register(cfg config.GlobalConfig) error {
return err return err
} }
//spew.Dump(wgclient) //spew.Dump(wgclient)
err = wireguard.InitGRPCWireguard(wgclient) // err = wireguard.InitGRPCWireguard(wgclient)
if err != nil { // if err != nil {
return err // return err
} // }
log.Println("registered netclient to " + cfg.Client.ServerPrivateAddress) log.Println("registered netclient to " + cfg.Client.ServerPrivateAddress)
return err return err
} }
func Unregister(cfg config.GlobalConfig) error { func Unregister(cfg config.GlobalConfig) error {
client := &http.Client{ Timeout: 7 * time.Second,} client := &http.Client{Timeout: 7 * time.Second}
publicaddress := net.JoinHostPort(cfg.Client.ServerPublicEndpoint, cfg.Client.ServerAPIPort) publicaddress := net.JoinHostPort(cfg.Client.ServerPublicEndpoint, cfg.Client.ServerAPIPort)
log.Println("sending delete request to: " + "http://"+publicaddress+"/api/intclient/"+cfg.Client.ClientID) log.Println("sending delete request to: " + "http://" + publicaddress + "/api/intclient/" + cfg.Client.ClientID)
req, err := http.NewRequest("DELETE", "http://"+publicaddress+"/api/intclient/"+cfg.Client.ClientID, nil) req, err := http.NewRequest("DELETE", "http://"+publicaddress+"/api/intclient/"+cfg.Client.ClientID, nil)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} else { } else {
res, err := client.Do(req) res, err := client.Do(req)
if res == nil { if res == nil {
err = errors.New("server not reachable at " + "http://"+publicaddress+"/api/intclient/"+cfg.Client.ClientID) err = errors.New("server not reachable at " + "http://" + publicaddress + "/api/intclient/" + cfg.Client.ClientID)
log.Println(err) log.Println(err)
} else if res.StatusCode != http.StatusOK { } else if res.StatusCode != http.StatusOK {
err = errors.New("request to server failed: " + res.Status) err = errors.New("request to server failed: " + res.Status)

View file

@ -1,14 +1,11 @@
package wireguard package wireguard
import ( import (
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"net"
"os" "os"
"os/exec" "os/exec"
"strconv"
"strings" "strings"
"github.com/gravitl/netmaker/models" "github.com/gravitl/netmaker/models"
@ -20,117 +17,6 @@ import (
//homedir "github.com/mitchellh/go-homedir" //homedir "github.com/mitchellh/go-homedir"
) )
func InitGRPCWireguard(client models.IntClient) error {
key, err := wgtypes.ParseKey(client.PrivateKey)
if err != nil {
return err
}
serverkey, err := wgtypes.ParseKey(client.ServerKey)
if err != nil {
return err
}
serverport, err := strconv.Atoi(client.ServerWGPort)
if err != nil {
return err
}
wgclient, err := wgctrl.New()
if err != nil {
log.Fatalf("failed to open client: %v", err)
}
defer wgclient.Close()
ifacename := "grpc-wg-001"
if client.Address6 == "" && client.Address == "" {
return errors.New("no address to configure")
}
currentiface, err := net.InterfaceByName(ifacename)
if err != nil {
_, err = local.RunCmd("ip link add dev " + ifacename + " type wireguard")
if err != nil && !strings.Contains(err.Error(), "exists") {
log.Println("Error creating interface")
}
}
match := false
match6 := false
addrs, _ := currentiface.Addrs()
//Add IPv4Address (make into separate function)
for _, a := range addrs {
if strings.Contains(a.String(), client.Address) {
match = true
}
if strings.Contains(a.String(), client.Address6) {
match6 = true
}
}
if !match && client.Address != "" {
_, err = local.RunCmd("ip address add dev " + ifacename + " " + client.Address + "/24")
if err != nil {
log.Println("Error adding ipv4 address")
fmt.Println(err)
}
}
if !match6 && client.Address6 != "" {
_, err = local.RunCmd("ip address add dev" + ifacename + " " + client.Address6 + "/64")
if err != nil {
log.Println("Error adding ipv6 address")
fmt.Println(err)
}
}
var peers []wgtypes.PeerConfig
var peeraddr = net.IPNet{
IP: net.ParseIP(client.ServerPrivateAddress),
Mask: net.CIDRMask(32, 32),
}
var allowedips []net.IPNet
allowedips = append(allowedips, peeraddr)
net.ParseIP(client.ServerPublicEndpoint)
peer := wgtypes.PeerConfig{
PublicKey: serverkey,
Endpoint: &net.UDPAddr{
IP: net.ParseIP(client.ServerPublicEndpoint),
Port: serverport,
},
ReplaceAllowedIPs: true,
AllowedIPs: allowedips,
}
peers = append(peers, peer)
conf := wgtypes.Config{
PrivateKey: &key,
ReplacePeers: true,
Peers: peers,
}
_, err = wgclient.Device(ifacename)
if err != nil {
if os.IsNotExist(err) {
log.Println("Device does not exist: ")
log.Println(err)
} else {
return err
}
}
err = wgclient.ConfigureDevice(ifacename, conf)
if err != nil {
if os.IsNotExist(err) {
log.Println("Device does not exist: ")
log.Println(err)
} else {
log.Printf("This is inconvenient: %v", err)
}
}
_, err = local.RunCmd("ip link set up dev " + ifacename)
_, err = local.RunCmd("ip link set down dev " + ifacename)
if err != nil {
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) error {
ipExec, err := exec.LookPath("ip") ipExec, err := exec.LookPath("ip")
@ -370,7 +256,7 @@ func SetPeers(iface string, keepalive int32, peers []wgtypes.PeerConfig) {
for _, currentPeer := range device.Peers { for _, currentPeer := range device.Peers {
if currentPeer.AllowedIPs[0].String() == peer.AllowedIPs[0].String() && if currentPeer.AllowedIPs[0].String() == peer.AllowedIPs[0].String() &&
currentPeer.PublicKey.String() == peer.PublicKey.String() { currentPeer.PublicKey.String() != peer.PublicKey.String() {
_, err := local.RunCmd("wg set " + iface + " peer " + currentPeer.PublicKey.String() + " delete") _, err := local.RunCmd("wg set " + iface + " peer " + currentPeer.PublicKey.String() + " delete")
if err != nil { if err != nil {
log.Println("error setting peer", peer.Endpoint.String()) log.Println("error setting peer", peer.Endpoint.String())