From 35b8a7ec2137ac9588402d408a7bfa3f50102410 Mon Sep 17 00:00:00 2001 From: Ketan Singh Date: Sun, 27 Mar 2022 14:01:09 +0530 Subject: [PATCH] Add socks5 authentication --- main.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 66351e4..e82309c 100644 --- a/main.go +++ b/main.go @@ -2,17 +2,17 @@ package main import ( "bufio" + "context" "encoding/base64" "encoding/hex" "errors" "fmt" "log" + "math/rand" "net" "os" - "strings" "strconv" - "context" - "math/rand" + "strings" "github.com/armon/go-socks5" @@ -39,6 +39,11 @@ type NetstackDNSResolver struct { type Configuration []ConfigSection +type CredentialValidator struct { + username string + password string +} + func (d NetstackDNSResolver) Resolve(ctx context.Context, name string) (context.Context, net.IP, error) { addrs, err := d.tnet.LookupContextHost(ctx, name) if err != nil { @@ -232,7 +237,16 @@ func socks5Routine(config map[string]string) (func(*netstack.Net), error) { } routine := func(tnet *netstack.Net) { - conf := &socks5.Config{ Dial: tnet.DialContext, Resolver: NetstackDNSResolver{ tnet: tnet } } + conf := &socks5.Config{Dial: tnet.DialContext, Resolver: NetstackDNSResolver{tnet: tnet}} + if username, ok := config["username"]; ok { + validator := CredentialValidator{username: username} + password, ok := config["password"] + if ok { + validator.password = password + } + + conf.Credentials = validator + } server, err := socks5.New(conf) if err != nil { log.Panic(err) @@ -246,6 +260,11 @@ func socks5Routine(config map[string]string) (func(*netstack.Net), error) { return routine, nil } +func (c CredentialValidator) Valid(username, password string) bool { + return c.username == username && c.password == password +} + + func connForward(bufSize int, from, to net.Conn) { buf := make([]byte, bufSize) for {