2019-12-20 13:28:28 +01:00
|
|
|
// Copyright 2019 fatedier, fatedier@gmail.com
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2020-09-23 07:49:14 +02:00
|
|
|
"github.com/fatedier/frp/pkg/util/util"
|
|
|
|
"github.com/fatedier/frp/pkg/util/xlog"
|
2019-12-20 13:28:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Manager struct {
|
2020-03-17 18:52:44 +01:00
|
|
|
loginPlugins []Plugin
|
|
|
|
newProxyPlugins []Plugin
|
|
|
|
pingPlugins []Plugin
|
|
|
|
newWorkConnPlugins []Plugin
|
2020-04-16 07:06:46 +02:00
|
|
|
newUserConnPlugins []Plugin
|
2019-12-20 13:28:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewManager() *Manager {
|
|
|
|
return &Manager{
|
2020-03-17 18:52:44 +01:00
|
|
|
loginPlugins: make([]Plugin, 0),
|
|
|
|
newProxyPlugins: make([]Plugin, 0),
|
|
|
|
pingPlugins: make([]Plugin, 0),
|
|
|
|
newWorkConnPlugins: make([]Plugin, 0),
|
2020-04-16 07:06:46 +02:00
|
|
|
newUserConnPlugins: make([]Plugin, 0),
|
2019-12-20 13:28:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) Register(p Plugin) {
|
|
|
|
if p.IsSupport(OpLogin) {
|
|
|
|
m.loginPlugins = append(m.loginPlugins, p)
|
|
|
|
}
|
|
|
|
if p.IsSupport(OpNewProxy) {
|
|
|
|
m.newProxyPlugins = append(m.newProxyPlugins, p)
|
|
|
|
}
|
2020-03-17 18:52:44 +01:00
|
|
|
if p.IsSupport(OpPing) {
|
|
|
|
m.pingPlugins = append(m.pingPlugins, p)
|
|
|
|
}
|
|
|
|
if p.IsSupport(OpNewWorkConn) {
|
2020-05-12 09:55:35 +02:00
|
|
|
m.newWorkConnPlugins = append(m.newWorkConnPlugins, p)
|
2020-03-17 18:52:44 +01:00
|
|
|
}
|
2020-04-16 07:06:46 +02:00
|
|
|
if p.IsSupport(OpNewUserConn) {
|
|
|
|
m.newUserConnPlugins = append(m.newUserConnPlugins, p)
|
|
|
|
}
|
2019-12-20 13:28:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) Login(content *LoginContent) (*LoginContent, error) {
|
2020-03-20 13:53:14 +01:00
|
|
|
if len(m.loginPlugins) == 0 {
|
|
|
|
return content, nil
|
|
|
|
}
|
|
|
|
|
2019-12-20 13:28:28 +01:00
|
|
|
var (
|
|
|
|
res = &Response{
|
|
|
|
Reject: false,
|
|
|
|
Unchange: true,
|
|
|
|
}
|
|
|
|
retContent interface{}
|
|
|
|
err error
|
|
|
|
)
|
2020-05-24 11:48:37 +02:00
|
|
|
reqid, _ := util.RandID()
|
2019-12-20 13:28:28 +01:00
|
|
|
xl := xlog.New().AppendPrefix("reqid: " + reqid)
|
|
|
|
ctx := xlog.NewContext(context.Background(), xl)
|
|
|
|
ctx = NewReqidContext(ctx, reqid)
|
|
|
|
|
|
|
|
for _, p := range m.loginPlugins {
|
|
|
|
res, retContent, err = p.Handle(ctx, OpLogin, *content)
|
|
|
|
if err != nil {
|
|
|
|
xl.Warn("send Login request to plugin [%s] error: %v", p.Name(), err)
|
|
|
|
return nil, errors.New("send Login request to plugin error")
|
|
|
|
}
|
|
|
|
if res.Reject {
|
|
|
|
return nil, fmt.Errorf("%s", res.RejectReason)
|
|
|
|
}
|
|
|
|
if !res.Unchange {
|
|
|
|
content = retContent.(*LoginContent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return content, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) NewProxy(content *NewProxyContent) (*NewProxyContent, error) {
|
2020-03-20 13:53:14 +01:00
|
|
|
if len(m.newProxyPlugins) == 0 {
|
|
|
|
return content, nil
|
|
|
|
}
|
|
|
|
|
2019-12-20 13:28:28 +01:00
|
|
|
var (
|
|
|
|
res = &Response{
|
|
|
|
Reject: false,
|
|
|
|
Unchange: true,
|
|
|
|
}
|
|
|
|
retContent interface{}
|
|
|
|
err error
|
|
|
|
)
|
2020-05-24 11:48:37 +02:00
|
|
|
reqid, _ := util.RandID()
|
2019-12-20 13:28:28 +01:00
|
|
|
xl := xlog.New().AppendPrefix("reqid: " + reqid)
|
|
|
|
ctx := xlog.NewContext(context.Background(), xl)
|
|
|
|
ctx = NewReqidContext(ctx, reqid)
|
|
|
|
|
|
|
|
for _, p := range m.newProxyPlugins {
|
|
|
|
res, retContent, err = p.Handle(ctx, OpNewProxy, *content)
|
|
|
|
if err != nil {
|
|
|
|
xl.Warn("send NewProxy request to plugin [%s] error: %v", p.Name(), err)
|
|
|
|
return nil, errors.New("send NewProxy request to plugin error")
|
|
|
|
}
|
|
|
|
if res.Reject {
|
|
|
|
return nil, fmt.Errorf("%s", res.RejectReason)
|
|
|
|
}
|
|
|
|
if !res.Unchange {
|
|
|
|
content = retContent.(*NewProxyContent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return content, nil
|
|
|
|
}
|
2020-03-17 18:52:44 +01:00
|
|
|
|
|
|
|
func (m *Manager) Ping(content *PingContent) (*PingContent, error) {
|
2020-03-20 13:53:14 +01:00
|
|
|
if len(m.pingPlugins) == 0 {
|
|
|
|
return content, nil
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:52:44 +01:00
|
|
|
var (
|
|
|
|
res = &Response{
|
|
|
|
Reject: false,
|
|
|
|
Unchange: true,
|
|
|
|
}
|
|
|
|
retContent interface{}
|
|
|
|
err error
|
|
|
|
)
|
2020-05-24 11:48:37 +02:00
|
|
|
reqid, _ := util.RandID()
|
2020-03-17 18:52:44 +01:00
|
|
|
xl := xlog.New().AppendPrefix("reqid: " + reqid)
|
|
|
|
ctx := xlog.NewContext(context.Background(), xl)
|
|
|
|
ctx = NewReqidContext(ctx, reqid)
|
|
|
|
|
|
|
|
for _, p := range m.pingPlugins {
|
|
|
|
res, retContent, err = p.Handle(ctx, OpPing, *content)
|
|
|
|
if err != nil {
|
|
|
|
xl.Warn("send Ping request to plugin [%s] error: %v", p.Name(), err)
|
|
|
|
return nil, errors.New("send Ping request to plugin error")
|
|
|
|
}
|
|
|
|
if res.Reject {
|
|
|
|
return nil, fmt.Errorf("%s", res.RejectReason)
|
|
|
|
}
|
|
|
|
if !res.Unchange {
|
|
|
|
content = retContent.(*PingContent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return content, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) NewWorkConn(content *NewWorkConnContent) (*NewWorkConnContent, error) {
|
2020-03-20 13:53:14 +01:00
|
|
|
if len(m.newWorkConnPlugins) == 0 {
|
|
|
|
return content, nil
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:52:44 +01:00
|
|
|
var (
|
|
|
|
res = &Response{
|
|
|
|
Reject: false,
|
|
|
|
Unchange: true,
|
|
|
|
}
|
|
|
|
retContent interface{}
|
|
|
|
err error
|
|
|
|
)
|
2020-05-24 11:48:37 +02:00
|
|
|
reqid, _ := util.RandID()
|
2020-03-17 18:52:44 +01:00
|
|
|
xl := xlog.New().AppendPrefix("reqid: " + reqid)
|
|
|
|
ctx := xlog.NewContext(context.Background(), xl)
|
|
|
|
ctx = NewReqidContext(ctx, reqid)
|
|
|
|
|
2021-08-02 07:07:28 +02:00
|
|
|
for _, p := range m.newWorkConnPlugins {
|
2020-03-17 18:52:44 +01:00
|
|
|
res, retContent, err = p.Handle(ctx, OpPing, *content)
|
|
|
|
if err != nil {
|
|
|
|
xl.Warn("send NewWorkConn request to plugin [%s] error: %v", p.Name(), err)
|
|
|
|
return nil, errors.New("send NewWorkConn request to plugin error")
|
|
|
|
}
|
|
|
|
if res.Reject {
|
|
|
|
return nil, fmt.Errorf("%s", res.RejectReason)
|
|
|
|
}
|
|
|
|
if !res.Unchange {
|
|
|
|
content = retContent.(*NewWorkConnContent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return content, nil
|
|
|
|
}
|
2020-04-16 07:06:46 +02:00
|
|
|
|
|
|
|
func (m *Manager) NewUserConn(content *NewUserConnContent) (*NewUserConnContent, error) {
|
|
|
|
if len(m.newUserConnPlugins) == 0 {
|
|
|
|
return content, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
res = &Response{
|
|
|
|
Reject: false,
|
|
|
|
Unchange: true,
|
|
|
|
}
|
|
|
|
retContent interface{}
|
|
|
|
err error
|
|
|
|
)
|
2020-05-24 11:48:37 +02:00
|
|
|
reqid, _ := util.RandID()
|
2020-04-16 07:06:46 +02:00
|
|
|
xl := xlog.New().AppendPrefix("reqid: " + reqid)
|
|
|
|
ctx := xlog.NewContext(context.Background(), xl)
|
|
|
|
ctx = NewReqidContext(ctx, reqid)
|
|
|
|
|
|
|
|
for _, p := range m.newUserConnPlugins {
|
|
|
|
res, retContent, err = p.Handle(ctx, OpNewUserConn, *content)
|
|
|
|
if err != nil {
|
|
|
|
xl.Info("send NewUserConn request to plugin [%s] error: %v", p.Name(), err)
|
|
|
|
return nil, errors.New("send NewUserConn request to plugin error")
|
|
|
|
}
|
|
|
|
if res.Reject {
|
|
|
|
return nil, fmt.Errorf("%s", res.RejectReason)
|
|
|
|
}
|
|
|
|
if !res.Unchange {
|
|
|
|
content = retContent.(*NewUserConnContent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return content, nil
|
|
|
|
}
|