Explorar el Código

unified dispatcher interface

Darien Raymond hace 8 años
padre
commit
43fb425fd7

+ 0 - 2
app/dispatcher/dispatcher.go

@@ -11,8 +11,6 @@ import (
 // Interface dispatch a packet and possibly further network payload to its destination.
 type Interface interface {
 	Dispatch(ctx context.Context, dest net.Destination) (ray.InboundRay, error)
-	Start() error
-	Close()
 }
 
 func FromSpace(space app.Space) Interface {

+ 0 - 2
app/dns/dns.go

@@ -9,8 +9,6 @@ import (
 // A Server is a DNS server for responding DNS queries.
 type Server interface {
 	Get(domain string) []net.IP
-	Start() error
-	Close()
 }
 
 func FromSpace(space app.Space) Server {

+ 6 - 23
app/proxyman/inbound/always.go

@@ -3,21 +3,17 @@ 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/common/errors"
 	"v2ray.com/core/common/net"
 	"v2ray.com/core/proxy"
-	"v2ray.com/core/transport/ray"
 )
 
 type AlwaysOnInboundHandler struct {
-	proxy      proxy.Inbound
-	workers    []worker
-	dispatcher dispatcher.Interface
+	proxy   proxy.Inbound
+	workers []worker
+	mux     *mux
 }
 
 func NewAlwaysOnInboundHandler(ctx context.Context, tag string, receiverConfig *proxyman.ReceiverConfig, proxyConfig interface{}) (*AlwaysOnInboundHandler, error) {
@@ -28,18 +24,9 @@ func NewAlwaysOnInboundHandler(ctx context.Context, tag string, receiverConfig *
 
 	h := &AlwaysOnInboundHandler{
 		proxy: p,
+		mux:   newMux(ctx),
 	}
 
-	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()
@@ -57,7 +44,7 @@ func NewAlwaysOnInboundHandler(ctx context.Context, tag string, receiverConfig *
 				recvOrigDest:     receiverConfig.ReceiveOriginalDestination,
 				tag:              tag,
 				allowPassiveConn: receiverConfig.AllowPassiveConnection,
-				dispatcher:       h,
+				dispatcher:       h.mux,
 			}
 			h.workers = append(h.workers, worker)
 		}
@@ -69,7 +56,7 @@ func NewAlwaysOnInboundHandler(ctx context.Context, tag string, receiverConfig *
 				address:      address,
 				port:         net.Port(port),
 				recvOrigDest: receiverConfig.ReceiveOriginalDestination,
-				dispatcher:   h,
+				dispatcher:   h.mux,
 			}
 			h.workers = append(h.workers, worker)
 		}
@@ -97,7 +84,3 @@ 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)
-}

+ 4 - 21
app/proxyman/inbound/dynamic.go

@@ -2,18 +2,14 @@ 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"
 	v2net "v2ray.com/core/common/net"
 	"v2ray.com/core/proxy"
-	"v2ray.com/core/transport/ray"
 )
 
 type DynamicInboundHandler struct {
@@ -27,7 +23,7 @@ type DynamicInboundHandler struct {
 	workerMutex    sync.RWMutex
 	worker         []worker
 	lastRefresh    time.Time
-	dispatcher     dispatcher.Interface
+	mux            *mux
 }
 
 func NewDynamicInboundHandler(ctx context.Context, tag string, receiverConfig *proxyman.ReceiverConfig, proxyConfig interface{}) (*DynamicInboundHandler, error) {
@@ -39,18 +35,9 @@ func NewDynamicInboundHandler(ctx context.Context, tag string, receiverConfig *p
 		proxyConfig:    proxyConfig,
 		receiverConfig: receiverConfig,
 		portsInUse:     make(map[v2net.Port]bool),
+		mux:            newMux(ctx),
 	}
 
-	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
 }
 
@@ -117,7 +104,7 @@ func (h *DynamicInboundHandler) refresh() error {
 				stream:           h.receiverConfig.StreamSettings,
 				recvOrigDest:     h.receiverConfig.ReceiveOriginalDestination,
 				allowPassiveConn: h.receiverConfig.AllowPassiveConnection,
-				dispatcher:       h,
+				dispatcher:       h.mux,
 			}
 			if err := worker.Start(); err != nil {
 				log.Warning("Proxyman:InboundHandler: Failed to create TCP worker: ", err)
@@ -133,7 +120,7 @@ func (h *DynamicInboundHandler) refresh() error {
 				address:      address,
 				port:         port,
 				recvOrigDest: h.receiverConfig.ReceiveOriginalDestination,
-				dispatcher:   h,
+				dispatcher:   h.mux,
 			}
 			if err := worker.Start(); err != nil {
 				log.Warning("Proxyman:InboundHandler: Failed to create UDP worker: ", err)
@@ -181,7 +168,3 @@ 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)
-}

+ 33 - 0
app/proxyman/inbound/mux.go

@@ -0,0 +1,33 @@
+package inbound
+
+import (
+	"context"
+	"errors"
+
+	"v2ray.com/core/app"
+	"v2ray.com/core/app/dispatcher"
+	"v2ray.com/core/common/net"
+	"v2ray.com/core/transport/ray"
+)
+
+type mux struct {
+	dispatcher dispatcher.Interface
+}
+
+func newMux(ctx context.Context) *mux {
+	m := &mux{}
+	space := app.SpaceFromContext(ctx)
+	space.OnInitialize(func() error {
+		d := dispatcher.FromSpace(space)
+		if d == nil {
+			return errors.New("Proxyman|DefaultInboundHandler: No dispatcher in space.")
+		}
+		m.dispatcher = d
+		return nil
+	})
+	return m
+}
+
+func (m *mux) Dispatch(ctx context.Context, dest net.Destination) (ray.InboundRay, error) {
+	return m.dispatcher.Dispatch(ctx, dest)
+}

+ 0 - 4
app/proxyman/proxyman.go

@@ -13,8 +13,6 @@ import (
 type InboundHandlerManager interface {
 	GetHandler(ctx context.Context, tag string) (InboundHandler, error)
 	AddHandler(ctx context.Context, config *InboundHandlerConfig) error
-	Start() error
-	Close()
 }
 
 type InboundHandler interface {
@@ -29,8 +27,6 @@ type OutboundHandlerManager interface {
 	GetHandler(tag string) OutboundHandler
 	GetDefaultHandler() OutboundHandler
 	AddHandler(ctx context.Context, config *OutboundHandlerConfig) error
-	Start() error
-	Close()
 }
 
 type OutboundHandler interface {