1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-08-23 12:54:16 -04:00
v2fly/shell/point/inbound_detour_dynamic.go

130 lines
3.1 KiB
Go
Raw Normal View History

package point
2016-01-22 10:25:01 -05:00
import (
"sync"
"time"
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/common/dice"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/common/retry"
"github.com/v2ray/v2ray-core/proxy"
proxyrepo "github.com/v2ray/v2ray-core/proxy/repo"
)
type InboundDetourHandlerDynamic struct {
sync.RWMutex
space app.Space
config *InboundDetourConfig
portsInUse map[v2net.Port]bool
ichInUse []proxy.InboundHandler
ich2Recycle []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-01-23 08:26:49 -05:00
ichCount := config.Allocation.Concurrency
ichArray := make([]proxy.InboundHandler, ichCount*2)
2016-02-05 16:06:27 -05:00
for idx := range ichArray {
2016-01-25 11:23:10 -05:00
ich, err := proxyrepo.CreateInboundHandler(config.Protocol, space, config.Settings)
2016-01-22 10:25:01 -05:00
if err != nil {
log.Error("Point: Failed to create inbound connection handler: ", err)
2016-01-23 08:26:49 -05:00
return nil, err
2016-01-22 10:25:01 -05:00
}
ichArray[idx] = ich
2016-01-22 10:25:01 -05:00
}
2016-01-23 08:26:49 -05:00
handler.ichInUse = ichArray[:ichCount]
handler.ich2Recycle = ichArray[ichCount:]
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
}
}
}
func (this *InboundDetourHandlerDynamic) GetConnectionHandler() (proxy.InboundHandler, int) {
2016-01-22 10:25:01 -05:00
this.RLock()
defer this.RUnlock()
ich := this.ichInUse[dice.Roll(len(this.ichInUse))]
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
}
return ich, int(until)
2016-01-22 10:25:01 -05:00
}
func (this *InboundDetourHandlerDynamic) Close() {
this.Lock()
defer this.Unlock()
for _, ich := range this.ichInUse {
ich.Close()
2016-01-22 10:25:01 -05:00
}
if this.ich2Recycle != nil {
for _, ich := range this.ich2Recycle {
if ich != nil {
ich.Close()
2016-01-22 10:25:01 -05:00
}
}
}
}
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-03-06 10:36:03 -05:00
for _, ich := range this.ich2Recycle {
port2Delete := ich.Port()
2016-02-23 15:50:19 -05:00
ich.Close()
2016-02-27 15:53:06 -05:00
err := retry.Timed(100 /* times */, 1000 /* ms */).On(func() error {
2016-03-06 10:36:03 -05:00
port := this.pickUnusedPort()
2016-05-29 10:37:52 -04:00
err := ich.Listen(this.config.ListenOn, port)
2016-01-22 10:25:01 -05:00
if err != nil {
log.Error("Point: Failed to start inbound detour on port ", port, ": ", err)
2016-01-22 10:25:01 -05:00
return err
}
2016-03-06 10:36:03 -05:00
this.portsInUse[port] = true
2016-01-22 10:25:01 -05:00
return nil
})
if err != nil {
2016-03-06 10:36:03 -05:00
continue
2016-01-22 10:25:01 -05:00
}
2016-03-06 10:36:03 -05:00
delete(this.portsInUse, port2Delete)
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()
this.ich2Recycle, this.ichInUse = this.ichInUse, this.ich2Recycle
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 {
return err
}
go func() {
for range time.Tick(time.Duration(this.config.Allocation.Refresh) * time.Minute) {
this.refresh()
}
}()
return nil
}