mirror of
https://github.com/gravitl/netmaker.git
synced 2025-11-08 07:41:41 +08:00
code clean up
This commit is contained in:
parent
c4ef546cd0
commit
bc45797542
7 changed files with 151 additions and 212 deletions
|
|
@ -1,14 +1,11 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/gravitl/netmaker/nm-proxy/wg"
|
||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
"github.com/gravitl/netmaker/nm-proxy/models"
|
||||
)
|
||||
|
||||
var IsHostNetwork bool
|
||||
|
|
@ -18,84 +15,17 @@ var IsRelayed bool
|
|||
var IsServer bool
|
||||
var InterfaceName string
|
||||
|
||||
const (
|
||||
NmProxyPort = 51722
|
||||
DefaultCIDR = "127.0.0.1/8"
|
||||
)
|
||||
var WgIFaceMap = make(map[string]models.WgIfaceConf)
|
||||
|
||||
type Conn struct {
|
||||
Config ConnConfig
|
||||
Proxy Proxy
|
||||
}
|
||||
var PeerKeyHashMap = make(map[string]models.RemotePeer)
|
||||
|
||||
// ConnConfig is a peer Connection configuration
|
||||
type ConnConfig struct {
|
||||
var WgIfaceKeyMap = make(map[string]models.RemotePeer)
|
||||
|
||||
// Key is a public key of a remote peer
|
||||
Key string
|
||||
// LocalKey is a public key of a local peer
|
||||
LocalKey string
|
||||
LocalWgPort int
|
||||
RemoteProxyIP net.IP
|
||||
RemoteWgPort int
|
||||
RemoteProxyPort int
|
||||
IsExtClient bool
|
||||
IsRelayed bool
|
||||
RelayedEndpoint *net.UDPAddr
|
||||
IsAttachedExtClient bool
|
||||
IngressGateWay *net.UDPAddr
|
||||
}
|
||||
var RelayPeerMap = make(map[string]map[string]models.RemotePeer)
|
||||
|
||||
type Config struct {
|
||||
Port int
|
||||
BodySize int
|
||||
Addr string
|
||||
RemoteKey string
|
||||
LocalKey string
|
||||
WgInterface *wg.WGIface
|
||||
PeerConf *wgtypes.PeerConfig
|
||||
}
|
||||
var ExtClientsWaitTh = make(map[string]models.ExtClientPeer)
|
||||
|
||||
// Proxy - WireguardProxy proxies
|
||||
type Proxy struct {
|
||||
Status bool
|
||||
Ctx context.Context
|
||||
Cancel context.CancelFunc
|
||||
|
||||
Config Config
|
||||
RemoteConn *net.UDPAddr
|
||||
LocalConn net.Conn
|
||||
}
|
||||
|
||||
type RemotePeer struct {
|
||||
PeerKey string
|
||||
Interface string
|
||||
Endpoint *net.UDPAddr
|
||||
IsExtClient bool
|
||||
IsAttachedExtClient bool
|
||||
}
|
||||
|
||||
type ExtClientPeer struct {
|
||||
CancelFunc context.CancelFunc
|
||||
CommChan chan *net.UDPAddr
|
||||
}
|
||||
|
||||
type WgIfaceConf struct {
|
||||
Iface *wgtypes.Device
|
||||
PeerMap map[string]*Conn
|
||||
}
|
||||
|
||||
var WgIFaceMap = make(map[string]WgIfaceConf)
|
||||
|
||||
var PeerKeyHashMap = make(map[string]RemotePeer)
|
||||
|
||||
var WgIfaceKeyMap = make(map[string]RemotePeer)
|
||||
|
||||
var RelayPeerMap = make(map[string]map[string]RemotePeer)
|
||||
|
||||
var ExtClientsWaitTh = make(map[string]ExtClientPeer)
|
||||
|
||||
var ExtSourceIpMap = make(map[string]RemotePeer)
|
||||
var ExtSourceIpMap = make(map[string]models.RemotePeer)
|
||||
|
||||
// RunCmd - runs a local command
|
||||
func RunCmd(command string, printerr bool) (string, error) {
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import (
|
|||
"runtime"
|
||||
|
||||
"github.com/gravitl/netmaker/nm-proxy/common"
|
||||
"github.com/gravitl/netmaker/nm-proxy/models"
|
||||
peerpkg "github.com/gravitl/netmaker/nm-proxy/peer"
|
||||
"github.com/gravitl/netmaker/nm-proxy/proxy"
|
||||
"github.com/gravitl/netmaker/nm-proxy/wg"
|
||||
|
|
@ -118,30 +119,30 @@ func (m *ManagerAction) RelayPeers() {
|
|||
for relayedNodePubKey, relayedNodeConf := range m.Payload.RelayedPeerConf {
|
||||
relayedNodePubKeyHash := fmt.Sprintf("%x", md5.Sum([]byte(relayedNodePubKey)))
|
||||
if _, ok := common.RelayPeerMap[relayedNodePubKeyHash]; !ok {
|
||||
common.RelayPeerMap[relayedNodePubKeyHash] = make(map[string]common.RemotePeer)
|
||||
common.RelayPeerMap[relayedNodePubKeyHash] = make(map[string]models.RemotePeer)
|
||||
}
|
||||
for _, peer := range relayedNodeConf.Peers {
|
||||
if peer.Endpoint != nil {
|
||||
peer.Endpoint.Port = common.NmProxyPort
|
||||
peer.Endpoint.Port = models.NmProxyPort
|
||||
remotePeerKeyHash := fmt.Sprintf("%x", md5.Sum([]byte(peer.PublicKey.String())))
|
||||
common.RelayPeerMap[relayedNodePubKeyHash][remotePeerKeyHash] = common.RemotePeer{
|
||||
common.RelayPeerMap[relayedNodePubKeyHash][remotePeerKeyHash] = models.RemotePeer{
|
||||
Endpoint: peer.Endpoint,
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
relayedNodeConf.RelayedPeerEndpoint.Port = common.NmProxyPort
|
||||
common.RelayPeerMap[relayedNodePubKeyHash][relayedNodePubKeyHash] = common.RemotePeer{
|
||||
relayedNodeConf.RelayedPeerEndpoint.Port = models.NmProxyPort
|
||||
common.RelayPeerMap[relayedNodePubKeyHash][relayedNodePubKeyHash] = models.RemotePeer{
|
||||
Endpoint: relayedNodeConf.RelayedPeerEndpoint,
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func cleanUpInterface(ifaceConf common.WgIfaceConf) {
|
||||
func cleanUpInterface(ifaceConf models.WgIfaceConf) {
|
||||
log.Println("########------------> CLEANING UP: ", ifaceConf.Iface.Name)
|
||||
for _, peerI := range ifaceConf.PeerMap {
|
||||
peerI.Proxy.Cancel()
|
||||
peerI.StopConn()
|
||||
}
|
||||
delete(common.WgIFaceMap, ifaceConf.Iface.Name)
|
||||
}
|
||||
|
|
@ -168,7 +169,7 @@ func (m *ManagerAction) processPayload() (*wg.WGIface, error) {
|
|||
log.Println("Failed init new interface: ", err)
|
||||
return nil, err
|
||||
}
|
||||
var wgProxyConf common.WgIfaceConf
|
||||
var wgProxyConf models.WgIfaceConf
|
||||
var ok bool
|
||||
if wgProxyConf, ok = common.WgIFaceMap[m.Payload.InterfaceName]; !ok {
|
||||
for i := len(m.Payload.Peers) - 1; i >= 0; i-- {
|
||||
|
|
@ -202,19 +203,45 @@ func (m *ManagerAction) processPayload() (*wg.WGIface, error) {
|
|||
return wgIface, nil
|
||||
}
|
||||
// check device conf different from proxy
|
||||
//wgProxyConf.Iface = wgIface.Device
|
||||
wgProxyConf.Iface = wgIface.Device
|
||||
// sync peer map with new update
|
||||
for _, currPeerI := range wgProxyConf.Iface.Peers {
|
||||
if _, ok := m.Payload.PeerMap[currPeerI.PublicKey.String()]; !ok {
|
||||
if val, ok := wgProxyConf.PeerMap[currPeerI.PublicKey.String()]; ok {
|
||||
if val.IsAttachedExtClient {
|
||||
log.Println("------> Deleting ExtClient Watch Thread: ", currPeerI.PublicKey.String())
|
||||
if val, ok := common.ExtClientsWaitTh[currPeerI.PublicKey.String()]; ok {
|
||||
val.CancelFunc()
|
||||
delete(common.ExtClientsWaitTh, currPeerI.PublicKey.String())
|
||||
}
|
||||
log.Println("-----> Deleting Ext Client from Src Ip Map: ", currPeerI.PublicKey.String())
|
||||
delete(common.ExtSourceIpMap, val.PeerConf.Endpoint.String())
|
||||
}
|
||||
val.StopConn()
|
||||
}
|
||||
|
||||
// delete peer from interface
|
||||
log.Println("CurrPeer Not Found, Deleting Peer from Interface: ", currPeerI.PublicKey.String())
|
||||
if err := wgIface.RemovePeer(currPeerI.PublicKey.String()); err != nil {
|
||||
log.Println("failed to remove peer: ", currPeerI.PublicKey.String(), err)
|
||||
}
|
||||
delete(common.PeerKeyHashMap, fmt.Sprintf("%x", md5.Sum([]byte(currPeerI.PublicKey.String()))))
|
||||
delete(wgProxyConf.PeerMap, currPeerI.PublicKey.String())
|
||||
|
||||
}
|
||||
}
|
||||
for i := len(m.Payload.Peers) - 1; i >= 0; i-- {
|
||||
|
||||
if currentPeer, ok := wgProxyConf.PeerMap[m.Payload.Peers[i].PublicKey.String()]; ok {
|
||||
if currentPeer.Config.IsAttachedExtClient {
|
||||
if currentPeer.IsAttachedExtClient {
|
||||
continue
|
||||
}
|
||||
// check if proxy is off for the peer
|
||||
if !m.Payload.PeerMap[m.Payload.Peers[i].PublicKey.String()].Proxy {
|
||||
|
||||
// cleanup proxy connections for the peer
|
||||
currentPeer.Proxy.Cancel()
|
||||
delete(wgProxyConf.PeerMap, currentPeer.Config.Key)
|
||||
currentPeer.StopConn()
|
||||
delete(wgProxyConf.PeerMap, currentPeer.Key)
|
||||
// update the peer with actual endpoint
|
||||
if err := wgIface.Update(m.Payload.Peers[i], false); err != nil {
|
||||
log.Println("falied to update peer: ", err)
|
||||
|
|
@ -224,51 +251,51 @@ func (m *ManagerAction) processPayload() (*wg.WGIface, error) {
|
|||
|
||||
}
|
||||
// check if peer is not connected to proxy
|
||||
devPeer, err := wg.GetPeer(m.Payload.InterfaceName, currentPeer.Config.Key)
|
||||
devPeer, err := wg.GetPeer(m.Payload.InterfaceName, currentPeer.Key)
|
||||
if err == nil {
|
||||
log.Printf("---------> COMAPRING ENDPOINT: DEV: %s, Proxy: %s", devPeer.Endpoint.String(), currentPeer.Proxy.LocalConn.LocalAddr().String())
|
||||
if devPeer.Endpoint.String() != currentPeer.Proxy.LocalConn.LocalAddr().String() {
|
||||
log.Println("---------> endpoint is not set to proxy: ", currentPeer.Config.Key)
|
||||
currentPeer.Proxy.Cancel()
|
||||
delete(wgProxyConf.PeerMap, currentPeer.Config.Key)
|
||||
log.Printf("---------> COMAPRING ENDPOINT: DEV: %s, Proxy: %s", devPeer.Endpoint.String(), currentPeer.LocalConn.LocalAddr().String())
|
||||
if devPeer.Endpoint.String() != currentPeer.LocalConn.LocalAddr().String() {
|
||||
log.Println("---------> endpoint is not set to proxy: ", currentPeer.Key)
|
||||
currentPeer.StopConn()
|
||||
delete(wgProxyConf.PeerMap, currentPeer.Key)
|
||||
continue
|
||||
}
|
||||
}
|
||||
//check if peer is being relayed
|
||||
if currentPeer.Config.IsRelayed != m.Payload.PeerMap[m.Payload.Peers[i].PublicKey.String()].IsRelayed {
|
||||
log.Println("---------> peer relay status has been changed: ", currentPeer.Config.Key)
|
||||
currentPeer.Proxy.Cancel()
|
||||
delete(wgProxyConf.PeerMap, currentPeer.Config.Key)
|
||||
if currentPeer.IsRelayed != m.Payload.PeerMap[m.Payload.Peers[i].PublicKey.String()].IsRelayed {
|
||||
log.Println("---------> peer relay status has been changed: ", currentPeer.Key)
|
||||
currentPeer.StopConn()
|
||||
delete(wgProxyConf.PeerMap, currentPeer.Key)
|
||||
continue
|
||||
}
|
||||
// check if relay endpoint has been changed
|
||||
if currentPeer.Config.RelayedEndpoint != nil &&
|
||||
if currentPeer.RelayedEndpoint != nil &&
|
||||
m.Payload.PeerMap[m.Payload.Peers[i].PublicKey.String()].RelayedTo != nil &&
|
||||
currentPeer.Config.RelayedEndpoint.String() != m.Payload.PeerMap[m.Payload.Peers[i].PublicKey.String()].RelayedTo.String() {
|
||||
log.Println("---------> peer relay endpoint has been changed: ", currentPeer.Config.Key)
|
||||
currentPeer.Proxy.Cancel()
|
||||
delete(wgProxyConf.PeerMap, currentPeer.Config.Key)
|
||||
currentPeer.RelayedEndpoint.String() != m.Payload.PeerMap[m.Payload.Peers[i].PublicKey.String()].RelayedTo.String() {
|
||||
log.Println("---------> peer relay endpoint has been changed: ", currentPeer.Key)
|
||||
currentPeer.StopConn()
|
||||
delete(wgProxyConf.PeerMap, currentPeer.Key)
|
||||
continue
|
||||
}
|
||||
if !reflect.DeepEqual(m.Payload.Peers[i], *currentPeer.Proxy.Config.PeerConf) {
|
||||
if currentPeer.Proxy.RemoteConn.IP.String() != m.Payload.Peers[i].Endpoint.IP.String() {
|
||||
log.Println("----------> Resetting proxy for Peer: ", currentPeer.Config.Key, m.Payload.InterfaceName)
|
||||
currentPeer.Proxy.Cancel()
|
||||
delete(wgProxyConf.PeerMap, currentPeer.Config.Key)
|
||||
if !reflect.DeepEqual(m.Payload.Peers[i], *currentPeer.PeerConf) {
|
||||
if currentPeer.RemoteConn.IP.String() != m.Payload.Peers[i].Endpoint.IP.String() {
|
||||
log.Println("----------> Resetting proxy for Peer: ", currentPeer.Key, m.Payload.InterfaceName)
|
||||
currentPeer.StopConn()
|
||||
delete(wgProxyConf.PeerMap, currentPeer.Key)
|
||||
|
||||
} else {
|
||||
|
||||
log.Println("----->##### Updating Peer on Interface: ", m.Payload.InterfaceName, currentPeer.Config.Key)
|
||||
log.Println("----->##### Updating Peer on Interface: ", m.Payload.InterfaceName, currentPeer.Key)
|
||||
updatePeerConf := m.Payload.Peers[i]
|
||||
localUdpAddr, err := net.ResolveUDPAddr("udp", currentPeer.Proxy.LocalConn.LocalAddr().String())
|
||||
localUdpAddr, err := net.ResolveUDPAddr("udp", currentPeer.LocalConn.LocalAddr().String())
|
||||
if err == nil {
|
||||
updatePeerConf.Endpoint = localUdpAddr
|
||||
}
|
||||
if err := wgIface.Update(updatePeerConf, true); err != nil {
|
||||
log.Println("failed to update peer: ", currentPeer.Config.Key, err)
|
||||
log.Println("failed to update peer: ", currentPeer.Key, err)
|
||||
}
|
||||
currentPeer.Proxy.Config.PeerConf = &m.Payload.Peers[i]
|
||||
wgProxyConf.PeerMap[currentPeer.Config.Key] = currentPeer
|
||||
currentPeer.PeerConf = &m.Payload.Peers[i]
|
||||
wgProxyConf.PeerMap[currentPeer.Key] = currentPeer
|
||||
// delete the peer from the list
|
||||
log.Println("-----------> deleting peer from list: ", m.Payload.Peers[i].PublicKey)
|
||||
m.Payload.Peers = append(m.Payload.Peers[:i], m.Payload.Peers[i+1:]...)
|
||||
|
|
@ -289,29 +316,6 @@ func (m *ManagerAction) processPayload() (*wg.WGIface, error) {
|
|||
m.Payload.Peers = append(m.Payload.Peers[:i], m.Payload.Peers[i+1:]...)
|
||||
}
|
||||
}
|
||||
// sync peer map with new update
|
||||
for _, currPeerI := range wgProxyConf.PeerMap {
|
||||
if _, ok := m.Payload.PeerMap[currPeerI.Config.Key]; !ok {
|
||||
if currPeerI.Config.IsAttachedExtClient {
|
||||
log.Println("------> Deleting ExtClient Watch Thread: ", currPeerI.Config.Key)
|
||||
if val, ok := common.ExtClientsWaitTh[currPeerI.Config.Key]; ok {
|
||||
val.CancelFunc()
|
||||
delete(common.ExtClientsWaitTh, currPeerI.Config.Key)
|
||||
}
|
||||
log.Println("-----> Deleting Ext Client from Src Ip Map: ", currPeerI.Config.Key)
|
||||
delete(common.ExtSourceIpMap, currPeerI.Proxy.Config.PeerConf.Endpoint.String())
|
||||
}
|
||||
currPeerI.Proxy.Cancel()
|
||||
// delete peer from interface
|
||||
log.Println("CurrPeer Not Found, Deleting Peer from Interface: ", currPeerI.Config.Key)
|
||||
if err := wgIface.RemovePeer(currPeerI.Config.Key); err != nil {
|
||||
log.Println("failed to remove peer: ", currPeerI.Config.Key, err)
|
||||
}
|
||||
delete(common.PeerKeyHashMap, fmt.Sprintf("%x", md5.Sum([]byte(currPeerI.Config.Key))))
|
||||
delete(wgProxyConf.PeerMap, currPeerI.Config.Key)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// sync dev peers with new update
|
||||
|
||||
|
|
@ -335,7 +339,7 @@ func (m *ManagerAction) AddInterfaceToProxy() error {
|
|||
log.Println("failed to get wg listen addr: ", err)
|
||||
return err
|
||||
}
|
||||
common.WgIfaceKeyMap[fmt.Sprintf("%x", md5.Sum([]byte(wgInterface.Device.PublicKey.String())))] = common.RemotePeer{
|
||||
common.WgIfaceKeyMap[fmt.Sprintf("%x", md5.Sum([]byte(wgInterface.Device.PublicKey.String())))] = models.RemotePeer{
|
||||
PeerKey: wgInterface.Device.PublicKey.String(),
|
||||
Interface: wgInterface.Name,
|
||||
Endpoint: wgListenAddr,
|
||||
|
|
@ -376,7 +380,7 @@ func (m *ManagerAction) AddInterfaceToProxy() error {
|
|||
peerI.Endpoint = peerConf.IngressGatewayEndPoint
|
||||
}
|
||||
if shouldProceed {
|
||||
common.PeerKeyHashMap[fmt.Sprintf("%x", md5.Sum([]byte(peerI.PublicKey.String())))] = common.RemotePeer{
|
||||
common.PeerKeyHashMap[fmt.Sprintf("%x", md5.Sum([]byte(peerI.PublicKey.String())))] = models.RemotePeer{
|
||||
Interface: m.Payload.InterfaceName,
|
||||
PeerKey: peerI.PublicKey.String(),
|
||||
IsExtClient: peerConf.IsExtClient,
|
||||
|
|
@ -398,21 +402,19 @@ func (m *ManagerAction) AddInterfaceToProxy() error {
|
|||
}
|
||||
if !shouldProceed && peerConf.IsAttachedExtClient {
|
||||
log.Println("Extclient endpoint not updated yet....skipping")
|
||||
// TODO - watch the interface for ext client update
|
||||
go func(wgInterface *wg.WGIface, peer *wgtypes.PeerConfig,
|
||||
isRelayed bool, relayTo *net.UDPAddr, peerConf PeerConf, ingGwAddr string) {
|
||||
addExtClient := false
|
||||
commChan := make(chan *net.UDPAddr, 100)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
common.ExtClientsWaitTh[peerI.PublicKey.String()] = common.ExtClientPeer{
|
||||
common.ExtClientsWaitTh[peerI.PublicKey.String()] = models.ExtClientPeer{
|
||||
CancelFunc: cancel,
|
||||
CommChan: commChan,
|
||||
}
|
||||
defer func() {
|
||||
if addExtClient {
|
||||
log.Println("GOT ENDPOINT for Extclient adding peer...")
|
||||
//go proxy.StartSniffer(ctx, wgInterface.Name, ingGwAddr, peerConf.Address, wgInterface.Port)
|
||||
common.PeerKeyHashMap[fmt.Sprintf("%x", md5.Sum([]byte(peer.PublicKey.String())))] = common.RemotePeer{
|
||||
common.PeerKeyHashMap[fmt.Sprintf("%x", md5.Sum([]byte(peer.PublicKey.String())))] = models.RemotePeer{
|
||||
Interface: wgInterface.Name,
|
||||
PeerKey: peer.PublicKey.String(),
|
||||
IsExtClient: peerConf.IsExtClient,
|
||||
|
|
@ -420,7 +422,7 @@ func (m *ManagerAction) AddInterfaceToProxy() error {
|
|||
Endpoint: peer.Endpoint,
|
||||
}
|
||||
|
||||
common.ExtSourceIpMap[peer.Endpoint.String()] = common.RemotePeer{
|
||||
common.ExtSourceIpMap[peer.Endpoint.String()] = models.RemotePeer{
|
||||
Interface: wgInterface.Name,
|
||||
PeerKey: peer.PublicKey.String(),
|
||||
IsExtClient: peerConf.IsExtClient,
|
||||
|
|
|
|||
46
nm-proxy/models/peer.go
Normal file
46
nm-proxy/models/peer.go
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
|
||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
)
|
||||
|
||||
const (
|
||||
NmProxyPort = 51722
|
||||
DefaultCIDR = "127.0.0.1/8"
|
||||
)
|
||||
|
||||
// ConnConfig is a peer Connection configuration
|
||||
type ConnConfig struct {
|
||||
|
||||
// Key is a public key of a remote peer
|
||||
Key string
|
||||
IsExtClient bool
|
||||
IsRelayed bool
|
||||
RelayedEndpoint *net.UDPAddr
|
||||
IsAttachedExtClient bool
|
||||
PeerConf *wgtypes.PeerConfig
|
||||
StopConn context.CancelFunc
|
||||
RemoteConn *net.UDPAddr
|
||||
LocalConn net.Conn
|
||||
}
|
||||
|
||||
type RemotePeer struct {
|
||||
PeerKey string
|
||||
Interface string
|
||||
Endpoint *net.UDPAddr
|
||||
IsExtClient bool
|
||||
IsAttachedExtClient bool
|
||||
}
|
||||
|
||||
type ExtClientPeer struct {
|
||||
CancelFunc context.CancelFunc
|
||||
CommChan chan *net.UDPAddr
|
||||
}
|
||||
|
||||
type WgIfaceConf struct {
|
||||
Iface *wgtypes.Device
|
||||
PeerMap map[string]*ConnConfig
|
||||
}
|
||||
|
|
@ -7,32 +7,12 @@ import (
|
|||
"net"
|
||||
|
||||
"github.com/gravitl/netmaker/nm-proxy/common"
|
||||
"github.com/gravitl/netmaker/nm-proxy/models"
|
||||
"github.com/gravitl/netmaker/nm-proxy/proxy"
|
||||
"github.com/gravitl/netmaker/nm-proxy/wg"
|
||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
)
|
||||
|
||||
type Conn struct {
|
||||
Config ConnConfig
|
||||
Proxy proxy.Proxy
|
||||
}
|
||||
|
||||
// ConnConfig is a peer Connection configuration
|
||||
type ConnConfig struct {
|
||||
|
||||
// Key is a public key of a remote peer
|
||||
Key string
|
||||
// LocalKey is a public key of a local peer
|
||||
LocalKey string
|
||||
|
||||
ProxyConfig proxy.Config
|
||||
AllowedIPs string
|
||||
LocalWgPort int
|
||||
RemoteProxyIP net.IP
|
||||
RemoteWgPort int
|
||||
RemoteProxyPort int
|
||||
}
|
||||
|
||||
func AddNewPeer(wgInterface *wg.WGIface, peer *wgtypes.PeerConfig, peerAddr string,
|
||||
isRelayed, isExtClient, isAttachedExtClient bool, relayTo *net.UDPAddr) error {
|
||||
|
||||
|
|
@ -45,7 +25,7 @@ func AddNewPeer(wgInterface *wg.WGIface, peer *wgtypes.PeerConfig, peerAddr stri
|
|||
PeerConf: peer,
|
||||
}
|
||||
p := proxy.NewProxy(c)
|
||||
peerPort := common.NmProxyPort
|
||||
peerPort := models.NmProxyPort
|
||||
if isExtClient && isAttachedExtClient {
|
||||
peerPort = peer.Endpoint.Port
|
||||
|
||||
|
|
@ -75,48 +55,27 @@ func AddNewPeer(wgInterface *wg.WGIface, peer *wgtypes.PeerConfig, peerAddr stri
|
|||
// log.Println("Not Starting Proxy for Attached ExtClient...")
|
||||
// }
|
||||
|
||||
connConf := common.ConnConfig{
|
||||
Key: peer.PublicKey.String(),
|
||||
LocalKey: wgInterface.Device.PublicKey.String(),
|
||||
LocalWgPort: wgInterface.Device.ListenPort,
|
||||
RemoteProxyIP: net.ParseIP(peer.Endpoint.IP.String()),
|
||||
RemoteWgPort: peer.Endpoint.Port,
|
||||
RemoteProxyPort: common.NmProxyPort,
|
||||
IsRelayed: isRelayed,
|
||||
RelayedEndpoint: relayTo,
|
||||
connConf := models.ConnConfig{
|
||||
Key: peer.PublicKey.String(),
|
||||
IsRelayed: isRelayed,
|
||||
RelayedEndpoint: relayTo,
|
||||
IsAttachedExtClient: isAttachedExtClient,
|
||||
PeerConf: peer,
|
||||
StopConn: p.Cancel,
|
||||
RemoteConn: remoteConn,
|
||||
LocalConn: p.LocalConn,
|
||||
}
|
||||
|
||||
peerProxy := common.Proxy{
|
||||
Ctx: p.Ctx,
|
||||
Cancel: p.Cancel,
|
||||
Config: common.Config{
|
||||
Port: peer.Endpoint.Port,
|
||||
LocalKey: wgInterface.Device.PublicKey.String(),
|
||||
RemoteKey: peer.PublicKey.String(),
|
||||
WgInterface: wgInterface,
|
||||
PeerConf: peer,
|
||||
},
|
||||
|
||||
RemoteConn: remoteConn,
|
||||
LocalConn: p.LocalConn,
|
||||
}
|
||||
if isRelayed {
|
||||
connConf.RemoteProxyIP = relayTo.IP
|
||||
}
|
||||
peerConn := common.Conn{
|
||||
Config: connConf,
|
||||
Proxy: peerProxy,
|
||||
}
|
||||
if _, ok := common.WgIFaceMap[wgInterface.Name]; ok {
|
||||
common.WgIFaceMap[wgInterface.Name].PeerMap[peer.PublicKey.String()] = &peerConn
|
||||
common.WgIFaceMap[wgInterface.Name].PeerMap[peer.PublicKey.String()] = &connConf
|
||||
} else {
|
||||
ifaceConf := common.WgIfaceConf{
|
||||
ifaceConf := models.WgIfaceConf{
|
||||
Iface: wgInterface.Device,
|
||||
PeerMap: make(map[string]*common.Conn),
|
||||
PeerMap: make(map[string]*models.ConnConfig),
|
||||
}
|
||||
|
||||
common.WgIFaceMap[wgInterface.Name] = ifaceConf
|
||||
common.WgIFaceMap[wgInterface.Name].PeerMap[peer.PublicKey.String()] = &peerConn
|
||||
common.WgIFaceMap[wgInterface.Name].PeerMap[peer.PublicKey.String()] = &connConf
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import (
|
|||
|
||||
"github.com/c-robinson/iplib"
|
||||
"github.com/gravitl/netmaker/nm-proxy/common"
|
||||
"github.com/gravitl/netmaker/nm-proxy/models"
|
||||
"github.com/gravitl/netmaker/nm-proxy/packet"
|
||||
"github.com/gravitl/netmaker/nm-proxy/server"
|
||||
"github.com/gravitl/netmaker/nm-proxy/wg"
|
||||
|
|
@ -65,7 +66,7 @@ func (p *Proxy) ProxyToRemote() {
|
|||
if peerI, ok := ifaceConf.PeerMap[p.Config.RemoteKey]; ok {
|
||||
var srcPeerKeyHash, dstPeerKeyHash string
|
||||
if !p.Config.IsExtClient {
|
||||
buf, n, srcPeerKeyHash, dstPeerKeyHash = packet.ProcessPacketBeforeSending(buf, n, peerI.Config.LocalKey, peerI.Config.Key)
|
||||
buf, n, srcPeerKeyHash, dstPeerKeyHash = packet.ProcessPacketBeforeSending(buf, n, ifaceConf.Iface.PublicKey.String(), peerI.Key)
|
||||
if err != nil {
|
||||
log.Println("failed to process pkt before sending: ", err)
|
||||
}
|
||||
|
|
@ -146,7 +147,7 @@ func (p *Proxy) Start(remoteConn *net.UDPAddr) error {
|
|||
var err error
|
||||
|
||||
//log.Printf("----> WGIFACE: %+v\n", p.Config.WgInterface)
|
||||
addr, err := GetFreeIp(common.DefaultCIDR, p.Config.WgInterface.Port)
|
||||
addr, err := GetFreeIp(models.DefaultCIDR, p.Config.WgInterface.Port)
|
||||
if err != nil {
|
||||
log.Println("Failed to get freeIp: ", err)
|
||||
return err
|
||||
|
|
@ -162,7 +163,7 @@ func (p *Proxy) Start(remoteConn *net.UDPAddr) error {
|
|||
//log.Println("--------->#### Wg Listen Addr: ", wgListenAddr.String())
|
||||
p.LocalConn, err = net.DialUDP("udp", &net.UDPAddr{
|
||||
IP: net.ParseIP(addr),
|
||||
Port: common.NmProxyPort,
|
||||
Port: models.NmProxyPort,
|
||||
}, wgListenAddr)
|
||||
if err != nil {
|
||||
log.Printf("failed dialing to local Wireguard port,Err: %v\n", err)
|
||||
|
|
@ -203,7 +204,7 @@ func GetFreeIp(cidrAddr string, dstPort int) (string, error) {
|
|||
|
||||
conn, err := net.DialUDP("udp", &net.UDPAddr{
|
||||
IP: net.ParseIP(newAddrs.String()),
|
||||
Port: common.NmProxyPort,
|
||||
Port: models.NmProxyPort,
|
||||
}, &net.UDPAddr{
|
||||
IP: net.ParseIP("127.0.0.1"),
|
||||
Port: dstPort,
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/gravitl/netmaker/nm-proxy/common"
|
||||
"github.com/gravitl/netmaker/nm-proxy/models"
|
||||
"github.com/gravitl/netmaker/nm-proxy/packet"
|
||||
)
|
||||
|
||||
|
|
@ -18,7 +19,7 @@ var (
|
|||
|
||||
const (
|
||||
defaultBodySize = 10000
|
||||
defaultPort = common.NmProxyPort
|
||||
defaultPort = models.NmProxyPort
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
|
|
@ -47,7 +48,7 @@ func (p *ProxyServer) Listen(ctx context.Context) {
|
|||
for iface, ifaceConf := range common.WgIFaceMap {
|
||||
log.Println("########------------> CLEANING UP: ", iface)
|
||||
for _, peerI := range ifaceConf.PeerMap {
|
||||
peerI.Proxy.Cancel()
|
||||
peerI.StopConn()
|
||||
}
|
||||
}
|
||||
// close server connection
|
||||
|
|
@ -120,9 +121,9 @@ func (p *ProxyServer) Listen(ctx context.Context) {
|
|||
if ifaceConf, ok := common.WgIFaceMap[peerInfo.Interface]; ok {
|
||||
if peerI, ok := ifaceConf.PeerMap[peerInfo.PeerKey]; ok {
|
||||
log.Printf("PROXING TO LOCAL!!!---> %s <<<< %s <<<<<<<< %s [[ RECV PKT [SRCKEYHASH: %s], [DSTKEYHASH: %s], SourceIP: [%s] ]]\n",
|
||||
peerI.Proxy.LocalConn.RemoteAddr(), peerI.Proxy.LocalConn.LocalAddr(),
|
||||
peerI.LocalConn.RemoteAddr(), peerI.LocalConn.LocalAddr(),
|
||||
fmt.Sprintf("%s:%d", source.IP.String(), source.Port), srcPeerKeyHash, dstPeerKeyHash, source.IP.String())
|
||||
_, err = peerI.Proxy.LocalConn.Write(buffer[:n])
|
||||
_, err = peerI.LocalConn.Write(buffer[:n])
|
||||
if err != nil {
|
||||
log.Println("Failed to proxy to Wg local interface: ", err)
|
||||
//continue
|
||||
|
|
@ -137,9 +138,9 @@ func (p *ProxyServer) Listen(ctx context.Context) {
|
|||
if ifaceConf, ok := common.WgIFaceMap[peerInfo.Interface]; ok {
|
||||
if peerI, ok := ifaceConf.PeerMap[peerInfo.PeerKey]; ok {
|
||||
log.Printf("PROXING TO LOCAL!!!---> %s <<<< %s <<<<<<<< %s [[ RECV PKT [SRCKEYHASH: %s], [DSTKEYHASH: %s], SourceIP: [%s] ]]\n",
|
||||
peerI.Proxy.LocalConn.RemoteAddr(), peerI.Proxy.LocalConn.LocalAddr(),
|
||||
peerI.LocalConn.RemoteAddr(), peerI.LocalConn.LocalAddr(),
|
||||
fmt.Sprintf("%s:%d", source.IP.String(), source.Port), srcPeerKeyHash, dstPeerKeyHash, source.IP.String())
|
||||
_, err = peerI.Proxy.LocalConn.Write(buffer[:origBufferLen])
|
||||
_, err = peerI.LocalConn.Write(buffer[:origBufferLen])
|
||||
if err != nil {
|
||||
log.Println("Failed to proxy to Wg local interface: ", err)
|
||||
//continue
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import (
|
|||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gravitl/netmaker/nm-proxy/common"
|
||||
"github.com/gravitl/netmaker/nm-proxy/models"
|
||||
"gortc.io/stun"
|
||||
)
|
||||
|
||||
|
|
@ -29,7 +29,7 @@ func GetHostInfo(stunHostAddr string) (info HostInfo) {
|
|||
}
|
||||
l := &net.UDPAddr{
|
||||
IP: net.ParseIP(""),
|
||||
Port: common.NmProxyPort,
|
||||
Port: models.NmProxyPort,
|
||||
}
|
||||
conn, err := net.DialUDP("udp", l, s)
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue