Skip to content

Commit b3d51e1

Browse files
committed
some socks connect handler done
1 parent 4aad46b commit b3d51e1

File tree

5 files changed

+88
-1
lines changed

5 files changed

+88
-1
lines changed

handler/adapter.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,30 @@ func (w wrappedH1RespWriter) Close() error {
134134
}
135135

136136
var _ io.ReadWriteCloser = wrappedH1RespWriter{}
137+
138+
type wrappedSOCKS struct {
139+
r io.Reader
140+
w io.Writer
141+
}
142+
143+
func wrapSOCKS(r io.Reader, w io.Writer) wrappedSOCKS {
144+
return wrappedSOCKS{
145+
r: r,
146+
w: w,
147+
}
148+
}
149+
150+
func (w wrappedSOCKS) Read(p []byte) (n int, err error) {
151+
return w.r.Read(p)
152+
}
153+
154+
func (w wrappedSOCKS) Write(p []byte) (n int, err error) {
155+
return w.w.Write(p)
156+
}
157+
158+
func (w wrappedSOCKS) Close() error {
159+
// can't really close SOCKS reader or writer
160+
return nil
161+
}
162+
163+
var _ io.ReadWriteCloser = wrappedSOCKS{}

handler/handler.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ type HandlerDialer interface {
2828
DialContext(ctx context.Context, net, address string) (net.Conn, error)
2929
}
3030

31+
type ForwardFunc = func(ctx context.Context, username string, incoming, outgoing io.ReadWriteCloser) error
32+
3133
type ProxyHandler struct {
3234
auth auth.Auth
3335
logger *clog.CondLogger
3436
dialer HandlerDialer
35-
forward func(ctx context.Context, username string, incoming, outgoing io.ReadWriteCloser) error
37+
forward ForwardFunc
3638
httptransport http.RoundTripper
3739
outbound map[string]string
3840
outboundMux sync.RWMutex

handler/socks.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package handler
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io"
7+
"strings"
8+
9+
ddto "github.com/SenseUnit/dumbproxy/dialer/dto"
10+
clog "github.com/SenseUnit/dumbproxy/log"
11+
"github.com/things-go/go-socks5"
12+
"github.com/things-go/go-socks5/statute"
13+
)
14+
15+
func SOCKSHandler(dialer HandlerDialer, logger *clog.CondLogger, forward ForwardFunc) func(ctx context.Context, writer io.Writer, request *socks5.Request) error {
16+
return func(ctx context.Context, writer io.Writer, request *socks5.Request) error {
17+
username := ""
18+
if request.AuthContext != nil {
19+
username = request.AuthContext.Payload["username"]
20+
}
21+
localAddr := request.LocalAddr.String()
22+
ctx = ddto.BoundDialerParamsToContext(ctx, nil, trimAddrPort(localAddr))
23+
ctx = ddto.FilterParamsToContext(ctx, nil, username)
24+
// TODO: add request logging
25+
target, err := dialer.DialContext(ctx, "tcp", request.DestAddr.String())
26+
if err != nil {
27+
msg := err.Error()
28+
resp := statute.RepHostUnreachable
29+
if strings.Contains(msg, "refused") {
30+
resp = statute.RepConnectionRefused
31+
} else if strings.Contains(msg, "network is unreachable") {
32+
resp = statute.RepNetworkUnreachable
33+
}
34+
if err := socks5.SendReply(writer, resp, nil); err != nil {
35+
return fmt.Errorf("failed to send reply, %v", err)
36+
}
37+
return fmt.Errorf("connect to %v failed, %v", request.RawDestAddr, err)
38+
}
39+
defer target.Close() // nolint: errcheck
40+
41+
// Send success
42+
if err := socks5.SendReply(writer, statute.RepSuccess, target.LocalAddr()); err != nil {
43+
return fmt.Errorf("failed to send reply, %v", err)
44+
}
45+
46+
return forward(ctx, username, wrapSOCKS(request.Reader, writer), target)
47+
}
48+
}

jsext/dto.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ type JSRequestInfo struct {
2424
}
2525

2626
func JSRequestInfoFromRequest(req *http.Request) *JSRequestInfo {
27+
if req == nil {
28+
return nil
29+
}
2730
return &JSRequestInfo{
2831
Method: req.Method,
2932
URL: req.URL.String(),

main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,13 @@ func run() int {
827827
EnableConnect: true,
828828
},
829829
),
830+
socks5.WithConnectHandle(
831+
handler.SOCKSHandler(
832+
dialerRoot,
833+
proxyLogger,
834+
forwarder,
835+
),
836+
),
830837
}
831838
switch cs := authProvider.(type) {
832839
case auth.NoAuth:

0 commit comments

Comments
 (0)