1
0

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. frpIo "github.com/fatedier/golib/io"
  24. "github.com/fatedier/frp/pkg/config"
  25. "github.com/fatedier/frp/pkg/msg"
  26. plugin "github.com/fatedier/frp/pkg/plugin/server"
  27. frpNet "github.com/fatedier/frp/pkg/util/net"
  28. "github.com/fatedier/frp/pkg/util/xlog"
  29. "github.com/fatedier/frp/server/controller"
  30. "github.com/fatedier/frp/server/metrics"
  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,
  166. ) (pxy Proxy, err error) {
  167. xl := xlog.FromContextSafe(ctx).Spawn().AppendPrefix(pxyConf.GetBaseInfo().ProxyName)
  168. basePxy := BaseProxy{
  169. name: pxyConf.GetBaseInfo().ProxyName,
  170. rc: rc,
  171. listeners: make([]net.Listener, 0),
  172. poolCount: poolCount,
  173. getWorkConnFn: getWorkConnFn,
  174. serverCfg: serverCfg,
  175. xl: xl,
  176. ctx: xlog.NewContext(ctx, xl),
  177. userInfo: userInfo,
  178. }
  179. switch cfg := pxyConf.(type) {
  180. case *config.TCPProxyConf:
  181. basePxy.usedPortsNum = 1
  182. pxy = &TCPProxy{
  183. BaseProxy: &basePxy,
  184. cfg: cfg,
  185. }
  186. case *config.TCPMuxProxyConf:
  187. pxy = &TCPMuxProxy{
  188. BaseProxy: &basePxy,
  189. cfg: cfg,
  190. }
  191. case *config.HTTPProxyConf:
  192. pxy = &HTTPProxy{
  193. BaseProxy: &basePxy,
  194. cfg: cfg,
  195. }
  196. case *config.HTTPSProxyConf:
  197. pxy = &HTTPSProxy{
  198. BaseProxy: &basePxy,
  199. cfg: cfg,
  200. }
  201. case *config.UDPProxyConf:
  202. basePxy.usedPortsNum = 1
  203. pxy = &UDPProxy{
  204. BaseProxy: &basePxy,
  205. cfg: cfg,
  206. }
  207. case *config.STCPProxyConf:
  208. pxy = &STCPProxy{
  209. BaseProxy: &basePxy,
  210. cfg: cfg,
  211. }
  212. case *config.XTCPProxyConf:
  213. pxy = &XTCPProxy{
  214. BaseProxy: &basePxy,
  215. cfg: cfg,
  216. }
  217. case *config.SUDPProxyConf:
  218. pxy = &SUDPProxy{
  219. BaseProxy: &basePxy,
  220. cfg: cfg,
  221. }
  222. default:
  223. return pxy, fmt.Errorf("proxy type not support")
  224. }
  225. return
  226. }
  227. // HandleUserTCPConnection is used for incoming user TCP connections.
  228. // It can be used for tcp, http, https type.
  229. func HandleUserTCPConnection(pxy Proxy, userConn net.Conn, serverCfg config.ServerCommonConf) {
  230. xl := xlog.FromContextSafe(pxy.Context())
  231. defer userConn.Close()
  232. // server plugin hook
  233. rc := pxy.GetResourceController()
  234. content := &plugin.NewUserConnContent{
  235. User: pxy.GetUserInfo(),
  236. ProxyName: pxy.GetName(),
  237. ProxyType: pxy.GetConf().GetBaseInfo().ProxyType,
  238. RemoteAddr: userConn.RemoteAddr().String(),
  239. }
  240. _, err := rc.PluginManager.NewUserConn(content)
  241. if err != nil {
  242. xl.Warn("the user conn [%s] was rejected, err:%v", content.RemoteAddr, err)
  243. return
  244. }
  245. // try all connections from the pool
  246. workConn, err := pxy.GetWorkConnFromPool(userConn.RemoteAddr(), userConn.LocalAddr())
  247. if err != nil {
  248. return
  249. }
  250. defer workConn.Close()
  251. var local io.ReadWriteCloser = workConn
  252. cfg := pxy.GetConf().GetBaseInfo()
  253. xl.Trace("handler user tcp connection, use_encryption: %t, use_compression: %t", cfg.UseEncryption, cfg.UseCompression)
  254. if cfg.UseEncryption {
  255. local, err = frpIo.WithEncryption(local, []byte(serverCfg.Token))
  256. if err != nil {
  257. xl.Error("create encryption stream error: %v", err)
  258. return
  259. }
  260. }
  261. if cfg.UseCompression {
  262. local = frpIo.WithCompression(local)
  263. }
  264. xl.Debug("join connections, workConn(l[%s] r[%s]) userConn(l[%s] r[%s])", workConn.LocalAddr().String(),
  265. workConn.RemoteAddr().String(), userConn.LocalAddr().String(), userConn.RemoteAddr().String())
  266. name := pxy.GetName()
  267. proxyType := pxy.GetConf().GetBaseInfo().ProxyType
  268. metrics.Server.OpenConnection(name, proxyType)
  269. inCount, outCount := frpIo.Join(local, userConn)
  270. metrics.Server.CloseConnection(name, proxyType)
  271. metrics.Server.AddTrafficIn(name, proxyType, inCount)
  272. metrics.Server.AddTrafficOut(name, proxyType, outCount)
  273. xl.Debug("join connections closed")
  274. }
  275. type Manager struct {
  276. // proxies indexed by proxy name
  277. pxys map[string]Proxy
  278. mu sync.RWMutex
  279. }
  280. func NewManager() *Manager {
  281. return &Manager{
  282. pxys: make(map[string]Proxy),
  283. }
  284. }
  285. func (pm *Manager) Add(name string, pxy Proxy) error {
  286. pm.mu.Lock()
  287. defer pm.mu.Unlock()
  288. if _, ok := pm.pxys[name]; ok {
  289. return fmt.Errorf("proxy name [%s] is already in use", name)
  290. }
  291. pm.pxys[name] = pxy
  292. return nil
  293. }
  294. func (pm *Manager) Del(name string) {
  295. pm.mu.Lock()
  296. defer pm.mu.Unlock()
  297. delete(pm.pxys, name)
  298. }
  299. func (pm *Manager) GetByName(name string) (pxy Proxy, ok bool) {
  300. pm.mu.RLock()
  301. defer pm.mu.RUnlock()
  302. pxy, ok = pm.pxys[name]
  303. return
  304. }