leverage global object creator in proxies.

This commit is contained in:
Darien Raymond
2017-01-13 00:56:21 +01:00
parent db1c9131f0
commit 148e4832eb
23 changed files with 353 additions and 269 deletions
+42 -33
View File
@@ -4,6 +4,8 @@ import (
"io"
"sync"
"context"
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/app/proxyman"
@@ -82,6 +84,43 @@ type VMessInboundHandler struct {
meta *proxy.InboundHandlerMeta
}
func New(ctx context.Context, config *Config) (*VMessInboundHandler, error) {
space := app.SpaceFromContext(ctx)
if space == nil {
return nil, errors.New("VMess|Inbound: No space in context.")
}
meta := proxy.InboundMetaFromContext(ctx)
if meta == nil {
return nil, errors.New("VMess|Inbound: No inbound meta in context.")
}
allowedClients := vmess.NewTimedUserValidator(protocol.DefaultIDHash)
for _, user := range config.User {
allowedClients.Add(user)
}
handler := &VMessInboundHandler{
clients: allowedClients,
detours: config.Detour,
usersByEmail: NewUserByEmail(config.User, config.GetDefaultValue()),
meta: meta,
}
space.OnInitialize(func() error {
handler.packetDispatcher = dispatcher.FromSpace(space)
if handler.packetDispatcher == nil {
return errors.New("VMess|Inbound: Dispatcher is not found in space.")
}
handler.inboundHandlerManager = proxyman.InboundHandlerManagerFromSpace(space)
if handler.inboundHandlerManager == nil {
return errors.New("VMess|Inbound: InboundHandlerManager is not found is space.")
}
return nil
})
return handler, nil
}
func (v *VMessInboundHandler) Port() v2net.Port {
return v.meta.Port
}
@@ -251,38 +290,8 @@ func (v *VMessInboundHandler) HandleConnection(connection internet.Connection) {
}
}
type Factory struct{}
func (v *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
config := rawConfig.(*Config)
allowedClients := vmess.NewTimedUserValidator(protocol.DefaultIDHash)
for _, user := range config.User {
allowedClients.Add(user)
}
handler := &VMessInboundHandler{
clients: allowedClients,
detours: config.Detour,
usersByEmail: NewUserByEmail(config.User, config.GetDefaultValue()),
meta: meta,
}
space.OnInitialize(func() error {
handler.packetDispatcher = dispatcher.FromSpace(space)
if handler.packetDispatcher == nil {
return errors.New("VMess|Inbound: Dispatcher is not found in space.")
}
handler.inboundHandlerManager = proxyman.InboundHandlerManagerFromSpace(space)
if handler.inboundHandlerManager == nil {
return errors.New("VMess|Inbound: InboundHandlerManager is not found is space.")
}
return nil
})
return handler, nil
}
func init() {
common.Must(proxy.RegisterInboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)))
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return New(ctx, config.(*Config))
}))
}
+28 -21
View File
@@ -3,6 +3,8 @@ package outbound
import (
"time"
"context"
"v2ray.com/core/app"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
@@ -12,7 +14,6 @@ import (
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/retry"
"v2ray.com/core/common/serial"
"v2ray.com/core/common/signal"
"v2ray.com/core/proxy"
"v2ray.com/core/proxy/vmess"
@@ -28,6 +29,29 @@ type VMessOutboundHandler struct {
meta *proxy.OutboundHandlerMeta
}
func New(ctx context.Context, config *Config) (*VMessOutboundHandler, error) {
space := app.SpaceFromContext(ctx)
if space == nil {
return nil, errors.New("VMess|Outbound: No space in context.")
}
meta := proxy.OutboundMetaFromContext(ctx)
if meta == nil {
return nil, errors.New("VMess|Outbound: No outbound meta in context.")
}
serverList := protocol.NewServerList()
for _, rec := range config.Receiver {
serverList.AddServer(protocol.NewServerSpecFromPB(*rec))
}
handler := &VMessOutboundHandler{
serverList: serverList,
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
meta: meta,
}
return handler, nil
}
// Dispatch implements OutboundHandler.Dispatch().
func (v *VMessOutboundHandler) Dispatch(target v2net.Destination, outboundRay ray.OutboundRay) {
var rec *protocol.ServerSpec
@@ -142,25 +166,8 @@ func (v *VMessOutboundHandler) Dispatch(target v2net.Destination, outboundRay ra
return
}
// Factory is a proxy factory for VMess outbound.
type Factory struct{}
func (v *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
vOutConfig := rawConfig.(*Config)
serverList := protocol.NewServerList()
for _, rec := range vOutConfig.Receiver {
serverList.AddServer(protocol.NewServerSpecFromPB(*rec))
}
handler := &VMessOutboundHandler{
serverList: serverList,
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
meta: meta,
}
return handler, nil
}
func init() {
common.Must(proxy.RegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)))
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return New(ctx, config.(*Config))
}))
}