2017-01-26 14:46:44 -05:00
|
|
|
package inbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2018-02-08 09:39:46 -05:00
|
|
|
"v2ray.com/core"
|
2017-01-26 14:46:44 -05:00
|
|
|
"v2ray.com/core/app/proxyman"
|
2017-04-02 08:06:20 -04:00
|
|
|
"v2ray.com/core/app/proxyman/mux"
|
2017-01-26 14:46:44 -05:00
|
|
|
"v2ray.com/core/common/dice"
|
2017-08-29 06:56:57 -04:00
|
|
|
"v2ray.com/core/common/net"
|
2018-05-27 07:02:29 -04:00
|
|
|
"v2ray.com/core/common/task"
|
2017-01-26 14:46:44 -05:00
|
|
|
"v2ray.com/core/proxy"
|
2018-09-07 08:50:25 -04:00
|
|
|
"v2ray.com/core/transport/internet"
|
2017-01-26 14:46:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type DynamicInboundHandler struct {
|
|
|
|
tag string
|
2018-02-08 09:39:46 -05:00
|
|
|
v *core.Instance
|
2017-01-26 14:46:44 -05:00
|
|
|
proxyConfig interface{}
|
|
|
|
receiverConfig *proxyman.ReceiverConfig
|
2018-09-07 08:50:25 -04:00
|
|
|
streamSettings *internet.MemoryStreamConfig
|
2017-01-30 15:35:34 -05:00
|
|
|
portMutex sync.Mutex
|
2017-08-29 06:56:57 -04:00
|
|
|
portsInUse map[net.Port]bool
|
2017-01-30 15:35:34 -05:00
|
|
|
workerMutex sync.RWMutex
|
|
|
|
worker []worker
|
2017-01-26 14:46:44 -05:00
|
|
|
lastRefresh time.Time
|
2017-04-02 08:06:20 -04:00
|
|
|
mux *mux.Server
|
2018-05-27 07:02:29 -04:00
|
|
|
task *task.Periodic
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDynamicInboundHandler(ctx context.Context, tag string, receiverConfig *proxyman.ReceiverConfig, proxyConfig interface{}) (*DynamicInboundHandler, error) {
|
2018-02-21 11:05:29 -05:00
|
|
|
v := core.MustFromContext(ctx)
|
2017-01-26 14:46:44 -05:00
|
|
|
h := &DynamicInboundHandler{
|
|
|
|
tag: tag,
|
|
|
|
proxyConfig: proxyConfig,
|
|
|
|
receiverConfig: receiverConfig,
|
2017-08-29 06:56:57 -04:00
|
|
|
portsInUse: make(map[net.Port]bool),
|
2017-04-02 08:06:20 -04:00
|
|
|
mux: mux.NewServer(ctx),
|
2018-02-08 09:39:46 -05:00
|
|
|
v: v,
|
|
|
|
}
|
|
|
|
|
2018-09-07 08:50:25 -04:00
|
|
|
mss, err := internet.ToMemoryStreamConfig(receiverConfig.StreamSettings)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to parse stream settings").Base(err).AtWarning()
|
|
|
|
}
|
2018-09-17 09:12:58 -04:00
|
|
|
if receiverConfig.ReceiveOriginalDestination {
|
|
|
|
if mss.SocketSettings == nil {
|
|
|
|
mss.SocketSettings = &internet.SocketConfig{}
|
|
|
|
}
|
|
|
|
if mss.SocketSettings.Tproxy == internet.SocketConfig_Off {
|
|
|
|
mss.SocketSettings.Tproxy = internet.SocketConfig_Redirect
|
|
|
|
}
|
|
|
|
mss.SocketSettings.ReceiveOriginalDestAddress = true
|
|
|
|
}
|
2018-09-07 08:50:25 -04:00
|
|
|
|
|
|
|
h.streamSettings = mss
|
|
|
|
|
2018-05-27 07:02:29 -04:00
|
|
|
h.task = &task.Periodic{
|
2018-02-08 09:39:46 -05:00
|
|
|
Interval: time.Minute * time.Duration(h.receiverConfig.AllocationStrategy.GetRefreshValue()),
|
|
|
|
Execute: h.refresh,
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return h, nil
|
|
|
|
}
|
|
|
|
|
2017-08-29 06:56:57 -04:00
|
|
|
func (h *DynamicInboundHandler) allocatePort() net.Port {
|
2017-01-26 14:46:44 -05:00
|
|
|
from := int(h.receiverConfig.PortRange.From)
|
|
|
|
delta := int(h.receiverConfig.PortRange.To) - from + 1
|
2017-01-30 15:35:34 -05:00
|
|
|
|
|
|
|
h.portMutex.Lock()
|
|
|
|
defer h.portMutex.Unlock()
|
2017-01-26 14:46:44 -05:00
|
|
|
|
|
|
|
for {
|
|
|
|
r := dice.Roll(delta)
|
2017-08-29 06:56:57 -04:00
|
|
|
port := net.Port(from + r)
|
2017-01-26 14:46:44 -05:00
|
|
|
_, used := h.portsInUse[port]
|
|
|
|
if !used {
|
|
|
|
h.portsInUse[port] = true
|
|
|
|
return port
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 09:39:46 -05:00
|
|
|
func (h *DynamicInboundHandler) closeWorkers(workers []worker) {
|
2017-08-29 06:56:57 -04:00
|
|
|
ports2Del := make([]net.Port, len(workers))
|
2017-01-30 15:35:34 -05:00
|
|
|
for idx, worker := range workers {
|
|
|
|
ports2Del[idx] = worker.Port()
|
2018-05-31 05:55:11 -04:00
|
|
|
if err := worker.Close(); err != nil {
|
|
|
|
newError("failed to close worker").Base(err).WriteToLog()
|
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
2017-01-30 15:35:34 -05:00
|
|
|
h.portMutex.Lock()
|
2017-01-26 14:46:44 -05:00
|
|
|
for _, port := range ports2Del {
|
|
|
|
delete(h.portsInUse, port)
|
|
|
|
}
|
2017-01-30 15:35:34 -05:00
|
|
|
h.portMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DynamicInboundHandler) refresh() error {
|
|
|
|
h.lastRefresh = time.Now()
|
2017-01-26 14:46:44 -05:00
|
|
|
|
2017-01-30 15:37:50 -05:00
|
|
|
timeout := time.Minute * time.Duration(h.receiverConfig.AllocationStrategy.GetRefreshValue()) * 2
|
|
|
|
concurrency := h.receiverConfig.AllocationStrategy.GetConcurrencyValue()
|
|
|
|
workers := make([]worker, 0, concurrency)
|
2017-01-26 14:46:44 -05:00
|
|
|
|
2017-01-27 17:52:29 -05:00
|
|
|
address := h.receiverConfig.Listen.AsAddress()
|
|
|
|
if address == nil {
|
2017-08-29 06:56:57 -04:00
|
|
|
address = net.AnyIP
|
2017-01-27 17:52:29 -05:00
|
|
|
}
|
2018-04-11 18:10:14 -04:00
|
|
|
|
|
|
|
uplinkCounter, downlinkCounter := getStatCounter(h.v, h.tag)
|
|
|
|
|
2017-01-30 15:37:50 -05:00
|
|
|
for i := uint32(0); i < concurrency; i++ {
|
2017-01-26 14:46:44 -05:00
|
|
|
port := h.allocatePort()
|
2018-05-31 05:55:11 -04:00
|
|
|
rawProxy, err := core.CreateObject(h.v, h.proxyConfig)
|
2017-01-26 14:46:44 -05:00
|
|
|
if err != nil {
|
2017-12-19 15:28:12 -05:00
|
|
|
newError("failed to create proxy instance").Base(err).AtWarning().WriteToLog()
|
2017-01-26 14:46:44 -05:00
|
|
|
continue
|
|
|
|
}
|
2018-02-08 09:39:46 -05:00
|
|
|
p := rawProxy.(proxy.Inbound)
|
2017-01-26 14:46:44 -05:00
|
|
|
nl := p.Network()
|
2017-08-29 06:56:57 -04:00
|
|
|
if nl.HasNetwork(net.Network_TCP) {
|
2017-01-26 14:46:44 -05:00
|
|
|
worker := &tcpWorker{
|
2018-04-11 18:10:14 -04:00
|
|
|
tag: h.tag,
|
|
|
|
address: address,
|
|
|
|
port: port,
|
|
|
|
proxy: p,
|
2018-09-07 08:50:25 -04:00
|
|
|
stream: h.streamSettings,
|
2018-04-11 18:10:14 -04:00
|
|
|
recvOrigDest: h.receiverConfig.ReceiveOriginalDestination,
|
|
|
|
dispatcher: h.mux,
|
2018-07-16 07:47:00 -04:00
|
|
|
sniffingConfig: h.receiverConfig.GetEffectiveSniffingSettings(),
|
2018-04-11 18:10:14 -04:00
|
|
|
uplinkCounter: uplinkCounter,
|
|
|
|
downlinkCounter: downlinkCounter,
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
if err := worker.Start(); err != nil {
|
2017-12-19 15:28:12 -05:00
|
|
|
newError("failed to create TCP worker").Base(err).AtWarning().WriteToLog()
|
2017-01-30 15:35:34 -05:00
|
|
|
continue
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
2017-01-30 15:35:34 -05:00
|
|
|
workers = append(workers, worker)
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
2017-08-29 06:56:57 -04:00
|
|
|
if nl.HasNetwork(net.Network_UDP) {
|
2017-01-26 14:46:44 -05:00
|
|
|
worker := &udpWorker{
|
2018-04-11 18:10:14 -04:00
|
|
|
tag: h.tag,
|
|
|
|
proxy: p,
|
|
|
|
address: address,
|
|
|
|
port: port,
|
|
|
|
dispatcher: h.mux,
|
|
|
|
uplinkCounter: uplinkCounter,
|
|
|
|
downlinkCounter: downlinkCounter,
|
2018-09-17 09:12:58 -04:00
|
|
|
stream: h.streamSettings,
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
if err := worker.Start(); err != nil {
|
2017-12-19 15:28:12 -05:00
|
|
|
newError("failed to create UDP worker").Base(err).AtWarning().WriteToLog()
|
2017-01-30 15:35:34 -05:00
|
|
|
continue
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
2017-01-30 15:35:34 -05:00
|
|
|
workers = append(workers, worker)
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-30 15:35:34 -05:00
|
|
|
h.workerMutex.Lock()
|
|
|
|
h.worker = workers
|
|
|
|
h.workerMutex.Unlock()
|
|
|
|
|
2018-02-08 09:39:46 -05:00
|
|
|
time.AfterFunc(timeout, func() {
|
|
|
|
h.closeWorkers(workers)
|
|
|
|
})
|
2017-01-30 15:35:34 -05:00
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DynamicInboundHandler) Start() error {
|
2018-02-08 09:39:46 -05:00
|
|
|
return h.task.Start()
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
2018-02-08 09:39:46 -05:00
|
|
|
func (h *DynamicInboundHandler) Close() error {
|
|
|
|
return h.task.Close()
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
func (h *DynamicInboundHandler) GetRandomInboundProxy() (interface{}, net.Port, int) {
|
2017-01-30 15:35:34 -05:00
|
|
|
h.workerMutex.RLock()
|
|
|
|
defer h.workerMutex.RUnlock()
|
|
|
|
|
2017-02-13 07:08:28 -05:00
|
|
|
if len(h.worker) == 0 {
|
|
|
|
return nil, 0, 0
|
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
w := h.worker[dice.Roll(len(h.worker))]
|
|
|
|
expire := h.receiverConfig.AllocationStrategy.GetRefreshValue() - uint32(time.Since(h.lastRefresh)/time.Minute)
|
2017-01-30 15:35:34 -05:00
|
|
|
return w.Proxy(), w.Port(), int(expire)
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
2018-01-10 06:22:37 -05:00
|
|
|
|
|
|
|
func (h *DynamicInboundHandler) Tag() string {
|
|
|
|
return h.tag
|
|
|
|
}
|