From 84a7a2ad610fe1dfffdcda1a188628bfbf495bc8 Mon Sep 17 00:00:00 2001 From: v2ray Date: Sat, 23 Jul 2016 11:09:49 +0200 Subject: [PATCH] correct initialization in socks server --- proxy/socks/server.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/proxy/socks/server.go b/proxy/socks/server.go index a39d00746..2f50deef5 100644 --- a/proxy/socks/server.go +++ b/proxy/socks/server.go @@ -38,12 +38,20 @@ type Server struct { } // NewServer creates a new Server object. -func NewServer(config *Config, packetDispatcher dispatcher.PacketDispatcher, meta *proxy.InboundHandlerMeta) *Server { - return &Server{ - config: config, - packetDispatcher: packetDispatcher, - meta: meta, +func NewServer(config *Config, space app.Space, meta *proxy.InboundHandlerMeta) *Server { + s := &Server{ + config: config, + meta: meta, } + space.InitializeApplication(func() error { + if !space.HasApp(dispatcher.APP_ID) { + log.Error("Socks|Server: Dispatcher is not found in the space.") + return app.ErrMissingApplication + } + s.packetDispatcher = space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher) + return nil + }) + return s } // Port implements InboundHandler.Port(). @@ -310,13 +318,7 @@ func (this *ServerFactory) StreamCapability() internet.StreamConnectionType { } func (this *ServerFactory) Create(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) { - if !space.HasApp(dispatcher.APP_ID) { - return nil, internal.ErrBadConfiguration - } - return NewServer( - rawConfig.(*Config), - space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher), - meta), nil + return NewServer(rawConfig.(*Config), space, meta), nil } func init() {