mirror of
https://github.com/v2ray/v2ray-core.git
synced 2026-11-03 00:22:05 +00:00
simplify dialer and dispatcher parameters
This commit is contained in:
19 files changed
+152
-147
No files matched your search
@@ -3,16 +3,21 @@ package inbound
|
||||
import (
|
||||
"context"
|
||||
|
||||
"v2ray.com/core/app"
|
||||
"v2ray.com/core/app/dispatcher"
|
||||
"v2ray.com/core/app/log"
|
||||
"v2ray.com/core/app/proxyman"
|
||||
"v2ray.com/core/common/dice"
|
||||
"v2ray.com/core/app/log"
|
||||
"v2ray.com/core/common/errors"
|
||||
"v2ray.com/core/common/net"
|
||||
"v2ray.com/core/proxy"
|
||||
"v2ray.com/core/transport/ray"
|
||||
)
|
||||
|
||||
type AlwaysOnInboundHandler struct {
|
||||
proxy proxy.Inbound
|
||||
workers []worker
|
||||
proxy proxy.Inbound
|
||||
workers []worker
|
||||
dispatcher dispatcher.Interface
|
||||
}
|
||||
|
||||
func NewAlwaysOnInboundHandler(ctx context.Context, tag string, receiverConfig *proxyman.ReceiverConfig, proxyConfig interface{}) (*AlwaysOnInboundHandler, error) {
|
||||
@@ -25,6 +30,16 @@ func NewAlwaysOnInboundHandler(ctx context.Context, tag string, receiverConfig *
|
||||
proxy: p,
|
||||
}
|
||||
|
||||
space := app.SpaceFromContext(ctx)
|
||||
space.OnInitialize(func() error {
|
||||
d := dispatcher.FromSpace(space)
|
||||
if d == nil {
|
||||
return errors.New("Proxyman|DefaultInboundHandler: No dispatcher in space.")
|
||||
}
|
||||
h.dispatcher = d
|
||||
return nil
|
||||
})
|
||||
|
||||
nl := p.Network()
|
||||
pr := receiverConfig.PortRange
|
||||
address := receiverConfig.Listen.AsAddress()
|
||||
@@ -42,6 +57,7 @@ func NewAlwaysOnInboundHandler(ctx context.Context, tag string, receiverConfig *
|
||||
recvOrigDest: receiverConfig.ReceiveOriginalDestination,
|
||||
tag: tag,
|
||||
allowPassiveConn: receiverConfig.AllowPassiveConnection,
|
||||
dispatcher: h,
|
||||
}
|
||||
h.workers = append(h.workers, worker)
|
||||
}
|
||||
@@ -53,6 +69,7 @@ func NewAlwaysOnInboundHandler(ctx context.Context, tag string, receiverConfig *
|
||||
address: address,
|
||||
port: net.Port(port),
|
||||
recvOrigDest: receiverConfig.ReceiveOriginalDestination,
|
||||
dispatcher: h,
|
||||
}
|
||||
h.workers = append(h.workers, worker)
|
||||
}
|
||||
@@ -80,3 +97,7 @@ func (h *AlwaysOnInboundHandler) GetRandomInboundProxy() (proxy.Inbound, net.Por
|
||||
w := h.workers[dice.Roll(len(h.workers))]
|
||||
return w.Proxy(), w.Port(), 9999
|
||||
}
|
||||
|
||||
func (h *AlwaysOnInboundHandler) Dispatch(ctx context.Context, dest net.Destination) (ray.InboundRay, error) {
|
||||
return h.dispatcher.Dispatch(ctx, dest)
|
||||
}
|
||||
@@ -2,14 +2,18 @@ package inbound
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"v2ray.com/core/app"
|
||||
"v2ray.com/core/app/dispatcher"
|
||||
"v2ray.com/core/app/log"
|
||||
"v2ray.com/core/app/proxyman"
|
||||
"v2ray.com/core/common/dice"
|
||||
"v2ray.com/core/app/log"
|
||||
v2net "v2ray.com/core/common/net"
|
||||
"v2ray.com/core/proxy"
|
||||
"v2ray.com/core/transport/ray"
|
||||
)
|
||||
|
||||
type DynamicInboundHandler struct {
|
||||
@@ -23,6 +27,7 @@ type DynamicInboundHandler struct {
|
||||
workerMutex sync.RWMutex
|
||||
worker []worker
|
||||
lastRefresh time.Time
|
||||
dispatcher dispatcher.Interface
|
||||
}
|
||||
|
||||
func NewDynamicInboundHandler(ctx context.Context, tag string, receiverConfig *proxyman.ReceiverConfig, proxyConfig interface{}) (*DynamicInboundHandler, error) {
|
||||
@@ -36,6 +41,16 @@ func NewDynamicInboundHandler(ctx context.Context, tag string, receiverConfig *p
|
||||
portsInUse: make(map[v2net.Port]bool),
|
||||
}
|
||||
|
||||
space := app.SpaceFromContext(ctx)
|
||||
space.OnInitialize(func() error {
|
||||
d := dispatcher.FromSpace(space)
|
||||
if d == nil {
|
||||
return errors.New("Proxyman|DefaultInboundHandler: No dispatcher in space.")
|
||||
}
|
||||
h.dispatcher = d
|
||||
return nil
|
||||
})
|
||||
|
||||
return h, nil
|
||||
}
|
||||
|
||||
@@ -102,6 +117,7 @@ func (h *DynamicInboundHandler) refresh() error {
|
||||
stream: h.receiverConfig.StreamSettings,
|
||||
recvOrigDest: h.receiverConfig.ReceiveOriginalDestination,
|
||||
allowPassiveConn: h.receiverConfig.AllowPassiveConnection,
|
||||
dispatcher: h,
|
||||
}
|
||||
if err := worker.Start(); err != nil {
|
||||
log.Warning("Proxyman:InboundHandler: Failed to create TCP worker: ", err)
|
||||
@@ -117,6 +133,7 @@ func (h *DynamicInboundHandler) refresh() error {
|
||||
address: address,
|
||||
port: port,
|
||||
recvOrigDest: h.receiverConfig.ReceiveOriginalDestination,
|
||||
dispatcher: h,
|
||||
}
|
||||
if err := worker.Start(); err != nil {
|
||||
log.Warning("Proxyman:InboundHandler: Failed to create UDP worker: ", err)
|
||||
@@ -164,3 +181,7 @@ func (h *DynamicInboundHandler) GetRandomInboundProxy() (proxy.Inbound, v2net.Po
|
||||
expire := h.receiverConfig.AllocationStrategy.GetRefreshValue() - uint32(time.Since(h.lastRefresh)/time.Minute)
|
||||
return w.Proxy(), w.Port(), int(expire)
|
||||
}
|
||||
|
||||
func (h *DynamicInboundHandler) Dispatch(ctx context.Context, dest v2net.Destination) (ray.InboundRay, error) {
|
||||
return h.dispatcher.Dispatch(ctx, dest)
|
||||
}
|
||||
@@ -8,6 +8,8 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"v2ray.com/core/app/dispatcher"
|
||||
"v2ray.com/core/app/log"
|
||||
"v2ray.com/core/common/buf"
|
||||
v2net "v2ray.com/core/common/net"
|
||||
"v2ray.com/core/proxy"
|
||||
@@ -31,6 +33,7 @@ type tcpWorker struct {
|
||||
recvOrigDest bool
|
||||
tag string
|
||||
allowPassiveConn bool
|
||||
dispatcher dispatcher.Interface
|
||||
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
@@ -51,7 +54,9 @@ func (w *tcpWorker) callback(conn internet.Connection) {
|
||||
ctx = proxy.ContextWithAllowPassiveConnection(ctx, w.allowPassiveConn)
|
||||
ctx = proxy.ContextWithInboundDestination(ctx, v2net.TCPDestination(w.address, w.port))
|
||||
ctx = proxy.ContextWithSource(ctx, v2net.DestinationFromAddr(conn.RemoteAddr()))
|
||||
w.proxy.Process(ctx, v2net.Network_TCP, conn)
|
||||
if err := w.proxy.Process(ctx, v2net.Network_TCP, conn, w.dispatcher); err != nil {
|
||||
log.Info("Proxyman|TCPWorker: Connection ends with ", err)
|
||||
}
|
||||
cancel()
|
||||
conn.Close()
|
||||
}
|
||||
@@ -151,6 +156,7 @@ type udpWorker struct {
|
||||
port v2net.Port
|
||||
recvOrigDest bool
|
||||
tag string
|
||||
dispatcher dispatcher.Interface
|
||||
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
@@ -206,7 +212,9 @@ func (w *udpWorker) callback(b *buf.Buffer, source v2net.Destination, originalDe
|
||||
}
|
||||
ctx = proxy.ContextWithSource(ctx, source)
|
||||
ctx = proxy.ContextWithInboundDestination(ctx, v2net.UDPDestination(w.address, w.port))
|
||||
w.proxy.Process(ctx, v2net.Network_UDP, conn)
|
||||
if err := w.proxy.Process(ctx, v2net.Network_UDP, conn, w.dispatcher); err != nil {
|
||||
log.Info("Proxyman|UDPWorker: Connection ends with ", err)
|
||||
}
|
||||
w.removeConn(source)
|
||||
cancel()
|
||||
}()
|
||||
|
||||
@@ -7,10 +7,10 @@ import (
|
||||
"time"
|
||||
|
||||
"v2ray.com/core/app"
|
||||
"v2ray.com/core/app/log"
|
||||
"v2ray.com/core/app/proxyman"
|
||||
"v2ray.com/core/common/buf"
|
||||
"v2ray.com/core/common/errors"
|
||||
"v2ray.com/core/app/log"
|
||||
v2net "v2ray.com/core/common/net"
|
||||
"v2ray.com/core/proxy"
|
||||
"v2ray.com/core/transport/internet"
|
||||
@@ -64,8 +64,7 @@ func NewHandler(ctx context.Context, config *proxyman.OutboundHandlerConfig) (*H
|
||||
}
|
||||
|
||||
func (h *Handler) Dispatch(ctx context.Context, outboundRay ray.OutboundRay) {
|
||||
ctx = proxy.ContextWithDialer(ctx, h)
|
||||
err := h.proxy.Process(ctx, outboundRay)
|
||||
err := h.proxy.Process(ctx, outboundRay, h)
|
||||
// Ensure outbound ray is properly closed.
|
||||
if err != nil && errors.Cause(err) != io.EOF {
|
||||
outboundRay.OutboundOutput().CloseError()
|
||||
|
||||
Reference in new issue
Block a user