| // |
| 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 |
| 15 | package udp |
| 16 |
| 17 | import ( |
| 18 | "net" |
| 19 | "sync" |
| 20 | "time" |
| 21 |
| 22 | "github.com/fatedier/golib/errors" |
| 23 | "github.com/fatedier/golib/pool" |
| 24 |
| 25 | "github.com/fatedier/frp/pkg/msg" |
| 26 | netpkg "github.com/fatedier/frp/pkg/util/net" |
| 27 | ) |
| 28 |
| 29 | func NewUDPPacket(buf []byte, laddr, raddr *net.UDPAddr) *msg.UDPPacket { |
| 30 | content := make([]byte, len(buf)) |
| 31 | copy(content, buf) |
| 32 | return &msg.UDPPacket{ |
| 33 | Content: content, |
| 34 | LocalAddr: laddr, |
| 35 | RemoteAddr: raddr, |
| 36 | } |
| 37 | } |
| 38 |
| 39 | func GetContent(m *msg.UDPPacket) (buf []byte, err error) { |
| 40 | return m.Content, nil |
| 41 | } |
| 42 |
| 43 | func ForwardUserConn(udpConn *net.UDPConn, readCh <-chan *msg.UDPPacket, sendCh chan<- *msg.UDPPacket, bufSize int) { |
| 44 | // read |
| 45 | go func() { |
| 46 | for udpMsg := range readCh { |
| 47 | buf, err := GetContent(udpMsg) |
| 48 | if err != nil { |
| 49 | continue |
| 50 | } |
| 51 | _, _ = udpConn.WriteToUDP(buf, udpMsg.RemoteAddr) |
| 52 | } |
| 53 | }() |
| 54 |
| 55 | // write |
| 56 | buf := pool.GetBuf(bufSize) |
| 57 | defer pool.PutBuf(buf) |
| 58 | for { |
| 59 | n, remoteAddr, err := udpConn.ReadFromUDP(buf) |
| 60 | if err != nil { |
| 61 | return |
| 62 | } |
| 63 | // NewUDPPacket copies buf[:n], so the read buffer can be reused |
| 64 | udpMsg := NewUDPPacket(buf[:n], nil, remoteAddr) |
| 65 |
| 66 | select { |
| 67 | case sendCh <- udpMsg: |
| 68 | default: |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 |
| 73 | func Forwarder(dstAddr *net.UDPAddr, readCh <-chan *msg.UDPPacket, sendCh chan<- msg.Message, bufSize int, proxyProtocolVersion string) { |
| 74 | var mu sync.RWMutex |
| 75 | udpConnMap := make(map[string]*net.UDPConn) |
| 76 |
| 77 | // read from dstAddr and write to sendCh |
| 78 | writerFn := func(raddr *net.UDPAddr, udpConn *net.UDPConn) { |
| 79 | addr := raddr.String() |
| 80 | defer func() { |
| 81 | mu.Lock() |
| 82 | delete(udpConnMap, addr) |
| 83 | mu.Unlock() |
| 84 | udpConn.Close() |
| 85 | }() |
| 86 |
| 87 | buf := pool.GetBuf(bufSize) |
| 88 | defer pool.PutBuf(buf) |
| 89 | for { |
| 90 | _ = udpConn.SetReadDeadline(time.Now().Add(30 * time.Second)) |
| 91 | n, _, err := udpConn.ReadFromUDP(buf) |
| 92 | if err != nil { |
| 93 | return |
| 94 | } |
| 95 |
| 96 | udpMsg := NewUDPPacket(buf[:n], nil, raddr) |
| 97 | if err = errors.PanicToError(func() { |
| 98 | select { |
| 99 | case sendCh <- udpMsg: |
| 100 | default: |
| 101 | } |
| 102 | }); err != nil { |
| 103 | return |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 |
| 108 | // read from readCh |
| 109 | go func() { |
| 110 | for udpMsg := range readCh { |
| 111 | buf, err := GetContent(udpMsg) |
| 112 | if err != nil { |
| 113 | continue |
| 114 | } |
| 115 |
| 116 | mu.Lock() |
| 117 | udpConn, ok := udpConnMap[udpMsg.RemoteAddr.String()] |
| 118 | if !ok { |
| 119 | udpConn, err = net.DialUDP("udp", nil, dstAddr) |
| 120 | if err != nil { |
| 121 | mu.Unlock() |
| 122 | continue |
| 123 | } |
| 124 | udpConnMap[udpMsg.RemoteAddr.String()] = udpConn |
| 125 | } |
| 126 | mu.Unlock() |
| 127 |
| 128 | // Add proxy protocol header if configured (only for the first packet of a new connection) |
| 129 | if !ok && proxyProtocolVersion != "" && udpMsg.RemoteAddr != nil { |
| 130 | ppBuf, err := netpkg.BuildProxyProtocolHeader(udpMsg.RemoteAddr, dstAddr, proxyProtocolVersion) |
| 131 | if err == nil { |
| 132 | // Prepend proxy protocol header to the UDP payload |
| 133 | finalBuf := make([]byte, len(ppBuf)+len(buf)) |
| 134 | copy(finalBuf, ppBuf) |
| 135 | copy(finalBuf[len(ppBuf):], buf) |
| 136 | buf = finalBuf |
| 137 | } |
| 138 | } |
| 139 |
| 140 | _, err = udpConn.Write(buf) |
| 141 | if err != nil { |
| 142 | udpConn.Close() |
| 143 | } else { |
| 144 | _ = udpConn.SetReadDeadline(time.Now().Add(30 * time.Second)) |
| 145 | } |
| 146 |
| 147 | if !ok { |
| 148 | go writerFn(udpMsg.RemoteAddr, udpConn) |
| 149 | } |
| 150 | } |
| 151 | }() |
| 152 | } |