-
Notifications
You must be signed in to change notification settings - Fork 0
/
dokodemo.go
216 lines (189 loc) · 5.41 KB
/
dokodemo.go
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package dokodemo
import (
"sync"
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/loader"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
"v2ray.com/core/proxy/registry"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/internet/udp"
)
type DokodemoDoor struct {
tcpMutex sync.RWMutex
udpMutex sync.RWMutex
config *Config
accepting bool
address v2net.Address
port v2net.Port
packetDispatcher dispatcher.PacketDispatcher
tcpListener *internet.TCPHub
udpHub *udp.UDPHub
udpServer *udp.UDPServer
meta *proxy.InboundHandlerMeta
}
func NewDokodemoDoor(config *Config, space app.Space, meta *proxy.InboundHandlerMeta) *DokodemoDoor {
d := &DokodemoDoor{
config: config,
address: config.GetPredefinedAddress(),
port: v2net.Port(config.Port),
meta: meta,
}
space.InitializeApplication(func() error {
if !space.HasApp(dispatcher.APP_ID) {
return errors.New("Dokodemo: Dispatcher is not found in the space.")
}
d.packetDispatcher = space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)
return nil
})
return d
}
func (v *DokodemoDoor) Port() v2net.Port {
return v.meta.Port
}
func (v *DokodemoDoor) Close() {
v.accepting = false
if v.tcpListener != nil {
v.tcpMutex.Lock()
v.tcpListener.Close()
v.tcpListener = nil
v.tcpMutex.Unlock()
}
if v.udpHub != nil {
v.udpMutex.Lock()
v.udpHub.Close()
v.udpHub = nil
v.udpMutex.Unlock()
}
}
func (v *DokodemoDoor) Start() error {
if v.accepting {
return nil
}
v.accepting = true
if v.config.NetworkList.HasNetwork(v2net.Network_TCP) {
err := v.ListenTCP()
if err != nil {
return err
}
}
if v.config.NetworkList.HasNetwork(v2net.Network_UDP) {
err := v.ListenUDP()
if err != nil {
return err
}
}
return nil
}
func (v *DokodemoDoor) ListenUDP() error {
v.udpServer = udp.NewUDPServer(v.packetDispatcher)
udpHub, err := udp.ListenUDP(
v.meta.Address, v.meta.Port, udp.ListenOption{
Callback: v.handleUDPPackets,
ReceiveOriginalDest: v.config.FollowRedirect,
Concurrency: 2,
})
if err != nil {
log.Error("Dokodemo failed to listen on ", v.meta.Address, ":", v.meta.Port, ": ", err)
return err
}
v.udpMutex.Lock()
v.udpHub = udpHub
v.udpMutex.Unlock()
return nil
}
func (v *DokodemoDoor) handleUDPPackets(payload *buf.Buffer, session *proxy.SessionInfo) {
if session.Destination.Network == v2net.Network_Unknown && v.address != nil && v.port > 0 {
session.Destination = v2net.UDPDestination(v.address, v.port)
}
if session.Destination.Network == v2net.Network_Unknown {
log.Info("Dokodemo: Unknown destination, stop forwarding...")
return
}
session.Inbound = v.meta
v.udpServer.Dispatch(session, payload, v.handleUDPResponse)
}
func (v *DokodemoDoor) handleUDPResponse(dest v2net.Destination, payload *buf.Buffer) {
defer payload.Release()
v.udpMutex.RLock()
defer v.udpMutex.RUnlock()
if !v.accepting {
return
}
v.udpHub.WriteTo(payload.Bytes(), dest)
}
func (v *DokodemoDoor) ListenTCP() error {
tcpListener, err := internet.ListenTCP(v.meta.Address, v.meta.Port, v.HandleTCPConnection, v.meta.StreamSettings)
if err != nil {
log.Error("Dokodemo: Failed to listen on ", v.meta.Address, ":", v.meta.Port, ": ", err)
return err
}
v.tcpMutex.Lock()
v.tcpListener = tcpListener
v.tcpMutex.Unlock()
return nil
}
func (v *DokodemoDoor) HandleTCPConnection(conn internet.Connection) {
defer conn.Close()
var dest v2net.Destination
if v.config.FollowRedirect {
originalDest := GetOriginalDestination(conn)
if originalDest.Network != v2net.Network_Unknown {
log.Info("Dokodemo: Following redirect to: ", originalDest)
dest = originalDest
}
}
if dest.Network == v2net.Network_Unknown && v.address != nil && v.port > v2net.Port(0) {
dest = v2net.TCPDestination(v.address, v.port)
}
if dest.Network == v2net.Network_Unknown {
log.Info("Dokodemo: Unknown destination, stop forwarding...")
return
}
log.Info("Dokodemo: Handling request to ", dest)
ray := v.packetDispatcher.DispatchToOutbound(&proxy.SessionInfo{
Source: v2net.DestinationFromAddr(conn.RemoteAddr()),
Destination: dest,
Inbound: v.meta,
})
defer ray.InboundOutput().Release()
var wg sync.WaitGroup
reader := v2net.NewTimeOutReader(v.config.Timeout, conn)
defer reader.Release()
wg.Add(1)
go func() {
v2reader := buf.NewReader(reader)
defer v2reader.Release()
if err := buf.PipeUntilEOF(v2reader, ray.InboundInput()); err != nil {
log.Info("Dokodemo: Failed to transport all TCP request: ", err)
}
wg.Done()
ray.InboundInput().Close()
}()
wg.Add(1)
go func() {
v2writer := buf.NewWriter(conn)
defer v2writer.Release()
if err := buf.PipeUntilEOF(ray.InboundOutput(), v2writer); err != nil {
log.Info("Dokodemo: Failed to transport all TCP response: ", err)
}
wg.Done()
}()
wg.Wait()
}
type Factory struct{}
func (v *Factory) StreamCapability() v2net.NetworkList {
return v2net.NetworkList{
Network: []v2net.Network{v2net.Network_RawTCP},
}
}
func (v *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
return NewDokodemoDoor(rawConfig.(*Config), space, meta), nil
}
func init() {
registry.MustRegisterInboundHandlerCreator(loader.GetType(new(Config)), new(Factory))
}