core plugin

This commit is contained in:
刘河 2019-10-14 23:04:14 +08:00
parent 98ab48aaca
commit 8f45d86cee
6 changed files with 39 additions and 92 deletions

View File

@ -3,6 +3,7 @@ package core
import (
"context"
"errors"
"net"
)
type Stage uint8
@ -28,7 +29,7 @@ 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")
CLIENT_ID_NOT_EXIST = errors.New("the request has finished")
)
// Plugin interface, all plugins must implement those functions.
@ -39,3 +40,30 @@ type Plugin interface {
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)
}

View File

@ -8,6 +8,7 @@ import (
)
type Proxy struct {
core.NpsPlugin
clientConn net.Conn
ctx context.Context
}
@ -15,20 +16,9 @@ type Proxy struct {
func (proxy *Proxy) GetConfigName() *core.NpsConfigs {
return core.NewNpsConfigs("socks5_proxy", "proxy to inet")
}
func (proxy *Proxy) GetStage() core.Stage {
return core.STAGE_RUN
}
func (proxy *Proxy) Start(ctx context.Context, config map[string]string) error {
return nil
}
func (proxy *Proxy) Run(ctx context.Context, config map[string]string) error {
clientCtxConn := ctx.Value(core.CLIENT_CONNECTION)
if clientCtxConn == nil {
return core.CLIENT_CONNECTION_NOT_EXIST
}
proxy.clientConn = clientCtxConn.(net.Conn)
proxy.clientConn = proxy.GetClientConn(ctx)
proxy.ctx = ctx
bg := ctx.Value(core.BRIDGE)
if bg == nil {
@ -53,7 +43,3 @@ func (proxy *Proxy) Run(ctx context.Context, config map[string]string) error {
core.CopyBuffer(clientCtxConn.(net.Conn), severConn)
return nil
}
func (proxy *Proxy) End(ctx context.Context, config map[string]string) error {
return nil
}

View File

@ -8,6 +8,7 @@ import (
)
type CheckAccess struct {
core.NpsPlugin
clientConn net.Conn
clientUsername string
clientPassword string
@ -22,30 +23,14 @@ func (check *CheckAccess) GetConfigName() *core.NpsConfigs {
return c
}
func (check *CheckAccess) GetStage() core.Stage {
return core.STAGE_RUN
}
func (check *CheckAccess) Start(ctx context.Context, config map[string]string) error {
return nil
}
func (check *CheckAccess) Run(ctx context.Context, config map[string]string) error {
clientCtxConn := ctx.Value(core.CLIENT_CONNECTION)
if clientCtxConn == nil {
return core.CLIENT_CONNECTION_NOT_EXIST
}
check.clientConn = clientCtxConn.(net.Conn)
check.clientConn = check.GetClientConn(ctx)
check.configUsername = config["socks5_access_username"]
check.configPassword = config["socks5_access_password"]
return nil
}
func (check *CheckAccess) End(ctx context.Context, config map[string]string) error {
return nil
}
func (check *CheckAccess) checkAuth(configUserName, configPassword string) error {
if check.clientUsername == configUserName && check.clientPassword == configPassword {
_, err := check.clientConn.Write([]byte{userAuthVersion, authSuccess})

View File

@ -6,30 +6,14 @@ import (
"fmt"
"github.com/cnlh/nps/core"
"io"
"net"
)
type Handshake struct {
}
func (handshake *Handshake) GetConfigName()*core.NpsConfigs{
return nil
}
func (handshake *Handshake) GetStage() core.Stage {
return core.STAGE_RUN
}
func (handshake *Handshake) Start(ctx context.Context, config map[string]string) error {
return nil
core.NpsPlugin
}
func (handshake *Handshake) Run(ctx context.Context, config map[string]string) error {
clientCtxConn := ctx.Value(core.CLIENT_CONNECTION)
if clientCtxConn == nil {
return core.CLIENT_CONNECTION_NOT_EXIST
}
clientConn := clientCtxConn.(net.Conn)
clientConn := handshake.GetClientConn(ctx)
buf := make([]byte, 2)
if _, err := io.ReadFull(clientConn, buf); err != nil {
return errors.New("negotiation err while read 2 bytes from client connection: " + err.Error())
@ -50,7 +34,3 @@ func (handshake *Handshake) Run(ctx context.Context, config map[string]string) e
return nil
}
func (handshake *Handshake) End(ctx context.Context, config map[string]string) error {
return nil
}

View File

@ -17,6 +17,7 @@ const (
)
type Access struct {
core.NpsPlugin
clientConn net.Conn
}
@ -24,23 +25,8 @@ func (access *Access) GetConfigName() *core.NpsConfigs {
return core.NewNpsConfigs("socks5_check_access_check", "need check the permission simply")
}
func (access *Access) GetStage() core.Stage {
return core.STAGE_RUN
}
func (access *Access) Start(ctx context.Context, config map[string]string) error {
return nil
}
func (access *Access) End(ctx context.Context, config map[string]string) error {
return nil
}
func (access *Access) Run(ctx context.Context, config map[string]string) error {
clientCtxConn := ctx.Value(core.CLIENT_CONNECTION)
if clientCtxConn == nil {
return core.CLIENT_CONNECTION_NOT_EXIST
}
access.clientConn = clientCtxConn.(net.Conn)
access.clientConn = access.GetClientConn(ctx)
if config["socks5_check_access"] != "true" {
return access.sendAccessMsgToClient(UserNoAuth)
}

View File

@ -11,6 +11,7 @@ import (
)
type Request struct {
core.NpsPlugin
clientConn net.Conn
ctx context.Context
}
@ -31,27 +32,8 @@ const (
addrTypeNotSupported = 8
)
func (request *Request) GetConfigName() []*core.Config {
return nil
}
func (request *Request) GetStage() core.Stage {
return core.STAGE_RUN
}
func (request *Request) Start(ctx context.Context, config map[string]string) error {
return nil
}
func (request *Request) End(ctx context.Context, config map[string]string) error {
return nil
}
func (request *Request) Run(ctx context.Context, config map[string]string) error {
clientCtxConn := ctx.Value(core.CLIENT_CONNECTION)
if clientCtxConn == nil {
return core.CLIENT_CONNECTION_NOT_EXIST
}
request.clientConn = clientCtxConn.(net.Conn)
request.clientConn = request.GetClientConn(ctx)
request.ctx = ctx
/*