2017-01-26 14:46:44 -05:00
|
|
|
package inbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2018-04-11 18:10:14 -04: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"
|
2018-02-09 17:07:38 -05:00
|
|
|
"v2ray.com/core/common"
|
2017-01-26 14:46:44 -05:00
|
|
|
"v2ray.com/core/common/dice"
|
|
|
|
"v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/proxy"
|
|
|
|
)
|
|
|
|
|
2018-04-11 18:10:14 -04:00
|
|
|
func getStatCounter(v *core.Instance, tag string) (core.StatCounter, core.StatCounter) {
|
|
|
|
var uplinkCounter core.StatCounter
|
|
|
|
var downlinkCounter core.StatCounter
|
|
|
|
|
|
|
|
policy := v.PolicyManager()
|
|
|
|
stats := v.Stats()
|
|
|
|
if len(tag) > 0 && policy.ForSystem().Stats.InboundUplink {
|
|
|
|
name := "inbound>>>" + tag + ">>>traffic>>>uplink"
|
|
|
|
c, _ := core.GetOrRegisterStatCounter(stats, name)
|
|
|
|
if c != nil {
|
|
|
|
uplinkCounter = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(tag) > 0 && policy.ForSystem().Stats.InboundDownlink {
|
|
|
|
name := "inbound>>>" + tag + ">>>traffic>>>downlink"
|
|
|
|
c, _ := core.GetOrRegisterStatCounter(stats, name)
|
|
|
|
if c != nil {
|
|
|
|
downlinkCounter = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return uplinkCounter, downlinkCounter
|
|
|
|
}
|
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
type AlwaysOnInboundHandler struct {
|
2017-02-03 16:50:01 -05:00
|
|
|
proxy proxy.Inbound
|
|
|
|
workers []worker
|
2017-04-02 08:06:20 -04:00
|
|
|
mux *mux.Server
|
2018-01-10 06:22:37 -05:00
|
|
|
tag string
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewAlwaysOnInboundHandler(ctx context.Context, tag string, receiverConfig *proxyman.ReceiverConfig, proxyConfig interface{}) (*AlwaysOnInboundHandler, error) {
|
2018-02-09 17:07:38 -05:00
|
|
|
rawProxy, err := common.CreateObject(ctx, proxyConfig)
|
2017-01-26 14:46:44 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-02-09 17:07:38 -05:00
|
|
|
p, ok := rawProxy.(proxy.Inbound)
|
|
|
|
if !ok {
|
|
|
|
return nil, newError("not an inbound proxy.")
|
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
|
|
|
|
h := &AlwaysOnInboundHandler{
|
|
|
|
proxy: p,
|
2017-04-02 08:06:20 -04:00
|
|
|
mux: mux.NewServer(ctx),
|
2018-01-10 06:22:37 -05:00
|
|
|
tag: tag,
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
2018-04-11 18:10:14 -04:00
|
|
|
uplinkCounter, downlinkCounter := getStatCounter(core.MustFromContext(ctx), tag)
|
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
nl := p.Network()
|
|
|
|
pr := receiverConfig.PortRange
|
2017-01-27 17:52:29 -05:00
|
|
|
address := receiverConfig.Listen.AsAddress()
|
|
|
|
if address == nil {
|
|
|
|
address = net.AnyIP
|
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
for port := pr.From; port <= pr.To; port++ {
|
|
|
|
if nl.HasNetwork(net.Network_TCP) {
|
2017-12-27 16:25:12 -05:00
|
|
|
newError("creating stream worker on ", address, ":", port).AtDebug().WriteToLog()
|
2017-01-26 14:46:44 -05:00
|
|
|
worker := &tcpWorker{
|
2018-04-11 18:10:14 -04:00
|
|
|
address: address,
|
|
|
|
port: net.Port(port),
|
|
|
|
proxy: p,
|
|
|
|
stream: receiverConfig.StreamSettings,
|
|
|
|
recvOrigDest: receiverConfig.ReceiveOriginalDestination,
|
|
|
|
tag: tag,
|
|
|
|
dispatcher: h.mux,
|
|
|
|
sniffers: receiverConfig.DomainOverride,
|
|
|
|
uplinkCounter: uplinkCounter,
|
|
|
|
downlinkCounter: downlinkCounter,
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
h.workers = append(h.workers, worker)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nl.HasNetwork(net.Network_UDP) {
|
|
|
|
worker := &udpWorker{
|
2018-04-11 18:10:14 -04:00
|
|
|
tag: tag,
|
|
|
|
proxy: p,
|
|
|
|
address: address,
|
|
|
|
port: net.Port(port),
|
|
|
|
recvOrigDest: receiverConfig.ReceiveOriginalDestination,
|
|
|
|
dispatcher: h.mux,
|
|
|
|
uplinkCounter: uplinkCounter,
|
|
|
|
downlinkCounter: downlinkCounter,
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
h.workers = append(h.workers, worker)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return h, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *AlwaysOnInboundHandler) Start() error {
|
|
|
|
for _, worker := range h.workers {
|
|
|
|
if err := worker.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-08 09:39:46 -05:00
|
|
|
func (h *AlwaysOnInboundHandler) Close() error {
|
2017-01-26 14:46:44 -05:00
|
|
|
for _, worker := range h.workers {
|
|
|
|
worker.Close()
|
|
|
|
}
|
2018-02-09 17:07:38 -05:00
|
|
|
h.mux.Close()
|
2018-02-08 09:39:46 -05:00
|
|
|
return nil
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
func (h *AlwaysOnInboundHandler) GetRandomInboundProxy() (interface{}, net.Port, int) {
|
2017-02-13 07:08:28 -05:00
|
|
|
if len(h.workers) == 0 {
|
|
|
|
return nil, 0, 0
|
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
w := h.workers[dice.Roll(len(h.workers))]
|
|
|
|
return w.Proxy(), w.Port(), 9999
|
|
|
|
}
|
2018-01-10 06:22:37 -05:00
|
|
|
|
|
|
|
func (h *AlwaysOnInboundHandler) Tag() string {
|
|
|
|
return h.tag
|
|
|
|
}
|
2018-02-05 17:38:24 -05:00
|
|
|
|
|
|
|
func (h *AlwaysOnInboundHandler) GetInbound() proxy.Inbound {
|
|
|
|
return h.proxy
|
|
|
|
}
|