mirror of
https://github.com/ehang-io/nps.git
synced 2025-09-04 21:56:53 +00:00
module
This commit is contained in:
@@ -4,21 +4,22 @@ package core
|
||||
type Config struct {
|
||||
ConfigName string
|
||||
Description string
|
||||
ConfigLevel ConfigLevel
|
||||
}
|
||||
|
||||
type NpsConfigs struct {
|
||||
configs []*Config
|
||||
}
|
||||
|
||||
func NewNpsConfigs(name, des string) *NpsConfigs {
|
||||
func NewNpsConfigs(name, des string, level ConfigLevel) *NpsConfigs {
|
||||
c := &NpsConfigs{}
|
||||
c.configs = make([]*Config, 0)
|
||||
c.Add(name, des)
|
||||
c.Add(name, des, level)
|
||||
return c
|
||||
}
|
||||
|
||||
func (config *NpsConfigs) Add(name, des string) {
|
||||
config.configs = append(config.configs, &Config{ConfigName: name, Description: des})
|
||||
func (config *NpsConfigs) Add(name, des string, level ConfigLevel) {
|
||||
config.configs = append(config.configs, &Config{ConfigName: name, Description: des, ConfigLevel: level})
|
||||
}
|
||||
|
||||
func (config *NpsConfigs) GetAll() []*Config {
|
||||
|
@@ -9,40 +9,51 @@ import (
|
||||
// Plugin interface, all plugins must implement those functions.
|
||||
type Plugin interface {
|
||||
GetConfigName() *NpsConfigs
|
||||
GetConfigLevel() ConfigLevel
|
||||
GetStage() Stage
|
||||
Start(ctx context.Context, config map[string]string) (context.Context, error)
|
||||
Run(ctx context.Context, config map[string]string) (context.Context, error)
|
||||
End(ctx context.Context, config map[string]string) (context.Context, error)
|
||||
InitConfig(globalConfig, clientConfig, pluginConfig map[string]string)
|
||||
GetStage() []Stage
|
||||
Start(ctx context.Context) (context.Context, error)
|
||||
Run(ctx context.Context) (context.Context, error)
|
||||
End(ctx context.Context) (context.Context, error)
|
||||
}
|
||||
|
||||
type NpsPlugin struct {
|
||||
Version string
|
||||
Configs map[string]string
|
||||
}
|
||||
|
||||
func (npsPlugin *NpsPlugin) GetConfigName() *NpsConfigs {
|
||||
return nil
|
||||
}
|
||||
|
||||
// describe the config level
|
||||
func (npsPlugin *NpsPlugin) GetConfigLevel() ConfigLevel {
|
||||
return CONFIG_LEVEL_PLUGIN
|
||||
func (npsPlugin *NpsPlugin) InitConfig(globalConfig, clientConfig, pluginConfig map[string]string) {
|
||||
npsPlugin.Configs = make(map[string]string)
|
||||
for _, cfg := range npsPlugin.GetConfigName().GetAll() {
|
||||
switch cfg.ConfigLevel {
|
||||
case CONFIG_LEVEL_PLUGIN:
|
||||
npsPlugin.Configs[cfg.ConfigName] = pluginConfig[cfg.ConfigName]
|
||||
case CONFIG_LEVEL_CLIENT:
|
||||
npsPlugin.Configs[cfg.ConfigName] = clientConfig[cfg.ConfigName]
|
||||
case CONFIG_LEVEL_GLOBAL:
|
||||
npsPlugin.Configs[cfg.ConfigName] = globalConfig[cfg.ConfigName]
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// describe the stage of the plugin
|
||||
func (npsPlugin *NpsPlugin) GetStage() Stage {
|
||||
return STAGE_RUN
|
||||
func (npsPlugin *NpsPlugin) GetStage() []Stage {
|
||||
return []Stage{STAGE_RUN}
|
||||
}
|
||||
|
||||
func (npsPlugin *NpsPlugin) Start(ctx context.Context, config map[string]string) (context.Context, error) {
|
||||
func (npsPlugin *NpsPlugin) Start(ctx context.Context) (context.Context, error) {
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
func (npsPlugin *NpsPlugin) Run(ctx context.Context, config map[string]string) (context.Context, error) {
|
||||
func (npsPlugin *NpsPlugin) Run(ctx context.Context) (context.Context, error) {
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
func (npsPlugin *NpsPlugin) End(ctx context.Context, config map[string]string) (context.Context, error) {
|
||||
func (npsPlugin *NpsPlugin) End(ctx context.Context) (context.Context, error) {
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
@@ -50,6 +61,10 @@ func (npsPlugin *NpsPlugin) GetClientConn(ctx context.Context) net.Conn {
|
||||
return ctx.Value(CLIENT_CONNECTION).(net.Conn)
|
||||
}
|
||||
|
||||
func (npsPlugin *NpsPlugin) SetClientConn(ctx context.Context, conn net.Conn) context.Context {
|
||||
return context.WithValue(ctx, CLIENT_CONNECTION, conn)
|
||||
}
|
||||
|
||||
func (npsPlugin *NpsPlugin) GetBridge(ctx context.Context) *bridge.Bridge {
|
||||
return ctx.Value(BRIDGE).(*bridge.Bridge)
|
||||
}
|
||||
@@ -59,17 +74,44 @@ func (npsPlugin *NpsPlugin) GetClientId(ctx context.Context) int {
|
||||
}
|
||||
|
||||
type Plugins struct {
|
||||
pgs []Plugin
|
||||
StartPgs []Plugin
|
||||
RunPgs []Plugin
|
||||
EndPgs []Plugin
|
||||
AllPgs []Plugin
|
||||
}
|
||||
|
||||
func NewPlugins() *Plugins {
|
||||
p := &Plugins{}
|
||||
p.pgs = make([]Plugin, 0)
|
||||
p.StartPgs = make([]Plugin, 0)
|
||||
p.RunPgs = make([]Plugin, 0)
|
||||
p.EndPgs = make([]Plugin, 0)
|
||||
p.AllPgs = make([]Plugin, 0)
|
||||
return p
|
||||
}
|
||||
|
||||
func (pl *Plugins) Add(plugins ...Plugin) {
|
||||
for _, plugin := range plugins {
|
||||
pl.pgs = append(pl.pgs, plugin)
|
||||
for _, v := range plugin.GetStage() {
|
||||
pl.AllPgs = append(pl.RunPgs, plugin)
|
||||
switch v {
|
||||
case STAGE_RUN:
|
||||
pl.RunPgs = append(pl.RunPgs, plugin)
|
||||
case STAGE_END:
|
||||
pl.EndPgs = append(pl.EndPgs, plugin)
|
||||
case STAGE_START:
|
||||
pl.StartPgs = append(pl.StartPgs, plugin)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func RunPlugin(ctx context.Context, pgs []Plugin) error {
|
||||
var err error
|
||||
for _, pg := range pgs {
|
||||
ctx, err = pg.Start(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@@ -8,11 +8,7 @@ type Stage uint8
|
||||
|
||||
// These constants are meant to describe the stage in which the plugin is running.
|
||||
const (
|
||||
STAGE_START_RUN_END Stage = iota
|
||||
STAGE_START_RUN
|
||||
STAGE_START_END
|
||||
STAGE_RUN_END
|
||||
STAGE_START
|
||||
STAGE_START Stage = iota
|
||||
STAGE_END
|
||||
STAGE_RUN
|
||||
PROXY_CONNECTION_TYPE = "proxy_target_type"
|
||||
@@ -33,8 +29,7 @@ const (
|
||||
|
||||
var (
|
||||
CLIENT_CONNECTION_NOT_EXIST = errors.New("the client connection is not exist")
|
||||
BRIDGE_NOT_EXIST = errors.New("the client connection is not exist")
|
||||
BRIDGE_NOT_EXIST = errors.New("the bridge is not exist")
|
||||
REQUEST_EOF = errors.New("the request has finished")
|
||||
CLIENT_ID_NOT_EXIST = errors.New("the client id is not exist")
|
||||
)
|
||||
|
||||
|
@@ -4,8 +4,10 @@ import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"io"
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func CopyBuffer(dst io.Writer, src io.Reader) (written int64, err error) {
|
||||
@@ -69,3 +71,28 @@ func GetLenBytes(buf []byte) (b []byte, err error) {
|
||||
b = raw.Bytes()
|
||||
return
|
||||
}
|
||||
|
||||
func NewTcpListenerAndProcess(addr string, f func(c net.Conn), listener *net.Listener) error {
|
||||
var err error
|
||||
*listener, err = net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
Accept(*listener, f)
|
||||
return nil
|
||||
}
|
||||
|
||||
func Accept(l net.Listener, f func(c net.Conn)) {
|
||||
for {
|
||||
c, err := l.Accept()
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "use of closed network connection") {
|
||||
break
|
||||
}
|
||||
logs.Warn(err)
|
||||
continue
|
||||
}
|
||||
go f(c)
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user