module advance

This commit is contained in:
unknown
2019-10-14 18:12:37 +08:00
parent eed3efd18f
commit 3904f0c797
10 changed files with 385 additions and 60 deletions

View File

@@ -0,0 +1,60 @@
package socks5
import (
"context"
"errors"
"github.com/cnlh/nps/core"
"net"
)
type CheckAccess struct {
clientConn net.Conn
clientUsername string
clientPassword string
configUsername string
configPassword string
}
func (check *CheckAccess) GetConfigName() *core.NpsConfigs {
c := core.NewNpsConfigs("socks5_simple_access_check", "need check the permission simply")
c.Add("socks5_simple_access_username", "simple auth username")
c.Add("socks5_simple_access_password", "simple auth password")
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.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})
return err
} else {
_, err := check.clientConn.Write([]byte{userAuthVersion, authFailure})
if err != nil {
return err
}
return errors.New("auth check error,username or password does not match")
}
}

View File

@@ -12,23 +12,21 @@ import (
type Handshake struct {
}
func (handshake *Handshake) GetConfigName() []*core.Config {
func (handshake *Handshake) GetConfigName()*core.NpsConfigs{
return nil
}
func (handshake *Handshake) GetStage() core.Stage {
return core.STAGE_RUN
}
func (handshake *Handshake) GetBeforePlugin() core.Plugin {
return nil
}
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 {
clientCtxConn := ctx.Value("clientConn")
clientCtxConn := ctx.Value(core.CLIENT_CONNECTION)
if clientCtxConn == nil {
return errors.New("the client connection is not exist")
return core.CLIENT_CONNECTION_NOT_EXIST
}
clientConn := clientCtxConn.(net.Conn)

View File

@@ -18,26 +18,16 @@ const (
type Access struct {
clientConn net.Conn
username string
password string
}
func (access *Access) GetConfigName() []*core.Config {
c := make([]*core.Config, 0)
c = append(c, &core.Config{ConfigName: "socks5_check_access", Description: "need check the permission?"})
c = append(c, &core.Config{ConfigName: "socks5_access_username", Description: "auth username"})
c = append(c, &core.Config{ConfigName: "socks5_access_password", Description: "auth password"})
return nil
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) GetBeforePlugin() core.Plugin {
return &Handshake{}
}
func (access *Access) Start(ctx context.Context, config map[string]string) error {
return nil
}
@@ -46,32 +36,27 @@ func (access *Access) End(ctx context.Context, config map[string]string) error {
}
func (access *Access) Run(ctx context.Context, config map[string]string) error {
clientCtxConn := ctx.Value("clientConn")
clientCtxConn := ctx.Value(core.CLIENT_CONNECTION)
if clientCtxConn == nil {
return errors.New("the client access.clientConnection is not exist")
return core.CLIENT_CONNECTION_NOT_EXIST
}
access.clientConn = clientCtxConn.(net.Conn)
if config["socks5_check_access"] != "true" {
return access.sendAccessMsgToClient(UserNoAuth)
}
configUsername := config["socks5_access_username"]
configPassword := config["socks5_access_password"]
if configUsername == "" || configPassword == "" {
return access.sendAccessMsgToClient(UserNoAuth)
}
// need auth
if err := access.sendAccessMsgToClient(UserPassAuth); err != nil {
return err
}
// send auth reply to client ,and get the auth information
var err error
access.username, access.password, err = access.getAuthInfoFromClient()
username, password, err := access.getAuthInfoFromClient()
if err != nil {
return err
}
context.WithValue(ctx, access.username, access.password)
context.WithValue(ctx, "socks_client_username", username)
context.WithValue(ctx, "socks_client_password", password)
// check
return access.checkAuth(configUsername, configPassword)
return nil
}
func (access *Access) sendAccessMsgToClient(auth uint8) error {
@@ -113,16 +98,3 @@ func (access *Access) getAuthInfoFromClient() (username string, password string,
password = string(pass)
return
}
func (access *Access) checkAuth(configUserName, configPassword string) error {
if access.username == configUserName && access.password == configPassword {
_, err := access.clientConn.Write([]byte{userAuthVersion, authSuccess})
return err
} else {
_, err := access.clientConn.Write([]byte{userAuthVersion, authFailure})
if err != nil {
return err
}
return errors.New("auth check error,username or password does not match")
}
}

View File

@@ -32,10 +32,6 @@ const (
)
func (request *Request) GetConfigName() []*core.Config {
c := make([]*core.Config, 0)
c = append(c, &core.Config{ConfigName: "socks5_check_request", Description: "need check the permission?"})
c = append(c, &core.Config{ConfigName: "socks5_request_username", Description: "auth username"})
c = append(c, &core.Config{ConfigName: "socks5_request_password", Description: "auth password"})
return nil
}
@@ -51,9 +47,9 @@ func (request *Request) End(ctx context.Context, config map[string]string) error
}
func (request *Request) Run(ctx context.Context, config map[string]string) error {
clientCtxConn := ctx.Value("clientConn")
clientCtxConn := ctx.Value(core.CLIENT_CONNECTION)
if clientCtxConn == nil {
return errors.New("the client request.clientConnection is not exist")
return core.CLIENT_CONNECTION_NOT_EXIST
}
request.clientConn = clientCtxConn.(net.Conn)
request.ctx = ctx
@@ -76,12 +72,12 @@ func (request *Request) Run(ctx context.Context, config map[string]string) error
switch header[1] {
case connectMethod:
context.WithValue(request.ctx, "socks5_target_type", "tcp")
context.WithValue(request.ctx, core.PROXY_CONNECTION_TYPE, "tcp")
return request.doConnect()
case bindMethod:
return request.handleBind()
case associateMethod:
context.WithValue(request.ctx, "socks5_target_type", "udp")
context.WithValue(request.ctx, core.PROXY_CONNECTION_TYPE, "udp")
return request.handleUDP()
default:
request.sendReply(commandNotSupported)
@@ -97,7 +93,6 @@ func (request *Request) sendReply(rep uint8) error {
0,
1,
}
localAddr := request.clientConn.LocalAddr().String()
localHost, localPort, _ := net.SplitHostPort(localAddr)
ipBytes := net.ParseIP(localHost).To4()
@@ -139,8 +134,8 @@ func (request *Request) doConnect() error {
var port uint16
binary.Read(request.clientConn, binary.BigEndian, &port)
context.WithValue(request.ctx, "socks5_target_host", host)
context.WithValue(request.ctx, "socks5_target_port", port)
context.WithValue(request.ctx, core.PROXY_CONNECTION_ADDR, host)
context.WithValue(request.ctx, core.PROXY_CONNECTION_PORT, port)
return nil
}