diff --git a/proxy/blackhole/blackhole.go b/proxy/blackhole/blackhole.go index a5c0483e3..ff6d6eb36 100644 --- a/proxy/blackhole/blackhole.go +++ b/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 + }) } diff --git a/proxy/dokodemo/dokodemo_factory.go b/proxy/dokodemo/dokodemo_factory.go index 32c8426f8..da9572f68 100644 --- a/proxy/dokodemo/dokodemo_factory.go +++ b/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 + }) } diff --git a/proxy/freedom/freedom_test.go b/proxy/freedom/freedom_test.go index 47081ab68..918f41d2d 100644 --- a/proxy/freedom/freedom_test.go +++ b/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() diff --git a/proxy/freedom/freedomfactory.go b/proxy/freedom/freedomfactory.go index d44464604..b002857ef 100644 --- a/proxy/freedom/freedomfactory.go +++ b/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 + }) } diff --git a/proxy/http/http_factory.go b/proxy/http/http_factory.go index d5e96734d..0f4e810bd 100644 --- a/proxy/http/http_factory.go +++ b/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 + }) } diff --git a/proxy/internal/handler_cache.go b/proxy/internal/handler_cache.go index 3d4352ffa..155b64f4c 100644 --- a/proxy/internal/handler_cache.go +++ b/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 { diff --git a/proxy/socks/socks_test.go b/proxy/socks/socks_test.go index 92fe275a7..4de9bd0c6 100644 --- a/proxy/socks/socks_test.go +++ b/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{ diff --git a/proxy/socks/socksfactory.go b/proxy/socks/socksfactory.go index 08f2e5a52..9f9a8ad97 100644 --- a/proxy/socks/socksfactory.go +++ b/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 + }) } diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index 3b6081b7e..457fb0351 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/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 + }) } diff --git a/proxy/vmess/outbound/outbound.go b/proxy/vmess/outbound/outbound.go index 67e2d5580..d81c8c70e 100644 --- a/proxy/vmess/outbound/outbound.go +++ b/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 + }) }