-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package compose | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/shoriwe/fullproxy/v3/circuit" | ||
"golang.org/x/net/proxy" | ||
) | ||
|
||
const ( | ||
KnotForward = "forward" | ||
KnotSocks5 = "socks5" | ||
KnotSSH = "ssh" | ||
) | ||
|
||
var AvailableKnots = [3]string{KnotForward, KnotSocks5, KnotSSH} | ||
|
||
type Knot struct { | ||
Type string `yaml:"type,omitempty" json:"type,omitempty"` | ||
Network string `yaml:"network,omitempty" json:"network,omitempty"` | ||
Address string `yaml:"address,omitempty" json:"address,omitempty"` | ||
Auth *Auth `yaml:"auth,omitempty" json:"auth,omitempty"` | ||
} | ||
|
||
func (k *Knot) Compile() (circuit.Knot, error) { | ||
switch k.Type { | ||
case KnotForward: | ||
return &circuit.Forward{ | ||
Network: k.Network, | ||
Address: k.Address, | ||
}, nil | ||
case KnotSocks5: | ||
var ( | ||
auth *proxy.Auth | ||
err error | ||
) | ||
if k.Auth != nil { | ||
auth, err = k.Auth.Socks5() | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return &circuit.Socks5{ | ||
Network: k.Network, | ||
Address: k.Address, | ||
Auth: auth, | ||
}, nil | ||
case KnotSSH: | ||
if k.Auth == nil { | ||
return nil, fmt.Errorf("no auth provided for SSH") | ||
} | ||
config, err := k.Auth.SSHClientConfig() | ||
return &circuit.SSH{ | ||
Network: k.Network, | ||
Address: k.Address, | ||
Config: *config, | ||
}, err | ||
default: | ||
return nil, fmt.Errorf("unknown knot %s; available knots are %s", k.Type, AvailableKnots) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package compose | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestKnot_Compile_INVALID(t *testing.T) { | ||
knot := Knot{ | ||
Type: "INVALID", | ||
Network: "tcp", | ||
Address: "127.0.0.1:0", | ||
} | ||
_, err := knot.Compile() | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestKnot_Compile_Forward(t *testing.T) { | ||
knot := Knot{ | ||
Type: KnotForward, | ||
Network: "tcp", | ||
Address: "127.0.0.1:0", | ||
} | ||
_, err := knot.Compile() | ||
assert.Nil(t, err) | ||
} | ||
|
||
func TestKnot_Compile_Socks5(t *testing.T) { | ||
t.Run("No Auth", func(tt *testing.T) { | ||
knot := Knot{ | ||
Type: KnotSocks5, | ||
Network: "tcp", | ||
Address: "127.0.0.1:0", | ||
} | ||
_, err := knot.Compile() | ||
assert.Nil(t, err) | ||
}) | ||
t.Run("Auth", func(tt *testing.T) { | ||
knot := Knot{ | ||
Type: KnotSocks5, | ||
Network: "tcp", | ||
Address: "127.0.0.1:0", | ||
Auth: &Auth{ | ||
Username: new(string), | ||
Password: new(string), | ||
}, | ||
} | ||
*knot.Auth.Username = "sulcud" | ||
*knot.Auth.Password = "password" | ||
_, err := knot.Compile() | ||
assert.Nil(t, err) | ||
}) | ||
t.Run("Invalid Auth", func(tt *testing.T) { | ||
knot := Knot{ | ||
Type: KnotSocks5, | ||
Network: "tcp", | ||
Address: "127.0.0.1:0", | ||
Auth: &Auth{ | ||
Username: new(string), | ||
}, | ||
} | ||
*knot.Auth.Username = "sulcud" | ||
_, err := knot.Compile() | ||
assert.NotNil(t, err) | ||
}) | ||
} | ||
|
||
func TestKnot_Compile_SSH(t *testing.T) { | ||
t.Run("No Auth", func(tt *testing.T) { | ||
knot := Knot{ | ||
Type: KnotSSH, | ||
Network: "tcp", | ||
Address: "127.0.0.1:0", | ||
} | ||
_, err := knot.Compile() | ||
assert.NotNil(t, err) | ||
}) | ||
t.Run("Auth", func(tt *testing.T) { | ||
knot := Knot{ | ||
Type: KnotSSH, | ||
Network: "tcp", | ||
Address: "127.0.0.1:0", | ||
Auth: &Auth{ | ||
Username: new(string), | ||
Password: new(string), | ||
}, | ||
} | ||
*knot.Auth.Username = "sulcud" | ||
*knot.Auth.Password = "password" | ||
_, err := knot.Compile() | ||
assert.Nil(t, err) | ||
}) | ||
t.Run("Invalid Auth", func(tt *testing.T) { | ||
knot := Knot{ | ||
Type: KnotSSH, | ||
Network: "tcp", | ||
Address: "127.0.0.1:0", | ||
Auth: &Auth{ | ||
Username: new(string), | ||
}, | ||
} | ||
*knot.Auth.Username = "sulcud" | ||
_, err := knot.Compile() | ||
assert.NotNil(t, err) | ||
}) | ||
} |