|
@@ -1,6 +1,7 @@
|
|
|
package server
|
|
|
|
|
|
import (
|
|
|
+ "context"
|
|
|
"fmt"
|
|
|
"log"
|
|
|
"net"
|
|
@@ -32,63 +33,80 @@ type ProxyServer struct {
|
|
|
}
|
|
|
|
|
|
// Proxy.Listen - begins listening for packets
|
|
|
-func (p *ProxyServer) Listen() {
|
|
|
+func (p *ProxyServer) Listen(ctx context.Context) {
|
|
|
|
|
|
// Buffer with indicated body size
|
|
|
buffer := make([]byte, 1532)
|
|
|
for {
|
|
|
- // Read Packet
|
|
|
- n, source, err := p.Server.ReadFromUDP(buffer)
|
|
|
- if err != nil { // in future log errors?
|
|
|
- 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 common.IsRelay && dstPeerKeyHash != "" && srcPeerKeyHash != "" {
|
|
|
- if _, ok := common.WgIfaceKeyMap[dstPeerKeyHash]; !ok {
|
|
|
-
|
|
|
- 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)
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- if remoteMap, ok := common.RelayPeerMap[dstPeerKeyHash]; ok {
|
|
|
+
|
|
|
+ select {
|
|
|
+ case <-ctx.Done():
|
|
|
+ log.Println("--------->### Shutting down Proxy.....")
|
|
|
+ // clean up proxy connections
|
|
|
+ for iface, peers := range common.WgIFaceMap {
|
|
|
+ log.Println("########------------> CLEANING UP: ", iface)
|
|
|
+ for _, peerI := range peers {
|
|
|
+ peerI.Proxy.Cancel()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // close server connection
|
|
|
+ NmProxyServer.Server.Close()
|
|
|
+ return
|
|
|
+ default:
|
|
|
+ // Read Packet
|
|
|
+ n, source, err := p.Server.ReadFromUDP(buffer)
|
|
|
+ if err != nil { // in future log errors?
|
|
|
+ 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 common.IsRelay && dstPeerKeyHash != "" && srcPeerKeyHash != "" {
|
|
|
+ if _, ok := common.WgIfaceKeyMap[dstPeerKeyHash]; !ok {
|
|
|
+
|
|
|
+ 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)
|
|
|
}
|
|
|
}
|
|
|
+ } 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)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- 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
|
|
|
- }
|
|
|
+ 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
|
|
|
+ }
|
|
|
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|
|
|
}
|