speed test

This commit is contained in:
Abhishek Kondur 2022-11-15 22:48:07 +05:30
parent 3f279119c7
commit a75879109a
4 changed files with 90 additions and 81 deletions

View file

@ -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()

View file

@ -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(), )
} }

View file

@ -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] go func() {
if peerI, ok := peers[p.Config.RemoteKey]; ok {
var srcPeerKeyHash, dstPeerKeyHash string if peerI, ok := peers[p.Config.RemoteKey]; ok {
buf, n, srcPeerKeyHash, dstPeerKeyHash = packet.ProcessPacketBeforeSending(buf, n, peerI.Config.LocalKey, peerI.Config.Key) var srcPeerKeyHash, dstPeerKeyHash string
if err != nil { buf, n, srcPeerKeyHash, dstPeerKeyHash = packet.ProcessPacketBeforeSending(buf, n, peerI.Config.LocalKey, peerI.Config.Key)
log.Println("failed to process pkt before sending: ", err) 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) log.Printf("PROXING TO REMOTE!!!---> %s >>>>> %s >>>>> %s [[ SrcPeerHash: %s, DstPeerHash: %s ]]\n",
} else { p.LocalConn.LocalAddr(), server.NmProxyServer.Server.LocalAddr().String(), p.RemoteConn.String(), srcPeerKeyHash, dstPeerKeyHash)
log.Printf("Peer: %s not found in config\n", p.Config.RemoteKey) } else {
p.Cancel() log.Printf("Peer: %s not found in config\n", p.Config.RemoteKey)
continue p.Cancel()
} return
//test(n, buf) }
//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)
}
} }
} }
} }

View file

@ -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 go func() {
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", var srcPeerKeyHash, dstPeerKeyHash string
// source.String(), srcPeerKeyHash, val.Endpoint.String(), dstPeerKeyHash) n, srcPeerKeyHash, dstPeerKeyHash = packet.ExtractInfo(buffer, n)
// _, err = NmProxyServer.Server.WriteToUDP(buffer[:n], val.Endpoint) //log.Printf("--------> RECV PKT [DSTPORT: %d], [SRCKEYHASH: %s], SourceIP: [%s] \n", localWgPort, srcPeerKeyHash, source.IP.String())
// if err != nil { if _, ok := common.WgIfaceKeyMap[dstPeerKeyHash]; !ok {
// log.Println("Failed to send to remote: ", err) // if common.IsIngressGateway {
// } // log.Println("----> fowarding PKT to EXT client...")
// continue // 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######") if common.IsRelay {
// check for routing map and forward to right proxy
if remoteMap, ok := common.RelayPeerMap[srcPeerKeyHash]; ok { log.Println("----------> Relaying######")
if conf, ok := remoteMap[dstPeerKeyHash]; ok { // check for routing map and forward to right proxy
log.Printf("--------> Relaying PKT [ SourceIP: %s:%d ], [ SourceKeyHash: %s ], [ DstIP: %s:%d ], [ DstHashKey: %s ] \n", if remoteMap, ok := common.RelayPeerMap[srcPeerKeyHash]; ok {
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 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", log.Printf("--------> Relaying PKT [ SourceIP: %s:%d ], [ SourceKeyHash: %s ], [ DstIP: %s:%d ], [ DstHashKey: %s ] \n",
source.String(), srcPeerKeyHash, conf.Endpoint.String(), dstPeerKeyHash) 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()
// }
// }
} }
} }