Browse Source

Added working UDP proxy

Toby Chui 9 months ago
parent
commit
c38362861f
3 changed files with 51 additions and 18 deletions
  1. 10 2
      mod/tcpprox/conn.go
  2. 6 6
      mod/tcpprox/tcpprox.go
  3. 35 10
      mod/tcpprox/udpprox.go

+ 10 - 2
mod/tcpprox/conn.go

@@ -133,9 +133,17 @@ func (c *ProxyRelayConfig) ValidateConfigs() error {
 		}
 
 		return nil
-	} else {
-		return errors.New("invalid mode given")
+	} else if c.Mode == ProxyMode_UDP {
+		//UDP
+		if !isValidPort(c.PortA) {
+			return errors.New("first address must be a valid port number")
+		}
+
+		return nil
 	}
+
+	return errors.New("invalid mode given")
+
 }
 
 // Start a proxy if stopped

+ 6 - 6
mod/tcpprox/tcpprox.go

@@ -44,8 +44,8 @@ type ProxyRelayConfig struct {
 	stopChan                    chan bool    //Stop channel to stop the listener
 	aTobAccumulatedByteTransfer atomic.Int64 //Accumulated byte transfer from A to B
 	bToaAccumulatedByteTransfer atomic.Int64 //Accumulated byte transfer from B to A
-
-	parent *Manager `json:"-"`
+	udpClientMap                sync.Map     //map storing the UDP client-server connections
+	parent                      *Manager     `json:"-"`
 }
 
 type Options struct {
@@ -60,8 +60,8 @@ type Manager struct {
 	Configs []*ProxyRelayConfig
 
 	//Realtime Statistics
-	Connections  int      //currently connected connect counts
-	UDPClientMap sync.Map //map storing the UDP client-server connections
+	Connections int //currently connected connect counts
+
 }
 
 func NewTCProxy(options *Options) *Manager {
@@ -116,8 +116,8 @@ func (m *Manager) NewConfig(config *ProxyRelayOptions) string {
 		stopChan:                    nil,
 		aTobAccumulatedByteTransfer: aAcc,
 		bToaAccumulatedByteTransfer: bAcc,
-
-		parent: m,
+		udpClientMap:                sync.Map{},
+		parent:                      m,
 	}
 	m.Configs = append(m.Configs, &thisConfig)
 	m.SaveConfigToDatabase()

+ 35 - 10
mod/tcpprox/udpprox.go

@@ -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
 		}
 	}