proxy.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. "context"
  17. "fmt"
  18. "io"
  19. "net"
  20. "strconv"
  21. "sync"
  22. "time"
  23. "github.com/fatedier/frp/pkg/config"
  24. "github.com/fatedier/frp/pkg/msg"
  25. plugin "github.com/fatedier/frp/pkg/plugin/server"
  26. frpNet "github.com/fatedier/frp/pkg/util/net"
  27. "github.com/fatedier/frp/pkg/util/xlog"
  28. "github.com/fatedier/frp/server/controller"
  29. "github.com/fatedier/frp/server/metrics"
  30. frpIo "github.com/fatedier/golib/io"
  31. )
  32. type GetWorkConnFn func() (net.Conn, error)
  33. type Proxy interface {
  34. Context() context.Context
  35. Run() (remoteAddr string, err error)
  36. GetName() string
  37. GetConf() config.ProxyConf
  38. GetWorkConnFromPool(src, dst net.Addr) (workConn net.Conn, err error)
  39. GetUsedPortsNum() int
  40. GetResourceController() *controller.ResourceController
  41. GetUserInfo() plugin.UserInfo
  42. Close()
  43. }
  44. type BaseProxy struct {
  45. name string
  46. rc *controller.ResourceController
  47. listeners []net.Listener
  48. usedPortsNum int
  49. poolCount int
  50. getWorkConnFn GetWorkConnFn
  51. serverCfg config.ServerCommonConf
  52. userInfo plugin.UserInfo
  53. mu sync.RWMutex
  54. xl *xlog.Logger
  55. ctx context.Context
  56. }
  57. func (pxy *BaseProxy) GetName() string {
  58. return pxy.name
  59. }
  60. func (pxy *BaseProxy) Context() context.Context {
  61. return pxy.ctx
  62. }
  63. func (pxy *BaseProxy) GetUsedPortsNum() int {
  64. return pxy.usedPortsNum
  65. }
  66. func (pxy *BaseProxy) GetResourceController() *controller.ResourceController {
  67. return pxy.rc
  68. }
  69. func (pxy *BaseProxy) GetUserInfo() plugin.UserInfo {
  70. return pxy.userInfo
  71. }
  72. func (pxy *BaseProxy) Close() {
  73. xl := xlog.FromContextSafe(pxy.ctx)
  74. xl.Info("proxy closing")
  75. for _, l := range pxy.listeners {
  76. l.Close()
  77. }
  78. }
  79. // GetWorkConnFromPool try to get a new work connections from pool
  80. // for quickly response, we immediately send the StartWorkConn message to frpc after take out one from pool
  81. func (pxy *BaseProxy) GetWorkConnFromPool(src, dst net.Addr) (workConn net.Conn, err error) {
  82. xl := xlog.FromContextSafe(pxy.ctx)
  83. // try all connections from the pool
  84. for i := 0; i < pxy.poolCount+1; i++ {
  85. if workConn, err = pxy.getWorkConnFn(); err != nil {
  86. xl.Warn("failed to get work connection: %v", err)
  87. return
  88. }
  89. xl.Debug("get a new work connection: [%s]", workConn.RemoteAddr().String())
  90. xl.Spawn().AppendPrefix(pxy.GetName())
  91. workConn = frpNet.NewContextConn(pxy.ctx, workConn)
  92. var (
  93. srcAddr string
  94. dstAddr string
  95. srcPortStr string
  96. dstPortStr string
  97. srcPort int
  98. dstPort int
  99. )
  100. if src != nil {
  101. srcAddr, srcPortStr, _ = net.SplitHostPort(src.String())
  102. srcPort, _ = strconv.Atoi(srcPortStr)
  103. }
  104. if dst != nil {
  105. dstAddr, dstPortStr, _ = net.SplitHostPort(dst.String())
  106. dstPort, _ = strconv.Atoi(dstPortStr)
  107. }
  108. err := msg.WriteMsg(workConn, &msg.StartWorkConn{
  109. ProxyName: pxy.GetName(),
  110. SrcAddr: srcAddr,
  111. SrcPort: uint16(srcPort),
  112. DstAddr: dstAddr,
  113. DstPort: uint16(dstPort),
  114. Error: "",
  115. })
  116. if err != nil {
  117. xl.Warn("failed to send message to work connection from pool: %v, times: %d", err, i)
  118. workConn.Close()
  119. } else {
  120. break
  121. }
  122. }
  123. if err != nil {
  124. xl.Error("try to get work connection failed in the end")
  125. return
  126. }
  127. return
  128. }
  129. // startListenHandler start a goroutine handler for each listener.
  130. // p: p will just be passed to handler(Proxy, frpNet.Conn).
  131. // handler: each proxy type can set different handler function to deal with connections accepted from listeners.
  132. func (pxy *BaseProxy) startListenHandler(p Proxy, handler func(Proxy, net.Conn, config.ServerCommonConf)) {
  133. xl := xlog.FromContextSafe(pxy.ctx)
  134. for _, listener := range pxy.listeners {
  135. go func(l net.Listener) {
  136. var tempDelay time.Duration // how long to sleep on accept failure
  137. for {
  138. // block
  139. // if listener is closed, err returned
  140. c, err := l.Accept()
  141. if err != nil {
  142. if err, ok := err.(interface{ Temporary() bool }); ok && err.Temporary() {
  143. if tempDelay == 0 {
  144. tempDelay = 5 * time.Millisecond
  145. } else {
  146. tempDelay *= 2
  147. }
  148. if max := 1 * time.Second; tempDelay > max {
  149. tempDelay = max
  150. }
  151. xl.Info("met temporary error: %s, sleep for %s ...", err, tempDelay)
  152. time.Sleep(tempDelay)
  153. continue
  154. }
  155. xl.Warn("listener is closed: %s", err)
  156. return
  157. }
  158. xl.Info("get a user connection [%s]", c.RemoteAddr().String())
  159. go handler(p, c, pxy.serverCfg)
  160. }
  161. }(listener)
  162. }
  163. }
  164. func NewProxy(ctx context.Context, userInfo plugin.UserInfo, rc *controller.ResourceController, poolCount int,
  165. getWorkConnFn GetWorkConnFn, pxyConf config.ProxyConf, serverCfg config.ServerCommonConf) (pxy Proxy, err error) {
  166. xl := xlog.FromContextSafe(ctx).Spawn().AppendPrefix(pxyConf.GetBaseInfo().ProxyName)
  167. basePxy := BaseProxy{
  168. name: pxyConf.GetBaseInfo().ProxyName,
  169. rc: rc,
  170. listeners: make([]net.Listener, 0),
  171. poolCount: poolCount,
  172. getWorkConnFn: getWorkConnFn,
  173. serverCfg: serverCfg,
  174. xl: xl,
  175. ctx: xlog.NewContext(ctx, xl),
  176. userInfo: userInfo,
  177. }
  178. switch cfg := pxyConf.(type) {
  179. case *config.TCPProxyConf:
  180. basePxy.usedPortsNum = 1
  181. pxy = &TCPProxy{
  182. BaseProxy: &basePxy,
  183. cfg: cfg,
  184. }
  185. case *config.TCPMuxProxyConf:
  186. pxy = &TCPMuxProxy{
  187. BaseProxy: &basePxy,
  188. cfg: cfg,
  189. }
  190. case *config.HTTPProxyConf:
  191. pxy = &HTTPProxy{
  192. BaseProxy: &basePxy,
  193. cfg: cfg,
  194. }
  195. case *config.HTTPSProxyConf:
  196. pxy = &HTTPSProxy{
  197. BaseProxy: &basePxy,
  198. cfg: cfg,
  199. }
  200. case *config.UDPProxyConf:
  201. basePxy.usedPortsNum = 1
  202. pxy = &UDPProxy{
  203. BaseProxy: &basePxy,
  204. cfg: cfg,
  205. }
  206. case *config.STCPProxyConf:
  207. pxy = &STCPProxy{
  208. BaseProxy: &basePxy,
  209. cfg: cfg,
  210. }
  211. case *config.XTCPProxyConf:
  212. pxy = &XTCPProxy{
  213. BaseProxy: &basePxy,
  214. cfg: cfg,
  215. }
  216. case *config.SUDPProxyConf:
  217. pxy = &SUDPProxy{
  218. BaseProxy: &basePxy,
  219. cfg: cfg,
  220. }
  221. default:
  222. return pxy, fmt.Errorf("proxy type not support")
  223. }
  224. return
  225. }
  226. // HandleUserTCPConnection is used for incoming user TCP connections.
  227. // It can be used for tcp, http, https type.
  228. func HandleUserTCPConnection(pxy Proxy, userConn net.Conn, serverCfg config.ServerCommonConf) {
  229. xl := xlog.FromContextSafe(pxy.Context())
  230. defer userConn.Close()
  231. // server plugin hook
  232. rc := pxy.GetResourceController()
  233. content := &plugin.NewUserConnContent{
  234. User: pxy.GetUserInfo(),
  235. ProxyName: pxy.GetName(),
  236. ProxyType: pxy.GetConf().GetBaseInfo().ProxyType,
  237. RemoteAddr: userConn.RemoteAddr().String(),
  238. }
  239. _, err := rc.PluginManager.NewUserConn(content)
  240. if err != nil {
  241. xl.Warn("the user conn [%s] was rejected, err:%v", content.RemoteAddr, err)
  242. return
  243. }
  244. // try all connections from the pool
  245. workConn, err := pxy.GetWorkConnFromPool(userConn.RemoteAddr(), userConn.LocalAddr())
  246. if err != nil {
  247. return
  248. }
  249. defer workConn.Close()
  250. var local io.ReadWriteCloser = workConn
  251. cfg := pxy.GetConf().GetBaseInfo()
  252. xl.Trace("handler user tcp connection, use_encryption: %t, use_compression: %t", cfg.UseEncryption, cfg.UseCompression)
  253. if cfg.UseEncryption {
  254. local, err = frpIo.WithEncryption(local, []byte(serverCfg.Token))
  255. if err != nil {
  256. xl.Error("create encryption stream error: %v", err)
  257. return
  258. }
  259. }
  260. if cfg.UseCompression {
  261. local = frpIo.WithCompression(local)
  262. }
  263. xl.Debug("join connections, workConn(l[%s] r[%s]) userConn(l[%s] r[%s])", workConn.LocalAddr().String(),
  264. workConn.RemoteAddr().String(), userConn.LocalAddr().String(), userConn.RemoteAddr().String())
  265. name := pxy.GetName()
  266. proxyType := pxy.GetConf().GetBaseInfo().ProxyType
  267. metrics.Server.OpenConnection(name, proxyType)
  268. inCount, outCount := frpIo.Join(local, userConn)
  269. metrics.Server.CloseConnection(name, proxyType)
  270. metrics.Server.AddTrafficIn(name, proxyType, inCount)
  271. metrics.Server.AddTrafficOut(name, proxyType, outCount)
  272. xl.Debug("join connections closed")
  273. }
  274. type Manager struct {
  275. // proxies indexed by proxy name
  276. pxys map[string]Proxy
  277. mu sync.RWMutex
  278. }
  279. func NewManager() *Manager {
  280. return &Manager{
  281. pxys: make(map[string]Proxy),
  282. }
  283. }
  284. func (pm *Manager) Add(name string, pxy Proxy) error {
  285. pm.mu.Lock()
  286. defer pm.mu.Unlock()
  287. if _, ok := pm.pxys[name]; ok {
  288. return fmt.Errorf("proxy name [%s] is already in use", name)
  289. }
  290. pm.pxys[name] = pxy
  291. return nil
  292. }
  293. func (pm *Manager) Del(name string) {
  294. pm.mu.Lock()
  295. defer pm.mu.Unlock()
  296. delete(pm.pxys, name)
  297. }
  298. func (pm *Manager) GetByName(name string) (pxy Proxy, ok bool) {
  299. pm.mu.RLock()
  300. defer pm.mu.RUnlock()
  301. pxy, ok = pm.pxys[name]
  302. return
  303. }