udpprox.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package streamproxy
  2. import (
  3. "errors"
  4. "log"
  5. "net"
  6. "strings"
  7. "time"
  8. )
  9. /*
  10. UDP Proxy Module
  11. */
  12. // Information maintained for each client/server connection
  13. type udpClientServerConn struct {
  14. ClientAddr *net.UDPAddr // Address of the client
  15. ServerConn *net.UDPConn // UDP connection to server
  16. }
  17. // Generate a new connection by opening a UDP connection to the server
  18. func createNewUDPConn(srvAddr, cliAddr *net.UDPAddr) *udpClientServerConn {
  19. conn := new(udpClientServerConn)
  20. conn.ClientAddr = cliAddr
  21. srvudp, err := net.DialUDP("udp", nil, srvAddr)
  22. if err != nil {
  23. return nil
  24. }
  25. conn.ServerConn = srvudp
  26. return conn
  27. }
  28. // Start listener, return inbound lisener and proxy target UDP address
  29. func initUDPConnections(listenAddr string, targetAddress string) (*net.UDPConn, *net.UDPAddr, error) {
  30. // Set up Proxy
  31. saddr, err := net.ResolveUDPAddr("udp", listenAddr)
  32. if err != nil {
  33. return nil, nil, err
  34. }
  35. inboundConn, err := net.ListenUDP("udp", saddr)
  36. if err != nil {
  37. return nil, nil, err
  38. }
  39. log.Println("[UDP] Proxy listening on " + listenAddr)
  40. outboundConn, err := net.ResolveUDPAddr("udp", targetAddress)
  41. if err != nil {
  42. return nil, nil, err
  43. }
  44. return inboundConn, outboundConn, nil
  45. }
  46. // Go routine which manages connection from server to single client
  47. func (c *ProxyRelayConfig) RunUDPConnectionRelay(conn *udpClientServerConn, lisenter *net.UDPConn) {
  48. var buffer [1500]byte
  49. for {
  50. // Read from server
  51. n, err := conn.ServerConn.Read(buffer[0:])
  52. if err != nil {
  53. if errors.Is(err, net.ErrClosed) {
  54. return
  55. }
  56. continue
  57. }
  58. // Relay it to client
  59. _, err = lisenter.WriteToUDP(buffer[0:n], conn.ClientAddr)
  60. if err != nil {
  61. continue
  62. }
  63. }
  64. }
  65. // Close all connections that waiting for read from server
  66. func (c *ProxyRelayConfig) CloseAllUDPConnections() {
  67. c.udpClientMap.Range(func(clientAddr, clientServerConn interface{}) bool {
  68. conn := clientServerConn.(*udpClientServerConn)
  69. conn.ServerConn.Close()
  70. return true
  71. })
  72. }
  73. func (c *ProxyRelayConfig) ForwardUDP(address1, address2 string, stopChan chan bool) error {
  74. //By default the incoming listen Address is int
  75. //We need to add the loopback address into it
  76. if isValidPort(address1) {
  77. //Port number only. Missing the : in front
  78. address1 = ":" + address1
  79. }
  80. if strings.HasPrefix(address1, ":") {
  81. //Prepend 127.0.0.1 to the address
  82. address1 = "127.0.0.1" + address1
  83. }
  84. lisener, targetAddr, err := initUDPConnections(address1, address2)
  85. if err != nil {
  86. return err
  87. }
  88. go func() {
  89. //Stop channel receiver
  90. for {
  91. select {
  92. case <-stopChan:
  93. //Stop signal received
  94. //Stop server -> client forwarder
  95. c.CloseAllUDPConnections()
  96. //Stop client -> server forwarder
  97. //Force close, will terminate ReadFromUDP for inbound listener
  98. lisener.Close()
  99. return
  100. default:
  101. time.Sleep(100 * time.Millisecond)
  102. }
  103. }
  104. }()
  105. var buffer [1500]byte
  106. for {
  107. n, cliaddr, err := lisener.ReadFromUDP(buffer[0:])
  108. if err != nil {
  109. if errors.Is(err, net.ErrClosed) {
  110. //Proxy stopped
  111. return nil
  112. }
  113. continue
  114. }
  115. c.aTobAccumulatedByteTransfer.Add(int64(n))
  116. saddr := cliaddr.String()
  117. rawConn, found := c.udpClientMap.Load(saddr)
  118. var conn *udpClientServerConn
  119. if !found {
  120. conn = createNewUDPConn(targetAddr, cliaddr)
  121. if conn == nil {
  122. continue
  123. }
  124. c.udpClientMap.Store(saddr, conn)
  125. log.Println("[UDP] Created new connection for client " + saddr)
  126. // Fire up routine to manage new connection
  127. go c.RunUDPConnectionRelay(conn, lisener)
  128. } else {
  129. log.Println("[UDP] Found connection for client " + saddr)
  130. conn = rawConn.(*udpClientServerConn)
  131. }
  132. // Relay to server
  133. _, err = conn.ServerConn.Write(buffer[0:n])
  134. if err != nil {
  135. continue
  136. }
  137. }
  138. }