Skip to content

Commit

Permalink
feat: auth method added to proxy compose config
Browse files Browse the repository at this point in the history
  • Loading branch information
shoriwe committed Jun 12, 2023
1 parent d122560 commit 1345875
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 11 deletions.
35 changes: 35 additions & 0 deletions compose/auth_method.go
Original file line number Diff line number Diff line change
@@ -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
}
46 changes: 46 additions & 0 deletions compose/auth_method_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
28 changes: 17 additions & 11 deletions compose/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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) {
Expand Down Expand Up @@ -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,
}
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions docs/Compose.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ portRange:
### Dependencies
- [Network](#Network)
- [Auth method](#Auth method)
### Definition
Expand All @@ -335,6 +336,8 @@ dialer:
<<NETWORK>>|EMPTY
network: tcp|EMPTY
address: HOST:PORT|EMPTY
authMethods:
- <<AUTH_METHOD>>
```
### Examples
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1345875

Please sign in to comment.