nps/core/struct.go
2019-10-15 13:59:08 +08:00

79 lines
2.0 KiB
Go

package core
import (
"context"
"errors"
"github.com/cnlh/nps/bridge"
"net"
)
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_END
STAGE_RUN
PROXY_CONNECTION_TYPE = "proxy_target_type"
PROXY_CONNECTION_ADDR = "proxy_target_addr"
PROXY_CONNECTION_PORT = "proxy_target_port"
CLIENT_CONNECTION = "clientConn"
BRIDGE = "bridge"
CLIENT_ID = "client_id"
)
var (
CLIENT_CONNECTION_NOT_EXIST = errors.New("the client connection is not exist")
BRIDGE_NOT_EXIST = errors.New("the client connection is not exist")
REQUEST_EOF = errors.New("the request has finished")
CLIENT_ID_NOT_EXIST = errors.New("the request has finished")
)
// Plugin interface, all plugins must implement those functions.
type Plugin interface {
GetConfigName() *NpsConfigs
GetStage() Stage
Start(ctx context.Context, config map[string]string) error
Run(ctx context.Context, config map[string]string) error
End(ctx context.Context, config map[string]string) error
}
type NpsPlugin struct {
}
func (npsPlugin *NpsPlugin) GetConfigName() *NpsConfigs {
return nil
}
func (npsPlugin *NpsPlugin) GetStage() Stage {
return STAGE_RUN
}
func (npsPlugin *NpsPlugin) Start(ctx context.Context, config map[string]string) error {
return nil
}
func (npsPlugin *NpsPlugin) Run(ctx context.Context, config map[string]string) error {
return nil
}
func (npsPlugin *NpsPlugin) End(ctx context.Context, config map[string]string) error {
return nil
}
func (npsPlugin *NpsPlugin) GetClientConn(ctx context.Context) net.Conn {
return ctx.Value(CLIENT_CONNECTION).(net.Conn)
}
func (npsPlugin *NpsPlugin) GetBridge(ctx context.Context) *bridge.Bridge {
return ctx.Value(BRIDGE).(*bridge.Bridge)
}
func (npsPlugin *NpsPlugin) GetClientId(ctx context.Context) int {
return ctx.Value(CLIENT_ID).(int)
}