Browse Source

speed test

Abhishek Kondur 2 years ago
parent
commit
1434147279
2 changed files with 90 additions and 90 deletions
  1. 21 20
      nm-proxy/proxy/wireguard.go
  2. 69 70
      nm-proxy/server/server.go

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

@@ -31,6 +31,7 @@ func (p *Proxy) ProxyToRemote() {
 		log.Println("Closing connection for: ", p.LocalConn.LocalAddr().String())
 		log.Println("Closing connection for: ", p.LocalConn.LocalAddr().String())
 		p.LocalConn.Close()
 		p.LocalConn.Close()
 	}()
 	}()
+	buf := make([]byte, 65535)
 	for {
 	for {
 		select {
 		select {
 		case <-p.Ctx.Done():
 		case <-p.Ctx.Done():
@@ -53,34 +54,34 @@ func (p *Proxy) ProxyToRemote() {
 
 
 			return
 			return
 		default:
 		default:
-			buf := make([]byte, 1500)
+
 			n, err := p.LocalConn.Read(buf)
 			n, err := p.LocalConn.Read(buf)
 			if err != nil {
 			if err != nil {
 				log.Println("ERRR READ: ", err)
 				log.Println("ERRR READ: ", err)
 				continue
 				continue
 			}
 			}
-			go func(buf []byte, n int) {
-
-				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)
+			// go func(buf []byte, n int) {
 
 
-				_, err = server.NmProxyServer.Server.WriteToUDP(buf[:n], p.RemoteConn)
+			if peerI, ok := peers[p.Config.RemoteKey]; ok {
+				//var srcPeerKeyHash, dstPeerKeyHash string
+				buf, n, _, _ = packet.ProcessPacketBeforeSending(buf, n, peerI.Config.LocalKey, peerI.Config.Key)
 				if err != nil {
 				if err != nil {
-					log.Println("Failed to send to remote: ", err)
+					log.Println("failed to process pkt before sending: ", err)
 				}
 				}
-			}(buf, n)
+				// 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)
+			}
+			// }(buf, n)
 
 
 		}
 		}
 	}
 	}

+ 69 - 70
nm-proxy/server/server.go

@@ -2,7 +2,6 @@ package server
 
 
 import (
 import (
 	"context"
 	"context"
-	"fmt"
 	"log"
 	"log"
 	"net"
 	"net"
 	"time"
 	"time"
@@ -36,7 +35,7 @@ type ProxyServer struct {
 func (p *ProxyServer) Listen(ctx context.Context) {
 func (p *ProxyServer) Listen(ctx context.Context) {
 
 
 	// Buffer with indicated body size
 	// Buffer with indicated body size
-
+	buffer := make([]byte, 65535+32)
 	for {
 	for {
 
 
 		select {
 		select {
@@ -54,97 +53,97 @@ func (p *ProxyServer) Listen(ctx context.Context) {
 			return
 			return
 		default:
 		default:
 			// Read Packet
 			// Read Packet
-			buffer := make([]byte, 1532)
+
 			n, source, err := p.Server.ReadFromUDP(buffer)
 			n, source, err := p.Server.ReadFromUDP(buffer)
 			if err != nil { // in future log errors?
 			if err != nil { // in future log errors?
 				log.Println("RECV ERROR: ", err)
 				log.Println("RECV ERROR: ", err)
 				continue
 				continue
 			}
 			}
-			go func(buffer []byte, source *net.UDPAddr, n int) {
-
-				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 {
+			// go func(buffer []byte, source *net.UDPAddr, n int) {
+
+			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)
+							}
+							return
+						}
+					} else {
+						if remoteMap, ok := common.RelayPeerMap[dstPeerKeyHash]; ok {
 							if conf, ok := remoteMap[dstPeerKeyHash]; 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)
+								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)
 								_, 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)
 								}
 								}
 								return
 								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)
+							return
 						}
 						}
-					}
-					return
 
 
+					}
 				}
 				}
-				// // 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()
-				// 	}
+				return
 
 
-				// }
-			}(buffer, source, n)
+			}
+			// // 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()
+			// 	}
+
+			// }
+			// }(buffer, source, n)
 		}
 		}
 
 
 	}
 	}