|
@@ -9,9 +9,9 @@ import (
|
|
"net"
|
|
"net"
|
|
"reflect"
|
|
"reflect"
|
|
"runtime"
|
|
"runtime"
|
|
- "time"
|
|
|
|
|
|
|
|
"github.com/gravitl/netmaker/nm-proxy/common"
|
|
"github.com/gravitl/netmaker/nm-proxy/common"
|
|
|
|
+ "github.com/gravitl/netmaker/nm-proxy/models"
|
|
peerpkg "github.com/gravitl/netmaker/nm-proxy/peer"
|
|
peerpkg "github.com/gravitl/netmaker/nm-proxy/peer"
|
|
"github.com/gravitl/netmaker/nm-proxy/proxy"
|
|
"github.com/gravitl/netmaker/nm-proxy/proxy"
|
|
"github.com/gravitl/netmaker/nm-proxy/wg"
|
|
"github.com/gravitl/netmaker/nm-proxy/wg"
|
|
@@ -32,6 +32,7 @@ type ProxyAction string
|
|
|
|
|
|
type ManagerPayload struct {
|
|
type ManagerPayload struct {
|
|
InterfaceName string `json:"interface_name"`
|
|
InterfaceName string `json:"interface_name"`
|
|
|
|
+ WgAddr string `json:"wg_addr"`
|
|
Peers []wgtypes.PeerConfig `json:"peers"`
|
|
Peers []wgtypes.PeerConfig `json:"peers"`
|
|
PeerMap map[string]PeerConf `json:"peer_map"`
|
|
PeerMap map[string]PeerConf `json:"peer_map"`
|
|
IsRelayed bool `json:"is_relayed"`
|
|
IsRelayed bool `json:"is_relayed"`
|
|
@@ -76,7 +77,7 @@ func StartProxyManager(manageChan chan *ManagerAction) {
|
|
switch mI.Action {
|
|
switch mI.Action {
|
|
case AddInterface:
|
|
case AddInterface:
|
|
|
|
|
|
- mI.ExtClients()
|
|
|
|
|
|
+ mI.SetIngressGateway()
|
|
err := mI.AddInterfaceToProxy()
|
|
err := mI.AddInterfaceToProxy()
|
|
if err != nil {
|
|
if err != nil {
|
|
log.Printf("failed to add interface: [%s] to proxy: %v\n ", mI.Payload.InterfaceName, err)
|
|
log.Printf("failed to add interface: [%s] to proxy: %v\n ", mI.Payload.InterfaceName, err)
|
|
@@ -108,7 +109,7 @@ func (m *ManagerAction) RelayUpdate() {
|
|
common.IsRelay = m.Payload.IsRelay
|
|
common.IsRelay = m.Payload.IsRelay
|
|
}
|
|
}
|
|
|
|
|
|
-func (m *ManagerAction) ExtClients() {
|
|
|
|
|
|
+func (m *ManagerAction) SetIngressGateway() {
|
|
common.IsIngressGateway = m.Payload.IsIngress
|
|
common.IsIngressGateway = m.Payload.IsIngress
|
|
|
|
|
|
}
|
|
}
|
|
@@ -118,30 +119,30 @@ func (m *ManagerAction) RelayPeers() {
|
|
for relayedNodePubKey, relayedNodeConf := range m.Payload.RelayedPeerConf {
|
|
for relayedNodePubKey, relayedNodeConf := range m.Payload.RelayedPeerConf {
|
|
relayedNodePubKeyHash := fmt.Sprintf("%x", md5.Sum([]byte(relayedNodePubKey)))
|
|
relayedNodePubKeyHash := fmt.Sprintf("%x", md5.Sum([]byte(relayedNodePubKey)))
|
|
if _, ok := common.RelayPeerMap[relayedNodePubKeyHash]; !ok {
|
|
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 {
|
|
for _, peer := range relayedNodeConf.Peers {
|
|
if peer.Endpoint != nil {
|
|
if peer.Endpoint != nil {
|
|
- peer.Endpoint.Port = common.NmProxyPort
|
|
|
|
|
|
+ peer.Endpoint.Port = models.NmProxyPort
|
|
remotePeerKeyHash := fmt.Sprintf("%x", md5.Sum([]byte(peer.PublicKey.String())))
|
|
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,
|
|
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,
|
|
Endpoint: relayedNodeConf.RelayedPeerEndpoint,
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-func cleanUpInterface(ifaceConf common.WgIfaceConf) {
|
|
|
|
|
|
+func cleanUpInterface(ifaceConf models.WgIfaceConf) {
|
|
log.Println("########------------> CLEANING UP: ", ifaceConf.Iface.Name)
|
|
log.Println("########------------> CLEANING UP: ", ifaceConf.Iface.Name)
|
|
for _, peerI := range ifaceConf.PeerMap {
|
|
for _, peerI := range ifaceConf.PeerMap {
|
|
- peerI.Proxy.Cancel()
|
|
|
|
|
|
+ peerI.StopConn()
|
|
}
|
|
}
|
|
delete(common.WgIFaceMap, ifaceConf.Iface.Name)
|
|
delete(common.WgIFaceMap, ifaceConf.Iface.Name)
|
|
}
|
|
}
|
|
@@ -162,12 +163,13 @@ func (m *ManagerAction) processPayload() (*wg.WGIface, error) {
|
|
log.Println("failed to get real iface: ", err)
|
|
log.Println("failed to get real iface: ", err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ common.InterfaceName = m.Payload.InterfaceName
|
|
wgIface, err = wg.NewWGIFace(m.Payload.InterfaceName, "127.0.0.1/32", wg.DefaultMTU)
|
|
wgIface, err = wg.NewWGIFace(m.Payload.InterfaceName, "127.0.0.1/32", wg.DefaultMTU)
|
|
if err != nil {
|
|
if err != nil {
|
|
log.Println("Failed init new interface: ", err)
|
|
log.Println("Failed init new interface: ", err)
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
- var wgProxyConf common.WgIfaceConf
|
|
|
|
|
|
+ var wgProxyConf models.WgIfaceConf
|
|
var ok bool
|
|
var ok bool
|
|
if wgProxyConf, ok = common.WgIFaceMap[m.Payload.InterfaceName]; !ok {
|
|
if wgProxyConf, ok = common.WgIFaceMap[m.Payload.InterfaceName]; !ok {
|
|
for i := len(m.Payload.Peers) - 1; i >= 0; i-- {
|
|
for i := len(m.Payload.Peers) - 1; i >= 0; i-- {
|
|
@@ -177,6 +179,7 @@ func (m *ManagerAction) processPayload() (*wg.WGIface, error) {
|
|
log.Println("falied to update peer: ", err)
|
|
log.Println("falied to update peer: ", err)
|
|
}
|
|
}
|
|
m.Payload.Peers = append(m.Payload.Peers[:i], m.Payload.Peers[i+1:]...)
|
|
m.Payload.Peers = append(m.Payload.Peers[:i], m.Payload.Peers[i+1:]...)
|
|
|
|
+ continue
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return wgIface, nil
|
|
return wgIface, nil
|
|
@@ -200,17 +203,46 @@ func (m *ManagerAction) processPayload() (*wg.WGIface, error) {
|
|
return wgIface, nil
|
|
return wgIface, nil
|
|
}
|
|
}
|
|
// check device conf different from proxy
|
|
// 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-- {
|
|
for i := len(m.Payload.Peers) - 1; i >= 0; i-- {
|
|
|
|
|
|
if currentPeer, ok := wgProxyConf.PeerMap[m.Payload.Peers[i].PublicKey.String()]; ok {
|
|
if currentPeer, ok := wgProxyConf.PeerMap[m.Payload.Peers[i].PublicKey.String()]; ok {
|
|
|
|
+ if currentPeer.IsAttachedExtClient {
|
|
|
|
+ m.Payload.Peers = append(m.Payload.Peers[:i], m.Payload.Peers[i+1:]...)
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
// check if proxy is off for the peer
|
|
// check if proxy is off for the peer
|
|
if !m.Payload.PeerMap[m.Payload.Peers[i].PublicKey.String()].Proxy {
|
|
if !m.Payload.PeerMap[m.Payload.Peers[i].PublicKey.String()].Proxy {
|
|
|
|
|
|
// cleanup proxy connections for the peer
|
|
// cleanup proxy connections for the peer
|
|
- currentPeer.Proxy.Cancel()
|
|
|
|
- time.Sleep(time.Second * 3)
|
|
|
|
- delete(wgProxyConf.PeerMap, currentPeer.Config.Key)
|
|
|
|
|
|
+ currentPeer.StopConn()
|
|
|
|
+ delete(wgProxyConf.PeerMap, currentPeer.Key)
|
|
// update the peer with actual endpoint
|
|
// update the peer with actual endpoint
|
|
if err := wgIface.Update(m.Payload.Peers[i], false); err != nil {
|
|
if err := wgIface.Update(m.Payload.Peers[i], false); err != nil {
|
|
log.Println("falied to update peer: ", err)
|
|
log.Println("falied to update peer: ", err)
|
|
@@ -220,55 +252,51 @@ func (m *ManagerAction) processPayload() (*wg.WGIface, error) {
|
|
|
|
|
|
}
|
|
}
|
|
// check if peer is not connected to proxy
|
|
// 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 {
|
|
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()
|
|
|
|
- time.Sleep(time.Second * 3)
|
|
|
|
- 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
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
}
|
|
//check if peer is being relayed
|
|
//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()
|
|
|
|
- time.Sleep(time.Second * 3)
|
|
|
|
- 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
|
|
continue
|
|
}
|
|
}
|
|
// check if relay endpoint has been changed
|
|
// 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 &&
|
|
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()
|
|
|
|
- time.Sleep(time.Second * 3)
|
|
|
|
- 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
|
|
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()
|
|
|
|
- time.Sleep(time.Second * 3)
|
|
|
|
- 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 {
|
|
} 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]
|
|
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 {
|
|
if err == nil {
|
|
updatePeerConf.Endpoint = localUdpAddr
|
|
updatePeerConf.Endpoint = localUdpAddr
|
|
}
|
|
}
|
|
if err := wgIface.Update(updatePeerConf, true); err != nil {
|
|
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
|
|
// delete the peer from the list
|
|
log.Println("-----------> deleting peer from list: ", m.Payload.Peers[i].PublicKey)
|
|
log.Println("-----------> deleting peer from list: ", m.Payload.Peers[i].PublicKey)
|
|
m.Payload.Peers = append(m.Payload.Peers[:i], m.Payload.Peers[i+1:]...)
|
|
m.Payload.Peers = append(m.Payload.Peers[:i], m.Payload.Peers[i+1:]...)
|
|
@@ -281,7 +309,7 @@ func (m *ManagerAction) processPayload() (*wg.WGIface, error) {
|
|
m.Payload.Peers = append(m.Payload.Peers[:i], m.Payload.Peers[i+1:]...)
|
|
m.Payload.Peers = append(m.Payload.Peers[:i], m.Payload.Peers[i+1:]...)
|
|
}
|
|
}
|
|
|
|
|
|
- } else if !m.Payload.PeerMap[m.Payload.Peers[i].PublicKey.String()].Proxy {
|
|
|
|
|
|
+ } else if !m.Payload.PeerMap[m.Payload.Peers[i].PublicKey.String()].Proxy && !m.Payload.PeerMap[m.Payload.Peers[i].PublicKey.String()].IsAttachedExtClient {
|
|
log.Println("-----------> skipping peer, proxy is off: ", m.Payload.Peers[i].PublicKey)
|
|
log.Println("-----------> skipping peer, proxy is off: ", m.Payload.Peers[i].PublicKey)
|
|
if err := wgIface.Update(m.Payload.Peers[i], false); err != nil {
|
|
if err := wgIface.Update(m.Payload.Peers[i], false); err != nil {
|
|
log.Println("falied to update peer: ", err)
|
|
log.Println("falied to update peer: ", err)
|
|
@@ -289,35 +317,10 @@ func (m *ManagerAction) processPayload() (*wg.WGIface, error) {
|
|
m.Payload.Peers = append(m.Payload.Peers[:i], m.Payload.Peers[i+1:]...)
|
|
m.Payload.Peers = append(m.Payload.Peers[:i], m.Payload.Peers[i+1:]...)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- for _, currPeerI := range wgProxyConf.PeerMap {
|
|
|
|
- if _, ok := m.Payload.PeerMap[currPeerI.Config.Key]; !ok {
|
|
|
|
- currPeerI.Proxy.Cancel()
|
|
|
|
- time.Sleep(time.Second * 3)
|
|
|
|
- // 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(wgProxyConf.PeerMap, currPeerI.Config.Key)
|
|
|
|
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- common.WgIFaceMap[m.Payload.InterfaceName] = wgProxyConf
|
|
|
|
|
|
+ // sync dev peers with new update
|
|
|
|
|
|
- // if peers, ok := common.WgIFaceMap[iface]; ok {
|
|
|
|
- // log.Println("########------------> CLEANING UP: ", iface)
|
|
|
|
- // for _, peerI := range peers {
|
|
|
|
- // peerI.Proxy.Cancel()
|
|
|
|
- // }
|
|
|
|
- // }
|
|
|
|
- // delete(common.WgIFaceMap, iface)
|
|
|
|
- // delete(common.PeerAddrMap, iface)
|
|
|
|
- // if waitThs, ok := common.ExtClientsWaitTh[iface]; ok {
|
|
|
|
- // for _, cancelF := range waitThs {
|
|
|
|
- // cancelF()
|
|
|
|
- // }
|
|
|
|
- // delete(common.ExtClientsWaitTh, iface)
|
|
|
|
- // }
|
|
|
|
|
|
+ common.WgIFaceMap[m.Payload.InterfaceName] = wgProxyConf
|
|
|
|
|
|
log.Println("CLEANED UP..........")
|
|
log.Println("CLEANED UP..........")
|
|
return wgIface, nil
|
|
return wgIface, nil
|
|
@@ -337,7 +340,7 @@ func (m *ManagerAction) AddInterfaceToProxy() error {
|
|
log.Println("failed to get wg listen addr: ", err)
|
|
log.Println("failed to get wg listen addr: ", err)
|
|
return 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(),
|
|
PeerKey: wgInterface.Device.PublicKey.String(),
|
|
Interface: wgInterface.Name,
|
|
Interface: wgInterface.Name,
|
|
Endpoint: wgListenAddr,
|
|
Endpoint: wgListenAddr,
|
|
@@ -349,41 +352,10 @@ func (m *ManagerAction) AddInterfaceToProxy() error {
|
|
log.Println("Endpoint nil for peer: ", peerI.PublicKey.String())
|
|
log.Println("Endpoint nil for peer: ", peerI.PublicKey.String())
|
|
continue
|
|
continue
|
|
}
|
|
}
|
|
- if peerConf.IsExtClient && !common.IsIngressGateway {
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
- shouldProceed := false
|
|
|
|
- if peerConf.IsExtClient && peerConf.IsAttachedExtClient {
|
|
|
|
- // check if ext client got endpoint,otherwise continue
|
|
|
|
- for _, devpeerI := range wgInterface.Device.Peers {
|
|
|
|
- if devpeerI.PublicKey.String() == peerI.PublicKey.String() && devpeerI.Endpoint != nil {
|
|
|
|
- peerI.Endpoint = devpeerI.Endpoint
|
|
|
|
- shouldProceed = true
|
|
|
|
- break
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- } else {
|
|
|
|
- shouldProceed = true
|
|
|
|
- }
|
|
|
|
- if peerConf.IsExtClient && peerConf.IsAttachedExtClient && shouldProceed {
|
|
|
|
- //ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
- //common.ExtClientsWaitTh[wgInterface.Name] = append(common.ExtClientsWaitTh[wgInterface.Name], cancel)
|
|
|
|
- //go proxy.StartSniffer(ctx, wgInterface.Name, peerConf.Address, wgInterface.Port)
|
|
|
|
- }
|
|
|
|
|
|
|
|
if peerConf.IsExtClient && !peerConf.IsAttachedExtClient {
|
|
if peerConf.IsExtClient && !peerConf.IsAttachedExtClient {
|
|
peerI.Endpoint = peerConf.IngressGatewayEndPoint
|
|
peerI.Endpoint = peerConf.IngressGatewayEndPoint
|
|
}
|
|
}
|
|
- if shouldProceed {
|
|
|
|
- common.PeerKeyHashMap[fmt.Sprintf("%x", md5.Sum([]byte(peerI.PublicKey.String())))] = common.RemotePeer{
|
|
|
|
- Interface: m.Payload.InterfaceName,
|
|
|
|
- PeerKey: peerI.PublicKey.String(),
|
|
|
|
- IsExtClient: peerConf.IsExtClient,
|
|
|
|
- Endpoint: peerI.Endpoint,
|
|
|
|
- IsAttachedExtClient: peerConf.IsAttachedExtClient,
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
|
|
var isRelayed bool
|
|
var isRelayed bool
|
|
var relayedTo *net.UDPAddr
|
|
var relayedTo *net.UDPAddr
|
|
@@ -396,19 +368,29 @@ func (m *ManagerAction) AddInterfaceToProxy() error {
|
|
relayedTo = peerConf.RelayedTo
|
|
relayedTo = peerConf.RelayedTo
|
|
|
|
|
|
}
|
|
}
|
|
- if !shouldProceed && peerConf.IsAttachedExtClient {
|
|
|
|
- log.Println("Extclient endpoint not updated yet....skipping")
|
|
|
|
- // TODO - watch the interface for ext client update
|
|
|
|
|
|
+ if peerConf.IsAttachedExtClient {
|
|
|
|
+ log.Println("Extclient Thread...")
|
|
go func(wgInterface *wg.WGIface, peer *wgtypes.PeerConfig,
|
|
go func(wgInterface *wg.WGIface, peer *wgtypes.PeerConfig,
|
|
- isRelayed bool, relayTo *net.UDPAddr, peerConf PeerConf) {
|
|
|
|
|
|
+ isRelayed bool, relayTo *net.UDPAddr, peerConf PeerConf, ingGwAddr string) {
|
|
addExtClient := false
|
|
addExtClient := false
|
|
|
|
+ commChan := make(chan *net.UDPAddr, 100)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
- common.ExtClientsWaitTh[wgInterface.Name] = append(common.ExtClientsWaitTh[wgInterface.Name], cancel)
|
|
|
|
|
|
+ common.ExtClientsWaitTh[peerI.PublicKey.String()] = models.ExtClientPeer{
|
|
|
|
+ CancelFunc: cancel,
|
|
|
|
+ CommChan: commChan,
|
|
|
|
+ }
|
|
defer func() {
|
|
defer func() {
|
|
if addExtClient {
|
|
if addExtClient {
|
|
log.Println("GOT ENDPOINT for Extclient adding peer...")
|
|
log.Println("GOT ENDPOINT for Extclient adding peer...")
|
|
- //go proxy.StartSniffer(ctx, wgInterface.Name, 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,
|
|
|
|
+ IsAttachedExtClient: peerConf.IsAttachedExtClient,
|
|
|
|
+ Endpoint: peer.Endpoint,
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ common.ExtSourceIpMap[peer.Endpoint.String()] = models.RemotePeer{
|
|
Interface: wgInterface.Name,
|
|
Interface: wgInterface.Name,
|
|
PeerKey: peer.PublicKey.String(),
|
|
PeerKey: peer.PublicKey.String(),
|
|
IsExtClient: peerConf.IsExtClient,
|
|
IsExtClient: peerConf.IsExtClient,
|
|
@@ -418,34 +400,35 @@ func (m *ManagerAction) AddInterfaceToProxy() error {
|
|
|
|
|
|
peerpkg.AddNewPeer(wgInterface, peer, peerConf.Address, isRelayed,
|
|
peerpkg.AddNewPeer(wgInterface, peer, peerConf.Address, isRelayed,
|
|
peerConf.IsExtClient, peerConf.IsAttachedExtClient, relayedTo)
|
|
peerConf.IsExtClient, peerConf.IsAttachedExtClient, relayedTo)
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
+ log.Println("Exiting extclient watch Thread for: ", peer.PublicKey.String())
|
|
}()
|
|
}()
|
|
for {
|
|
for {
|
|
select {
|
|
select {
|
|
case <-ctx.Done():
|
|
case <-ctx.Done():
|
|
- log.Println("Exiting extclient watch Thread for: ", wgInterface.Device.PublicKey.String())
|
|
|
|
return
|
|
return
|
|
- default:
|
|
|
|
- wgInterface, err := wg.NewWGIFace(m.Payload.InterfaceName, "127.0.0.1/32", wg.DefaultMTU)
|
|
|
|
- if err != nil {
|
|
|
|
- log.Println("Failed init new interface: ", err)
|
|
|
|
|
|
+ case endpoint := <-commChan:
|
|
|
|
+ if endpoint != nil {
|
|
|
|
+ addExtClient = true
|
|
|
|
+ peer.Endpoint = endpoint
|
|
|
|
+ delete(common.ExtClientsWaitTh, peer.PublicKey.String())
|
|
return
|
|
return
|
|
}
|
|
}
|
|
- for _, devpeerI := range wgInterface.Device.Peers {
|
|
|
|
- if devpeerI.PublicKey.String() == peer.PublicKey.String() && devpeerI.Endpoint != nil {
|
|
|
|
- peer.Endpoint = devpeerI.Endpoint
|
|
|
|
- addExtClient = true
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- time.Sleep(time.Second * 5)
|
|
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- }(wgInterface, &peerI, isRelayed, relayedTo, peerConf)
|
|
|
|
|
|
+ }(wgInterface, &peerI, isRelayed, relayedTo, peerConf, m.Payload.WgAddr)
|
|
continue
|
|
continue
|
|
}
|
|
}
|
|
|
|
+ common.PeerKeyHashMap[fmt.Sprintf("%x", md5.Sum([]byte(peerI.PublicKey.String())))] = models.RemotePeer{
|
|
|
|
+ Interface: m.Payload.InterfaceName,
|
|
|
|
+ PeerKey: peerI.PublicKey.String(),
|
|
|
|
+ IsExtClient: peerConf.IsExtClient,
|
|
|
|
+ Endpoint: peerI.Endpoint,
|
|
|
|
+ IsAttachedExtClient: peerConf.IsAttachedExtClient,
|
|
|
|
+ }
|
|
|
|
|
|
peerpkg.AddNewPeer(wgInterface, &peerI, peerConf.Address, isRelayed,
|
|
peerpkg.AddNewPeer(wgInterface, &peerI, peerConf.Address, isRelayed,
|
|
peerConf.IsExtClient, peerConf.IsAttachedExtClient, relayedTo)
|
|
peerConf.IsExtClient, peerConf.IsAttachedExtClient, relayedTo)
|