1
0

proxy.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 proxy
  15. import (
  16. "bytes"
  17. "context"
  18. "io"
  19. "net"
  20. "strconv"
  21. "strings"
  22. "sync"
  23. "time"
  24. frpIo "github.com/fatedier/golib/io"
  25. libdial "github.com/fatedier/golib/net/dial"
  26. pp "github.com/pires/go-proxyproto"
  27. "golang.org/x/time/rate"
  28. "github.com/fatedier/frp/pkg/config"
  29. "github.com/fatedier/frp/pkg/msg"
  30. plugin "github.com/fatedier/frp/pkg/plugin/client"
  31. "github.com/fatedier/frp/pkg/transport"
  32. "github.com/fatedier/frp/pkg/util/limit"
  33. "github.com/fatedier/frp/pkg/util/xlog"
  34. )
  35. // Proxy defines how to handle work connections for different proxy type.
  36. type Proxy interface {
  37. Run() error
  38. // InWorkConn accept work connections registered to server.
  39. InWorkConn(net.Conn, *msg.StartWorkConn)
  40. Close()
  41. }
  42. func NewProxy(
  43. ctx context.Context,
  44. pxyConf config.ProxyConf,
  45. clientCfg config.ClientCommonConf,
  46. msgTransporter transport.MessageTransporter,
  47. ) (pxy Proxy) {
  48. var limiter *rate.Limiter
  49. limitBytes := pxyConf.GetBaseInfo().BandwidthLimit.Bytes()
  50. if limitBytes > 0 && pxyConf.GetBaseInfo().BandwidthLimitMode == config.BandwidthLimitModeClient {
  51. limiter = rate.NewLimiter(rate.Limit(float64(limitBytes)), int(limitBytes))
  52. }
  53. baseProxy := BaseProxy{
  54. clientCfg: clientCfg,
  55. limiter: limiter,
  56. msgTransporter: msgTransporter,
  57. xl: xlog.FromContextSafe(ctx),
  58. ctx: ctx,
  59. }
  60. switch cfg := pxyConf.(type) {
  61. case *config.TCPProxyConf:
  62. pxy = &TCPProxy{
  63. BaseProxy: &baseProxy,
  64. cfg: cfg,
  65. }
  66. case *config.TCPMuxProxyConf:
  67. pxy = &TCPMuxProxy{
  68. BaseProxy: &baseProxy,
  69. cfg: cfg,
  70. }
  71. case *config.UDPProxyConf:
  72. pxy = &UDPProxy{
  73. BaseProxy: &baseProxy,
  74. cfg: cfg,
  75. }
  76. case *config.HTTPProxyConf:
  77. pxy = &HTTPProxy{
  78. BaseProxy: &baseProxy,
  79. cfg: cfg,
  80. }
  81. case *config.HTTPSProxyConf:
  82. pxy = &HTTPSProxy{
  83. BaseProxy: &baseProxy,
  84. cfg: cfg,
  85. }
  86. case *config.STCPProxyConf:
  87. pxy = &STCPProxy{
  88. BaseProxy: &baseProxy,
  89. cfg: cfg,
  90. }
  91. case *config.XTCPProxyConf:
  92. pxy = &XTCPProxy{
  93. BaseProxy: &baseProxy,
  94. cfg: cfg,
  95. }
  96. case *config.SUDPProxyConf:
  97. pxy = &SUDPProxy{
  98. BaseProxy: &baseProxy,
  99. cfg: cfg,
  100. closeCh: make(chan struct{}),
  101. }
  102. }
  103. return
  104. }
  105. type BaseProxy struct {
  106. closed bool
  107. clientCfg config.ClientCommonConf
  108. msgTransporter transport.MessageTransporter
  109. limiter *rate.Limiter
  110. mu sync.RWMutex
  111. xl *xlog.Logger
  112. ctx context.Context
  113. }
  114. // TCP
  115. type TCPProxy struct {
  116. *BaseProxy
  117. cfg *config.TCPProxyConf
  118. proxyPlugin plugin.Plugin
  119. }
  120. func (pxy *TCPProxy) Run() (err error) {
  121. if pxy.cfg.Plugin != "" {
  122. pxy.proxyPlugin, err = plugin.Create(pxy.cfg.Plugin, pxy.cfg.PluginParams)
  123. if err != nil {
  124. return
  125. }
  126. }
  127. return
  128. }
  129. func (pxy *TCPProxy) Close() {
  130. if pxy.proxyPlugin != nil {
  131. pxy.proxyPlugin.Close()
  132. }
  133. }
  134. func (pxy *TCPProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) {
  135. HandleTCPWorkConnection(pxy.ctx, &pxy.cfg.LocalSvrConf, pxy.proxyPlugin, pxy.cfg.GetBaseInfo(), pxy.limiter,
  136. conn, []byte(pxy.clientCfg.Token), m)
  137. }
  138. // TCP Multiplexer
  139. type TCPMuxProxy struct {
  140. *BaseProxy
  141. cfg *config.TCPMuxProxyConf
  142. proxyPlugin plugin.Plugin
  143. }
  144. func (pxy *TCPMuxProxy) Run() (err error) {
  145. if pxy.cfg.Plugin != "" {
  146. pxy.proxyPlugin, err = plugin.Create(pxy.cfg.Plugin, pxy.cfg.PluginParams)
  147. if err != nil {
  148. return
  149. }
  150. }
  151. return
  152. }
  153. func (pxy *TCPMuxProxy) Close() {
  154. if pxy.proxyPlugin != nil {
  155. pxy.proxyPlugin.Close()
  156. }
  157. }
  158. func (pxy *TCPMuxProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) {
  159. HandleTCPWorkConnection(pxy.ctx, &pxy.cfg.LocalSvrConf, pxy.proxyPlugin, pxy.cfg.GetBaseInfo(), pxy.limiter,
  160. conn, []byte(pxy.clientCfg.Token), m)
  161. }
  162. // HTTP
  163. type HTTPProxy struct {
  164. *BaseProxy
  165. cfg *config.HTTPProxyConf
  166. proxyPlugin plugin.Plugin
  167. }
  168. func (pxy *HTTPProxy) Run() (err error) {
  169. if pxy.cfg.Plugin != "" {
  170. pxy.proxyPlugin, err = plugin.Create(pxy.cfg.Plugin, pxy.cfg.PluginParams)
  171. if err != nil {
  172. return
  173. }
  174. }
  175. return
  176. }
  177. func (pxy *HTTPProxy) Close() {
  178. if pxy.proxyPlugin != nil {
  179. pxy.proxyPlugin.Close()
  180. }
  181. }
  182. func (pxy *HTTPProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) {
  183. HandleTCPWorkConnection(pxy.ctx, &pxy.cfg.LocalSvrConf, pxy.proxyPlugin, pxy.cfg.GetBaseInfo(), pxy.limiter,
  184. conn, []byte(pxy.clientCfg.Token), m)
  185. }
  186. // HTTPS
  187. type HTTPSProxy struct {
  188. *BaseProxy
  189. cfg *config.HTTPSProxyConf
  190. proxyPlugin plugin.Plugin
  191. }
  192. func (pxy *HTTPSProxy) Run() (err error) {
  193. if pxy.cfg.Plugin != "" {
  194. pxy.proxyPlugin, err = plugin.Create(pxy.cfg.Plugin, pxy.cfg.PluginParams)
  195. if err != nil {
  196. return
  197. }
  198. }
  199. return
  200. }
  201. func (pxy *HTTPSProxy) Close() {
  202. if pxy.proxyPlugin != nil {
  203. pxy.proxyPlugin.Close()
  204. }
  205. }
  206. func (pxy *HTTPSProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) {
  207. HandleTCPWorkConnection(pxy.ctx, &pxy.cfg.LocalSvrConf, pxy.proxyPlugin, pxy.cfg.GetBaseInfo(), pxy.limiter,
  208. conn, []byte(pxy.clientCfg.Token), m)
  209. }
  210. // STCP
  211. type STCPProxy struct {
  212. *BaseProxy
  213. cfg *config.STCPProxyConf
  214. proxyPlugin plugin.Plugin
  215. }
  216. func (pxy *STCPProxy) Run() (err error) {
  217. if pxy.cfg.Plugin != "" {
  218. pxy.proxyPlugin, err = plugin.Create(pxy.cfg.Plugin, pxy.cfg.PluginParams)
  219. if err != nil {
  220. return
  221. }
  222. }
  223. return
  224. }
  225. func (pxy *STCPProxy) Close() {
  226. if pxy.proxyPlugin != nil {
  227. pxy.proxyPlugin.Close()
  228. }
  229. }
  230. func (pxy *STCPProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) {
  231. HandleTCPWorkConnection(pxy.ctx, &pxy.cfg.LocalSvrConf, pxy.proxyPlugin, pxy.cfg.GetBaseInfo(), pxy.limiter,
  232. conn, []byte(pxy.clientCfg.Token), m)
  233. }
  234. // Common handler for tcp work connections.
  235. func HandleTCPWorkConnection(ctx context.Context, localInfo *config.LocalSvrConf, proxyPlugin plugin.Plugin,
  236. baseInfo *config.BaseProxyConf, limiter *rate.Limiter, workConn net.Conn, encKey []byte, m *msg.StartWorkConn,
  237. ) {
  238. xl := xlog.FromContextSafe(ctx)
  239. var (
  240. remote io.ReadWriteCloser
  241. err error
  242. )
  243. remote = workConn
  244. if limiter != nil {
  245. remote = frpIo.WrapReadWriteCloser(limit.NewReader(workConn, limiter), limit.NewWriter(workConn, limiter), func() error {
  246. return workConn.Close()
  247. })
  248. }
  249. xl.Trace("handle tcp work connection, use_encryption: %t, use_compression: %t",
  250. baseInfo.UseEncryption, baseInfo.UseCompression)
  251. if baseInfo.UseEncryption {
  252. remote, err = frpIo.WithEncryption(remote, encKey)
  253. if err != nil {
  254. workConn.Close()
  255. xl.Error("create encryption stream error: %v", err)
  256. return
  257. }
  258. }
  259. if baseInfo.UseCompression {
  260. remote = frpIo.WithCompression(remote)
  261. }
  262. // check if we need to send proxy protocol info
  263. var extraInfo []byte
  264. if baseInfo.ProxyProtocolVersion != "" {
  265. if m.SrcAddr != "" && m.SrcPort != 0 {
  266. if m.DstAddr == "" {
  267. m.DstAddr = "127.0.0.1"
  268. }
  269. srcAddr, _ := net.ResolveTCPAddr("tcp", net.JoinHostPort(m.SrcAddr, strconv.Itoa(int(m.SrcPort))))
  270. dstAddr, _ := net.ResolveTCPAddr("tcp", net.JoinHostPort(m.DstAddr, strconv.Itoa(int(m.DstPort))))
  271. h := &pp.Header{
  272. Command: pp.PROXY,
  273. SourceAddr: srcAddr,
  274. DestinationAddr: dstAddr,
  275. }
  276. if strings.Contains(m.SrcAddr, ".") {
  277. h.TransportProtocol = pp.TCPv4
  278. } else {
  279. h.TransportProtocol = pp.TCPv6
  280. }
  281. if baseInfo.ProxyProtocolVersion == "v1" {
  282. h.Version = 1
  283. } else if baseInfo.ProxyProtocolVersion == "v2" {
  284. h.Version = 2
  285. }
  286. buf := bytes.NewBuffer(nil)
  287. _, _ = h.WriteTo(buf)
  288. extraInfo = buf.Bytes()
  289. }
  290. }
  291. if proxyPlugin != nil {
  292. // if plugin is set, let plugin handle connections first
  293. xl.Debug("handle by plugin: %s", proxyPlugin.Name())
  294. proxyPlugin.Handle(remote, workConn, extraInfo)
  295. xl.Debug("handle by plugin finished")
  296. return
  297. }
  298. localConn, err := libdial.Dial(
  299. net.JoinHostPort(localInfo.LocalIP, strconv.Itoa(localInfo.LocalPort)),
  300. libdial.WithTimeout(10*time.Second),
  301. )
  302. if err != nil {
  303. workConn.Close()
  304. xl.Error("connect to local service [%s:%d] error: %v", localInfo.LocalIP, localInfo.LocalPort, err)
  305. return
  306. }
  307. xl.Debug("join connections, localConn(l[%s] r[%s]) workConn(l[%s] r[%s])", localConn.LocalAddr().String(),
  308. localConn.RemoteAddr().String(), workConn.LocalAddr().String(), workConn.RemoteAddr().String())
  309. if len(extraInfo) > 0 {
  310. if _, err := localConn.Write(extraInfo); err != nil {
  311. workConn.Close()
  312. xl.Error("write extraInfo to local conn error: %v", err)
  313. return
  314. }
  315. }
  316. _, _, errs := frpIo.Join(localConn, remote)
  317. xl.Debug("join connections closed")
  318. if len(errs) > 0 {
  319. xl.Trace("join connections errors: %v", errs)
  320. }
  321. }