2017-01-26 19:46:44 +00:00
|
|
|
package inbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2017-02-03 21:35:09 +00:00
|
|
|
"v2ray.com/core/app/log"
|
2017-01-26 19:46:44 +00:00
|
|
|
"v2ray.com/core/app/proxyman"
|
2017-04-02 12:06:20 +00:00
|
|
|
"v2ray.com/core/app/proxyman/mux"
|
2017-01-26 19:46:44 +00:00
|
|
|
"v2ray.com/core/common/dice"
|
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/proxy"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DynamicInboundHandler struct {
|
|
|
|
tag string
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
proxyConfig interface{}
|
|
|
|
receiverConfig *proxyman.ReceiverConfig
|
2017-01-30 20:35:34 +00:00
|
|
|
portMutex sync.Mutex
|
2017-01-26 19:46:44 +00:00
|
|
|
portsInUse map[v2net.Port]bool
|
2017-01-30 20:35:34 +00:00
|
|
|
workerMutex sync.RWMutex
|
|
|
|
worker []worker
|
2017-01-26 19:46:44 +00:00
|
|
|
lastRefresh time.Time
|
2017-04-02 12:06:20 +00:00
|
|
|
mux *mux.Server
|
2017-01-26 19:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDynamicInboundHandler(ctx context.Context, tag string, receiverConfig *proxyman.ReceiverConfig, proxyConfig interface{}) (*DynamicInboundHandler, error) {
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
h := &DynamicInboundHandler{
|
|
|
|
ctx: ctx,
|
|
|
|
tag: tag,
|
|
|
|
cancel: cancel,
|
|
|
|
proxyConfig: proxyConfig,
|
|
|
|
receiverConfig: receiverConfig,
|
|
|
|
portsInUse: make(map[v2net.Port]bool),
|
2017-04-02 12:06:20 +00:00
|
|
|
mux: mux.NewServer(ctx),
|
2017-01-26 19:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return h, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DynamicInboundHandler) allocatePort() v2net.Port {
|
|
|
|
from := int(h.receiverConfig.PortRange.From)
|
|
|
|
delta := int(h.receiverConfig.PortRange.To) - from + 1
|
2017-01-30 20:35:34 +00:00
|
|
|
|
|
|
|
h.portMutex.Lock()
|
|
|
|
defer h.portMutex.Unlock()
|
2017-01-26 19:46:44 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
r := dice.Roll(delta)
|
|
|
|
port := v2net.Port(from + r)
|
|
|
|
_, used := h.portsInUse[port]
|
|
|
|
if !used {
|
|
|
|
h.portsInUse[port] = true
|
|
|
|
return port
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-30 20:35:34 +00:00
|
|
|
func (h *DynamicInboundHandler) waitAnyCloseWorkers(ctx context.Context, cancel context.CancelFunc, workers []worker, duration time.Duration) {
|
|
|
|
time.Sleep(duration)
|
|
|
|
cancel()
|
|
|
|
ports2Del := make([]v2net.Port, len(workers))
|
|
|
|
for idx, worker := range workers {
|
|
|
|
ports2Del[idx] = worker.Port()
|
2017-01-26 19:46:44 +00:00
|
|
|
worker.Close()
|
|
|
|
}
|
|
|
|
|
2017-01-30 20:35:34 +00:00
|
|
|
h.portMutex.Lock()
|
2017-01-26 19:46:44 +00:00
|
|
|
for _, port := range ports2Del {
|
|
|
|
delete(h.portsInUse, port)
|
|
|
|
}
|
2017-01-30 20:35:34 +00:00
|
|
|
h.portMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DynamicInboundHandler) refresh() error {
|
|
|
|
h.lastRefresh = time.Now()
|
2017-01-26 19:46:44 +00:00
|
|
|
|
2017-01-30 20:37:50 +00:00
|
|
|
timeout := time.Minute * time.Duration(h.receiverConfig.AllocationStrategy.GetRefreshValue()) * 2
|
|
|
|
concurrency := h.receiverConfig.AllocationStrategy.GetConcurrencyValue()
|
2017-01-30 20:35:34 +00:00
|
|
|
ctx, cancel := context.WithTimeout(h.ctx, timeout)
|
2017-01-30 20:37:50 +00:00
|
|
|
workers := make([]worker, 0, concurrency)
|
2017-01-26 19:46:44 +00:00
|
|
|
|
2017-01-27 22:52:29 +00:00
|
|
|
address := h.receiverConfig.Listen.AsAddress()
|
|
|
|
if address == nil {
|
|
|
|
address = v2net.AnyIP
|
|
|
|
}
|
2017-01-30 20:37:50 +00:00
|
|
|
for i := uint32(0); i < concurrency; i++ {
|
2017-01-26 19:46:44 +00:00
|
|
|
port := h.allocatePort()
|
2017-01-29 11:58:52 +00:00
|
|
|
p, err := proxy.CreateInboundHandler(ctx, h.proxyConfig)
|
2017-01-26 19:46:44 +00:00
|
|
|
if err != nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
log.Trace(newError("failed to create proxy instance").Base(err).AtWarning())
|
2017-01-26 19:46:44 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
nl := p.Network()
|
|
|
|
if nl.HasNetwork(v2net.Network_TCP) {
|
|
|
|
worker := &tcpWorker{
|
2017-02-07 16:37:26 +00:00
|
|
|
tag: h.tag,
|
|
|
|
address: address,
|
|
|
|
port: port,
|
|
|
|
proxy: p,
|
|
|
|
stream: h.receiverConfig.StreamSettings,
|
|
|
|
recvOrigDest: h.receiverConfig.ReceiveOriginalDestination,
|
|
|
|
dispatcher: h.mux,
|
2017-01-26 19:46:44 +00:00
|
|
|
}
|
|
|
|
if err := worker.Start(); err != nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
log.Trace(newError("failed to create TCP worker").Base(err).AtWarning())
|
2017-01-30 20:35:34 +00:00
|
|
|
continue
|
2017-01-26 19:46:44 +00:00
|
|
|
}
|
2017-01-30 20:35:34 +00:00
|
|
|
workers = append(workers, worker)
|
2017-01-26 19:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if nl.HasNetwork(v2net.Network_UDP) {
|
|
|
|
worker := &udpWorker{
|
|
|
|
tag: h.tag,
|
|
|
|
proxy: p,
|
2017-01-27 22:52:29 +00:00
|
|
|
address: address,
|
2017-01-26 19:46:44 +00:00
|
|
|
port: port,
|
|
|
|
recvOrigDest: h.receiverConfig.ReceiveOriginalDestination,
|
2017-02-03 21:50:01 +00:00
|
|
|
dispatcher: h.mux,
|
2017-01-26 19:46:44 +00:00
|
|
|
}
|
|
|
|
if err := worker.Start(); err != nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
log.Trace(newError("failed to create UDP worker").Base(err).AtWarning())
|
2017-01-30 20:35:34 +00:00
|
|
|
continue
|
2017-01-26 19:46:44 +00:00
|
|
|
}
|
2017-01-30 20:35:34 +00:00
|
|
|
workers = append(workers, worker)
|
2017-01-26 19:46:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-30 20:35:34 +00:00
|
|
|
h.workerMutex.Lock()
|
|
|
|
h.worker = workers
|
|
|
|
h.workerMutex.Unlock()
|
|
|
|
|
|
|
|
go h.waitAnyCloseWorkers(ctx, cancel, workers, timeout)
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DynamicInboundHandler) monitor() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-h.ctx.Done():
|
|
|
|
return
|
|
|
|
case <-time.After(time.Minute * time.Duration(h.receiverConfig.AllocationStrategy.GetRefreshValue())):
|
|
|
|
h.refresh()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DynamicInboundHandler) Start() error {
|
|
|
|
err := h.refresh()
|
|
|
|
go h.monitor()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DynamicInboundHandler) Close() {
|
|
|
|
h.cancel()
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:57:18 +00:00
|
|
|
func (h *DynamicInboundHandler) GetRandomInboundProxy() (proxy.Inbound, v2net.Port, int) {
|
2017-01-30 20:35:34 +00:00
|
|
|
h.workerMutex.RLock()
|
|
|
|
defer h.workerMutex.RUnlock()
|
|
|
|
|
2017-02-13 12:08:28 +00:00
|
|
|
if len(h.worker) == 0 {
|
|
|
|
return nil, 0, 0
|
|
|
|
}
|
2017-01-26 19:46:44 +00:00
|
|
|
w := h.worker[dice.Roll(len(h.worker))]
|
|
|
|
expire := h.receiverConfig.AllocationStrategy.GetRefreshValue() - uint32(time.Since(h.lastRefresh)/time.Minute)
|
2017-01-30 20:35:34 +00:00
|
|
|
return w.Proxy(), w.Port(), int(expire)
|
2017-01-26 19:46:44 +00:00
|
|
|
}
|