Skip to content

Commit 2a29eab

Browse files
author
Blokhin Dmitriy
committed
change contract for bind command
1 parent 90560ea commit 2a29eab

File tree

4 files changed

+51
-55
lines changed

4 files changed

+51
-55
lines changed

protocol.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ const (
7070
// SOCKS5 implements SOCKS5 protocol.
7171
type SOCKS5 struct {
7272
auth map[authMethod]authHandler
73-
bind func() (net.Listener, error) // bind for BIND command
74-
connect func(addressType int, addr []byte, port string) (net.Conn, error)
73+
listen func() (net.Listener, error) // listen for BIND command
74+
connect func(addressType int, addr []byte, port int) (net.Conn, error)
7575
}
7676

7777
// state is state through the SOCKS5 protocol negotiations.
@@ -179,7 +179,7 @@ func getCommand(state *state) (transition, error) {
179179
}
180180

181181
func runBind(state *state) (transition, error) {
182-
if state.opts.bind == nil {
182+
if state.opts.listen == nil {
183183
state.status = notAllowed
184184
return failCommand, nil
185185
}
@@ -195,7 +195,7 @@ func runConnect(state *state) (transition, error) {
195195
// connect
196196
addrType := int(state.command.addressType) //nolint
197197
addr := state.command.addr
198-
port := strconv.Itoa(int(state.command.port))
198+
port := int(state.command.port)
199199

200200
conn, err := state.opts.connect(addrType, addr, port)
201201
if err != nil {
@@ -272,10 +272,10 @@ func parseAddress(addr net.Addr) (addressType, net.IP, int, error) {
272272
}
273273

274274
func defaultBind(state *state) (transition, error) {
275-
ls, err := state.opts.bind()
275+
ls, err := state.opts.listen()
276276
if err != nil {
277277
state.status = sockFailure
278-
return failCommand, fmt.Errorf("bind: %w", err)
278+
return failCommand, fmt.Errorf("listen: %w", err)
279279
}
280280
defer ls.Close() // nolint
281281

@@ -302,7 +302,7 @@ func defaultBind(state *state) (transition, error) {
302302
conn, err := ls.Accept()
303303
if err != nil {
304304
state.status = sockFailure
305-
return failCommand, fmt.Errorf("bind accept: %w", err)
305+
return failCommand, fmt.Errorf("listen accept: %w", err)
306306
}
307307

308308
// parse remote addr
@@ -326,7 +326,7 @@ func defaultBind(state *state) (transition, error) {
326326
return nil, nil
327327
}
328328

329-
func defaultConnect(addressType int, addr []byte, port string) (net.Conn, error) {
329+
func defaultConnect(addressType int, addr []byte, port int) (net.Conn, error) {
330330
// make connection string for net.Dial
331331
address := buildDialAddress(addressType, addr, port)
332332

@@ -353,15 +353,15 @@ func defaultConnect(addressType int, addr []byte, port string) (net.Conn, error)
353353
}
354354

355355
// buildDialAddress returns address in net.Dial format from SOCKS5 details.
356-
func buildDialAddress(addressType int, addr []byte, port string) string {
356+
func buildDialAddress(addressType int, addr []byte, port int) string {
357357
var host string
358358
if addressType != int(domainName) {
359359
host = net.IP(addr).String()
360360
} else {
361361
host = string(addr)
362362
}
363363

364-
return net.JoinHostPort(host, port)
364+
return net.JoinHostPort(host, strconv.Itoa(port))
365365
}
366366

367367
// nolint

protocol_test.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ func Test_runBind(t *testing.T) {
669669
wantErr bool
670670
}{
671671
{
672-
name: "no bind",
672+
name: "no listen",
673673
args: args{
674674
state: &state{},
675675
},
@@ -687,11 +687,11 @@ func Test_runBind(t *testing.T) {
687687
},
688688
},
689689
{
690-
name: "yes bind",
690+
name: "yes listen",
691691
args: args{
692692
state: &state{
693693
opts: SOCKS5{
694-
bind: func() (net.Listener, error) {
694+
listen: func() (net.Listener, error) {
695695
return nil, nil
696696
},
697697
},
@@ -824,16 +824,16 @@ func Test_runConnect(t *testing.T) {
824824
args: args{
825825
state: &state{
826826
opts: SOCKS5{
827-
connect: func(addressType int, addr []byte, port string) (net.Conn, error) {
827+
connect: func(addressType int, addr []byte, port int) (net.Conn, error) {
828828
// check that all params are passed well
829829
if addressType != int(ipv4) {
830830
return nil, fmt.Errorf("got invalid address type")
831831
}
832832
if !bytes.Equal(addr, ipaddr.IP.To4()) {
833833
return nil, fmt.Errorf("got invalid ip address")
834834
}
835-
if port != strconv.Itoa(ipaddr.Port) {
836-
return nil, fmt.Errorf("got invalid port %q, want %q", port, ipaddr.Port)
835+
if port != ipaddr.Port {
836+
return nil, fmt.Errorf("got port %d, want %d", port, ipaddr.Port)
837837
}
838838
return nil, ErrNotAllowed
839839
},
@@ -865,7 +865,7 @@ func Test_runConnect(t *testing.T) {
865865
args: args{
866866
state: &state{
867867
opts: SOCKS5{
868-
connect: func(addressType int, addr []byte, port string) (net.Conn, error) {
868+
connect: func(addressType int, addr []byte, port int) (net.Conn, error) {
869869
return nil, ErrHostUnreachable
870870
},
871871
},
@@ -896,7 +896,7 @@ func Test_runConnect(t *testing.T) {
896896
args: args{
897897
state: &state{
898898
opts: SOCKS5{
899-
connect: func(addressType int, addr []byte, port string) (net.Conn, error) {
899+
connect: func(addressType int, addr []byte, port int) (net.Conn, error) {
900900
return nil, ErrConnectionRefused
901901
},
902902
},
@@ -927,7 +927,7 @@ func Test_runConnect(t *testing.T) {
927927
args: args{
928928
state: &state{
929929
opts: SOCKS5{
930-
connect: func(addressType int, addr []byte, port string) (net.Conn, error) {
930+
connect: func(addressType int, addr []byte, port int) (net.Conn, error) {
931931
return nil, ErrNetworkUnreachable
932932
},
933933
},
@@ -958,7 +958,7 @@ func Test_runConnect(t *testing.T) {
958958
args: args{
959959
state: &state{
960960
opts: SOCKS5{
961-
connect: func(addressType int, addr []byte, port string) (net.Conn, error) {
961+
connect: func(addressType int, addr []byte, port int) (net.Conn, error) {
962962
return nil, ErrTTLExpired
963963
},
964964
},
@@ -989,7 +989,7 @@ func Test_runConnect(t *testing.T) {
989989
args: args{
990990
state: &state{
991991
opts: SOCKS5{
992-
connect: func(addressType int, addr []byte, port string) (net.Conn, error) {
992+
connect: func(addressType int, addr []byte, port int) (net.Conn, error) {
993993
return nil, io.EOF // any other error
994994
},
995995
},
@@ -1020,7 +1020,7 @@ func Test_runConnect(t *testing.T) {
10201020
args: args{
10211021
state: &state{
10221022
opts: SOCKS5{
1023-
connect: func(addressType int, addr []byte, port string) (net.Conn, error) {
1023+
connect: func(addressType int, addr []byte, port int) (net.Conn, error) {
10241024
return &net.UDPConn{}, nil
10251025
},
10261026
},
@@ -1048,7 +1048,7 @@ func Test_runConnect(t *testing.T) {
10481048
args: args{
10491049
state: &state{
10501050
opts: SOCKS5{
1051-
connect: func(addressType int, addr []byte, port string) (net.Conn, error) {
1051+
connect: func(addressType int, addr []byte, port int) (net.Conn, error) {
10521052
return validTCPConn, nil
10531053
},
10541054
},
@@ -1080,7 +1080,7 @@ func Test_runConnect(t *testing.T) {
10801080
args: args{
10811081
state: &state{
10821082
opts: SOCKS5{
1083-
connect: func(addressType int, addr []byte, port string) (net.Conn, error) {
1083+
connect: func(addressType int, addr []byte, port int) (net.Conn, error) {
10841084
return validTCPConn, nil
10851085
},
10861086
},
@@ -1146,15 +1146,15 @@ func Test_runConnect(t *testing.T) {
11461146
}
11471147

11481148
func Test_buildDialAddress(t *testing.T) {
1149-
port := "777"
1149+
port := 777
11501150
ip4 := "192.168.1.1"
11511151
ip6 := "2001:db8::1"
11521152
domain := "google.com"
11531153

11541154
type args struct {
11551155
addressType int
11561156
addr []byte
1157-
port string
1157+
port int
11581158
}
11591159
tests := []struct {
11601160
name string
@@ -1168,7 +1168,7 @@ func Test_buildDialAddress(t *testing.T) {
11681168
addr: net.ParseIP(ip4).To4(),
11691169
port: port,
11701170
},
1171-
want: net.JoinHostPort(ip4, port),
1171+
want: net.JoinHostPort(ip4, strconv.Itoa(port)),
11721172
},
11731173
{
11741174
name: "ipv6",
@@ -1177,7 +1177,7 @@ func Test_buildDialAddress(t *testing.T) {
11771177
addr: net.ParseIP(ip6).To16(),
11781178
port: port,
11791179
},
1180-
want: net.JoinHostPort(ip6, port),
1180+
want: net.JoinHostPort(ip6, strconv.Itoa(port)),
11811181
},
11821182
{
11831183
name: "domain",
@@ -1186,7 +1186,7 @@ func Test_buildDialAddress(t *testing.T) {
11861186
addr: []byte(domain),
11871187
port: port,
11881188
},
1189-
want: net.JoinHostPort(domain, port),
1189+
want: net.JoinHostPort(domain, strconv.Itoa(port)),
11901190
},
11911191
}
11921192
for _, tt := range tests {

server.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"io"
66
"net"
7-
"time"
87
)
98

109
// GSSAPI provides contract to implement GSSAPI boilerplate.
@@ -107,17 +106,13 @@ type Options struct {
107106
// o DOMAINNAME: X'03' -> addr contains domain name
108107
// o IP V6 address: X'04' -> addr contains net.IP
109108
// OPTIONAL
110-
Connect func(addressType int, addr []byte, port string) (net.Conn, error)
109+
Connect func(addressType int, addr []byte, port int) (net.Conn, error)
111110

112-
// Bind returns listener to accept incoming connections for protocol BIND operation:
111+
// Listen returns listener to accept incoming connections for protocol BIND operation:
113112
// incoming traffic from outside to client sock.
114113
// If not specified the SOCKS5 BIND operation will be rejected with notAllowed status.
115114
// OPTIONAL.
116-
Bind func() (net.Listener, error)
117-
118-
// MaxConnIdle defines maximum duration for inactive tcp connections.
119-
// OPTIONAL, default 3 minutes.
120-
MaxConnIdle time.Duration
115+
Listen func() (net.Listener, error)
121116
}
122117

123118
// New creates and returns a new object implemented the SOCKS5 protocol handler configured with the provided options.
@@ -139,14 +134,15 @@ type Options struct {
139134
//
140135
// Example:
141136
//
142-
// opts := Options{
143-
// Connect: customConnectHandler,
144-
// Bind: customBindHandler,
145-
// MaxConnIdle: 10 * time.Minute,
146-
// }
147-
// socks5, _ := proxyme.New(opts)
148-
// clientConn, _ := ls.Accept()
149-
// socks5.Handle(clientConn, nil)
137+
// ```
138+
// opts := Options{
139+
// Connect: customConnectHandler,
140+
// }
141+
// socks5, _ := proxyme.New(opts)
142+
// ls, _ := net.Listen("tcp", ":1080")
143+
// clientConn, _ := ls.Accept()
144+
// socks5.Handle(clientConn, nil)
145+
// ```
150146
//
151147
// The returned SOCKS5 protocol object can be used to handle incoming TCP connections by calling its Handle method.
152148
func New(opts Options) (*SOCKS5, error) {
@@ -165,7 +161,7 @@ func New(opts Options) (*SOCKS5, error) {
165161

166162
return &SOCKS5{
167163
auth: auth,
168-
bind: opts.Bind,
164+
listen: opts.Listen,
169165
connect: connectFn,
170166
}, nil
171167
}

server_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,18 +183,18 @@ func TestNew(t *testing.T) {
183183
if socks5.connect == nil {
184184
return fmt.Errorf("invalid connect callback")
185185
}
186-
if socks5.bind != nil {
187-
return fmt.Errorf("expected nil bind callback")
186+
if socks5.listen != nil {
187+
return fmt.Errorf("expected nil listen callback")
188188
}
189189
return nil
190190
},
191191
},
192192
{
193-
name: "common case: specify bind",
193+
name: "common case: specify listen",
194194
args: args{
195195
opts: Options{
196196
AllowNoAuth: true,
197-
Bind: func() (net.Listener, error) {
197+
Listen: func() (net.Listener, error) {
198198
return nil, nil
199199
}},
200200
},
@@ -211,8 +211,8 @@ func TestNew(t *testing.T) {
211211
if socks5.connect == nil {
212212
return fmt.Errorf("invalid connect callback")
213213
}
214-
if socks5.bind == nil {
215-
return fmt.Errorf("invalid bind callback")
214+
if socks5.listen == nil {
215+
return fmt.Errorf("invalid listen callback")
216216
}
217217
return nil
218218
},
@@ -234,8 +234,8 @@ func TestSOCKS5_Handle(t *testing.T) {
234234

235235
type fields struct {
236236
auth map[authMethod]authHandler
237-
bind func() (net.Listener, error)
238-
connect func(addressType int, addr []byte, port string) (net.Conn, error)
237+
listen func() (net.Listener, error)
238+
connect func(addressType int, addr []byte, port int) (net.Conn, error)
239239
}
240240
type args struct {
241241
conn io.ReadWriteCloser
@@ -275,7 +275,7 @@ func TestSOCKS5_Handle(t *testing.T) {
275275
t.Run(tt.name, func(t *testing.T) {
276276
s := SOCKS5{
277277
auth: tt.fields.auth,
278-
bind: tt.fields.bind,
278+
listen: tt.fields.listen,
279279
connect: tt.fields.connect,
280280
}
281281
called = false // initialize

0 commit comments

Comments
 (0)