浏览代码

simplify connection handler registration

v2ray 9 年之前
父节点
当前提交
5b1854f842

+ 4 - 5
proxy/blackhole/blackhole.go

@@ -31,9 +31,8 @@ func (this *BlackHole) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) e
 }
 
 func init() {
-	if err := internal.RegisterOutboundConnectionHandlerFactory("blackhole", func(space app.Space, config interface{}) (proxy.OutboundConnectionHandler, error) {
-		return NewBlackHole(), nil
-	}); err != nil {
-		panic(err)
-	}
+	internal.MustRegisterOutboundConnectionHandlerCreator("blackhole",
+		func(space app.Space, config interface{}) (proxy.OutboundConnectionHandler, error) {
+			return NewBlackHole(), nil
+		})
 }

+ 5 - 6
proxy/dokodemo/dokodemo_factory.go

@@ -7,10 +7,9 @@ import (
 )
 
 func init() {
-	if err := internal.RegisterInboundConnectionHandlerFactory("dokodemo-door", func(space app.Space, rawConfig interface{}) (proxy.InboundConnectionHandler, error) {
-		config := rawConfig.(Config)
-		return NewDokodemoDoor(space, config), nil
-	}); err != nil {
-		panic(err)
-	}
+	internal.MustRegisterInboundConnectionHandlerCreator("dokodemo-door",
+		func(space app.Space, rawConfig interface{}) (proxy.InboundConnectionHandler, error) {
+			config := rawConfig.(Config)
+			return NewDokodemoDoor(space, config), nil
+		})
 }

+ 5 - 4
proxy/freedom/freedom_test.go

@@ -49,10 +49,11 @@ func TestUDPSend(t *testing.T) {
 		ConnOutput: connOutput,
 	}
 
-	protocol, err := proxytesting.RegisterInboundConnectionHandlerCreator("mock_ich", func(space app.Space, config interface{}) (v2proxy.InboundConnectionHandler, error) {
-		ich.Space = space
-		return ich, nil
-	})
+	protocol, err := proxytesting.RegisterInboundConnectionHandlerCreator("mock_ich",
+		func(space app.Space, config interface{}) (v2proxy.InboundConnectionHandler, error) {
+			ich.Space = space
+			return ich, nil
+		})
 	assert.Error(err).IsNil()
 
 	pointPort := v2nettesting.PickPort()

+ 4 - 5
proxy/freedom/freedomfactory.go

@@ -7,9 +7,8 @@ import (
 )
 
 func init() {
-	if err := internal.RegisterOutboundConnectionHandlerFactory("freedom", func(space app.Space, config interface{}) (proxy.OutboundConnectionHandler, error) {
-		return &FreedomConnection{space: space}, nil
-	}); err != nil {
-		panic(err)
-	}
+	internal.MustRegisterOutboundConnectionHandlerCreator("freedom",
+		func(space app.Space, config interface{}) (proxy.OutboundConnectionHandler, error) {
+			return &FreedomConnection{space: space}, nil
+		})
 }

+ 4 - 5
proxy/http/http_factory.go

@@ -7,9 +7,8 @@ import (
 )
 
 func init() {
-	if err := internal.RegisterInboundConnectionHandlerFactory("http", func(space app.Space, rawConfig interface{}) (proxy.InboundConnectionHandler, error) {
-		return NewHttpProxyServer(space, rawConfig.(Config)), nil
-	}); err != nil {
-		panic(err)
-	}
+	internal.MustRegisterInboundConnectionHandlerCreator("http",
+		func(space app.Space, rawConfig interface{}) (proxy.InboundConnectionHandler, error) {
+			return NewHttpProxyServer(space, rawConfig.(Config)), nil
+		})
 }

+ 12 - 0
proxy/internal/handler_cache.go

@@ -25,6 +25,12 @@ func RegisterInboundConnectionHandlerFactory(name string, creator InboundConnect
 	return nil
 }
 
