Skip to content

Commit 8a68539

Browse files
authored
refactoring: migrate to cobra cli (#8)
1 parent 181f8c1 commit 8a68539

File tree

13 files changed

+215
-93
lines changed

13 files changed

+215
-93
lines changed

.drone.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ clone:
1818

1919
steps:
2020
- name: lint
21-
image: golangci/golangci-lint:v1.43-alpine
21+
image: golangci/golangci-lint:v1.49-alpine
2222
volumes:
2323
- name: deps
2424
path: /go
2525
commands:
2626
- golangci-lint run -v
27-
- go install github.com/mgechev/revive@v1.1.2
27+
- go install github.com/mgechev/revive@v1.2.3
2828
- revive -config .revive.toml -formatter friendly ./...
2929

3030
- name: test & build
31-
image: golang:1.17-alpine
31+
image: golang:1.19-alpine
3232
environment:
3333
CGO_ENABLED: "0"
3434
volumes:

.revive.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ warningCode = 1
1616
[rule.increment-decrement]
1717
[rule.var-naming]
1818
[rule.var-declaration]
19-
[rule.package-comments]
2019
[rule.range]
2120
[rule.receiver-naming]
2221
[rule.time-naming]

README.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,13 @@ Create a plain text file with one socks5 proxy per line. For demonstration purpo
1818
Start **wirez** on the localhost on port 1080:
1919

2020
```
21-
wirez -f proxies.txt -l 127.0.0.1:1080
21+
wirez server -f proxies.txt -l 127.0.0.1:1080
2222
```
2323

2424
Now every socks5 request on 1080 port will be load banacled between socks5 proxies in the `proxies.txt` file. Enjoy!
2525

2626
## Usage
2727

2828
```
29-
$ ./wirez -h
30-
Usage of ./wirez:
31-
-f string
32-
SOCKS5 proxies file (default "proxies.txt")
33-
-l string
34-
SOCKS5 server address (default ":1080")
29+
wirez help
3530
```

parse.go renamed to command/config.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package command
22

33
import (
44
"bufio"
@@ -7,9 +7,11 @@ import (
77
"net"
88
"net/url"
99
"strings"
10+
11+
"github.com/v-byte-cpu/wirez/pkg/connect"
1012
)
1113

12-
func parseProxyFile(proxyFile io.Reader) (socksAddrs []*SocksAddr, err error) {
14+
func parseProxyFile(proxyFile io.Reader) (socksAddrs []*connect.SocksAddr, err error) {
1315
bs := bufio.NewScanner(proxyFile)
1416
for bs.Scan() {
1517
rawSocksAddr := strings.Trim(bs.Text(), " ")
@@ -29,7 +31,7 @@ func parseProxyFile(proxyFile io.Reader) (socksAddrs []*SocksAddr, err error) {
2931
if _, _, err := net.SplitHostPort(socksURL.Host); err != nil {
3032
return nil, err
3133
}
32-
socksAddrs = append(socksAddrs, &SocksAddr{Address: socksURL.Host, Auth: socksURL.User})
34+
socksAddrs = append(socksAddrs, &connect.SocksAddr{Address: socksURL.Host, Auth: socksURL.User})
3335
}
3436
err = bs.Err()
3537
return

parse_test.go renamed to command/config_test.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
package main
1+
package command
22

33
import (
44
"net/url"
55
"strings"
66
"testing"
77

88
"github.com/stretchr/testify/require"
9+
"github.com/v-byte-cpu/wirez/pkg/connect"
910
)
1011

1112
func TestParseProxyFile(t *testing.T) {
1213
tests := []struct {
1314
name string
1415
input string
15-
expected []*SocksAddr
16+
expected []*connect.SocksAddr
1617
expectedErr bool
1718
}{
1819
{
@@ -46,52 +47,52 @@ func TestParseProxyFile(t *testing.T) {
4647
{
4748
name: "OneIPPort",
4849
input: "10.10.10.10:1111",
49-
expected: []*SocksAddr{{Address: "10.10.10.10:1111"}},
50+
expected: []*connect.SocksAddr{{Address: "10.10.10.10:1111"}},
5051
},
5152
{
5253
name: "OneIPPortWithSpaces",
5354
input: " 10.10.10.10:1111 ",
54-
expected: []*SocksAddr{{Address: "10.10.10.10:1111"}},
55+
expected: []*connect.SocksAddr{{Address: "10.10.10.10:1111"}},
5556
},
5657
{
5758
name: "OneHostPort",
5859
input: "example.com:1111",
59-
expected: []*SocksAddr{{Address: "example.com:1111"}},
60+
expected: []*connect.SocksAddr{{Address: "example.com:1111"}},
6061
},
6162
{
6263
name: "TwoIPPortLines",
6364
input: "10.10.10.10:1111\n20.20.20.20:2222",
64-
expected: []*SocksAddr{{Address: "10.10.10.10:1111"}, {Address: "20.20.20.20:2222"}},
65+
expected: []*connect.SocksAddr{{Address: "10.10.10.10:1111"}, {Address: "20.20.20.20:2222"}},
6566
},
6667
{
6768
name: "TwoHostPortLines",
6869
input: "example.com:1111\nexample.org:2222",
69-
expected: []*SocksAddr{{Address: "example.com:1111"}, {Address: "example.org:2222"}},
70+
expected: []*connect.SocksAddr{{Address: "example.com:1111"}, {Address: "example.org:2222"}},
7071
},
7172
{
7273
name: "OneIPPortWithUsername",
7374
input: "[email protected]:1111",
74-
expected: []*SocksAddr{{Address: "10.10.10.10:1111", Auth: url.User("abc")}},
75+
expected: []*connect.SocksAddr{{Address: "10.10.10.10:1111", Auth: url.User("abc")}},
7576
},
7677
{
7778
name: "OneIPPortWithUsernamePassword",
7879
input: "abc:[email protected]:1111",
79-
expected: []*SocksAddr{{Address: "10.10.10.10:1111", Auth: url.UserPassword("abc", "def")}},
80+
expected: []*connect.SocksAddr{{Address: "10.10.10.10:1111", Auth: url.UserPassword("abc", "def")}},
8081
},
8182
{
8283
name: "OneHostPortWithUsernamePassword",
8384
input: "abc:[email protected]:1111",
84-
expected: []*SocksAddr{{Address: "example.com:1111", Auth: url.UserPassword("abc", "def")}},
85+
expected: []*connect.SocksAddr{{Address: "example.com:1111", Auth: url.UserPassword("abc", "def")}},
8586
},
8687
{
8788
name: "WithSocks5Scheme",
8889
input: "socks5://10.10.10.10:1111",
89-
expected: []*SocksAddr{{Address: "10.10.10.10:1111"}},
90+
expected: []*connect.SocksAddr{{Address: "10.10.10.10:1111"}},
9091
},
9192
{
9293
name: "TwoWithSocks5Schemes",
9394
input: "socks5://10.10.10.10:1111\nsocks5://20.20.20.20:2221",
94-
expected: []*SocksAddr{{Address: "10.10.10.10:1111"}, {Address: "20.20.20.20:2221"}},
95+
expected: []*connect.SocksAddr{{Address: "10.10.10.10:1111"}, {Address: "20.20.20.20:2221"}},
9596
},
9697
{
9798
name: "WithInvalidScheme",

command/root.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package command
2+
3+
import (
4+
"os"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
func Main(version string) {
10+
if err := newRootCmd(version).Execute(); err != nil {
11+
os.Exit(1)
12+
}
13+
}
14+
15+
func newRootCmd(version string) *cobra.Command {
16+
cmd := &cobra.Command{
17+
Use: "wirez",
18+
Short: "socks5 proxy rotator",
19+
Version: version,
20+
}
21+
22+
cmd.AddCommand(
23+
newServerCmd().cmd,
24+
)
25+
26+
return cmd
27+
}

command/server.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package command
2+
3+
import (
4+
"context"
5+
"errors"
6+
"log"
7+
"net"
8+
"os"
9+
"os/signal"
10+
"syscall"
11+
12+
"github.com/ginuerzh/gosocks5/server"
13+
"github.com/spf13/cobra"
14+
"github.com/v-byte-cpu/wirez/pkg/connect"
15+
)
16+
17+
func newServerCmd() *serverCmd {
18+
c := &serverCmd{}
19+
20+
cmd := &cobra.Command{
21+
Use: "server [flags]",
22+
Example: "server -l 127.0.0.1:1080 -f proxies.txt",
23+
Short: "Start SOCKS5 server to load-balance requests",
24+
RunE: func(cmd *cobra.Command, args []string) (err error) {
25+
f, err := os.Open(c.opts.proxyFile)
26+
if err != nil {
27+
return err
28+
}
29+
defer f.Close()
30+
socksAddrs, err := parseProxyFile(f)
31+
if err != nil {
32+
return err
33+
}
34+
35+
log.Printf("starting listening on %s...\n", c.opts.listenAddr)
36+
ln, err := net.Listen("tcp", c.opts.listenAddr)
37+
if err != nil {
38+
return err
39+
}
40+
srv := &server.Server{
41+
Listener: ln,
42+
}
43+
44+
dconn := connect.NewDirectConnector()
45+
proxies := make([]connect.Connector, 0, len(socksAddrs))
46+
for _, socksAddr := range socksAddrs {
47+
socksConn := connect.NewSOCKS5Connector(dconn, socksAddr)
48+
proxies = append(proxies, socksConn)
49+
}
50+
rotationConn := connect.NewRotationConnector(proxies)
51+
52+
go func() {
53+
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
54+
defer cancel()
55+
<-ctx.Done()
56+
if err := srv.Close(); err != nil {
57+
log.Println(err)
58+
}
59+
}()
60+
61+
err = srv.Serve(connect.NewSOCKS5ServerHandler(rotationConn))
62+
if err != nil && !errors.Is(err, net.ErrClosed) {
63+
return err
64+
}
65+
return nil
66+
},
67+
}
68+
69+
c.opts.initCliFlags(cmd)
70+
71+
c.cmd = cmd
72+
return c
73+
}
74+
75+
type serverCmd struct {
76+
cmd *cobra.Command
77+
opts serverCmdOpts
78+
}
79+
80+
type serverCmdOpts struct {
81+
listenAddr string
82+
proxyFile string
83+
}
84+
85+
func (o *serverCmdOpts) initCliFlags(cmd *cobra.Command) {
86+
cmd.Flags().StringVarP(&o.listenAddr, "listen", "l", ":1080", "SOCKS5 server address")
87+
cmd.Flags().StringVarP(&o.proxyFile, "file", "f", "proxies.txt", "SOCKS5 proxies file")
88+
}

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
module github.com/v-byte-cpu/wirez
22

3-
go 1.17
3+
go 1.19
44

55
require (
66
github.com/ginuerzh/gosocks5 v0.2.0
7+
github.com/spf13/cobra v1.5.0
78
github.com/stretchr/testify v1.7.0
89
go.uber.org/multierr v1.7.0
910
)
1011

1112
require (
1213
github.com/davecgh/go-spew v1.1.1 // indirect
14+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
1315
github.com/pmezard/go-difflib v1.0.0 // indirect
16+
github.com/spf13/pflag v1.0.5 // indirect
1417
go.uber.org/atomic v1.7.0 // indirect
1518
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
1619
)

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1+
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
12
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
23
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
34
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
45
github.com/ginuerzh/gosocks5 v0.2.0 h1:K0Ua23U9LU3BZrf3XpGDcs0mP8DiEpa6PJE4TA/MU3s=
56
github.com/ginuerzh/gosocks5 v0.2.0/go.mod h1:qp22mr6tH/prEoaN0pFukq76LlScIE+F2rP2ZP5ZHno=
7+
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
8+
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
69
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
710
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
11+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
12+
github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU=
13+
github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM=
14+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
15+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
816
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
917
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
1018
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
@@ -15,6 +23,7 @@ go.uber.org/multierr v1.7.0 h1:zaiO/rmgFjbmCXdSYJWQcdvOCsthmdaHfr3Gm2Kx4Ec=
1523
go.uber.org/multierr v1.7.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
1624
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1725
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
26+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
1827
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1928
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
2029
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)