mirror of
https://github.com/gravitl/netmaker.git
synced 2025-11-08 07:41:41 +08:00
speed test
This commit is contained in:
parent
3f279119c7
commit
a75879109a
4 changed files with 90 additions and 81 deletions
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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(), )
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
// }
|
||||
|
||||
// }
|
||||
// }
|
||||
}()
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue