|
@@ -1,6 +1,7 @@
|
|
|
package tcpprox
|
|
|
|
|
|
import (
|
|
|
+ "fmt"
|
|
|
"log"
|
|
|
"net"
|
|
|
)
|
|
@@ -49,7 +50,30 @@ func initUDPConnections(listenAddr string, targetAddress string) (*net.UDPConn,
|
|
|
return inboundConn, outboundConn, nil
|
|
|
}
|
|
|
|
|
|
+// Go routine which manages connection from server to single client
|
|
|
+func (c *ProxyRelayConfig) RunUDPConnection(conn *udpClientServerConn, lisenter *net.UDPConn) {
|
|
|
+ var buffer [1500]byte
|
|
|
+ for {
|
|
|
+ // Read from server
|
|
|
+ n, err := conn.ServerConn.Read(buffer[0:])
|
|
|
+ if err != nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ // Relay it to client
|
|
|
+ _, err = lisenter.WriteToUDP(buffer[0:n], conn.ClientAddr)
|
|
|
+ if err != nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ log.Println("[UDP] Relayed '%s' from server to %s.\n",
|
|
|
+ string(buffer[0:n]), conn.ClientAddr.String())
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func (c *ProxyRelayConfig) ForwardUDP(address1, address2 string, stopChan chan bool) error {
|
|
|
+ //By default the incoming listen Address is int
|
|
|
+ //We need to add the loopback address into it
|
|
|
+ address1 = "127.0.0.1:" + address1
|
|
|
+ fmt.Println(address1, address2)
|
|
|
lisener, targetAddr, err := initUDPConnections(address1, address2)
|
|
|
if err != nil {
|
|
|
return err
|
|
@@ -63,26 +87,27 @@ func (c *ProxyRelayConfig) ForwardUDP(address1, address2 string, stopChan chan b
|
|
|
}
|
|
|
c.aTobAccumulatedByteTransfer.Add(int64(n))
|
|
|
saddr := cliaddr.String()
|
|
|
- dlock()
|
|
|
- conn, found := ClientDict[saddr]
|
|
|
+ rawConn, found := c.udpClientMap.Load(saddr)
|
|
|
+ var conn *udpClientServerConn
|
|
|
if !found {
|
|
|
conn = createNewUDPConn(targetAddr, cliaddr)
|
|
|
if conn == nil {
|
|
|
- dunlock()
|
|
|
continue
|
|
|
}
|
|
|
- ClientDict[saddr] = conn
|
|
|
- dunlock()
|
|
|
- Vlogf(2, "Created new connection for client %s\n", saddr)
|
|
|
+ c.udpClientMap.Store(saddr, conn)
|
|
|
+ fmt.Println(saddr)
|
|
|
+ log.Println("[UDP] Created new connection for client " + saddr)
|
|
|
// Fire up routine to manage new connection
|
|
|
- go RunConnection(conn)
|
|
|
+ go c.RunUDPConnection(conn, lisener)
|
|
|
+
|
|
|
} else {
|
|
|
- Vlogf(5, "Found connection for client %s\n", saddr)
|
|
|
- dunlock()
|
|
|
+ log.Println("[UDP] Found connection for client " + saddr)
|
|
|
+ conn = rawConn.(*udpClientServerConn)
|
|
|
}
|
|
|
+
|
|
|
// Relay to server
|
|
|
_, err = conn.ServerConn.Write(buffer[0:n])
|
|
|
- if checkreport(1, err) {
|
|
|
+ if err != nil {
|
|
|
continue
|
|
|
}
|
|
|
}
|