Bläddra i källkod

Unix listeners(sync commit)

Shelikhoo 7 år sedan
förälder
incheckning
43abfc9463

+ 1 - 2
app/proxyman/inbound/inbound.go

@@ -143,8 +143,7 @@ func NewHandler(ctx context.Context, config *core.InboundHandlerConfig) (core.In
 	return nil, newError("unknown allocation strategy: ", receiverSettings.AllocationStrategy.Type).AtError()
 }
 
-func (m *Manager) asUnixReceiverConfig(ctx context.Context, config *core.InboundHandlerConfig) {
-
+func (m *Manager) asUnixReceiverConfig(ctx context.Context, config *core.InboundHandlerConfig, unixrx proxyman.UnixReceiverConfig) {
 	return
 }
 

+ 65 - 0
app/proxyman/inbound/unix.go

@@ -0,0 +1,65 @@
+package inbound
+
+import (
+	"context"
+
+	"v2ray.com/core/app/proxyman"
+	"v2ray.com/core/app/proxyman/mux"
+	"v2ray.com/core/common"
+	"v2ray.com/core/common/net"
+	"v2ray.com/core/proxy"
+	"v2ray.com/core/transport/internet/domainsocket"
+)
+
+type UnixInboundHandler struct {
+	tag            string
+	listenerHolder *domainsocket.Listener
+	ctx            context.Context
+	path           string
+	proxy          proxy.Inbound
+	mux            *mux.Server
+}
+
+func (uih *UnixInboundHandler) Start() {
+	var err error
+	uih.listenerHolder, err = domainsocket.ListenDS(uih.ctx, uih.path)
+	newError(err).AtError().WriteToLog()
+}
+func (uih *UnixInboundHandler) Close() {
+	if uih.listenerHolder != nil {
+		uih.listenerHolder.Down()
+	} else {
+		newError("Called UnixInboundHandler.Close while listenerHolder is nil").AtDebug().WriteToLog()
+	}
+
+}
+func (uih *UnixInboundHandler) Tag() string {
+	return uih.tag
+}
+
+func (uih *UnixInboundHandler) GetRandomInboundProxy() (interface{}, net.Port, int) {
+	//It makes bo sense to support it
+	return nil, 0, 0
+}
+
+func NewUnixInboundHandler(ctx context.Context, tag string, receiverConfig *proxyman.UnixReceiverConfig, proxyConfig interface{}) (*UnixInboundHandler, error) {
+	rawProxy, err := common.CreateObject(ctx, proxyConfig)
+	if err != nil {
+		return nil, err
+	}
+	p, ok := rawProxy.(proxy.Inbound)
+	if !ok {
+		return nil, newError("not an inbound proxy.")
+	}
+
+	h := &UnixInboundHandler{
+		proxy: p,
+		mux:   mux.NewServer(ctx),
+		tag:   tag,
+		ctx:   ctx,
+		path:  receiverConfig.DomainSockSettings.GetPath(),
+	}
+
+	return h, nil
+
+}

+ 6 - 2
transport/internet/domainsocket/listener.go

@@ -12,11 +12,15 @@ type Listener struct {
 func ListenDS(ctx context.Context, path string) (*Listener, error) {
 	addr := new(net.UnixAddr)
 	addr.Name = path
-	addr.Net = "unixpacket"
-	li, err := net.ListenUnix("unixpacket", addr)
+	addr.Net = "unix"
+	li, err := net.ListenUnix("unix", addr)
 	if err != nil {
 		return nil, err
 	}
 	vln := &Listener{ln: li}
 	return vln, nil
 }
+
+func (ls *Listener) Down() {
+	ls.ln.Close()
+}