-
Notifications
You must be signed in to change notification settings - Fork 0
/
inbound_detour_dynamic.go
158 lines (140 loc) · 3.84 KB
/
inbound_detour_dynamic.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
package core
import (
"sync"
"time"
"v2ray.com/core/app"
"v2ray.com/core/common/dice"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/retry"
"v2ray.com/core/proxy"
)
type InboundDetourHandlerDynamic struct {
sync.RWMutex
space app.Space
config *InboundConnectionConfig
portsInUse map[v2net.Port]bool
ichs []proxy.InboundHandler
ich2Recyle []proxy.InboundHandler
lastRefresh time.Time
}
func NewInboundDetourHandlerDynamic(space app.Space, config *InboundConnectionConfig) (*InboundDetourHandlerDynamic, error) {
handler := &InboundDetourHandlerDynamic{
space: space,
config: config,
portsInUse: make(map[v2net.Port]bool),
}
handler.ichs = make([]proxy.InboundHandler, config.GetAllocationStrategyValue().Concurrency.GetValue())
// To test configuration
ichConfig, err := config.GetTypedSettings()
if err != nil {
return nil, err
}
ich, err := proxy.CreateInboundHandler(config.Settings.Type, space, ichConfig, &proxy.InboundHandlerMeta{
Address: config.GetListenOnValue(),
Port: 0,
Tag: config.Tag,
StreamSettings: config.StreamSettings,
AllowPassiveConnection: config.AllowPassiveConnection,
})
if err != nil {
log.Error("Point: Failed to create inbound connection handler: ", err)
return nil, err
}
ich.Close()
return handler, nil
}
func (v *InboundDetourHandlerDynamic) pickUnusedPort() v2net.Port {
delta := int(v.config.PortRange.To) - int(v.config.PortRange.From) + 1
for {
r := dice.Roll(delta)
port := v.config.PortRange.FromPort() + v2net.Port(r)
_, used := v.portsInUse[port]
if !used {
return port
}
}
}
func (v *InboundDetourHandlerDynamic) GetConnectionHandler() (proxy.InboundHandler, int) {
v.RLock()
defer v.RUnlock()
ich := v.ichs[dice.Roll(len(v.ichs))]
until := int(v.config.GetAllocationStrategyValue().Refresh.GetValue()) - int((time.Now().Unix()-v.lastRefresh.Unix())/60/1000)
if until < 0 {
until = 0
}
return ich, int(until)
}
func (v *InboundDetourHandlerDynamic) Close() {
v.Lock()
defer v.Unlock()
for _, ich := range v.ichs {
ich.Close()
}
}
func (v *InboundDetourHandlerDynamic) RecyleHandles() {
if v.ich2Recyle != nil {
for _, ich := range v.ich2Recyle {
if ich == nil {
continue
}
port := ich.Port()
ich.Close()
delete(v.portsInUse, port)
}
v.ich2Recyle = nil
}
}
func (v *InboundDetourHandlerDynamic) refresh() error {
v.lastRefresh = time.Now()
config := v.config
v.ich2Recyle = v.ichs
newIchs := make([]proxy.InboundHandler, config.GetAllocationStrategyValue().Concurrency.GetValue())
for idx := range newIchs {
err := retry.Timed(5, 100).On(func() error {
port := v.pickUnusedPort()
ichConfig, _ := config.GetTypedSettings()
ich, err := proxy.CreateInboundHandler(config.Settings.Type, v.space, ichConfig, &proxy.InboundHandlerMeta{
Address: config.GetListenOnValue(), Port: port, Tag: config.Tag, StreamSettings: config.StreamSettings})
if err != nil {
delete(v.portsInUse, port)
return err
}
err = ich.Start()
if err != nil {
delete(v.portsInUse, port)
return err
}
v.portsInUse[port] = true
newIchs[idx] = ich
return nil
})
if err != nil {
log.Error("Point: Failed to create inbound connection handler: ", err)
return err
}
}
v.Lock()
v.ichs = newIchs
v.Unlock()
return nil
}
func (v *InboundDetourHandlerDynamic) Start() error {
err := v.refresh()
if err != nil {
log.Error("Point: Failed to refresh dynamic allocations: ", err)
return err
}
go func() {
for {
time.Sleep(time.Duration(v.config.GetAllocationStrategyValue().Refresh.GetValue())*time.Minute - 1)
v.RecyleHandles()
err := v.refresh()
if err != nil {
log.Error("Point: Failed to refresh dynamic allocations: ", err)
}
time.Sleep(time.Minute)
}
}()
return nil
}