mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-01-04 16:25:21 -05:00
leverage global object creator in proxies.
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
package shadowsocks
|
||||
|
||||
import (
|
||||
"v2ray.com/core/app"
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"v2ray.com/core/common"
|
||||
"v2ray.com/core/common/buf"
|
||||
"v2ray.com/core/common/bufio"
|
||||
"v2ray.com/core/common/log"
|
||||
@@ -21,7 +24,11 @@ type Client struct {
|
||||
}
|
||||
|
||||
// NewClient create a new Shadowsocks client.
|
||||
func NewClient(config *ClientConfig, space app.Space, meta *proxy.OutboundHandlerMeta) (*Client, error) {
|
||||
func NewClient(ctx context.Context, config *ClientConfig) (*Client, error) {
|
||||
meta := proxy.OutboundMetaFromContext(ctx)
|
||||
if meta == nil {
|
||||
return nil, errors.New("Shadowsocks|Client: No outbound meta in context.")
|
||||
}
|
||||
serverList := protocol.NewServerList()
|
||||
for _, rec := range config.Server {
|
||||
serverList.AddServer(protocol.NewServerSpecFromPB(*rec))
|
||||
@@ -164,10 +171,8 @@ func (v *Client) Dispatch(destination v2net.Destination, ray ray.OutboundRay) {
|
||||
}
|
||||
}
|
||||
|
||||
// ClientFactory is a OutboundHandlerFactory.
|
||||
type ClientFactory struct{}
|
||||
|
||||
// Create implements OutboundHandlerFactory.Create().
|
||||
func (ClientFactory) Create(space app.Space, rawConfig interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
|
||||
return NewClient(rawConfig.(*ClientConfig), space, meta)
|
||||
func init() {
|
||||
common.Must(common.RegisterConfig((*ClientConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
||||
return NewClient(ctx, config.(*ClientConfig))
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
package shadowsocks
|
||||
|
||||
import (
|
||||
"v2ray.com/core/common"
|
||||
"v2ray.com/core/common/serial"
|
||||
"v2ray.com/core/proxy"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// Must happen after config is initialized
|
||||
common.Must(proxy.RegisterOutboundHandlerCreator(serial.GetMessageType(new(ClientConfig)), new(ClientFactory)))
|
||||
common.Must(proxy.RegisterInboundHandlerCreator(serial.GetMessageType(new(ServerConfig)), new(ServerFactory)))
|
||||
}
|
||||
@@ -1,8 +1,11 @@
|
||||
package shadowsocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"v2ray.com/core/app"
|
||||
"v2ray.com/core/app/dispatcher"
|
||||
"v2ray.com/core/common"
|
||||
"v2ray.com/core/common/buf"
|
||||
"v2ray.com/core/common/bufio"
|
||||
"v2ray.com/core/common/errors"
|
||||
@@ -27,7 +30,15 @@ type Server struct {
|
||||
udpServer *udp.Server
|
||||
}
|
||||
|
||||
func NewServer(config *ServerConfig, space app.Space, meta *proxy.InboundHandlerMeta) (*Server, error) {
|
||||
func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
|
||||
space := app.SpaceFromContext(ctx)
|
||||
if space == nil {
|
||||
return nil, errors.New("Shadowsocks|Server: No space in context.")
|
||||
}
|
||||
meta := proxy.InboundMetaFromContext(ctx)
|
||||
if meta == nil {
|
||||
return nil, errors.New("Shadowsocks|Server: No inbound meta in context.")
|
||||
}
|
||||
if config.GetUser() == nil {
|
||||
return nil, protocol.ErrUserMissing
|
||||
}
|
||||
@@ -216,8 +227,8 @@ func (v *Server) handleConnection(conn internet.Connection) {
|
||||
}
|
||||
}
|
||||
|
||||
type ServerFactory struct{}
|
||||
|
||||
func (v *ServerFactory) Create(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
|
||||
return NewServer(rawConfig.(*ServerConfig), space, meta)
|
||||
func init() {
|
||||
common.Must(common.RegisterConfig((*ServerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
||||
return NewServer(ctx, config.(*ServerConfig))
|
||||
}))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user