manager.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/utils/util"
  20. "github.com/fatedier/frp/utils/xlog"
  21. )
  22. type Manager struct {
  23. loginPlugins []Plugin
  24. newProxyPlugins []Plugin
  25. }
  26. func NewManager() *Manager {
  27. return &Manager{
  28. loginPlugins: make([]Plugin, 0),
  29. newProxyPlugins: make([]Plugin, 0),
  30. }
  31. }
  32. func (m *Manager) Register(p Plugin) {
  33. if p.IsSupport(OpLogin) {
  34. m.loginPlugins = append(m.loginPlugins, p)
  35. }
  36. if p.IsSupport(OpNewProxy) {
  37. m.newProxyPlugins = append(m.newProxyPlugins, p)
  38. }
  39. }
  40. func (m *Manager) Login(content *LoginContent) (*LoginContent, error) {
  41. var (
  42. res = &Response{
  43. Reject: false,
  44. Unchange: true,
  45. }
  46. retContent interface{}
  47. err error
  48. )
  49. reqid, _ := util.RandId()
  50. xl := xlog.New().AppendPrefix("reqid: " + reqid)
  51. ctx := xlog.NewContext(context.Background(), xl)
  52. ctx = NewReqidContext(ctx, reqid)
  53. for _, p := range m.loginPlugins {
  54. res, retContent, err = p.Handle(ctx, OpLogin, *content)
  55. if err != nil {
  56. xl.Warn("send Login request to plugin [%s] error: %v", p.Name(), err)
  57. return nil, errors.New("send Login request to plugin error")
  58. }
  59. if res.Reject {
  60. return nil, fmt.Errorf("%s", res.RejectReason)
  61. }
  62. if !res.Unchange {
  63. content = retContent.(*LoginContent)
  64. }
  65. }
  66. return content, nil
  67. }
  68. func (m *Manager) NewProxy(content *NewProxyContent) (*NewProxyContent, error) {
  69. var (
  70. res = &Response{
  71. Reject: false,
  72. Unchange: true,
  73. }
  74. retContent interface{}
  75. err error
  76. )
  77. reqid, _ := util.RandId()
  78. xl := xlog.New().AppendPrefix("reqid: " + reqid)
  79. ctx := xlog.NewContext(context.Background(), xl)
  80. ctx = NewReqidContext(ctx, reqid)
  81. for _, p := range m.newProxyPlugins {
  82. res, retContent, err = p.Handle(ctx, OpNewProxy, *content)
  83. if err != nil {
  84. xl.Warn("send NewProxy request to plugin [%s] error: %v", p.Name(), err)
  85. return nil, errors.New("send NewProxy request to plugin error")
  86. }
  87. if res.Reject {
  88. return nil, fmt.Errorf("%s", res.RejectReason)
  89. }
  90. if !res.Unchange {
  91. content = retContent.(*NewProxyContent)
  92. }
  93. }
  94. return content, nil
  95. }