mirror of
https://github.com/v2ray/v2ray-core.git
synced 2026-10-29 16:10:22 +00:00
refactor dependency resolution
This commit is contained in:
@@ -35,7 +35,7 @@ func New(ctx context.Context, config *Config) (*DokodemoDoor, error) {
|
||||
config: config,
|
||||
address: config.GetPredefinedAddress(),
|
||||
port: net.Port(config.Port),
|
||||
policyManager: v.PolicyManager(),
|
||||
policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager),
|
||||
}
|
||||
|
||||
return d, nil
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"v2ray.com/core/features"
|
||||
|
||||
"v2ray.com/core"
|
||||
"v2ray.com/core/common"
|
||||
"v2ray.com/core/common/buf"
|
||||
@@ -31,13 +33,16 @@ type Handler struct {
|
||||
|
||||
// New creates a new Freedom handler.
|
||||
func New(ctx context.Context, config *Config) (*Handler, error) {
|
||||
v := core.MustFromContext(ctx)
|
||||
f := &Handler{
|
||||
config: *config,
|
||||
policyManager: v.PolicyManager(),
|
||||
dns: v.DNSClient(),
|
||||
config: *config,
|
||||
}
|
||||
|
||||
v := core.MustFromContext(ctx)
|
||||
v.RequireFeatures([]interface{}{policy.ManagerType(), dns.ClientType()}, func(fs []features.Feature) {
|
||||
f.policyManager = fs[0].(policy.Manager)
|
||||
f.dns = fs[1].(dns.Client)
|
||||
})
|
||||
|
||||
return f, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -28,15 +28,16 @@ import (
|
||||
|
||||
// Server is an HTTP proxy server.
|
||||
type Server struct {
|
||||
config *ServerConfig
|
||||
v *core.Instance
|
||||
config *ServerConfig
|
||||
policyManager policy.Manager
|
||||
}
|
||||
|
||||
// NewServer creates a new HTTP inbound handler.
|
||||
func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
|
||||
v := core.MustFromContext(ctx)
|
||||
s := &Server{
|
||||
config: config,
|
||||
v: core.MustFromContext(ctx),
|
||||
config: config,
|
||||
policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager),
|
||||
}
|
||||
|
||||
return s, nil
|
||||
@@ -44,7 +45,7 @@ func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
|
||||
|
||||
func (s *Server) policy() policy.Session {
|
||||
config := s.config
|
||||
p := s.v.PolicyManager().ForLevel(config.UserLevel)
|
||||
p := s.policyManager.ForLevel(config.UserLevel)
|
||||
if config.Timeout > 0 && config.UserLevel == 0 {
|
||||
p.Timeouts.ConnectionIdle = time.Duration(config.Timeout) * time.Second
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
|
||||
return &Server{
|
||||
user: user,
|
||||
account: account,
|
||||
policy: v.PolicyManager(),
|
||||
policy: v.GetFeature(policy.ManagerType()).(policy.Manager),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -3,25 +3,25 @@ package shadowsocks
|
||||
import (
|
||||
"context"
|
||||
|
||||
"v2ray.com/core/common/session"
|
||||
"v2ray.com/core/common/task"
|
||||
"v2ray.com/core/common/vio"
|
||||
|
||||
"v2ray.com/core"
|
||||
"v2ray.com/core/common"
|
||||
"v2ray.com/core/common/buf"
|
||||
"v2ray.com/core/common/net"
|
||||
"v2ray.com/core/common/protocol"
|
||||
"v2ray.com/core/common/retry"
|
||||
"v2ray.com/core/common/session"
|
||||
"v2ray.com/core/common/signal"
|
||||
"v2ray.com/core/common/task"
|
||||
"v2ray.com/core/common/vio"
|
||||
"v2ray.com/core/features/policy"
|
||||
"v2ray.com/core/proxy"
|
||||
"v2ray.com/core/transport/internet"
|
||||
)
|
||||
|
||||
// Client is a inbound handler for Shadowsocks protocol
|
||||
type Client struct {
|
||||
serverPicker protocol.ServerPicker
|
||||
v *core.Instance
|
||||
serverPicker protocol.ServerPicker
|
||||
policyManager policy.Manager
|
||||
}
|
||||
|
||||
// NewClient create a new Shadowsocks client.
|
||||
@@ -37,9 +37,11 @@ func NewClient(ctx context.Context, config *ClientConfig) (*Client, error) {
|
||||
if serverList.Size() == 0 {
|
||||
return nil, newError("0 server")
|
||||
}
|
||||
|
||||
v := core.MustFromContext(ctx)
|
||||
client := &Client{
|
||||
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
|
||||
v: core.MustFromContext(ctx),
|
||||
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
|
||||
policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager),
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
@@ -97,7 +99,7 @@ func (c *Client) Process(ctx context.Context, link *vio.Link, dialer proxy.Diale
|
||||
request.Option |= RequestOptionOneTimeAuth
|
||||
}
|
||||
|
||||
sessionPolicy := c.v.PolicyManager().ForLevel(user.Level)
|
||||
sessionPolicy := c.policyManager.ForLevel(user.Level)
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle)
|
||||
|
||||
|
||||
@@ -21,9 +21,9 @@ import (
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
config ServerConfig
|
||||
user *protocol.MemoryUser
|
||||
v *core.Instance
|
||||
config ServerConfig
|
||||
user *protocol.MemoryUser
|
||||
policyManager policy.Manager
|
||||
}
|
||||
|
||||
// NewServer create a new Shadowsocks server.
|
||||
@@ -37,10 +37,11 @@ func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
|
||||
return nil, newError("failed to parse user account").Base(err)
|
||||
}
|
||||
|
||||
v := core.MustFromContext(ctx)
|
||||
s := &Server{
|
||||
config: *config,
|
||||
user: mUser,
|
||||
v: core.MustFromContext(ctx),
|
||||
config: *config,
|
||||
user: mUser,
|
||||
policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager),
|
||||
}
|
||||
|
||||
return s, nil
|
||||
@@ -150,7 +151,7 @@ func (s *Server) handlerUDPPayload(ctx context.Context, conn internet.Connection
|
||||
}
|
||||
|
||||
func (s *Server) handleConnection(ctx context.Context, conn internet.Connection, dispatcher routing.Dispatcher) error {
|
||||
sessionPolicy := s.v.PolicyManager().ForLevel(s.user.Level)
|
||||
sessionPolicy := s.policyManager.ForLevel(s.user.Level)
|
||||
conn.SetReadDeadline(time.Now().Add(sessionPolicy.Timeouts.Handshake))
|
||||
|
||||
bufferedReader := buf.BufferedReader{Reader: buf.NewReader(conn)}
|
||||
|
||||
@@ -43,7 +43,7 @@ func NewClient(ctx context.Context, config *ClientConfig) (*Client, error) {
|
||||
v := core.MustFromContext(ctx)
|
||||
return &Client{
|
||||
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
|
||||
policyManager: v.PolicyManager(),
|
||||
policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
|
||||
v := core.MustFromContext(ctx)
|
||||
s := &Server{
|
||||
config: config,
|
||||
policyManager: v.PolicyManager(),
|
||||
policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager),
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
@@ -114,8 +114,8 @@ type Handler struct {
|
||||
func New(ctx context.Context, config *Config) (*Handler, error) {
|
||||
v := core.MustFromContext(ctx)
|
||||
handler := &Handler{
|
||||
policyManager: v.PolicyManager(),
|
||||
inboundHandlerManager: v.InboundHandlerManager(),
|
||||
policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager),
|
||||
inboundHandlerManager: v.GetFeature(feature_inbound.ManagerType()).(feature_inbound.Manager),
|
||||
clients: vmess.NewTimedUserValidator(protocol.DefaultIDHash),
|
||||
detours: config.Detour,
|
||||
usersByEmail: newUserByEmail(config.GetDefaultValue()),
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"v2ray.com/core/common/signal"
|
||||
"v2ray.com/core/common/task"
|
||||
"v2ray.com/core/common/vio"
|
||||
"v2ray.com/core/features/policy"
|
||||
"v2ray.com/core/proxy"
|
||||
"v2ray.com/core/proxy/vmess"
|
||||
"v2ray.com/core/proxy/vmess/encoding"
|
||||
@@ -25,9 +26,9 @@ import (
|
||||
|
||||
// Handler is an outbound connection handler for VMess protocol.
|
||||
type Handler struct {
|
||||
serverList *protocol.ServerList
|
||||
serverPicker protocol.ServerPicker
|
||||
v *core.Instance
|
||||
serverList *protocol.ServerList
|
||||
serverPicker protocol.ServerPicker
|
||||
policyManager policy.Manager
|
||||
}
|
||||
|
||||
// New creates a new VMess outbound handler.
|
||||
@@ -40,10 +41,12 @@ func New(ctx context.Context, config *Config) (*Handler, error) {
|
||||
}
|
||||
serverList.AddServer(s)
|
||||
}
|
||||
|
||||
v := core.MustFromContext(ctx)
|
||||
handler := &Handler{
|
||||
serverList: serverList,
|
||||
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
|
||||
v: core.MustFromContext(ctx),
|
||||
serverList: serverList,
|
||||
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
|
||||
policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager),
|
||||
}
|
||||
|
||||
return handler, nil
|
||||
@@ -109,7 +112,7 @@ func (v *Handler) Process(ctx context.Context, link *vio.Link, dialer proxy.Dial
|
||||
output := link.Writer
|
||||
|
||||
session := encoding.NewClientSession(protocol.DefaultIDHash)
|
||||
sessionPolicy := v.v.PolicyManager().ForLevel(request.User.Level)
|
||||
sessionPolicy := v.policyManager.ForLevel(request.User.Level)
|
||||
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle)
|
||||
|
||||
Reference in New Issue
Block a user