proxy.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Copyright 2017 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. "github.com/fatedier/frp/models/config"
  20. "github.com/fatedier/frp/models/msg"
  21. "github.com/fatedier/frp/models/proto/tcp"
  22. "github.com/fatedier/frp/models/proto/udp"
  23. "github.com/fatedier/frp/utils/errors"
  24. "github.com/fatedier/frp/utils/log"
  25. frpNet "github.com/fatedier/frp/utils/net"
  26. )
  27. // Proxy defines how to work for different proxy type.
  28. type Proxy interface {
  29. Run() error
  30. // InWorkConn accept work connections registered to server.
  31. InWorkConn(conn frpNet.Conn)
  32. Close()
  33. log.Logger
  34. }
  35. func NewProxy(ctl *Control, pxyConf config.ProxyConf) (pxy Proxy) {
  36. baseProxy := BaseProxy{
  37. ctl: ctl,
  38. Logger: log.NewPrefixLogger(pxyConf.GetName()),
  39. }
  40. switch cfg := pxyConf.(type) {
  41. case *config.TcpProxyConf:
  42. pxy = &TcpProxy{
  43. BaseProxy: baseProxy,
  44. cfg: cfg,
  45. }
  46. case *config.UdpProxyConf:
  47. pxy = &UdpProxy{
  48. BaseProxy: baseProxy,
  49. cfg: cfg,
  50. }
  51. case *config.HttpProxyConf:
  52. pxy = &HttpProxy{
  53. BaseProxy: baseProxy,
  54. cfg: cfg,
  55. }
  56. case *config.HttpsProxyConf:
  57. pxy = &HttpsProxy{
  58. BaseProxy: baseProxy,
  59. cfg: cfg,
  60. }
  61. }
  62. return
  63. }
  64. type BaseProxy struct {
  65. ctl *Control
  66. log.Logger
  67. }
  68. // TCP
  69. type TcpProxy struct {
  70. BaseProxy
  71. cfg *config.TcpProxyConf
  72. }
  73. func (pxy *TcpProxy) Run() (err error) {
  74. return
  75. }
  76. func (pxy *TcpProxy) Close() {
  77. }
  78. func (pxy *TcpProxy) InWorkConn(conn frpNet.Conn) {
  79. defer conn.Close()
  80. HandleTcpWorkConnection(&pxy.cfg.LocalSvrConf, &pxy.cfg.BaseProxyConf, conn)
  81. }
  82. // HTTP
  83. type HttpProxy struct {
  84. BaseProxy
  85. cfg *config.HttpProxyConf
  86. }
  87. func (pxy *HttpProxy) Run() (err error) {
  88. return
  89. }
  90. func (pxy *HttpProxy) Close() {
  91. }
  92. func (pxy *HttpProxy) InWorkConn(conn frpNet.Conn) {
  93. defer conn.Close()
  94. HandleTcpWorkConnection(&pxy.cfg.LocalSvrConf, &pxy.cfg.BaseProxyConf, conn)
  95. }
  96. // HTTPS
  97. type HttpsProxy struct {
  98. BaseProxy
  99. cfg *config.HttpsProxyConf
  100. }
  101. func (pxy *HttpsProxy) Run() (err error) {
  102. return
  103. }
  104. func (pxy *HttpsProxy) Close() {
  105. }
  106. func (pxy *HttpsProxy) InWorkConn(conn frpNet.Conn) {
  107. defer conn.Close()
  108. HandleTcpWorkConnection(&pxy.cfg.LocalSvrConf, &pxy.cfg.BaseProxyConf, conn)
  109. }
  110. // UDP
  111. type UdpProxy struct {
  112. BaseProxy
  113. cfg *config.UdpProxyConf
  114. localAddr *net.UDPAddr
  115. readCh chan *msg.UdpPacket
  116. sendCh chan *msg.UdpPacket
  117. workConn frpNet.Conn
  118. }
  119. func (pxy *UdpProxy) Run() (err error) {
  120. pxy.localAddr, err = net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", pxy.cfg.LocalIp, pxy.cfg.LocalPort))
  121. if err != nil {
  122. return
  123. }
  124. return
  125. }
  126. func (pxy *UdpProxy) Close() {
  127. pxy.workConn.Close()
  128. close(pxy.readCh)
  129. close(pxy.sendCh)
  130. }
  131. func (pxy *UdpProxy) InWorkConn(conn frpNet.Conn) {
  132. if pxy.workConn != nil {
  133. pxy.workConn.Close()
  134. close(pxy.readCh)
  135. close(pxy.sendCh)
  136. }
  137. pxy.workConn = conn
  138. pxy.readCh = make(chan *msg.UdpPacket, 64)
  139. pxy.sendCh = make(chan *msg.UdpPacket, 64)
  140. workConnReaderFn := func(conn net.Conn) {
  141. for {
  142. var udpMsg msg.UdpPacket
  143. if errRet := msg.ReadMsgInto(conn, &udpMsg); errRet != nil {
  144. pxy.Warn("read from workConn for udp error: %v", errRet)
  145. return
  146. }
  147. if errRet := errors.PanicToError(func() {
  148. pxy.readCh <- &udpMsg
  149. }); errRet != nil {
  150. pxy.Info("reader goroutine for udp work connection closed")
  151. return
  152. }
  153. }
  154. }
  155. workConnSenderFn := func(conn net.Conn) {
  156. var errRet error
  157. for udpMsg := range pxy.sendCh {
  158. if errRet = msg.WriteMsg(conn, udpMsg); errRet != nil {
  159. pxy.Info("sender goroutine for udp work connection closed")
  160. return
  161. }
  162. }
  163. }
  164. go workConnSenderFn(pxy.workConn)
  165. go workConnReaderFn(pxy.workConn)
  166. udp.Forwarder(pxy.localAddr, pxy.readCh, pxy.sendCh)
  167. }
  168. // Common handler for tcp work connections.
  169. func HandleTcpWorkConnection(localInfo *config.LocalSvrConf, baseInfo *config.BaseProxyConf, workConn frpNet.Conn) {
  170. localConn, err := frpNet.ConnectTcpServer(fmt.Sprintf("%s:%d", localInfo.LocalIp, localInfo.LocalPort))
  171. if err != nil {
  172. workConn.Error("connect to local service [%s:%d] error: %v", localInfo.LocalIp, localInfo.LocalPort, err)
  173. return
  174. }
  175. var remote io.ReadWriteCloser
  176. remote = workConn
  177. if baseInfo.UseEncryption {
  178. remote, err = tcp.WithEncryption(remote, []byte(config.ClientCommonCfg.PrivilegeToken))
  179. if err != nil {
  180. workConn.Error("create encryption stream error: %v", err)
  181. return
  182. }
  183. }
  184. if baseInfo.UseCompression {
  185. remote = tcp.WithCompression(remote)
  186. }
  187. workConn.Debug("join connections, localConn(l[%s] r[%s]) workConn(l[%s] r[%s])", localConn.LocalAddr().String(),
  188. localConn.RemoteAddr().String(), workConn.LocalAddr().String(), workConn.RemoteAddr().String())
  189. tcp.Join(localConn, remote)
  190. workConn.Debug("join connections closed")
  191. }