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 ( import (
"context" "context"
"errors" "errors"
"net"
) )
type Stage uint8 type Stage uint8
@ -28,7 +29,7 @@ var (
CLIENT_CONNECTION_NOT_EXIST = errors.New("the client connection is not exist") 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 client connection is not exist")
REQUEST_EOF = errors.New("the request has finished") 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. // Plugin interface, all plugins must implement those functions.
@ -39,3 +40,30 @@ type Plugin interface {
Run(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 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 { type Proxy struct {
core.NpsPlugin
clientConn net.Conn clientConn net.Conn
ctx context.Context ctx context.Context
} }
@ -15,20 +16,9 @@ type Proxy struct {
func (proxy *Proxy) GetConfigName() *core.NpsConfigs { func (proxy *Proxy) GetConfigName() *core.NpsConfigs {
return core.NewNpsConfigs("socks5_proxy", "proxy to inet") 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 { func (proxy *Proxy) Run(ctx context.Context, config map[string]string) error {
clientCtxConn := ctx.Value(core.CLIENT_CONNECTION) proxy.clientConn = proxy.GetClientConn(ctx)
if clientCtxConn == nil {
return core.CLIENT_CONNECTION_NOT_EXIST
}
proxy.clientConn = clientCtxConn.(net.Conn)
proxy.ctx = ctx proxy.ctx = ctx
bg := ctx.Value(core.BRIDGE) bg := ctx.Value(core.BRIDGE)
if bg == nil { 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) core.CopyBuffer(clientCtxConn.(net.Conn), severConn)
return nil 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 { type CheckAccess struct {
core.NpsPlugin
clientConn net.Conn clientConn net.Conn
clientUsername string clientUsername string
clientPassword string clientPassword string
@ -22,30 +23,14 @@ func (check *CheckAccess) GetConfigName() *core.NpsConfigs {
return c 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 { func (check *CheckAccess) Run(ctx context.Context, config map[string]string) error {
clientCtxConn := ctx.Value(core.CLIENT_CONNECTION) check.clientConn = check.GetClientConn(ctx)
if clientCtxConn == nil {
return core.CLIENT_CONNECTION_NOT_EXIST
}
check.clientConn = clientCtxConn.(net.Conn)
check.configUsername = config["socks5_access_username"] check.configUsername = config["socks5_access_username"]
check.configPassword = config["socks5_access_password"] check.configPassword = config["socks5_access_password"]
return nil return nil
} }
func (check *CheckAccess) End(ctx context.Context, config map[string]string) error {
return nil
}
func (check *CheckAccess) checkAuth(configUserName, configPassword string) error { func (check *CheckAccess) checkAuth(configUserName, configPassword string) error {
if check.clientUsername == configUserName && check.clientPassword == configPassword { if check.clientUsername == configUserName && check.clientPassword == configPassword {
_, err := check.clientConn.Write([]byte{userAuthVersion, authSuccess}) _, err := check.clientConn.Write([]byte{userAuthVersion, authSuccess})

View File

@ -6,30 +6,14 @@ import (
"fmt" "fmt"
"github.com/cnlh/nps/core" "github.com/cnlh/nps/core"
"io" "io"
"net"
) )
type Handshake struct { type Handshake struct {
} core.NpsPlugin
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
} }
func (handshake *Handshake) Run(ctx context.Context, config map[string]string) error { func (handshake *Handshake) Run(ctx context.Context, config map[string]string) error {
clientCtxConn := ctx.Value(core.CLIENT_CONNECTION) clientConn := handshake.GetClientConn(ctx)
if clientCtxConn == nil {
return core.CLIENT_CONNECTION_NOT_EXIST
}
clientConn := clientCtxConn.(net.Conn)
buf := make([]byte, 2) buf := make([]byte, 2)
if _, err := io.ReadFull(clientConn, buf); err != nil { if _, err := io.ReadFull(clientConn, buf); err != nil {
return errors.New("negotiation err while read 2 bytes from client connection: " + err.Error()) 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 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 { type Access struct {
core.NpsPlugin
clientConn net.Conn 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") 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 { func (access *Access) Run(ctx context.Context, config map[string]string) error {
clientCtxConn := ctx.Value(core.CLIENT_CONNECTION) access.clientConn = access.GetClientConn(ctx)
if clientCtxConn == nil {
return core.CLIENT_CONNECTION_NOT_EXIST
}
access.clientConn = clientCtxConn.(net.Conn)
if config["socks5_check_access"] != "true" { if config["socks5_check_access"] != "true" {
return access.sendAccessMsgToClient(UserNoAuth) return access.sendAccessMsgToClient(UserNoAuth)
} }

View File

@ -11,6 +11,7 @@ import (
) )
type Request struct { type Request struct {
core.NpsPlugin
clientConn net.Conn clientConn net.Conn
ctx context.Context ctx context.Context
} }
@ -31,27 +32,8 @@ const (
addrTypeNotSupported = 8 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 { func (request *Request) Run(ctx context.Context, config map[string]string) error {
clientCtxConn := ctx.Value(core.CLIENT_CONNECTION) request.clientConn = request.GetClientConn(ctx)
if clientCtxConn == nil {
return core.CLIENT_CONNECTION_NOT_EXIST
}
request.clientConn = clientCtxConn.(net.Conn)
request.ctx = ctx request.ctx = ctx
/* /*