Skip to content

Commit

Permalink
feat: knot configuration compose
Browse files Browse the repository at this point in the history
  • Loading branch information
shoriwe committed Jun 2, 2023
1 parent 44da474 commit 8a4a21d
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 0 deletions.
61 changes: 61 additions & 0 deletions compose/knot.go
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)
}
}
107 changes: 107 additions & 0 deletions compose/knot_test.go
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)
})
}

0 comments on commit 8a4a21d

Please sign in to comment.