| // |
| 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 | //go:build !frps |
| 16 |
| 17 | package proxy |
| 18 |
| 19 | import ( |
| 20 | "net" |
| 21 | "reflect" |
| 22 | "strconv" |
| 23 | "time" |
| 24 |
| 25 | "github.com/fatedier/golib/errors" |
| 26 |
| 27 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 28 | "github.com/fatedier/frp/pkg/msg" |
| 29 | "github.com/fatedier/frp/pkg/proto/udp" |
| 30 | netpkg "github.com/fatedier/frp/pkg/util/net" |
| 31 | ) |
| 32 |
| 33 | func init() { |
| 34 | RegisterProxyFactory(reflect.TypeFor[*v1.UDPProxyConfig](), NewUDPProxy) |
| 35 | } |
| 36 |
| 37 | type UDPProxy struct { |
| 38 | *BaseProxy |
| 39 |
| 40 | cfg *v1.UDPProxyConfig |
| 41 |
| 42 | localAddr *net.UDPAddr |
| 43 | readCh chan *msg.UDPPacket |
| 44 |
| 45 | // include msg.UDPPacket and msg.Ping |
| 46 | sendCh chan msg.Message |
| 47 | workConn net.Conn |
| 48 | closed bool |
| 49 | } |
| 50 |
| 51 | func NewUDPProxy(baseProxy *BaseProxy, cfg v1.ProxyConfigurer) Proxy { |
| 52 | unwrapped, ok := cfg.(*v1.UDPProxyConfig) |
| 53 | if !ok { |
| 54 | return nil |
| 55 | } |
| 56 | return &UDPProxy{ |
| 57 | BaseProxy: baseProxy, |
| 58 | cfg: unwrapped, |
| 59 | } |
| 60 | } |
| 61 |
| 62 | func (pxy *UDPProxy) Run() (err error) { |
| 63 | pxy.localAddr, err = net.ResolveUDPAddr("udp", net.JoinHostPort(pxy.cfg.LocalIP, strconv.Itoa(pxy.cfg.LocalPort))) |
| 64 | if err != nil { |
| 65 | return |
| 66 | } |
| 67 | return |
| 68 | } |
| 69 |
| 70 | func (pxy *UDPProxy) Close() { |
| 71 | pxy.mu.Lock() |
| 72 | defer pxy.mu.Unlock() |
| 73 |
| 74 | if !pxy.closed { |
| 75 | pxy.closed = true |
| 76 | if pxy.workConn != nil { |
| 77 | pxy.workConn.Close() |
| 78 | } |
| 79 | if pxy.readCh != nil { |
| 80 | close(pxy.readCh) |
| 81 | } |
| 82 | if pxy.sendCh != nil { |
| 83 | close(pxy.sendCh) |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 |
| 88 | func (pxy *UDPProxy) InWorkConn(conn net.Conn, _ *msg.StartWorkConn) { |
| 89 | xl := pxy.xl |
| 90 | xl.Infof("incoming a new work connection for udp proxy, %s", conn.RemoteAddr().String()) |
| 91 | // close resources related with old workConn |
| 92 | pxy.Close() |
| 93 |
| 94 | remote, _, err := pxy.wrapWorkConn(conn, pxy.encryptionKey) |
| 95 | if err != nil { |
| 96 | xl.Errorf("wrap work connection: %v", err) |
| 97 | return |
| 98 | } |
| 99 |
| 100 | pxy.mu.Lock() |
| 101 | pxy.workConn = netpkg.WrapReadWriteCloserToConn(remote, conn) |
| 102 | // Plain UDP payload follows the configured wire protocol for message framing. |
| 103 | payloadRW := msg.NewReadWriter(pxy.workConn, pxy.clientCfg.Transport.WireProtocol) |
| 104 | pxy.readCh = make(chan *msg.UDPPacket, 1024) |
| 105 | pxy.sendCh = make(chan msg.Message, 1024) |
| 106 | pxy.closed = false |
| 107 | pxy.mu.Unlock() |
| 108 |
| 109 | workConnReaderFn := func(rw msg.ReadWriter, readCh chan *msg.UDPPacket) { |
| 110 | for { |
| 111 | var udpMsg msg.UDPPacket |
| 112 | if errRet := rw.ReadMsgInto(&udpMsg); errRet != nil { |
| 113 | xl.Warnf("read from workConn for udp error: %v", errRet) |
| 114 | return |
| 115 | } |
| 116 | if errRet := errors.PanicToError(func() { |
| 117 | xl.Tracef("get udp package from workConn, len: %d", len(udpMsg.Content)) |
| 118 | readCh <- &udpMsg |
| 119 | }); errRet != nil { |
| 120 | xl.Infof("reader goroutine for udp work connection closed: %v", errRet) |
| 121 | return |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | workConnSenderFn := func(rw msg.ReadWriter, sendCh chan msg.Message) { |
| 126 | defer func() { |
| 127 | xl.Infof("writer goroutine for udp work connection closed") |
| 128 | }() |
| 129 | var errRet error |
| 130 | for rawMsg := range sendCh { |
| 131 | switch m := rawMsg.(type) { |
| 132 | case *msg.UDPPacket: |
| 133 | xl.Tracef("send udp package to workConn, len: %d", len(m.Content)) |
| 134 | case *msg.Ping: |
| 135 | xl.Tracef("send ping message to udp workConn") |
| 136 | } |
| 137 | if errRet = rw.WriteMsg(rawMsg); errRet != nil { |
| 138 | xl.Errorf("udp work write error: %v", errRet) |
| 139 | return |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | heartbeatFn := func(sendCh chan msg.Message) { |
| 144 | var errRet error |
| 145 | for { |
| 146 | time.Sleep(time.Duration(30) * time.Second) |
| 147 | if errRet = errors.PanicToError(func() { |
| 148 | sendCh <- &msg.Ping{} |
| 149 | }); errRet != nil { |
| 150 | xl.Tracef("heartbeat goroutine for udp work connection closed") |
| 151 | break |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 |
| 156 | go workConnSenderFn(payloadRW, pxy.sendCh) |
| 157 | go workConnReaderFn(payloadRW, pxy.readCh) |
| 158 | go heartbeatFn(pxy.sendCh) |
| 159 |
| 160 | // Call Forwarder with proxy protocol version (empty string means no proxy protocol) |
| 161 | udp.Forwarder(pxy.localAddr, pxy.readCh, pxy.sendCh, int(pxy.clientCfg.UDPPacketSize), pxy.cfg.Transport.ProxyProtocolVersion) |
| 162 | } |