From 43abfc9463ed38a2f642d61f264241ee59427bde Mon Sep 17 00:00:00 2001 From: Shelikhoo Date: Sun, 11 Mar 2018 13:44:21 +0800 Subject: [PATCH] Unix listeners(sync commit) --- app/proxyman/inbound/inbound.go | 3 +- app/proxyman/inbound/unix.go | 65 +++++++++++++++++++++ transport/internet/domainsocket/listener.go | 8 ++- 3 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 app/proxyman/inbound/unix.go diff --git a/app/proxyman/inbound/inbound.go b/app/proxyman/inbound/inbound.go index e7ed577cf..9d7b42930 100644 --- a/app/proxyman/inbound/inbound.go +++ b/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 } diff --git a/app/proxyman/inbound/unix.go b/app/proxyman/inbound/unix.go new file mode 100644 index 000000000..125b66022 --- /dev/null +++ b/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 + +} diff --git a/transport/internet/domainsocket/listener.go b/transport/internet/domainsocket/listener.go index ebeff7d9b..df58d1f06 100644 --- a/transport/internet/domainsocket/listener.go +++ b/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() +}