manager.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Copyright 2019 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 plugin
  15. import (
  16. "context"
  17. "errors"
  18. "fmt"
  19. "github.com/fatedier/frp/pkg/util/util"
  20. "github.com/fatedier/frp/pkg/util/xlog"
  21. )
  22. type Manager struct {
  23. loginPlugins []Plugin
  24. newProxyPlugins []Plugin
  25. pingPlugins []Plugin
  26. newWorkConnPlugins []Plugin
  27. newUserConnPlugins []Plugin
  28. }
  29. func NewManager() *Manager {
  30. return &Manager{
  31. loginPlugins: make([]Plugin, 0),
  32. newProxyPlugins: make([]Plugin, 0),
  33. pingPlugins: make([]Plugin, 0),
  34. newWorkConnPlugins: make([]Plugin, 0),
  35. newUserConnPlugins: make([]Plugin, 0),
  36. }
  37. }
  38. func (m *Manager) Register(p Plugin) {
  39. if p.IsSupport(OpLogin) {
  40. m.loginPlugins = append(m.loginPlugins, p)
  41. }
  42. if p.IsSupport(OpNewProxy) {
  43. m.newProxyPlugins = append(m.newProxyPlugins, p)
  44. }
  45. if p.IsSupport(OpPing) {
  46. m.pingPlugins = append(m.pingPlugins, p)
  47. }
  48. if p.IsSupport(OpNewWorkConn) {
  49. m.newWorkConnPlugins = append(m.newWorkConnPlugins, p)
  50. }
  51. if p.IsSupport(OpNewUserConn) {
  52. m.newUserConnPlugins = append(m.newUserConnPlugins, p)
  53. }
  54. }
  55. func (m *Manager) Login(content *LoginContent) (*LoginContent, error) {
  56. if len(m.loginPlugins) == 0 {
  57. return content, nil
  58. }
  59. var (
  60. res = &Response{
  61. Reject: false,
  62. Unchange: true,
  63. }
  64. retContent interface{}
  65. err error
  66. )
  67. reqid, _ := util.RandID()
  68. xl := xlog.New().AppendPrefix("reqid: " + reqid)
  69. ctx := xlog.NewContext(context.Background(), xl)
  70. ctx = NewReqidContext(ctx, reqid)
  71. for _, p := range m.loginPlugins {
  72. res, retContent, err = p.Handle(ctx, OpLogin, *content)
  73. if err != nil {
  74. xl.Warn("send Login request to plugin [%s] error: %v", p.Name(), err)
  75. return nil, errors.New("send Login request to plugin error")
  76. }
  77. if res.Reject {
  78. return nil, fmt.Errorf("%s", res.RejectReason)
  79. }
  80. if !res.Unchange {
  81. content = retContent.(*LoginContent)
  82. }
  83. }
  84. return content, nil
  85. }
  86. func (m *Manager) NewProxy(content *NewProxyContent) (*NewProxyContent, error) {
  87. if len(m.newProxyPlugins) == 0 {
  88. return content, nil
  89. }
  90. var (
  91. res = &Response{
  92. Reject: false,
  93. Unchange: true,
  94. }
  95. retContent interface{}
  96. err error
  97. )
  98. reqid, _ := util.RandID()
  99. xl := xlog.New().AppendPrefix("reqid: " + reqid)
  100. ctx := xlog.NewContext(context.Background(), xl)
  101. ctx = NewReqidContext(ctx, reqid)
  102. for _, p := range m.newProxyPlugins {
  103. res, retContent, err = p.Handle(ctx, OpNewProxy, *content)
  104. if err != nil {
  105. xl.Warn("send NewProxy request to plugin [%s] error: %v", p.Name(), err)
  106. return nil, errors.New("send NewProxy request to plugin error")
  107. }
  108. if res.Reject {
  109. return nil, fmt.Errorf("%s", res.RejectReason)
  110. }
  111. if !res.Unchange {
  112. content = retContent.(*NewProxyContent)
  113. }
  114. }
  115. return content, nil
  116. }
  117. func (m *Manager) Ping(content *PingContent) (*PingContent, error) {
  118. if len(m.pingPlugins) == 0 {
  119. return content, nil
  120. }
  121. var (
  122. res = &Response{
  123. Reject: false,
  124. Unchange: true,
  125. }
  126. retContent interface{}
  127. err error
  128. )
  129. reqid, _ := util.RandID()
  130. xl := xlog.New().AppendPrefix("reqid: " + reqid)
  131. ctx := xlog.NewContext(context.Background(), xl)
  132. ctx = NewReqidContext(ctx, reqid)
  133. for _, p := range m.pingPlugins {
  134. res, retContent, err = p.Handle(ctx, OpPing, *content)
  135. if err != nil {
  136. xl.Warn("send Ping request to plugin [%s] error: %v", p.Name(), err)
  137. return nil, errors.New("send Ping request to plugin error")
  138. }
  139. if res.Reject {
  140. return nil, fmt.Errorf("%s", res.RejectReason)
  141. }
  142. if !res.Unchange {
  143. content = retContent.(*PingContent)
  144. }
  145. }
  146. return content, nil
  147. }
  148. func (m *Manager) NewWorkConn(content *NewWorkConnContent) (*NewWorkConnContent, error) {
  149. if len(m.newWorkConnPlugins) == 0 {
  150. return content, nil
  151. }
  152. var (
  153. res = &Response{
  154. Reject: false,
  155. Unchange: true,
  156. }
  157. retContent interface{}
  158. err error
  159. )
  160. reqid, _ := util.RandID()
  161. xl := xlog.New().AppendPrefix("reqid: " + reqid)
  162. ctx := xlog.NewContext(context.Background(), xl)
  163. ctx = NewReqidContext(ctx, reqid)
  164. for _, p := range m.newWorkConnPlugins {
  165. res, retContent, err = p.Handle(ctx, OpPing, *content)
  166. if err != nil {
  167. xl.Warn("send NewWorkConn request to plugin [%s] error: %v", p.Name(), err)
  168. return nil, errors.New("send NewWorkConn request to plugin error")
  169. }
  170. if res.Reject {
  171. return nil, fmt.Errorf("%s", res.RejectReason)
  172. }
  173. if !res.Unchange {
  174. content = retContent.(*NewWorkConnContent)
  175. }
  176. }
  177. return content, nil
  178. }
  179. func (m *Manager) NewUserConn(content *NewUserConnContent) (*NewUserConnContent, error) {
  180. if len(m.newUserConnPlugins) == 0 {
  181. return content, nil
  182. }
  183. var (
  184. res = &Response{
  185. Reject: false,
  186. Unchange: true,
  187. }
  188. retContent interface{}
  189. err error
  190. )
  191. reqid, _ := util.RandID()
  192. xl := xlog.New().AppendPrefix("reqid: " + reqid)
  193. ctx := xlog.NewContext(context.Background(), xl)
  194. ctx = NewReqidContext(ctx, reqid)
  195. for _, p := range m.newUserConnPlugins {
  196. res, retContent, err = p.Handle(ctx, OpNewUserConn, *content)
  197. if err != nil {
  198. xl.Info("send NewUserConn request to plugin [%s] error: %v", p.Name(), err)
  199. return nil, errors.New("send NewUserConn request to plugin error")
  200. }
  201. if res.Reject {
  202. return nil, fmt.Errorf("%s", res.RejectReason)
  203. }
  204. if !res.Unchange {
  205. content = retContent.(*NewUserConnContent)
  206. }
  207. }
  208. return content, nil
  209. }