diff --git a/compose/auth_method.go b/compose/auth_method.go new file mode 100644 index 0000000..e028521 --- /dev/null +++ b/compose/auth_method.go @@ -0,0 +1,35 @@ +package compose + +import "github.com/things-go/go-socks5" + +type AuthMethod struct { + Raw map[string]string +} + +func (am *AuthMethod) Socks5() (auth socks5.Authenticator, err error) { + switch { + case am.Raw != nil: + auth = socks5.UserPassAuthenticator{ + Credentials: socks5.StaticCredentials(am.Raw), + } + default: + auth = nil + } + return auth, err +} + +type AuthMethods []AuthMethod + +func (am AuthMethods) Socks5() ([]socks5.Authenticator, error) { + authMethods := make([]socks5.Authenticator, 0, len(am)) + for _, a := range am { + auth, err := a.Socks5() + if err != nil { + return nil, err + } + if auth != nil { + authMethods = append(authMethods, auth) + } + } + return authMethods, nil +} diff --git a/compose/auth_method_test.go b/compose/auth_method_test.go new file mode 100644 index 0000000..f12ea8b --- /dev/null +++ b/compose/auth_method_test.go @@ -0,0 +1,46 @@ +package compose + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestAuthMethod_Socks5(t *testing.T) { + t.Run("Raw", func(tt *testing.T) { + a := AuthMethod{ + Raw: map[string]string{ + "sulcud": "password", + }, + } + auth, err := a.Socks5() + assert.Nil(tt, err) + assert.NotNil(tt, auth) + }) + t.Run("Nil", func(tt *testing.T) { + a := AuthMethod{} + auth, err := a.Socks5() + assert.Nil(tt, err) + assert.Nil(tt, auth) + }) +} + +func TestAuthMethods_Socks5(t *testing.T) { + t.Run("Raw", func(tt *testing.T) { + a := AuthMethod{ + Raw: map[string]string{ + "sulcud": "password", + }, + } + as := AuthMethods{a} + authMethods, err := as.Socks5() + assert.Nil(tt, err) + assert.NotNil(tt, authMethods) + }) + t.Run("Nil", func(tt *testing.T) { + as := AuthMethods{} + authMethods, err := as.Socks5() + assert.Nil(tt, err) + assert.Nil(tt, authMethods) + }) +} diff --git a/compose/proxy.go b/compose/proxy.go index 67ec36a..a050b53 100644 --- a/compose/proxy.go +++ b/compose/proxy.go @@ -6,6 +6,7 @@ import ( "github.com/shoriwe/fullproxy/v4/proxies" "github.com/shoriwe/fullproxy/v4/utils/network" + "github.com/things-go/go-socks5" ) const ( @@ -15,12 +16,13 @@ const ( ) type Proxy struct { - Type string `yaml:"type" json:"type"` - Listener Network `yaml:"listener" json:"listener"` - Dialer *Network `yaml:"dialer,omitempty" json:"dialer,omitempty"` - Network *string `yaml:"network,omitempty" json:"network,omitempty"` - Address *string `yaml:"address,omitempty" json:"address,omitempty"` - proxy proxies.Proxy + Type string `yaml:"type" json:"type"` + Listener Network `yaml:"listener" json:"listener"` + Dialer *Network `yaml:"dialer,omitempty" json:"dialer,omitempty"` + Network *string `yaml:"network,omitempty" json:"network,omitempty"` + Address *string `yaml:"address,omitempty" json:"address,omitempty"` + AuthMethods AuthMethods `yaml:"authMethods,omitempty" json:"authMethods,omitempty"` + proxy proxies.Proxy } func (p *Proxy) getDialFunc() (network.DialFunc, error) { @@ -82,11 +84,15 @@ func (p *Proxy) setupSocks5() (proxy proxies.Proxy, err error) { if err == nil { dialFunc, err = p.getDialFunc() if err == nil { - network.CloseOnError(&err, l) - proxy = &proxies.Socks5{ - Listener: l, - Dial: dialFunc, - // TODO: AuthMethods: authMethods, + var authMethods []socks5.Authenticator + authMethods, err = p.AuthMethods.Socks5() + if err == nil { + network.CloseOnError(&err, l) + proxy = &proxies.Socks5{ + Listener: l, + Dial: dialFunc, + AuthMethods: authMethods, + } } } } diff --git a/docs/Compose.md b/docs/Compose.md index 9c2075f..e801669 100644 --- a/docs/Compose.md +++ b/docs/Compose.md @@ -324,6 +324,7 @@ portRange: ### Dependencies - [Network](#Network) +- [Auth method](#Auth method) ### Definition @@ -335,6 +336,8 @@ dialer: <>|EMPTY network: tcp|EMPTY address: HOST:PORT|EMPTY +authMethods: + - <> ``` ### Examples @@ -367,6 +370,23 @@ dialer: privateKey: /home/admin/id_rsa ``` +### Auth method + +### Definition + +```yaml +raw: + USERNAME: PASSWORD +``` + +### Examples + +```yaml +raw: + sulcud: password + shoriwe: password +``` + ## Slaves ### Dependencies