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