-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
dispatcher.go
115 lines (98 loc) · 2.6 KB
/
dispatcher.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
package udp
import (
"context"
"sync"
"time"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
"v2ray.com/core/common/session"
"v2ray.com/core/common/signal"
"v2ray.com/core/features/routing"
"v2ray.com/core/transport"
)
type ResponseCallback func(ctx context.Context, payload *buf.Buffer)
type connEntry struct {
link *transport.Link
timer signal.ActivityUpdater
cancel context.CancelFunc
}
type Dispatcher struct {
sync.RWMutex
conns map[net.Destination]*connEntry
dispatcher routing.Dispatcher
callback ResponseCallback
}
func NewDispatcher(dispatcher routing.Dispatcher, callback ResponseCallback) *Dispatcher {
return &Dispatcher{
conns: make(map[net.Destination]*connEntry),
dispatcher: dispatcher,
callback: callback,
}
}
func (v *Dispatcher) RemoveRay(dest net.Destination) {
v.Lock()
defer v.Unlock()
if conn, found := v.conns[dest]; found {
common.Close(conn.link.Reader)
common.Close(conn.link.Writer)
delete(v.conns, dest)
}
}
func (v *Dispatcher) getInboundRay(ctx context.Context, dest net.Destination) *connEntry {
v.Lock()
defer v.Unlock()
if entry, found := v.conns[dest]; found {
return entry
}
newError("establishing new connection for ", dest).WriteToLog()
ctx, cancel := context.WithCancel(ctx)
removeRay := func() {
cancel()
v.RemoveRay(dest)
}
timer := signal.CancelAfterInactivity(ctx, removeRay, time.Second*4)
link, _ := v.dispatcher.Dispatch(ctx, dest)
entry := &connEntry{
link: link,
timer: timer,
cancel: removeRay,
}
v.conns[dest] = entry
go handleInput(ctx, entry, v.callback)
return entry
}
func (v *Dispatcher) Dispatch(ctx context.Context, destination net.Destination, payload *buf.Buffer) {
// TODO: Add user to destString
newError("dispatch request to: ", destination).AtDebug().WriteToLog(session.ExportIDToError(ctx))
conn := v.getInboundRay(ctx, destination)
outputStream := conn.link.Writer
if outputStream != nil {
if err := outputStream.WriteMultiBuffer(buf.MultiBuffer{payload}); err != nil {
newError("failed to write first UDP payload").Base(err).WriteToLog(session.ExportIDToError(ctx))
conn.cancel()
return
}
}
}
func handleInput(ctx context.Context, conn *connEntry, callback ResponseCallback) {
defer conn.cancel()
input := conn.link.Reader
timer := conn.timer
for {
select {
case <-ctx.Done():
return
default:
}
mb, err := input.ReadMultiBuffer()
if err != nil {
newError("failed to handle UDP input").Base(err).WriteToLog(session.ExportIDToError(ctx))
return
}
timer.Update()
for _, b := range mb {
callback(ctx, b)
}
}
}