mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-19 15:15:17 +08:00
added udp holepunching
This commit is contained in:
@@ -3,12 +3,16 @@ package controller
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gravitl/netmaker/database"
|
"github.com/gravitl/netmaker/database"
|
||||||
"github.com/gravitl/netmaker/functions"
|
"github.com/gravitl/netmaker/functions"
|
||||||
"github.com/gravitl/netmaker/models"
|
"github.com/gravitl/netmaker/models"
|
||||||
"github.com/gravitl/netmaker/servercfg"
|
"github.com/gravitl/netmaker/servercfg"
|
||||||
|
"github.com/gravitl/netmaker/serverctl"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,11 +20,10 @@ func GetPeersList(networkName string) ([]models.PeersResponse, error) {
|
|||||||
|
|
||||||
var peers []models.PeersResponse
|
var peers []models.PeersResponse
|
||||||
collection, err := database.FetchRecords(database.NODES_TABLE_NAME)
|
collection, err := database.FetchRecords(database.NODES_TABLE_NAME)
|
||||||
|
udppeers, errN := serverctl.GetPeers(networkName)
|
||||||
if err != nil {
|
if errN != nil {
|
||||||
return peers, err
|
log.Println(errN)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, value := range collection {
|
for _, value := range collection {
|
||||||
var node models.Node
|
var node models.Node
|
||||||
var peer models.PeersResponse
|
var peer models.PeersResponse
|
||||||
@@ -33,9 +36,23 @@ func GetPeersList(networkName string) ([]models.PeersResponse, error) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if node.Network == networkName && !node.IsPending {
|
if node.Network == networkName && !node.IsPending {
|
||||||
|
if node.UDPHolePunch == "yes" && errN == nil {
|
||||||
|
endpointstring := udppeers[peer.PublicKey]
|
||||||
|
endpointarr := strings.Split(endpointstring, ":")
|
||||||
|
if len(endpointarr) == 2 {
|
||||||
|
port, err := strconv.Atoi(endpointarr[1])
|
||||||
|
if err == nil {
|
||||||
|
peer.Endpoint = endpointarr[0]
|
||||||
|
peer.ListenPort = int32(port)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
peers = append(peers, peer)
|
peers = append(peers, peer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if err != nil {
|
||||||
|
return peers, err
|
||||||
|
}
|
||||||
|
|
||||||
return peers, err
|
return peers, err
|
||||||
}
|
}
|
||||||
|
@@ -59,63 +59,12 @@ func (s *NodeServiceServer) ReadNode(ctx context.Context, req *nodepb.ReadNodeRe
|
|||||||
Islocal: network.IsLocal == "yes",
|
Islocal: network.IsLocal == "yes",
|
||||||
Isdualstack: network.IsDualStack == "yes",
|
Isdualstack: network.IsDualStack == "yes",
|
||||||
Localrange: network.LocalRange,
|
Localrange: network.LocalRange,
|
||||||
|
Udpholepunch: node.UDPHolePunch,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
func (s *NodeServiceServer) GetConn(ctx context.Context, data *nodepb.Client) (*nodepb.Client, error) {
|
|
||||||
// Get the protobuf node type from the protobuf request type
|
|
||||||
// Essentially doing req.Node to access the struct with a nil check
|
|
||||||
// Now we have to convert this into a NodeItem type to convert into BSON
|
|
||||||
clientreq := models.IntClient{
|
|
||||||
// ID: primitive.NilObjectID,
|
|
||||||
Address: data.GetAddress(),
|
|
||||||
Address6: data.GetAddress6(),
|
|
||||||
AccessKey: data.GetAccesskey(),
|
|
||||||
PublicKey: data.GetPublickey(),
|
|
||||||
PrivateKey: data.GetPrivatekey(),
|
|
||||||
ServerPort: data.GetServerport(),
|
|
||||||
ServerKey: data.GetServerkey(),
|
|
||||||
ServerWGEndpoint: data.GetServerwgendpoint(),
|
|
||||||
}
|
|
||||||
|
|
||||||
//Check to see if key is valid
|
|
||||||
//TODO: Triple inefficient!!! This is the third call to the DB we make for networks
|
|
||||||
if servercfg.IsRegisterKeyRequired() {
|
|
||||||
validKey := functions.IsKeyValidGlobal(clientreq.AccessKey)
|
|
||||||
if !validKey {
|
|
||||||
return nil, status.Errorf(
|
|
||||||
codes.Internal,
|
|
||||||
fmt.Sprintf("Invalid key, and server does not allow no-key signups"),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
client, err := RegisterIntClient(clientreq)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
// return internal gRPC error to be handled later
|
|
||||||
return nil, status.Errorf(
|
|
||||||
codes.Internal,
|
|
||||||
fmt.Sprintf("Internal error: %v", err),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
// return the node in a CreateNodeRes type
|
|
||||||
response := &nodepb.Client{
|
|
||||||
Privatekey: client.PrivateKey,
|
|
||||||
Publickey: client.PublicKey,
|
|
||||||
Accesskey: client.AccessKey,
|
|
||||||
Address: client.Address,
|
|
||||||
Address6: client.Address6,
|
|
||||||
Serverwgendpoint: client.ServerWGEndpoint,
|
|
||||||
Serverport: client.ServerPort,
|
|
||||||
Serverkey: client.ServerKey,
|
|
||||||
}
|
|
||||||
|
|
||||||
return response, nil
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
func (s *NodeServiceServer) CreateNode(ctx context.Context, req *nodepb.CreateNodeReq) (*nodepb.CreateNodeRes, error) {
|
func (s *NodeServiceServer) CreateNode(ctx context.Context, req *nodepb.CreateNodeReq) (*nodepb.CreateNodeRes, error) {
|
||||||
// Get the protobuf node type from the protobuf request type
|
// Get the protobuf node type from the protobuf request type
|
||||||
// Essentially doing req.Node to access the struct with a nil check
|
// Essentially doing req.Node to access the struct with a nil check
|
||||||
@@ -137,6 +86,7 @@ func (s *NodeServiceServer) CreateNode(ctx context.Context, req *nodepb.CreateNo
|
|||||||
IsPending: data.GetIspending(),
|
IsPending: data.GetIspending(),
|
||||||
PublicKey: data.GetPublickey(),
|
PublicKey: data.GetPublickey(),
|
||||||
ListenPort: data.GetListenport(),
|
ListenPort: data.GetListenport(),
|
||||||
|
UDPHolePunch: data.GetUdpholepunch(),
|
||||||
}
|
}
|
||||||
|
|
||||||
err := node.Validate(false)
|
err := node.Validate(false)
|
||||||
@@ -197,6 +147,7 @@ func (s *NodeServiceServer) CreateNode(ctx context.Context, req *nodepb.CreateNo
|
|||||||
Islocal: network.IsLocal == "yes",
|
Islocal: network.IsLocal == "yes",
|
||||||
Isdualstack: network.IsDualStack == "yes",
|
Isdualstack: network.IsDualStack == "yes",
|
||||||
Localrange: network.LocalRange,
|
Localrange: network.LocalRange,
|
||||||
|
Udpholepunch: node.UDPHolePunch,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
err = SetNetworkNodesLastModified(node.Network)
|
err = SetNetworkNodesLastModified(node.Network)
|
||||||
@@ -225,6 +176,7 @@ func (s *NodeServiceServer) CheckIn(ctx context.Context, req *nodepb.CheckInReq)
|
|||||||
ListenPort: data.GetListenport(),
|
ListenPort: data.GetListenport(),
|
||||||
PersistentKeepalive: data.GetKeepalive(),
|
PersistentKeepalive: data.GetKeepalive(),
|
||||||
PublicKey: data.GetPublickey(),
|
PublicKey: data.GetPublickey(),
|
||||||
|
UDPHolePunch: data.GetUdpholepunch(),
|
||||||
}
|
}
|
||||||
|
|
||||||
checkinresponse, err := NodeCheckIn(node, node.Network)
|
checkinresponse, err := NodeCheckIn(node, node.Network)
|
||||||
@@ -274,6 +226,7 @@ func (s *NodeServiceServer) UpdateNode(ctx context.Context, req *nodepb.UpdateNo
|
|||||||
IsPending: data.GetIspending(),
|
IsPending: data.GetIspending(),
|
||||||
PublicKey: data.GetPublickey(),
|
PublicKey: data.GetPublickey(),
|
||||||
ListenPort: data.GetListenport(),
|
ListenPort: data.GetListenport(),
|
||||||
|
UDPHolePunch: data.GetUdpholepunch(),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert the Id string to a MongoDB ObjectId
|
// Convert the Id string to a MongoDB ObjectId
|
||||||
@@ -323,6 +276,7 @@ func (s *NodeServiceServer) UpdateNode(ctx context.Context, req *nodepb.UpdateNo
|
|||||||
Islocal: network.IsLocal == "yes",
|
Islocal: network.IsLocal == "yes",
|
||||||
Isdualstack: network.IsDualStack == "yes",
|
Isdualstack: network.IsDualStack == "yes",
|
||||||
Localrange: network.LocalRange,
|
Localrange: network.LocalRange,
|
||||||
|
Udpholepunch: newnode.UDPHolePunch,
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
1
go.mod
1
go.mod
@@ -3,6 +3,7 @@ module github.com/gravitl/netmaker
|
|||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/aws/aws-sdk-go v1.34.28
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
||||||
github.com/go-playground/validator/v10 v10.5.0
|
github.com/go-playground/validator/v10 v10.5.0
|
||||||
|
2392
grpc/node.pb.go
2392
grpc/node.pb.go
File diff suppressed because it is too large
Load Diff
@@ -36,7 +36,7 @@ message Node {
|
|||||||
string postup = 11;
|
string postup = 11;
|
||||||
string postdown = 12;
|
string postdown = 12;
|
||||||
int32 keepalive = 13;
|
int32 keepalive = 13;
|
||||||
bool saveconfig = 14;
|
string saveconfig = 14;
|
||||||
string accesskey = 15;
|
string accesskey = 15;
|
||||||
string interface = 16;
|
string interface = 16;
|
||||||
string lastcheckin = 17;
|
string lastcheckin = 17;
|
||||||
@@ -51,6 +51,7 @@ message Node {
|
|||||||
bool isdualstack = 27;
|
bool isdualstack = 27;
|
||||||
bool dnsoff = 24;
|
bool dnsoff = 24;
|
||||||
string localrange = 25;
|
string localrange = 25;
|
||||||
|
string udpholepunch = 30;
|
||||||
}
|
}
|
||||||
|
|
||||||
message CheckInResponse {
|
message CheckInResponse {
|
||||||
|
@@ -2,17 +2,19 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
//"github.com/davecgh/go-spew/spew"
|
//"github.com/davecgh/go-spew/spew"
|
||||||
"github.com/urfave/cli/v2"
|
|
||||||
"os"
|
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"encoding/json"
|
"os"
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
nodepb "github.com/gravitl/netmaker/grpc"
|
nodepb "github.com/gravitl/netmaker/grpc"
|
||||||
"github.com/gravitl/netmaker/models"
|
"github.com/gravitl/netmaker/models"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GlobalConfig struct {
|
type GlobalConfig struct {
|
||||||
GRPCWireGuard string `yaml:"grpcwg"`
|
GRPCWireGuard string `yaml:"grpcwg"`
|
||||||
Client models.IntClient
|
Client models.IntClient
|
||||||
@@ -69,6 +71,7 @@ type NodeConfig struct {
|
|||||||
StaticIP string `yaml:"staticip"`
|
StaticIP string `yaml:"staticip"`
|
||||||
StaticPubKey string `yaml:"staticpubkey"`
|
StaticPubKey string `yaml:"staticpubkey"`
|
||||||
IPForwarding string `yaml:"ipforwarding"`
|
IPForwarding string `yaml:"ipforwarding"`
|
||||||
|
UDPHolePunch string `yaml:"udpholepunch"`
|
||||||
}
|
}
|
||||||
|
|
||||||
//reading in the env file
|
//reading in the env file
|
||||||
@@ -95,6 +98,7 @@ func Write(config *ClientConfig, network string) error{
|
|||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
//reading in the env file
|
//reading in the env file
|
||||||
func WriteGlobal(config *GlobalConfig) error {
|
func WriteGlobal(config *GlobalConfig) error {
|
||||||
_, err := os.Stat("/etc/netclient")
|
_, err := os.Stat("/etc/netclient")
|
||||||
@@ -201,8 +205,6 @@ func WriteServer(server string, accesskey string, network string) error{
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func (config *ClientConfig) ReadConfig() {
|
func (config *ClientConfig) ReadConfig() {
|
||||||
|
|
||||||
nofile := false
|
nofile := false
|
||||||
@@ -282,13 +284,10 @@ func ModGlobalConfig(cfg models.IntClient) error{
|
|||||||
if cfg.ClientID != "" {
|
if cfg.ClientID != "" {
|
||||||
modconfig.Client.ClientID = cfg.ClientID
|
modconfig.Client.ClientID = cfg.ClientID
|
||||||
}
|
}
|
||||||
|
|
||||||
err = WriteGlobal(&modconfig)
|
err = WriteGlobal(&modconfig)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func ModConfig(node *nodepb.Node) error {
|
func ModConfig(node *nodepb.Node) error {
|
||||||
network := node.Nodenetwork
|
network := node.Nodenetwork
|
||||||
if network == "" {
|
if network == "" {
|
||||||
@@ -355,6 +354,9 @@ func ModConfig(node *nodepb.Node) error{
|
|||||||
if node.Isdualstack == true {
|
if node.Isdualstack == true {
|
||||||
nodecfg.IsDualStack = "yes"
|
nodecfg.IsDualStack = "yes"
|
||||||
}
|
}
|
||||||
|
if node.Udpholepunch != "" {
|
||||||
|
nodecfg.UDPHolePunch = node.Udpholepunch
|
||||||
|
}
|
||||||
if node.Isingressgateway {
|
if node.Isingressgateway {
|
||||||
nodecfg.IsIngressGateway = "yes"
|
nodecfg.IsIngressGateway = "yes"
|
||||||
} else {
|
} else {
|
||||||
@@ -464,6 +466,7 @@ func GetCLIConfig(c *cli.Context) (ClientConfig, error){
|
|||||||
cfg.Node.IPForwarding = c.String("ipforwarding")
|
cfg.Node.IPForwarding = c.String("ipforwarding")
|
||||||
cfg.OperatingSystem = c.String("operatingsystem")
|
cfg.OperatingSystem = c.String("operatingsystem")
|
||||||
cfg.Daemon = c.String("daemon")
|
cfg.Daemon = c.String("daemon")
|
||||||
|
cfg.Node.UDPHolePunch = c.String("udpholepunch")
|
||||||
|
|
||||||
return cfg, nil
|
return cfg, nil
|
||||||
}
|
}
|
||||||
@@ -530,7 +533,6 @@ func GetCLIConfigRegister(c *cli.Context) (GlobalConfig, error){
|
|||||||
return cfg, nil
|
return cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func ReadConfig(network string) (*ClientConfig, error) {
|
func ReadConfig(network string) (*ClientConfig, error) {
|
||||||
if network == "" {
|
if network == "" {
|
||||||
err := errors.New("No network provided. Exiting.")
|
err := errors.New("No network provided. Exiting.")
|
||||||
|
@@ -1,23 +1,24 @@
|
|||||||
package functions
|
package functions
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"google.golang.org/grpc/credentials"
|
|
||||||
"crypto/tls"
|
|
||||||
"fmt"
|
|
||||||
"errors"
|
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"net"
|
||||||
"time"
|
"time"
|
||||||
"github.com/gravitl/netmaker/netclient/config"
|
|
||||||
"github.com/gravitl/netmaker/netclient/wireguard"
|
|
||||||
"github.com/gravitl/netmaker/netclient/server"
|
|
||||||
"github.com/gravitl/netmaker/netclient/local"
|
|
||||||
nodepb "github.com/gravitl/netmaker/grpc"
|
nodepb "github.com/gravitl/netmaker/grpc"
|
||||||
|
"github.com/gravitl/netmaker/netclient/config"
|
||||||
|
"github.com/gravitl/netmaker/netclient/local"
|
||||||
|
"github.com/gravitl/netmaker/netclient/server"
|
||||||
|
"github.com/gravitl/netmaker/netclient/wireguard"
|
||||||
"golang.zx2c4.com/wireguard/wgctrl"
|
"golang.zx2c4.com/wireguard/wgctrl"
|
||||||
"google.golang.org/grpc"
|
|
||||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/credentials"
|
||||||
//homedir "github.com/mitchellh/go-homedir"
|
//homedir "github.com/mitchellh/go-homedir"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -253,4 +254,3 @@ func GenPass() string {
|
|||||||
}
|
}
|
||||||
return string(b)
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -2,13 +2,14 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/gravitl/netmaker/netclient/command"
|
|
||||||
"github.com/gravitl/netmaker/netclient/config"
|
|
||||||
"github.com/urfave/cli/v2"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/gravitl/netmaker/netclient/command"
|
||||||
|
"github.com/gravitl/netmaker/netclient/config"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -162,6 +163,12 @@ func main() {
|
|||||||
Value: "",
|
Value: "",
|
||||||
Usage: "Sets ipv6 address if 'yes'. Ignores if 'no'. Will retrieve from network if unset.",
|
Usage: "Sets ipv6 address if 'yes'. Ignores if 'no'. Will retrieve from network if unset.",
|
||||||
},
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "udpholepunch",
|
||||||
|
EnvVars: []string{"NETCLIENT_UDP_HOLEPUNCH"},
|
||||||
|
Value: "",
|
||||||
|
Usage: "Turns on udp holepunching if 'yes'. Ignores if 'no'. Will retrieve from network if unset.",
|
||||||
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "ipforwarding",
|
Name: "ipforwarding",
|
||||||
EnvVars: []string{"NETCLIENT_IPFORWARDING"},
|
EnvVars: []string{"NETCLIENT_IPFORWARDING"},
|
||||||
@@ -365,7 +372,6 @@ func main() {
|
|||||||
log.Fatal("This program must be run with elevated privileges (sudo). This program installs a SystemD service and configures WireGuard and networking rules. Please re-run with sudo/root.")
|
log.Fatal("This program must be run with elevated privileges (sudo). This program installs a SystemD service and configures WireGuard and networking rules. Please re-run with sudo/root.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
_, err = exec.LookPath("wg")
|
_, err = exec.LookPath("wg")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
@@ -2,30 +2,32 @@ package wireguard
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
//"github.com/davecgh/go-spew/spew"
|
//"github.com/davecgh/go-spew/spew"
|
||||||
"google.golang.org/grpc/credentials"
|
|
||||||
"crypto/tls"
|
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
"errors"
|
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"strings"
|
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"github.com/gravitl/netmaker/netclient/config"
|
"strconv"
|
||||||
"github.com/gravitl/netmaker/netclient/local"
|
"strings"
|
||||||
"github.com/gravitl/netmaker/netclient/auth"
|
|
||||||
"github.com/gravitl/netmaker/netclient/server"
|
|
||||||
nodepb "github.com/gravitl/netmaker/grpc"
|
nodepb "github.com/gravitl/netmaker/grpc"
|
||||||
"github.com/gravitl/netmaker/models"
|
"github.com/gravitl/netmaker/models"
|
||||||
|
"github.com/gravitl/netmaker/netclient/auth"
|
||||||
|
"github.com/gravitl/netmaker/netclient/config"
|
||||||
|
"github.com/gravitl/netmaker/netclient/local"
|
||||||
|
"github.com/gravitl/netmaker/netclient/server"
|
||||||
"golang.zx2c4.com/wireguard/wgctrl"
|
"golang.zx2c4.com/wireguard/wgctrl"
|
||||||
"google.golang.org/grpc"
|
|
||||||
"google.golang.org/grpc/metadata"
|
|
||||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/credentials"
|
||||||
|
"google.golang.org/grpc/metadata"
|
||||||
//homedir "github.com/mitchellh/go-homedir"
|
//homedir "github.com/mitchellh/go-homedir"
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitGRPCWireguard(client models.IntClient) error {
|
func InitGRPCWireguard(client models.IntClient) error {
|
||||||
//spew.Dump(client)
|
//spew.Dump(client)
|
||||||
|
|
||||||
@@ -146,8 +148,6 @@ func InitGRPCWireguard(client models.IntClient) error {
|
|||||||
|
|
||||||
func InitWireguard(node *nodepb.Node, privkey string, peers []wgtypes.PeerConfig, hasGateway bool, gateways []string) error {
|
func InitWireguard(node *nodepb.Node, privkey string, peers []wgtypes.PeerConfig, hasGateway bool, gateways []string) error {
|
||||||
|
|
||||||
//spew.Dump(node)
|
|
||||||
//spew.Dump(peers)
|
|
||||||
ipExec, err := exec.LookPath("ip")
|
ipExec, err := exec.LookPath("ip")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -158,18 +158,16 @@ func InitWireguard(node *nodepb.Node, privkey string, peers []wgtypes.PeerConfig
|
|||||||
}
|
}
|
||||||
|
|
||||||
wgclient, err := wgctrl.New()
|
wgclient, err := wgctrl.New()
|
||||||
//modcfg := config.Config
|
if err != nil {
|
||||||
//modcfg.ReadConfig()
|
return err
|
||||||
|
}
|
||||||
modcfg, err := config.ReadConfig(node.Nodenetwork)
|
modcfg, err := config.ReadConfig(node.Nodenetwork)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
nodecfg := modcfg.Node
|
nodecfg := modcfg.Node
|
||||||
servercfg := modcfg.Server
|
servercfg := modcfg.Server
|
||||||
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to open client: %v", err)
|
log.Fatalf("failed to open client: %v", err)
|
||||||
}
|
}
|
||||||
@@ -208,7 +206,6 @@ func InitWireguard(node *nodepb.Node, privkey string, peers []wgtypes.PeerConfig
|
|||||||
|
|
||||||
currentiface, err := net.InterfaceByName(ifacename)
|
currentiface, err := net.InterfaceByName(ifacename)
|
||||||
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = cmdIPDevLinkAdd.Run()
|
err = cmdIPDevLinkAdd.Run()
|
||||||
if err != nil && !strings.Contains(err.Error(), "exists") {
|
if err != nil && !strings.Contains(err.Error(), "exists") {
|
||||||
@@ -234,13 +231,21 @@ func InitWireguard(node *nodepb.Node, privkey string, peers []wgtypes.PeerConfig
|
|||||||
var nodeport int
|
var nodeport int
|
||||||
nodeport = int(node.Listenport)
|
nodeport = int(node.Listenport)
|
||||||
|
|
||||||
//pubkey := privkey.PublicKey()
|
conf := wgtypes.Config{}
|
||||||
conf := wgtypes.Config{
|
if nodecfg.UDPHolePunch == "yes" {
|
||||||
|
conf = wgtypes.Config{
|
||||||
|
PrivateKey: &key,
|
||||||
|
ReplacePeers: true,
|
||||||
|
Peers: peers,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
conf = wgtypes.Config{
|
||||||
PrivateKey: &key,
|
PrivateKey: &key,
|
||||||
ListenPort: &nodeport,
|
ListenPort: &nodeport,
|
||||||
ReplacePeers: true,
|
ReplacePeers: true,
|
||||||
Peers: peers,
|
Peers: peers,
|
||||||
}
|
}
|
||||||
|
}
|
||||||
_, err = wgclient.Device(ifacename)
|
_, err = wgclient.Device(ifacename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
@@ -267,7 +272,6 @@ func InitWireguard(node *nodepb.Node, privkey string, peers []wgtypes.PeerConfig
|
|||||||
}
|
}
|
||||||
//=========End DNS Setup=======\\
|
//=========End DNS Setup=======\\
|
||||||
|
|
||||||
|
|
||||||
cmdIPLinkUp := &exec.Cmd{
|
cmdIPLinkUp := &exec.Cmd{
|
||||||
Path: ipExec,
|
Path: ipExec,
|
||||||
Args: []string{ipExec, "link", "set", "up", "dev", ifacename},
|
Args: []string{ipExec, "link", "set", "up", "dev", ifacename},
|
||||||
@@ -302,7 +306,7 @@ func InitWireguard(node *nodepb.Node, privkey string, peers []wgtypes.PeerConfig
|
|||||||
fmt.Println("Error encountered running PostUp: " + err.Error())
|
fmt.Println("Error encountered running PostUp: " + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (hasGateway) {
|
if hasGateway {
|
||||||
for _, gateway := range gateways {
|
for _, gateway := range gateways {
|
||||||
out, err := exec.Command(ipExec, "-4", "route", "add", gateway, "dev", ifacename).Output()
|
out, err := exec.Command(ipExec, "-4", "route", "add", gateway, "dev", ifacename).Output()
|
||||||
fmt.Println(string(out))
|
fmt.Println(string(out))
|
||||||
@@ -311,7 +315,7 @@ func InitWireguard(node *nodepb.Node, privkey string, peers []wgtypes.PeerConfig
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (node.Address6 != "" && node.Isdualstack) {
|
if node.Address6 != "" && node.Isdualstack {
|
||||||
fmt.Println("Adding address: " + node.Address6)
|
fmt.Println("Adding address: " + node.Address6)
|
||||||
out, err := exec.Command(ipExec, "address", "add", "dev", ifacename, node.Address6+"/64").Output()
|
out, err := exec.Command(ipExec, "address", "add", "dev", ifacename, node.Address6+"/64").Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -323,7 +327,6 @@ func InitWireguard(node *nodepb.Node, privkey string, peers []wgtypes.PeerConfig
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func SetWGKeyConfig(network string, serveraddr string) error {
|
func SetWGKeyConfig(network string, serveraddr string) error {
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
@@ -375,7 +378,6 @@ func SetWGKeyConfig(network string, serveraddr string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
postnode := server.GetNode(network)
|
postnode := server.GetNode(network)
|
||||||
|
|
||||||
req := &nodepb.UpdateNodeReq{
|
req := &nodepb.UpdateNodeReq{
|
||||||
@@ -395,7 +397,6 @@ func SetWGKeyConfig(network string, serveraddr string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func SetWGConfig(network string) error {
|
func SetWGConfig(network string) error {
|
||||||
|
|
||||||
cfg, err := config.ReadConfig(network)
|
cfg, err := config.ReadConfig(network)
|
||||||
|
@@ -183,3 +183,26 @@ func ReconfigureServerWireGuard() error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetPeers(networkName string) (map[string]string, error) {
|
||||||
|
peers := make(map[string]string)
|
||||||
|
network, err := functions.GetParentNetwork(networkName)
|
||||||
|
if err != nil {
|
||||||
|
return peers, err
|
||||||
|
}
|
||||||
|
iface := network.DefaultInterface
|
||||||
|
|
||||||
|
client, err := wgctrl.New()
|
||||||
|
if err != nil {
|
||||||
|
return peers, err
|
||||||
|
}
|
||||||
|
device, err := client.Device(iface)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, peer := range device.Peers {
|
||||||
|
peers[peer.PublicKey.String()] = peer.Endpoint.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
return peers, nil
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user