Browse Source

speed test

Abhishek Kondur 2 years ago
parent
commit
a75879109a
4 changed files with 92 additions and 83 deletions
  1. 2 0
      nm-proxy/manager/manager.go
  2. 1 1
      nm-proxy/proxy/proxy.go
  3. 21 17
      nm-proxy/proxy/wireguard.go
  4. 68 65
      nm-proxy/server/server.go

+ 2 - 0
nm-proxy/manager/manager.go

@@ -25,6 +25,7 @@ TODO:-
 			-> start remote conn after endpoint is updated
 			-> start remote conn after endpoint is updated
 		-->
 		-->
 */
 */
+var sent bool
 
 
 type ProxyAction string
 type ProxyAction string
 
 
@@ -76,6 +77,7 @@ func StartProxyManager(manageChan chan *ManagerAction) {
 			log.Printf("-------> PROXY-MANAGER: %+v\n", mI)
 			log.Printf("-------> PROXY-MANAGER: %+v\n", mI)
 			switch mI.Action {
 			switch mI.Action {
 			case AddInterface:
 			case AddInterface:
+
 				common.IsRelay = mI.Payload.IsRelay
 				common.IsRelay = mI.Payload.IsRelay
 				if mI.Payload.IsRelay {
 				if mI.Payload.IsRelay {
 					mI.RelayPeers()
 					mI.RelayPeers()

+ 1 - 1
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 ip.DstIP.String() == extClientAddr || ip.SrcIP.String() == extClientAddr {
 						if ifacePeers, ok := common.PeerAddrMap[ifaceName]; ok {
 						if ifacePeers, ok := common.PeerAddrMap[ifaceName]; ok {
 							if peerConf, ok := ifacePeers[ip.DstIP.String()]; 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(),  )
 								//server.NmProxyServer.Server.WriteTo(packet.Data(),  )
 							}
 							}
 
 

+ 21 - 17
nm-proxy/proxy/wireguard.go

@@ -25,6 +25,7 @@ func NewProxy(config Config) *Proxy {
 // proxyToRemote proxies everything from Wireguard to the RemoteKey peer
 // proxyToRemote proxies everything from Wireguard to the RemoteKey peer
 func (p *Proxy) ProxyToRemote() {
 func (p *Proxy) ProxyToRemote() {
 	buf := make([]byte, 1500)
 	buf := make([]byte, 1500)
+	peers := common.WgIFaceMap[p.Config.WgInterface.Name]
 	go func() {
 	go func() {
 		<-p.Ctx.Done()
 		<-p.Ctx.Done()
 		log.Println("Closing connection for: ", p.LocalConn.LocalAddr().String())
 		log.Println("Closing connection for: ", p.LocalConn.LocalAddr().String())
@@ -58,26 +59,29 @@ func (p *Proxy) ProxyToRemote() {
 				log.Println("ERRR READ: ", err)
 				log.Println("ERRR READ: ", err)
 				continue
 				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)
+			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 {
 				if err != nil {
-					log.Println("failed to process pkt before sending: ", err)
+					log.Println("Failed to send to remote: ", 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)
+			}()
 
 
-			_, err = server.NmProxyServer.Server.WriteToUDP(buf[:n], p.RemoteConn)
-			if err != nil {
-				log.Println("Failed to send to remote: ", err)
-			}
 		}
 		}
 	}
 	}
 }
 }

+ 68 - 65
nm-proxy/server/server.go

@@ -59,88 +59,91 @@ func (p *ProxyServer) Listen(ctx context.Context) {
 				log.Println("RECV ERROR: ", err)
 				log.Println("RECV ERROR: ", err)
 				continue
 				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 {
+			go func() {
+
+				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 {
 							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)
 								_, err = NmProxyServer.Server.WriteToUDP(buffer[:n+32], conf.Endpoint)
 								if err != nil {
 								if err != nil {
 									log.Println("Failed to send to remote: ", err)
 									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()
-			// 	}
-
-			// }
+				// }
+			}()
 		}
 		}
 
 
 	}
 	}