From a75879109ab9aa6d9807f069abaf26dd9f4a9d75 Mon Sep 17 00:00:00 2001 From: Abhishek Kondur Date: Tue, 15 Nov 2022 22:48:07 +0530 Subject: [PATCH] speed test --- nm-proxy/manager/manager.go | 2 + nm-proxy/proxy/proxy.go | 2 +- nm-proxy/proxy/wireguard.go | 42 ++++++------ nm-proxy/server/server.go | 125 ++++++++++++++++++------------------ 4 files changed, 90 insertions(+), 81 deletions(-) diff --git a/nm-proxy/manager/manager.go b/nm-proxy/manager/manager.go index f3652b95..26be2024 100644 --- a/nm-proxy/manager/manager.go +++ b/nm-proxy/manager/manager.go @@ -25,6 +25,7 @@ TODO:- -> start remote conn after endpoint is updated --> */ +var sent bool type ProxyAction string @@ -76,6 +77,7 @@ func StartProxyManager(manageChan chan *ManagerAction) { log.Printf("-------> PROXY-MANAGER: %+v\n", mI) switch mI.Action { case AddInterface: + common.IsRelay = mI.Payload.IsRelay if mI.Payload.IsRelay { mI.RelayPeers() diff --git a/nm-proxy/proxy/proxy.go b/nm-proxy/proxy/proxy.go index bc3188e4..bc0b4b74 100644 --- a/nm-proxy/proxy/proxy.go +++ b/nm-proxy/proxy/proxy.go @@ -160,7 +160,7 @@ func StartSniffer(ctx context.Context, ifaceName, extClientAddr string, port int if ip.DstIP.String() == extClientAddr || ip.SrcIP.String() == extClientAddr { if ifacePeers, ok := common.PeerAddrMap[ifaceName]; ok { if peerConf, ok := ifacePeers[ip.DstIP.String()]; ok { - log.Println("-----> Fowarding PKT From ExtClient: ", extClientAddr, " to: ", peerConf.Config.RemoteProxyIP) + log.Println("-----> Fowarding PKT From ExtClient: ", extClientAddr, " to: ", peerConf) //server.NmProxyServer.Server.WriteTo(packet.Data(), ) } diff --git a/nm-proxy/proxy/wireguard.go b/nm-proxy/proxy/wireguard.go index 6cb0dd0d..4e81ed86 100644 --- a/nm-proxy/proxy/wireguard.go +++ b/nm-proxy/proxy/wireguard.go @@ -25,6 +25,7 @@ func NewProxy(config Config) *Proxy { // proxyToRemote proxies everything from Wireguard to the RemoteKey peer func (p *Proxy) ProxyToRemote() { buf := make([]byte, 1500) + peers := common.WgIFaceMap[p.Config.WgInterface.Name] go func() { <-p.Ctx.Done() log.Println("Closing connection for: ", p.LocalConn.LocalAddr().String()) @@ -58,26 +59,29 @@ func (p *Proxy) ProxyToRemote() { log.Println("ERRR READ: ", err) continue } - peers := common.WgIFaceMap[p.Config.WgInterface.Name] - if peerI, ok := peers[p.Config.RemoteKey]; ok { - var srcPeerKeyHash, dstPeerKeyHash string - buf, n, srcPeerKeyHash, dstPeerKeyHash = packet.ProcessPacketBeforeSending(buf, n, peerI.Config.LocalKey, peerI.Config.Key) - if err != nil { - log.Println("failed to process pkt before sending: ", err) - } - log.Printf("PROXING TO REMOTE!!!---> %s >>>>> %s >>>>> %s [[ SrcPeerHash: %s, DstPeerHash: %s ]]\n", - p.LocalConn.LocalAddr(), server.NmProxyServer.Server.LocalAddr().String(), p.RemoteConn.String(), srcPeerKeyHash, dstPeerKeyHash) - } else { - log.Printf("Peer: %s not found in config\n", p.Config.RemoteKey) - p.Cancel() - continue - } - //test(n, buf) + go func() { + + if peerI, ok := peers[p.Config.RemoteKey]; ok { + var srcPeerKeyHash, dstPeerKeyHash string + buf, n, srcPeerKeyHash, dstPeerKeyHash = packet.ProcessPacketBeforeSending(buf, n, peerI.Config.LocalKey, peerI.Config.Key) + if err != nil { + log.Println("failed to process pkt before sending: ", err) + } + log.Printf("PROXING TO REMOTE!!!---> %s >>>>> %s >>>>> %s [[ SrcPeerHash: %s, DstPeerHash: %s ]]\n", + p.LocalConn.LocalAddr(), server.NmProxyServer.Server.LocalAddr().String(), p.RemoteConn.String(), srcPeerKeyHash, dstPeerKeyHash) + } else { + log.Printf("Peer: %s not found in config\n", p.Config.RemoteKey) + p.Cancel() + return + } + //test(n, buf) + + _, err = server.NmProxyServer.Server.WriteToUDP(buf[:n], p.RemoteConn) + if err != nil { + log.Println("Failed to send to remote: ", err) + } + }() - _, err = server.NmProxyServer.Server.WriteToUDP(buf[:n], p.RemoteConn) - if err != nil { - log.Println("Failed to send to remote: ", err) - } } } } diff --git a/nm-proxy/server/server.go b/nm-proxy/server/server.go index a5fecf4d..b3bc7b46 100644 --- a/nm-proxy/server/server.go +++ b/nm-proxy/server/server.go @@ -59,88 +59,91 @@ func (p *ProxyServer) Listen(ctx context.Context) { log.Println("RECV ERROR: ", err) continue } - var srcPeerKeyHash, dstPeerKeyHash string - n, srcPeerKeyHash, dstPeerKeyHash = packet.ExtractInfo(buffer, n) - //log.Printf("--------> RECV PKT [DSTPORT: %d], [SRCKEYHASH: %s], SourceIP: [%s] \n", localWgPort, srcPeerKeyHash, source.IP.String()) - if _, ok := common.WgIfaceKeyMap[dstPeerKeyHash]; !ok { - // if common.IsIngressGateway { - // log.Println("----> fowarding PKT to EXT client...") - // if val, ok := common.PeerKeyHashMap[dstPeerKeyHash]; ok && val.IsAttachedExtClient { + go func() { - // log.Printf("-------->Forwarding the pkt to extClient [ SourceIP: %s ], [ SourceKeyHash: %s ], [ DstIP: %s ], [ DstHashKey: %s ] \n", - // source.String(), srcPeerKeyHash, val.Endpoint.String(), dstPeerKeyHash) - // _, err = NmProxyServer.Server.WriteToUDP(buffer[:n], val.Endpoint) - // if err != nil { - // log.Println("Failed to send to remote: ", err) - // } - // continue + var srcPeerKeyHash, dstPeerKeyHash string + n, srcPeerKeyHash, dstPeerKeyHash = packet.ExtractInfo(buffer, n) + //log.Printf("--------> RECV PKT [DSTPORT: %d], [SRCKEYHASH: %s], SourceIP: [%s] \n", localWgPort, srcPeerKeyHash, source.IP.String()) + if _, ok := common.WgIfaceKeyMap[dstPeerKeyHash]; !ok { + // if common.IsIngressGateway { + // log.Println("----> fowarding PKT to EXT client...") + // if val, ok := common.PeerKeyHashMap[dstPeerKeyHash]; ok && val.IsAttachedExtClient { - // } - // } + // log.Printf("-------->Forwarding the pkt to extClient [ SourceIP: %s ], [ SourceKeyHash: %s ], [ DstIP: %s ], [ DstHashKey: %s ] \n", + // source.String(), srcPeerKeyHash, val.Endpoint.String(), dstPeerKeyHash) + // _, err = NmProxyServer.Server.WriteToUDP(buffer[:n], val.Endpoint) + // if err != nil { + // log.Println("Failed to send to remote: ", err) + // } + // continue - if common.IsRelay { + // } + // } - log.Println("----------> Relaying######") - // check for routing map and forward to right proxy - if remoteMap, ok := common.RelayPeerMap[srcPeerKeyHash]; ok { - if conf, ok := remoteMap[dstPeerKeyHash]; ok { - log.Printf("--------> Relaying PKT [ SourceIP: %s:%d ], [ SourceKeyHash: %s ], [ DstIP: %s:%d ], [ DstHashKey: %s ] \n", - source.IP.String(), source.Port, srcPeerKeyHash, conf.Endpoint.String(), conf.Endpoint.Port, dstPeerKeyHash) - _, err = NmProxyServer.Server.WriteToUDP(buffer[:n+32], conf.Endpoint) - if err != nil { - log.Println("Failed to send to remote: ", err) - } - continue - } - } else { - if remoteMap, ok := common.RelayPeerMap[dstPeerKeyHash]; ok { + if common.IsRelay { + + log.Println("----------> Relaying######") + // check for routing map and forward to right proxy + if remoteMap, ok := common.RelayPeerMap[srcPeerKeyHash]; ok { if conf, ok := remoteMap[dstPeerKeyHash]; ok { - log.Printf("--------> Relaying BACK TO RELAYED NODE PKT [ SourceIP: %s ], [ SourceKeyHash: %s ], [ DstIP: %s ], [ DstHashKey: %s ] \n", - source.String(), srcPeerKeyHash, conf.Endpoint.String(), dstPeerKeyHash) + log.Printf("--------> Relaying PKT [ SourceIP: %s:%d ], [ SourceKeyHash: %s ], [ DstIP: %s:%d ], [ DstHashKey: %s ] \n", + source.IP.String(), source.Port, srcPeerKeyHash, conf.Endpoint.String(), conf.Endpoint.Port, dstPeerKeyHash) _, err = NmProxyServer.Server.WriteToUDP(buffer[:n+32], conf.Endpoint) if err != nil { log.Println("Failed to send to remote: ", err) } - continue + return } + } else { + if remoteMap, ok := common.RelayPeerMap[dstPeerKeyHash]; ok { + if conf, ok := remoteMap[dstPeerKeyHash]; ok { + log.Printf("--------> Relaying BACK TO RELAYED NODE PKT [ SourceIP: %s ], [ SourceKeyHash: %s ], [ DstIP: %s ], [ DstHashKey: %s ] \n", + source.String(), srcPeerKeyHash, conf.Endpoint.String(), dstPeerKeyHash) + _, err = NmProxyServer.Server.WriteToUDP(buffer[:n+32], conf.Endpoint) + if err != nil { + log.Println("Failed to send to remote: ", err) + } + return + } + } + } } } - } + if peerInfo, ok := common.PeerKeyHashMap[srcPeerKeyHash]; ok { + if peers, ok := common.WgIFaceMap[peerInfo.Interface]; ok { + if peerI, ok := peers[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(), + fmt.Sprintf("%s:%d", source.IP.String(), source.Port), srcPeerKeyHash, dstPeerKeyHash, source.IP.String()) + _, err = peerI.Proxy.LocalConn.Write(buffer[:n]) + if err != nil { + log.Println("Failed to proxy to Wg local interface: ", err) + return + } - if peerInfo, ok := common.PeerKeyHashMap[srcPeerKeyHash]; ok { - if peers, ok := common.WgIFaceMap[peerInfo.Interface]; ok { - if peerI, ok := peers[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(), - fmt.Sprintf("%s:%d", source.IP.String(), source.Port), srcPeerKeyHash, dstPeerKeyHash, source.IP.String()) - _, err = peerI.Proxy.LocalConn.Write(buffer[:n]) - if err != nil { - log.Println("Failed to proxy to Wg local interface: ", err) - continue } - } + return + } - continue + // // forward to all interfaces + // for _, ifaceCfg := range common.WgIfaceKeyMap { + // log.Println("###--------> Forwarding Unknown PKT to ", ifaceCfg.Interface) + // conn, err := net.DialUDP("udp", nil, ifaceCfg.Endpoint) + // if err == nil { + // _, err := conn.Write(buffer[:n]) + // if err != nil { + // log.Println("Failed to forward the unknown pkt to ifcace: ", ifaceCfg.Interface, err) + // } + // conn.Close() + // } - } - // // forward to all interfaces - // for _, ifaceCfg := range common.WgIfaceKeyMap { - // log.Println("###--------> Forwarding Unknown PKT to ", ifaceCfg.Interface) - // conn, err := net.DialUDP("udp", nil, ifaceCfg.Endpoint) - // if err == nil { - // _, err := conn.Write(buffer[:n]) - // if err != nil { - // log.Println("Failed to forward the unknown pkt to ifcace: ", ifaceCfg.Interface, err) - // } - // conn.Close() - // } - - // } + // } + }() } }