Added OpExit

This commit is contained in:
LowK 2022-08-21 22:49:27 +07:00
parent 3e721d122b
commit 3c8e61bc90
6 changed files with 93 additions and 1 deletions

View File

@ -94,6 +94,29 @@ Client login operation
}
```
#### Exit
Client exit operation. When client cannot connect to server that will notify to admin.
Can use a new version (OpExit supported at: https://github.com/lowk3v/frp-notify/releases/tag/OpExitSupported)
```
{
"content": {
"version": <string>,
"hostname": <string>,
"os": <string>,
"arch": <string>,
"user": <string>,
"timestamp": <int64>,
"privilege_key": <string>,
"run_id": <string>,
"pool_count": <int>,
"metas": map<string>string,
"client_address": <string>
}
}
```
#### NewProxy
Create new proxy

View File

@ -83,6 +83,19 @@ type LoginResp struct {
Error string `json:"error,omitempty"`
}
// When frpc exit success, send this message to exit plugin server
type Exit struct {
Version string `json:"version,omitempty"`
Hostname string `json:"hostname,omitempty"`
Os string `json:"os,omitempty"`
Arch string `json:"arch,omitempty"`
User string `json:"user,omitempty"`
PrivilegeKey string `json:"privilege_key,omitempty"`
Timestamp int64 `json:"timestamp,omitempty"`
RunID string `json:"run_id,omitempty"`
Metas map[string]string `json:"metas,omitempty"`
}
// When frpc login success, send this message to frps for running a new proxy.
type NewProxy struct {
ProxyName string `json:"proxy_name,omitempty"`

View File

@ -26,6 +26,7 @@ import (
type Manager struct {
loginPlugins []Plugin
exitPlugins []Plugin
newProxyPlugins []Plugin
closeProxyPlugins []Plugin
pingPlugins []Plugin
@ -36,6 +37,7 @@ type Manager struct {
func NewManager() *Manager {
return &Manager{
loginPlugins: make([]Plugin, 0),
exitPlugins: make([]Plugin, 0),
newProxyPlugins: make([]Plugin, 0),
closeProxyPlugins: make([]Plugin, 0),
pingPlugins: make([]Plugin, 0),
@ -48,6 +50,9 @@ func (m *Manager) Register(p Plugin) {
if p.IsSupport(OpLogin) {
m.loginPlugins = append(m.loginPlugins, p)
}
if p.IsSupport(OpExit) {
m.exitPlugins = append(m.exitPlugins, p)
}
if p.IsSupport(OpNewProxy) {
m.newProxyPlugins = append(m.newProxyPlugins, p)
}
@ -99,6 +104,32 @@ func (m *Manager) Login(content *LoginContent) (*LoginContent, error) {
return content, nil
}
func (m *Manager) Exit(content *ExitContent) error {
if len(m.exitPlugins) == 0 {
return nil
}
errs := make([]string, 0)
reqid, _ := util.RandID()
xl := xlog.New().AppendPrefix("reqid: " + reqid)
ctx := xlog.NewContext(context.Background(), xl)
ctx = NewReqidContext(ctx, reqid)
for _, p := range m.exitPlugins {
_, _, err := p.Handle(ctx, OpExit, *content)
if err != nil {
xl.Warn("send Exit request to plugin [%s] error: %v", p.Name(), err)
errs = append(errs, fmt.Sprintf("[%s]: %v", p.Name(), err))
}
}
if len(errs) > 0 {
return fmt.Errorf("send Exit request to plugin errors: %s", strings.Join(errs, "; "))
} else {
return nil
}
}
func (m *Manager) NewProxy(content *NewProxyContent) (*NewProxyContent, error) {
if len(m.newProxyPlugins) == 0 {
return content, nil

View File

@ -19,9 +19,10 @@ import (
)
const (
APIVersion = "0.1.0"
APIVersion = "0.1.1"
OpLogin = "Login"
OpExit = "Exit"
OpNewProxy = "NewProxy"
OpCloseProxy = "CloseProxy"
OpPing = "Ping"

View File

@ -37,6 +37,11 @@ type LoginContent struct {
ClientAddress string `json:"client_address,omitempty"`
}
type ExitContent struct {
ClientAddress string `json:"client_address,omitempty"`
msg.Exit
}
type UserInfo struct {
User string `json:"user"`
Metas map[string]string `json:"metas"`

View File

@ -394,6 +394,25 @@ func (ctl *Control) stoper() {
ctl.allShutdown.Done()
xl.Info("client exit success")
notifyContent := &plugin.ExitContent{
ClientAddress: ctl.conn.RemoteAddr().String(),
Exit: msg.Exit{
Version: ctl.loginMsg.Version,
Hostname: ctl.loginMsg.Hostname,
Os: ctl.loginMsg.Os,
Arch: ctl.loginMsg.Arch,
User: ctl.loginMsg.User,
PrivilegeKey: ctl.loginMsg.PrivilegeKey,
Timestamp: ctl.loginMsg.Timestamp,
RunID: ctl.loginMsg.RunID,
Metas: ctl.loginMsg.Metas,
},
}
go func() {
_ = ctl.pluginManager.Exit(notifyContent)
}()
metrics.Server.CloseClient()
}