Add socks5 authentication

This commit is contained in:
Ketan Singh 2022-03-27 14:01:09 +05:30 committed by Ketan Singh
parent 1d5b792a86
commit 35b8a7ec21

27
main.go
View file

@ -2,17 +2,17 @@ package main
import ( import (
"bufio" "bufio"
"context"
"encoding/base64" "encoding/base64"
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt" "fmt"
"log" "log"
"math/rand"
"net" "net"
"os" "os"
"strings"
"strconv" "strconv"
"context" "strings"
"math/rand"
"github.com/armon/go-socks5" "github.com/armon/go-socks5"
@ -39,6 +39,11 @@ type NetstackDNSResolver struct {
type Configuration []ConfigSection type Configuration []ConfigSection
type CredentialValidator struct {
username string
password string
}
func (d NetstackDNSResolver) Resolve(ctx context.Context, name string) (context.Context, net.IP, error) { func (d NetstackDNSResolver) Resolve(ctx context.Context, name string) (context.Context, net.IP, error) {
addrs, err := d.tnet.LookupContextHost(ctx, name) addrs, err := d.tnet.LookupContextHost(ctx, name)
if err != nil { if err != nil {
@ -232,7 +237,16 @@ func socks5Routine(config map[string]string) (func(*netstack.Net), error) {
} }
routine := func(tnet *netstack.Net) { 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) server, err := socks5.New(conf)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
@ -246,6 +260,11 @@ func socks5Routine(config map[string]string) (func(*netstack.Net), error) {
return routine, nil 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) { func connForward(bufSize int, from, to net.Conn) {
buf := make([]byte, bufSize) buf := make([]byte, bufSize)
for { for {