-
Notifications
You must be signed in to change notification settings - Fork 0
/
inboundhandler.go
71 lines (55 loc) · 1.43 KB
/
inboundhandler.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
package mocks
import (
"io"
"sync"
"v2ray.com/core/app/dispatcher"
v2io "v2ray.com/core/common/io"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
)
type InboundConnectionHandler struct {
ListeningPort v2net.Port
ListeningAddress v2net.Address
PacketDispatcher dispatcher.PacketDispatcher
ConnInput io.Reader
ConnOutput io.Writer
}
func (this *InboundConnectionHandler) Start() error {
return nil
}
func (this *InboundConnectionHandler) Port() v2net.Port {
return this.ListeningPort
}
func (this *InboundConnectionHandler) Close() {
}
func (this *InboundConnectionHandler) Communicate(destination v2net.Destination) error {
ray := this.PacketDispatcher.DispatchToOutbound(&proxy.InboundHandlerMeta{
AllowPassiveConnection: false,
}, &proxy.SessionInfo{
Source: v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(0)),
Destination: destination,
})
input := ray.InboundInput()
output := ray.InboundOutput()
readFinish := &sync.Mutex{}
writeFinish := &sync.Mutex{}
readFinish.Lock()
writeFinish.Lock()
go func() {
v2reader := v2io.NewAdaptiveReader(this.ConnInput)
defer v2reader.Release()
v2io.Pipe(v2reader, input)
input.Close()
readFinish.Unlock()
}()
go func() {
v2writer := v2io.NewAdaptiveWriter(this.ConnOutput)
defer v2writer.Release()
v2io.Pipe(output, v2writer)
output.Release()
writeFinish.Unlock()
}()
readFinish.Lock()
writeFinish.Lock()
return nil
}