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
+13 -6
View File
@@ -1,7 +1,10 @@
package socks
import (
"v2ray.com/core/app"
"context"
"errors"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/log"
"v2ray.com/core/common/net"
@@ -18,7 +21,11 @@ type Client struct {
meta *proxy.OutboundHandlerMeta
}
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("Socks|Client: No outbound meta in context.")
}
serverList := protocol.NewServerList()
for _, rec := range config.Server {
serverList.AddServer(protocol.NewServerSpecFromPB(*rec))
@@ -112,8 +119,8 @@ func (c *Client) Dispatch(destination net.Destination, ray ray.OutboundRay) {
}
}
type ClientFactory struct{}
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))
}))
}
+16 -6
View File
@@ -1,12 +1,14 @@
package socks
import (
"context"
"io"
"sync"
"time"
"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"
@@ -34,7 +36,15 @@ type Server struct {
}
// NewServer creates a new Server object.
func NewServer(config *ServerConfig, space app.Space, meta *proxy.InboundHandlerMeta) *Server {
func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
space := app.SpaceFromContext(ctx)
if space == nil {
return nil, errors.New("Socks|Server: No space in context.")
}
meta := proxy.InboundMetaFromContext(ctx)
if meta == nil {
return nil, errors.New("Socks|Server: No inbound meta in context.")
}
s := &Server{
config: config,
meta: meta,
@@ -46,7 +56,7 @@ func NewServer(config *ServerConfig, space app.Space, meta *proxy.InboundHandler
}
return nil
})
return s
return s, nil
}
// Port implements InboundHandler.Port().
@@ -181,8 +191,8 @@ func (v *Server) transport(reader io.Reader, writer io.Writer, session *proxy.Se
}
}
type ServerFactory struct{}
func (v *ServerFactory) Create(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
return NewServer(rawConfig.(*ServerConfig), space, meta), nil
func init() {
common.Must(common.RegisterConfig((*ServerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return NewServer(ctx, config.(*ServerConfig))
}))
}
+1 -11
View File
@@ -1,12 +1,2 @@
// Package socks provides implements of Socks protocol 4, 4a and 5.
package socks
import (
"v2ray.com/core/common"
"v2ray.com/core/common/serial"
"v2ray.com/core/proxy"
)
func init() {
common.Must(proxy.RegisterOutboundHandlerCreator(serial.GetMessageType((*ClientConfig)(nil)), new(ClientFactory)))
common.Must(proxy.RegisterInboundHandlerCreator(serial.GetMessageType((*ServerConfig)(nil)), new(ServerFactory)))
}