feat: server plugin for Ping messages

This commit is contained in:
gulewin 2020-03-13 22:54:14 -04:00
parent 486e389c26
commit 025a6e1094
4 changed files with 54 additions and 1 deletions

View File

@ -26,6 +26,7 @@ import (
type Manager struct {
loginPlugins []Plugin
newProxyPlugins []Plugin
pingPlugins []Plugin
}
func NewManager() *Manager {
@ -42,6 +43,9 @@ func (m *Manager) Register(p Plugin) {
if p.IsSupport(OpNewProxy) {
m.newProxyPlugins = append(m.newProxyPlugins, p)
}
if p.IsSupport(OpPing) {
m.pingPlugins = append(m.pingPlugins, p)
}
}
func (m *Manager) Login(content *LoginContent) (*LoginContent, error) {
@ -103,3 +107,33 @@ func (m *Manager) NewProxy(content *NewProxyContent) (*NewProxyContent, error) {
}
return content, nil
}
func (m *Manager) Ping(content *PingContent) (*PingContent, error) {
var (
res = &Response{
Reject: false,
Unchange: true,
}
retContent interface{}
err error
)
reqid, _ := util.RandId()
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
}

View File

@ -23,6 +23,7 @@ const (
OpLogin = "Login"
OpNewProxy = "NewProxy"
OpPing = "Ping"
)
type Plugin interface {

View File

@ -45,3 +45,8 @@ type NewProxyContent struct {
User UserInfo `json:"user"`
msg.NewProxy
}
type PingContent struct {
User UserInfo `json:"user"`
msg.Ping
}

View File

@ -450,7 +450,20 @@ func (ctl *Control) manager() {
ctl.CloseProxy(m)
xl.Info("close proxy [%s] success", m.ProxyName)
case *msg.Ping:
if err := ctl.authVerifier.VerifyPing(m); err != nil {
content := &plugin.PingContent{
User: plugin.UserInfo{
User: ctl.loginMsg.User,
Metas: ctl.loginMsg.Metas,
RunId: ctl.loginMsg.RunId,
},
Ping: *m,
}
retContent, err := ctl.pluginManager.Ping(content)
if err == nil {
m = &retContent.Ping
err = ctl.authVerifier.VerifyPing(m)
}
if err != nil {
xl.Warn("received invalid ping: %v", err)
ctl.sendCh <- &msg.Pong{
Error: "invalid authentication in ping",