simplify connection handler registration

pull/69/head
v2ray 2016-01-06 16:23:54 +01:00
parent 350b31cad9
commit 5b1854f842
10 changed files with 59 additions and 52 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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