Skip to content

Commit 2c3ffc9

Browse files
committed
Clean up
1 parent 7c2b591 commit 2c3ffc9

File tree

6 files changed

+61
-17
lines changed

6 files changed

+61
-17
lines changed

all_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ func TestProxyCommandUnit(t *testing.T) {
2222
return nil, err
2323
}
2424
conn1, conn2 := net.Pipe()
25+
var buf1, buf2 [32 * 1024]byte
2526
go func() {
2627
ctx, cancel := context.WithCancel(ctx)
27-
err := Tunnel(ctx, &connWithCancel{conn, cancel}, conn1)
28+
err := Tunnel(ctx, &connWithCancel{conn, cancel}, conn1, buf1[:], buf2[:])
2829
if err != nil {
2930
t.Error(err)
3031
return

cmd/commandproxy/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ func main() {
2727
log.Fatalln(err)
2828
return
2929
}
30-
31-
err = commandproxy.Tunnel(context.Background(), commandproxy.Stdio, conn)
30+
var buf1, buf2 [32 * 1024]byte
31+
err = commandproxy.Tunnel(context.Background(), commandproxy.Stdio, conn, buf1[:], buf2[:])
3232
if err != nil {
3333
log.Fatalln(err)
3434
return

dial.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import (
77

88
type DialProxyCommand []string
99

10-
func (p *DialProxyCommand) Format(network string, address string) ([]string, error) {
10+
func (p *DialProxyCommand) Format(network string, address string) []string {
1111
host, port, err := net.SplitHostPort(address)
1212
if err != nil {
13-
return nil, err
13+
host = address
14+
} else if host == "" {
15+
host = "0.0.0.0"
1416
}
1517
m := map[byte]string{
1618
'n': network,
@@ -22,14 +24,11 @@ func (p *DialProxyCommand) Format(network string, address string) ([]string, err
2224
for i := range proxy {
2325
proxy[i] = ReplaceEscape(proxy[i], m)
2426
}
25-
return proxy, nil
27+
return proxy
2628
}
2729

2830
func (p *DialProxyCommand) DialContext(ctx context.Context, network string, address string) (net.Conn, error) {
29-
proxy, err := p.Format(network, address)
30-
if err != nil {
31-
return nil, err
32-
}
31+
proxy := p.Format(network, address)
3332
cp := ProxyCommand(ctx, proxy[0], proxy[1:]...)
3433
return cp.Stdio()
3534
}

dial_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package commandproxy
33
import (
44
"context"
55
"net"
6+
"reflect"
67
"testing"
78
)
89

@@ -26,3 +27,48 @@ func TestDialProxyCommand(t *testing.T) {
2627
t.Fatal(err)
2728
}
2829
}
30+
31+
func TestDialProxyCommand_Format(t *testing.T) {
32+
type args struct {
33+
network string
34+
address string
35+
}
36+
tests := []struct {
37+
name string
38+
p DialProxyCommand
39+
args args
40+
want []string
41+
}{
42+
{
43+
p: DialProxyCommand{"nc", "%h", "%p"},
44+
args: args{
45+
network: "tcp",
46+
address: ":1",
47+
},
48+
want: []string{"nc", "0.0.0.0", "1"},
49+
},
50+
{
51+
p: DialProxyCommand{"nc", "%h", "%p"},
52+
args: args{
53+
network: "tcp",
54+
address: "[::]:1",
55+
},
56+
want: []string{"nc", "::", "1"},
57+
},
58+
{
59+
p: DialProxyCommand{"nc", "-U", "%h"},
60+
args: args{
61+
network: "unix",
62+
address: "./x.socks",
63+
},
64+
want: []string{"nc", "-U", "./x.socks"},
65+
},
66+
}
67+
for _, tt := range tests {
68+
t.Run(tt.name, func(t *testing.T) {
69+
if got := tt.p.Format(tt.args.network, tt.args.address); !reflect.DeepEqual(got, tt.want) {
70+
t.Errorf("Format() = %v, want %v", got, tt.want)
71+
}
72+
})
73+
}
74+
}

tunnel.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@ import (
55
"io"
66
)
77

8-
func Tunnel(ctx context.Context, c1, c2 io.ReadWriteCloser) error {
8+
func Tunnel(ctx context.Context, c1, c2 io.ReadWriteCloser, buf1, buf2 []byte) error {
99
ctx, cancel := context.WithCancel(ctx)
1010
var errs tunnelErr
1111
go func() {
12-
var buf [32 * 1024]byte
13-
_, errs[0] = io.CopyBuffer(c1, c2, buf[:])
12+
_, errs[0] = io.CopyBuffer(c1, c2, buf1)
1413
cancel()
1514
}()
1615
go func() {
17-
var buf [32 * 1024]byte
18-
_, errs[1] = io.CopyBuffer(c2, c1, buf[:])
16+
_, errs[1] = io.CopyBuffer(c2, c1, buf2)
1917
cancel()
2018
}()
2119
<-ctx.Done()

util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import (
88

99
var (
1010
errQuoteNotClose = fmt.Errorf("quote is not closed")
11-
errEmptyCmmand = fmt.Errorf("cmd is empty")
11+
errEmptyCommand = fmt.Errorf("cmd is empty")
1212
)
1313

1414
func SplitCommand(cmd string) ([]string, error) {
1515
cmd = strings.TrimSpace(cmd)
1616
if cmd == "" {
17-
return nil, errEmptyCmmand
17+
return nil, errEmptyCommand
1818
}
1919
var ss []string
2020

0 commit comments

Comments
 (0)