-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver_handler_mbind.go
More file actions
155 lines (149 loc) · 3.36 KB
/
server_handler_mbind.go
File metadata and controls
155 lines (149 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package socksgo
import (
"context"
"net"
"sync"
"github.com/asciimoth/gonnect/helpers"
"github.com/asciimoth/socksgo/protocol"
"github.com/xtaci/smux"
)
// DefaultGostMBindHandler handles the Gost multiplexed BIND command.
//
// DefaultGostMBindHandler creates a TCP listener and uses smux to
// multiplex multiple incoming connections over a single TCP connection
// to the client.
//
// # Protocol Support
//
// - SOCKS4: Yes (Gost extension)
// - SOCKS5: Yes (Gost extension)
// - TLS: Yes
//
// # Behavior
//
// 1. Applies default listen host if address is unspecified
//
// 2. Validates local address against LaddrFilter
//
// 3. Creates TCP listener on requested address
//
// 4. Sends success reply with bound address
//
// 5. Upgrades connection to smux session
//
// 6. Spawns goroutine to accept smux streams (and discard)
//
// 7. Accepts incoming connections on listener
//
// 8. For each incoming connection:
// - Opens new smux stream
// - Pipes data between listener connection and stream
//
// 9. Continues until listener or session closes
//
// # Multiplexing
//
// MBIND uses smux (stream multiplexing) to carry multiple independent
// connections over a single TCP connection.
//
// # Reply
//
// Sends success reply (0x00) with the listener address.
//
// # Errors
//
// Returns error and sends appropriate reply status:
// - DisallowReply (0x02): Address filtered
// - FailReply (0x01): Listen failed
//
// # Examples
//
// // Enable on server
// server := &socksgo.Server{
// Handlers: socksgo.DefaultCommandHandlers,
// Smux: &smux.Config{
// MaxFrameSize: 65535,
// MaxReceiveBuffer: 4194304,
// },
// }
//
// # See Also
//
// - github.com/xtaci/smux: Stream multiplexing library
// - server_handler_bind.go: Standard BIND handler
// - protocol.PipeConn: Connection piping implementation
var DefaultGostMBindHandler = CommandHandler{
Socks4: true,
Socks5: true,
TLSCompat: true,
Handler: func(
ctx context.Context,
server *Server,
conn net.Conn,
ver string,
info protocol.AuthInfo,
cmd protocol.Cmd,
addr protocol.Addr) error {
pool := server.GetPool()
addr = addr.WithDefaultHost(server.GetDefaultListenHost())
err := server.CheckLaddr(&addr)
if err != nil {
protocol.Reject(ver, conn, protocol.DisallowReply, pool)
return err
}
listener, err := server.GetListener()(ctx, "tcp", addr.ToHostPort())
if err != nil {
protocol.Reject(ver, conn, errorToReplyStatus(err), pool)
return err
}
defer func() { _ = listener.Close() }()
// Send first reply with laddr
err = protocol.Reply(
ver,
conn,
protocol.SuccReply,
protocol.AddrFromNetAddr(listener.Addr()),
pool,
)
if err != nil {
return err
}
session, err := smux.Client(conn, server.GetSmux())
if err != nil {
_ = conn.Close()
return err
}
defer func() {
_ = conn.Close()
_ = session.Close()
}()
var wg sync.WaitGroup
wg.Go(func() {
for {
rw, err := session.Accept()
if err != nil {
_ = session.Close()
_ = listener.Close()
return
}
_ = rw.Close()
}
})
for {
var inc net.Conn
inc, err = listener.Accept()
if err != nil {
break
}
stream, err := session.OpenStream()
if err != nil {
break
}
wg.Go(func() {
_ = helpers.PipeConn(inc, stream)
})
}
wg.Wait()
return helpers.ClosedNetworkErrToNil(err)
},
}