1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 09:36:34 -05:00

simplify connection handler registration

This commit is contained in:
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() {
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
})
}

View File

@ -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
})
}

View File

@ -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()

View File

@ -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
})
}

View File

@ -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
})
}

View File

@ -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 {

View File

@ -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{

View File

@ -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
})
}

View File

@ -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
})
}

View File

@ -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
})
}