process_udp.go.bak 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2016 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package client
  15. import (
  16. "fmt"
  17. "io"
  18. "net"
  19. "sync"
  20. "time"
  21. "github.com/fatedier/frp/src/models/msg"
  22. "github.com/fatedier/frp/src/utils/conn"
  23. "github.com/fatedier/frp/src/utils/pool"
  24. )
  25. type UdpProcesser struct {
  26. tcpConn *conn.Conn
  27. closeCh chan struct{}
  28. localAddr string
  29. // cache local udp connections
  30. // key is remoteAddr
  31. localUdpConns map[string]*net.UDPConn
  32. mutex sync.RWMutex
  33. tcpConnMutex sync.RWMutex
  34. }
  35. func NewUdpProcesser(c *conn.Conn, localIp string, localPort int64) *UdpProcesser {
  36. return &UdpProcesser{
  37. tcpConn: c,
  38. closeCh: make(chan struct{}),
  39. localAddr: fmt.Sprintf("%s:%d", localIp, localPort),
  40. localUdpConns: make(map[string]*net.UDPConn),
  41. }
  42. }
  43. func (up *UdpProcesser) UpdateTcpConn(c *conn.Conn) {
  44. up.tcpConnMutex.Lock()
  45. defer up.tcpConnMutex.Unlock()
  46. up.tcpConn = c
  47. }
  48. func (up *UdpProcesser) Run() {
  49. go up.ReadLoop()
  50. }
  51. func (up *UdpProcesser) ReadLoop() {
  52. var (
  53. buf string
  54. err error
  55. )
  56. for {
  57. udpPacket := &msg.UdpPacket{}
  58. // read udp package from frps
  59. buf, err = up.tcpConn.ReadLine()
  60. if err != nil {
  61. if err == io.EOF {
  62. return
  63. } else {
  64. continue
  65. }
  66. }
  67. err = udpPacket.UnPack([]byte(buf))
  68. if err != nil {
  69. continue
  70. }
  71. // write to local udp port
  72. sendConn, ok := up.GetUdpConn(udpPacket.SrcStr)
  73. if !ok {
  74. dstAddr, err := net.ResolveUDPAddr("udp", up.localAddr)
  75. if err != nil {
  76. continue
  77. }
  78. sendConn, err = net.DialUDP("udp", nil, dstAddr)
  79. if err != nil {
  80. continue
  81. }
  82. up.SetUdpConn(udpPacket.SrcStr, sendConn)
  83. }
  84. _, err = sendConn.Write(udpPacket.Content)
  85. if err != nil {
  86. sendConn.Close()
  87. continue
  88. }
  89. if !ok {
  90. go up.Forward(udpPacket, sendConn)
  91. }
  92. }
  93. }
  94. func (up *UdpProcesser) Forward(udpPacket *msg.UdpPacket, singleConn *net.UDPConn) {
  95. addr := udpPacket.SrcStr
  96. defer up.RemoveUdpConn(addr)
  97. buf := pool.GetBuf(2048)
  98. for {
  99. singleConn.SetReadDeadline(time.Now().Add(120 * time.Second))
  100. n, remoteAddr, err := singleConn.ReadFromUDP(buf)
  101. if err != nil {
  102. return
  103. }
  104. // forward to frps
  105. forwardPacket := msg.NewUdpPacket(buf[0:n], remoteAddr, udpPacket.Src)
  106. up.tcpConnMutex.RLock()
  107. err = up.tcpConn.WriteString(string(forwardPacket.Pack()) + "\n")
  108. up.tcpConnMutex.RUnlock()
  109. if err != nil {
  110. return
  111. }
  112. }
  113. }
  114. func (up *UdpProcesser) GetUdpConn(addr string) (singleConn *net.UDPConn, ok bool) {
  115. up.mutex.RLock()
  116. defer up.mutex.RUnlock()
  117. singleConn, ok = up.localUdpConns[addr]
  118. return
  119. }
  120. func (up *UdpProcesser) SetUdpConn(addr string, conn *net.UDPConn) {
  121. up.mutex.Lock()
  122. defer up.mutex.Unlock()
  123. up.localUdpConns[addr] = conn
  124. }
  125. func (up *UdpProcesser) RemoveUdpConn(addr string) {
  126. up.mutex.Lock()
  127. defer up.mutex.Unlock()
  128. if c, ok := up.localUdpConns[addr]; ok {
  129. c.Close()
  130. }
  131. delete(up.localUdpConns, addr)
  132. }