+func MustRegisterInboundConnectionHandlerCreator(name string, creator InboundConnectionHandlerCreator) {
+	if err := RegisterInboundConnectionHandlerFactory(name, creator); err != nil {
+		panic(err)
+	}
+}
+
 func RegisterOutboundConnectionHandlerFactory(name string, creator OutboundConnectionHandlerCreator) error {
 	if _, found := outboundFactories[name]; found {
 		return ErrorNameExists
@@ -33,6 +39,12 @@ func RegisterOutboundConnectionHandlerFactory(name string, creator OutboundConne
 	return nil
 }
 
+func MustRegisterOutboundConnectionHandlerCreator(name string, creator OutboundConnectionHandlerCreator) {
+	if err := RegisterOutboundConnectionHandlerFactory(name, creator); err != nil {
+		panic(err)
+	}
+}
+
 func CreateInboundConnectionHandler(name string, space app.Space, rawConfig []byte) (proxy.InboundConnectionHandler, error) {
 	creator, found := inboundFactories[name]
 	if !found {

+ 4 - 3
proxy/socks/socks_test.go

@@ -249,9 +249,10 @@ func TestSocksUdpSend(t *testing.T) {
 		ConnOutput: connOutput,
 	}
 
-	protocol, err := proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och", func(space app.Space, config interface{}) (v2proxy.OutboundConnectionHandler, error) {
-		return och, nil
-	})
+	protocol, err := proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och",
+		func(space app.Space, config interface{}) (v2proxy.OutboundConnectionHandler, error) {
+			return och, nil
+		})
 	assert.Error(err).IsNil()
 
 	config := mocks.Config{

+ 4 - 5
proxy/socks/socksfactory.go

@@ -7,9 +7,8 @@ import (
 )
 
 func init() {
-	if err := internal.RegisterInboundConnectionHandlerFactory("socks", func(space app.Space, rawConfig interface{}) (proxy.InboundConnectionHandler, error) {
-		return NewSocksServer(space, rawConfig.(Config)), nil
-	}); err != nil {
-		panic(err)
-	}
+	internal.MustRegisterInboundConnectionHandlerCreator("socks",
+		func(space app.Space, rawConfig interface{}) (proxy.InboundConnectionHandler, error) {
+			return NewSocksServer(space, rawConfig.(Config)), nil
+		})
 }

+ 9 - 10
proxy/vmess/inbound/inbound.go

@@ -161,16 +161,15 @@ func handleOutput(request *protocol.VMessRequest, writer io.Writer, output <-cha
 }
 
 func init() {
-	if err := internal.RegisterInboundConnectionHandlerFactory("vmess", func(space app.Space, rawConfig interface{}) (proxy.InboundConnectionHandler, error) {
-		config := rawConfig.(Config)
+	internal.MustRegisterInboundConnectionHandlerCreator("vmess",
+		func(space app.Space, rawConfig interface{}) (proxy.InboundConnectionHandler, error) {
+			config := rawConfig.(Config)
 
-		allowedClients := user.NewTimedUserSet()
-		for _, user := range config.AllowedUsers() {
-			allowedClients.AddUser(user)
-		}
+			allowedClients := user.NewTimedUserSet()
+			for _, user := range config.AllowedUsers() {
+				allowedClients.AddUser(user)
+			}
 
-		return NewVMessInboundHandler(space, allowedClients), nil
-	}); err != nil {
-		panic(err)
-	}
+			return NewVMessInboundHandler(space, allowedClients), nil
+		})
 }

+ 8 - 9
proxy/vmess/outbound/outbound.go

@@ -193,13 +193,12 @@ func handleResponse(conn net.Conn, request *protocol.VMessRequest, output chan<-
 }
 
 func init() {
-	if err := internal.RegisterOutboundConnectionHandlerFactory("vmess", func(space app.Space, rawConfig interface{}) (proxy.OutboundConnectionHandler, error) {
-		vOutConfig := rawConfig.(Config)
-		return &VMessOutboundHandler{
-			space:           space,
-			receiverManager: NewReceiverManager(vOutConfig.Receivers()),
-		}, nil
-	}); err != nil {
-		panic(err)
-	}
+	internal.MustRegisterOutboundConnectionHandlerCreator("vmess",
+		func(space app.Space, rawConfig interface{}) (proxy.OutboundConnectionHandler, error) {
+			vOutConfig := rawConfig.(Config)
+			return &VMessOutboundHandler{
+				space:           space,
+				receiverManager: NewReceiverManager(vOutConfig.Receivers()),
+			}, nil
+		})
 }