| // |
| 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 |
| 15 | package server |
| 16 |
| 17 | import ( |
| 18 | "context" |
| 19 | "errors" |
| 20 | "fmt" |
| 21 | "strings" |
| 22 |
| 23 | "github.com/fatedier/frp/pkg/util/util" |
| 24 | "github.com/fatedier/frp/pkg/util/xlog" |
| 25 | ) |
| 26 |
| 27 | type Manager struct { |
| 28 | loginPlugins []Plugin |
| 29 | newProxyPlugins []Plugin |
| 30 | closeProxyPlugins []Plugin |
| 31 | pingPlugins []Plugin |
| 32 | newWorkConnPlugins []Plugin |
| 33 | newUserConnPlugins []Plugin |
| 34 | } |
| 35 |
| 36 | func NewManager() *Manager { |
| 37 | return &Manager{ |
| 38 | loginPlugins: make([]Plugin, 0), |
| 39 | newProxyPlugins: make([]Plugin, 0), |
| 40 | closeProxyPlugins: make([]Plugin, 0), |
| 41 | pingPlugins: make([]Plugin, 0), |
| 42 | newWorkConnPlugins: make([]Plugin, 0), |
| 43 | newUserConnPlugins: make([]Plugin, 0), |
| 44 | } |
| 45 | } |
| 46 |
| 47 | func newPluginRequestContext() (context.Context, *xlog.Logger) { |
| 48 | reqid, _ := util.RandID() |
| 49 | xl := xlog.New().AppendPrefix("reqid: " + reqid) |
| 50 | ctx := xlog.NewContext(context.Background(), xl) |
| 51 | return NewReqidContext(ctx, reqid), xl |
| 52 | } |
| 53 |
| 54 | type pluginErrorLogMode bool |
| 55 |
| 56 | const ( |
| 57 | // Warn is the zero value because it is the default for mutable plugin operations. |
| 58 | pluginErrorLogWarn pluginErrorLogMode = false |
| 59 | pluginErrorLogInfo pluginErrorLogMode = true |
| 60 | ) |
| 61 |
| 62 | func logPluginError(xl *xlog.Logger, p Plugin, op string, err error, mode pluginErrorLogMode) { |
| 63 | if mode == pluginErrorLogInfo { |
| 64 | xl.Infof("send %s request to plugin [%s] error: %v", op, p.Name(), err) |
| 65 | return |
| 66 | } |
| 67 | xl.Warnf("send %s request to plugin [%s] error: %v", op, p.Name(), err) |
| 68 | } |
| 69 |
| 70 | func handleMutableContent[T any]( |
| 71 | plugins []Plugin, |
| 72 | op string, |
| 73 | content *T, |
| 74 | logMode pluginErrorLogMode, |
| 75 | ) (*T, error) { |
| 76 | if len(plugins) == 0 { |
| 77 | return content, nil |
| 78 | } |
| 79 |
| 80 | var ( |
| 81 | res = &Response{ |
| 82 | Reject: false, |
| 83 | Unchange: true, |
| 84 | } |
| 85 | retContent any |
| 86 | err error |
| 87 | ) |
| 88 | ctx, xl := newPluginRequestContext() |
| 89 |
| 90 | for _, p := range plugins { |
| 91 | res, retContent, err = p.Handle(ctx, op, *content) |
| 92 | if err != nil { |
| 93 | logPluginError(xl, p, op, err, logMode) |
| 94 | return nil, errors.New("send " + op + " request to plugin error") |
| 95 | } |
| 96 | if res.Reject { |
| 97 | return nil, fmt.Errorf("%s", res.RejectReason) |
| 98 | } |
| 99 | if !res.Unchange { |
| 100 | // Preserve the existing Plugin contract: changed content must be *T. |
| 101 | // Buggy Plugin implementations still panic here, by design. |
| 102 | content = retContent.(*T) |
| 103 | } |
| 104 | } |
| 105 | return content, nil |
| 106 | } |
| 107 |
| 108 | func (m *Manager) Register(p Plugin) { |
| 109 | if p.IsSupport(OpLogin) { |
| 110 | m.loginPlugins = append(m.loginPlugins, p) |
| 111 | } |
| 112 | if p.IsSupport(OpNewProxy) { |
| 113 | m.newProxyPlugins = append(m.newProxyPlugins, p) |
| 114 | } |
| 115 | if p.IsSupport(OpCloseProxy) { |
| 116 | m.closeProxyPlugins = append(m.closeProxyPlugins, p) |
| 117 | } |
| 118 | if p.IsSupport(OpPing) { |
| 119 | m.pingPlugins = append(m.pingPlugins, p) |
| 120 | } |
| 121 | if p.IsSupport(OpNewWorkConn) { |
| 122 | m.newWorkConnPlugins = append(m.newWorkConnPlugins, p) |
| 123 | } |
| 124 | if p.IsSupport(OpNewUserConn) { |
| 125 | m.newUserConnPlugins = append(m.newUserConnPlugins, p) |
| 126 | } |
| 127 | } |
| 128 |
| 129 | func (m *Manager) Login(content *LoginContent) (*LoginContent, error) { |
| 130 | return handleMutableContent(m.loginPlugins, OpLogin, content, pluginErrorLogWarn) |
| 131 | } |
| 132 |
| 133 | func (m *Manager) NewProxy(content *NewProxyContent) (*NewProxyContent, error) { |
| 134 | return handleMutableContent(m.newProxyPlugins, OpNewProxy, content, pluginErrorLogWarn) |
| 135 | } |
| 136 |
| 137 | func (m *Manager) CloseProxy(content *CloseProxyContent) error { |
| 138 | if len(m.closeProxyPlugins) == 0 { |
| 139 | return nil |
| 140 | } |
| 141 |
| 142 | errs := make([]string, 0) |
| 143 | ctx, xl := newPluginRequestContext() |
| 144 |
| 145 | for _, p := range m.closeProxyPlugins { |
| 146 | _, _, err := p.Handle(ctx, OpCloseProxy, *content) |
| 147 | if err != nil { |
| 148 | xl.Warnf("send CloseProxy request to plugin [%s] error: %v", p.Name(), err) |
| 149 | errs = append(errs, fmt.Sprintf("[%s]: %v", p.Name(), err)) |
| 150 | } |
| 151 | } |
| 152 |
| 153 | if len(errs) > 0 { |
| 154 | return fmt.Errorf("send CloseProxy request to plugin errors: %s", strings.Join(errs, "; ")) |
| 155 | } |
| 156 | return nil |
| 157 | } |
| 158 |
| 159 | func (m *Manager) Ping(content *PingContent) (*PingContent, error) { |
| 160 | return handleMutableContent(m.pingPlugins, OpPing, content, pluginErrorLogWarn) |
| 161 | } |
| 162 |
| 163 | func (m *Manager) NewWorkConn(content *NewWorkConnContent) (*NewWorkConnContent, error) { |
| 164 | return handleMutableContent(m.newWorkConnPlugins, OpNewWorkConn, content, pluginErrorLogWarn) |
| 165 | } |
| 166 |
| 167 | func (m *Manager) NewUserConn(content *NewUserConnContent) (*NewUserConnContent, error) { |
| 168 | // Preserve the pre-refactor log level for NewUserConn plugin errors. |
| 169 | return handleMutableContent(m.newUserConnPlugins, OpNewUserConn, content, pluginErrorLogInfo) |
| 170 | } |