2016-10-12 14:46:02 +00:00
|
|
|
package core
|
2016-01-21 00:40:52 +00:00
|
|
|
|
2016-01-22 15:25:01 +00:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2016-08-20 18:55:45 +00:00
|
|
|
"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"
|
2016-01-22 15:25:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type InboundDetourHandlerDynamic struct {
|
|
|
|
sync.RWMutex
|
|
|
|
space app.Space
|
2016-10-14 20:21:45 +00:00
|
|
|
config *InboundConnectionConfig
|
2016-01-22 15:25:01 +00:00
|
|
|
portsInUse map[v2net.Port]bool
|
2016-06-03 22:38:22 +00:00
|
|
|
ichs []proxy.InboundHandler
|
2016-06-05 21:39:38 +00:00
|
|
|
ich2Recyle []proxy.InboundHandler
|
2016-01-22 15:25:01 +00:00
|
|
|
lastRefresh time.Time
|
|
|
|
}
|
|
|
|
|
2016-10-14 20:21:45 +00:00
|
|
|
func NewInboundDetourHandlerDynamic(space app.Space, config *InboundConnectionConfig) (*InboundDetourHandlerDynamic, error) {
|
2016-01-22 15:25:01 +00:00
|
|
|
handler := &InboundDetourHandlerDynamic{
|
|
|
|
space: space,
|
|
|
|
config: config,
|
|
|
|
portsInUse: make(map[v2net.Port]bool),
|
|
|
|
}
|
2016-10-14 20:21:45 +00:00
|
|
|
handler.ichs = make([]proxy.InboundHandler, config.GetAllocationStrategyValue().Concurrency.GetValue())
|
2016-06-03 22:38:22 +00:00
|
|
|
|
|
|
|
// To test configuration
|
2016-10-15 22:46:08 +00:00
|
|
|
ichConfig, err := config.GetTypedSettings()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-15 14:46:20 +00:00
|
|
|
ich, err := proxy.CreateInboundHandler(config.Settings.Type, space, ichConfig, &proxy.InboundHandlerMeta{
|
2016-10-17 12:35:13 +00:00
|
|
|
Address: config.GetListenOnValue(),
|
2016-08-12 21:37:21 +00:00
|
|
|
Port: 0,
|
|
|
|
Tag: config.Tag,
|
|
|
|
StreamSettings: config.StreamSettings,
|
|
|
|
AllowPassiveConnection: config.AllowPassiveConnection,
|
|
|
|
})
|
2016-06-03 22:38:22 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Point: Failed to create inbound connection handler: ", err)
|
|
|
|
return nil, err
|
2016-01-22 15:25:01 +00:00
|
|
|
}
|
2016-06-03 22:38:22 +00:00
|
|
|
ich.Close()
|
|
|
|
|
2016-01-23 13:26:49 +00:00
|
|
|
return handler, nil
|
2016-01-22 15:25:01 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *InboundDetourHandlerDynamic) pickUnusedPort() v2net.Port {
|
|
|
|
delta := int(v.config.PortRange.To) - int(v.config.PortRange.From) + 1
|
2016-01-22 15:25:01 +00:00
|
|
|
for {
|
|
|
|
r := dice.Roll(delta)
|
2016-11-27 20:39:09 +00:00
|
|
|
port := v.config.PortRange.FromPort() + v2net.Port(r)
|
|
|
|
_, used := v.portsInUse[port]
|
2016-01-22 15:25:01 +00:00
|
|
|
if !used {
|
|
|
|
return port
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
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)
|
2016-01-22 15:51:11 +00:00
|
|
|
if until < 0 {
|
|
|
|
until = 0
|
|
|
|
}
|
2016-01-25 20:24:55 +00:00
|
|
|
return ich, int(until)
|
2016-01-22 15:25:01 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *InboundDetourHandlerDynamic) Close() {
|
|
|
|
v.Lock()
|
|
|
|
defer v.Unlock()
|
|
|
|
for _, ich := range v.ichs {
|
2016-01-25 20:24:55 +00:00
|
|
|
ich.Close()
|
2016-01-22 15:25:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *InboundDetourHandlerDynamic) RecyleHandles() {
|
|
|
|
if v.ich2Recyle != nil {
|
|
|
|
for _, ich := range v.ich2Recyle {
|
2016-06-05 21:39:38 +00:00
|
|
|
if ich == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
port := ich.Port()
|
|
|
|
ich.Close()
|
2016-11-27 20:39:09 +00:00
|
|
|
delete(v.portsInUse, port)
|
2016-06-05 21:39:38 +00:00
|
|
|
}
|
2016-11-27 20:39:09 +00:00
|
|
|
v.ich2Recyle = nil
|
2016-06-05 21:39:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *InboundDetourHandlerDynamic) refresh() error {
|
|
|
|
v.lastRefresh = time.Now()
|
2016-01-23 13:26:49 +00:00
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
config := v.config
|
|
|
|
v.ich2Recyle = v.ichs
|
2016-10-14 20:21:45 +00:00
|
|
|
newIchs := make([]proxy.InboundHandler, config.GetAllocationStrategyValue().Concurrency.GetValue())
|
2016-02-23 20:50:19 +00:00
|
|
|
|
2016-07-24 11:44:29 +00:00
|
|
|
for idx := range newIchs {
|
2016-07-24 08:06:50 +00:00
|
|
|
err := retry.Timed(5, 100).On(func() error {
|
2016-11-27 20:39:09 +00:00
|
|
|
port := v.pickUnusedPort()
|
2016-10-17 12:35:13 +00:00
|
|
|
ichConfig, _ := config.GetTypedSettings()
|
2016-12-15 14:46:20 +00:00
|
|
|
ich, err := proxy.CreateInboundHandler(config.Settings.Type, v.space, ichConfig, &proxy.InboundHandlerMeta{
|
2016-10-18 08:04:15 +00:00
|
|
|
Address: config.GetListenOnValue(), Port: port, Tag: config.Tag, StreamSettings: config.StreamSettings})
|
2016-07-24 08:06:50 +00:00
|
|
|
if err != nil {
|
2016-11-27 20:39:09 +00:00
|
|
|
delete(v.portsInUse, port)
|
2016-07-24 08:06:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = ich.Start()
|
|
|
|
if err != nil {
|
2016-11-27 20:39:09 +00:00
|
|
|
delete(v.portsInUse, port)
|
2016-07-24 08:06:50 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-11-27 20:39:09 +00:00
|
|
|
v.portsInUse[port] = true
|
2016-07-24 08:06:50 +00:00
|
|
|
newIchs[idx] = ich
|
|
|
|
return nil
|
|
|
|
})
|
2016-01-22 15:25:01 +00:00
|
|
|
if err != nil {
|
2016-06-03 22:38:22 +00:00
|
|
|
log.Error("Point: Failed to create inbound connection handler: ", err)
|
|
|
|
return err
|
2016-01-22 15:25:01 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-23 13:26:49 +00:00
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
v.Lock()
|
|
|
|
v.ichs = newIchs
|
|
|
|
v.Unlock()
|
2016-03-06 15:36:03 +00:00
|
|
|
|
2016-01-22 15:25:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-01-23 22:19:11 +00:00
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *InboundDetourHandlerDynamic) Start() error {
|
|
|
|
err := v.refresh()
|
2016-01-23 22:19:11 +00:00
|
|
|
if err != nil {
|
2016-06-03 22:38:22 +00:00
|
|
|
log.Error("Point: Failed to refresh dynamic allocations: ", err)
|
2016-01-23 22:19:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2016-06-03 22:38:22 +00:00
|
|
|
for {
|
2016-11-27 20:39:09 +00:00
|
|
|
time.Sleep(time.Duration(v.config.GetAllocationStrategyValue().Refresh.GetValue())*time.Minute - 1)
|
|
|
|
v.RecyleHandles()
|
|
|
|
err := v.refresh()
|
2016-06-03 22:38:22 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Point: Failed to refresh dynamic allocations: ", err)
|
|
|
|
}
|
2016-06-05 23:31:25 +00:00
|
|
|
time.Sleep(time.Minute)
|
2016-01-23 22:19:11 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|