From 8f45d86cee4e9be3f1ddcad48b8565282ff2dfcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B2=B3?= Date: Mon, 14 Oct 2019 23:04:14 +0800 Subject: [PATCH] core plugin --- core/struct.go | 30 ++++++++++++++++++++- server/common/common_inet_proxy_handle.go | 18 ++----------- server/socks5/socks5_check_access_handle.go | 19 ++----------- server/socks5/socks5_handshake_handle.go | 24 ++--------------- server/socks5/socks5_read_access_handle.go | 18 ++----------- server/socks5/socks5_read_request_handle.go | 22 ++------------- 6 files changed, 39 insertions(+), 92 deletions(-) diff --git a/core/struct.go b/core/struct.go index f1a2a42..61b85b4 100644 --- a/core/struct.go +++ b/core/struct.go @@ -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) +} diff --git a/server/common/common_inet_proxy_handle.go b/server/common/common_inet_proxy_handle.go index 0632102..14fe1cd 100644 --- a/server/common/common_inet_proxy_handle.go +++ b/server/common/common_inet_proxy_handle.go @@ -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 -} diff --git a/server/socks5/socks5_check_access_handle.go b/server/socks5/socks5_check_access_handle.go index 19ffdcc..cd7ba01 100644 --- a/server/socks5/socks5_check_access_handle.go +++ b/server/socks5/socks5_check_access_handle.go @@ -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}) diff --git a/server/socks5/socks5_handshake_handle.go b/server/socks5/socks5_handshake_handle.go index 2ae9ab5..b00abe0 100644 --- a/server/socks5/socks5_handshake_handle.go +++ b/server/socks5/socks5_handshake_handle.go @@ -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 -} diff --git a/server/socks5/socks5_read_access_handle.go b/server/socks5/socks5_read_access_handle.go index a18b772..19f9c66 100644 --- a/server/socks5/socks5_read_access_handle.go +++ b/server/socks5/socks5_read_access_handle.go @@ -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) } diff --git a/server/socks5/socks5_read_request_handle.go b/server/socks5/socks5_read_request_handle.go index f2ec5f8..250210d 100644 --- a/server/socks5/socks5_read_request_handle.go +++ b/server/socks5/socks5_read_request_handle.go @@ -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 /*