| // |
| 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 | "sync" |
| 24 | "time" |
| 25 |
| 26 | "github.com/fatedier/golib/errors" |
| 27 |
| 28 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 29 | "github.com/fatedier/frp/pkg/msg" |
| 30 | "github.com/fatedier/frp/pkg/proto/udp" |
| 31 | netpkg "github.com/fatedier/frp/pkg/util/net" |
| 32 | ) |
| 33 |
| 34 | func init() { |
| 35 | RegisterProxyFactory(reflect.TypeFor[*v1.SUDPProxyConfig](), NewSUDPProxy) |
| 36 | } |
| 37 |
| 38 | type SUDPProxy struct { |
| 39 | *BaseProxy |
| 40 |
| 41 | cfg *v1.SUDPProxyConfig |
| 42 |
| 43 | localAddr *net.UDPAddr |
| 44 |
| 45 | closeCh chan struct{} |
| 46 | } |
| 47 |
| 48 | func NewSUDPProxy(baseProxy *BaseProxy, cfg v1.ProxyConfigurer) Proxy { |
| 49 | unwrapped, ok := cfg.(*v1.SUDPProxyConfig) |
| 50 | if !ok { |
| 51 | return nil |
| 52 | } |
| 53 | return &SUDPProxy{ |
| 54 | BaseProxy: baseProxy, |
| 55 | cfg: unwrapped, |
| 56 | closeCh: make(chan struct{}), |
| 57 | } |
| 58 | } |
| 59 |
| 60 | func (pxy *SUDPProxy) Run() (err error) { |
| 61 | pxy.localAddr, err = net.ResolveUDPAddr("udp", net.JoinHostPort(pxy.cfg.LocalIP, strconv.Itoa(pxy.cfg.LocalPort))) |
| 62 | if err != nil { |
| 63 | return |
| 64 | } |
| 65 | return |
| 66 | } |
| 67 |
| 68 | func (pxy *SUDPProxy) Close() { |
| 69 | pxy.mu.Lock() |
| 70 | defer pxy.mu.Unlock() |
| 71 | select { |
| 72 | case <-pxy.closeCh: |
| 73 | return |
| 74 | default: |
| 75 | close(pxy.closeCh) |
| 76 | } |
| 77 | } |
| 78 |
| 79 | func (pxy *SUDPProxy) InWorkConn(conn net.Conn, _ *msg.StartWorkConn) { |
| 80 | xl := pxy.xl |
| 81 | xl.Infof("incoming a new work connection for sudp proxy, %s", conn.RemoteAddr().String()) |
| 82 |
| 83 | remote, _, err := pxy.wrapWorkConn(conn, pxy.encryptionKey) |
| 84 | if err != nil { |
| 85 | xl.Errorf("wrap work connection: %v", err) |
| 86 | return |
| 87 | } |
| 88 |
| 89 | workConn := netpkg.WrapReadWriteCloserToConn(remote, conn) |
| 90 | payloadConn := msg.NewConn(workConn, msg.NewReadWriter(workConn, pxy.clientCfg.Transport.WireProtocol)) |
| 91 | readCh := make(chan *msg.UDPPacket, 1024) |
| 92 | sendCh := make(chan msg.Message, 1024) |
| 93 | isClose := false |
| 94 |
| 95 | mu := &sync.Mutex{} |
| 96 |
| 97 | closeFn := func() { |
| 98 | mu.Lock() |
| 99 | defer mu.Unlock() |
| 100 | if isClose { |
| 101 | return |
| 102 | } |
| 103 |
| 104 | isClose = true |
| 105 | if workConn != nil { |
| 106 | workConn.Close() |
| 107 | } |
| 108 | close(readCh) |
| 109 | close(sendCh) |
| 110 | } |
| 111 |
| 112 | // udp service <- frpc <- frps <- frpc visitor <- user |
| 113 | workConnReaderFn := func(payloadConn *msg.Conn, readCh chan *msg.UDPPacket) { |
| 114 | defer closeFn() |
| 115 |
| 116 | for { |
| 117 | // first to check sudp proxy is closed or not |
| 118 | select { |
| 119 | case <-pxy.closeCh: |
| 120 | xl.Tracef("frpc sudp proxy is closed") |
| 121 | return |
| 122 | default: |
| 123 | } |
| 124 |
| 125 | var udpMsg msg.UDPPacket |
| 126 | if errRet := payloadConn.ReadMsgInto(&udpMsg); errRet != nil { |
| 127 | xl.Warnf("read from workConn for sudp error: %v", errRet) |
| 128 | return |
| 129 | } |
| 130 |
| 131 | if errRet := errors.PanicToError(func() { |
| 132 | readCh <- &udpMsg |
| 133 | }); errRet != nil { |
| 134 | xl.Warnf("reader goroutine for sudp work connection closed: %v", errRet) |
| 135 | return |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 |
| 140 | // udp service -> frpc -> frps -> frpc visitor -> user |
| 141 | workConnSenderFn := func(payloadConn *msg.Conn, sendCh chan msg.Message) { |
| 142 | defer func() { |
| 143 | closeFn() |
| 144 | xl.Infof("writer goroutine for sudp work connection closed") |
| 145 | }() |
| 146 |
| 147 | var errRet error |
| 148 | for rawMsg := range sendCh { |
| 149 | switch m := rawMsg.(type) { |
| 150 | case *msg.UDPPacket: |
| 151 | xl.Tracef("frpc send udp package to frpc visitor, [udp local: %v, remote: %v], [tcp work conn local: %v, remote: %v]", |
| 152 | m.LocalAddr.String(), m.RemoteAddr.String(), payloadConn.LocalAddr().String(), payloadConn.RemoteAddr().String()) |
| 153 | case *msg.Ping: |
| 154 | xl.Tracef("frpc send ping message to frpc visitor") |
| 155 | } |
| 156 |
| 157 | if errRet = payloadConn.WriteMsg(rawMsg); errRet != nil { |
| 158 | xl.Errorf("sudp work write error: %v", errRet) |
| 159 | return |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 |
| 164 | heartbeatFn := func(sendCh chan msg.Message) { |
| 165 | ticker := time.NewTicker(30 * time.Second) |
| 166 | defer func() { |
| 167 | ticker.Stop() |
| 168 | closeFn() |
| 169 | }() |
| 170 |
| 171 | var errRet error |
| 172 | for { |
| 173 | select { |
| 174 | case <-ticker.C: |
| 175 | if errRet = errors.PanicToError(func() { |
| 176 | sendCh <- &msg.Ping{} |
| 177 | }); errRet != nil { |
| 178 | xl.Warnf("heartbeat goroutine for sudp work connection closed") |
| 179 | return |
| 180 | } |
| 181 | case <-pxy.closeCh: |
| 182 | xl.Tracef("frpc sudp proxy is closed") |
| 183 | return |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 |
| 188 | go workConnSenderFn(payloadConn, sendCh) |
| 189 | go workConnReaderFn(payloadConn, readCh) |
| 190 | go heartbeatFn(sendCh) |
| 191 |
| 192 | udp.Forwarder(pxy.localAddr, readCh, sendCh, int(pxy.clientCfg.UDPPacketSize), pxy.cfg.Transport.ProxyProtocolVersion) |
| 193 | } |