mirror of
https://github.com/v2ray/v2ray-core.git
synced 2026-10-31 16:15:28 +00:00
simplify dependency resolution
This commit is contained in:
@@ -1 +1,2 @@
|
||||
// Package app contains feature implementations of V2Ray. The features may be enabled during runtime.
|
||||
package app
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
"v2ray.com/core"
|
||||
"v2ray.com/core/common"
|
||||
"v2ray.com/core/common/signal/done"
|
||||
"v2ray.com/core/features"
|
||||
"v2ray.com/core/features/outbound"
|
||||
)
|
||||
|
||||
@@ -31,9 +30,8 @@ func NewCommander(ctx context.Context, config *Config) (*Commander, error) {
|
||||
tag: config.Tag,
|
||||
}
|
||||
|
||||
v := core.MustFromContext(ctx)
|
||||
v.RequireFeatures([]interface{}{outbound.ManagerType()}, func(fs []features.Feature) {
|
||||
c.ohm = fs[0].(outbound.Manager)
|
||||
core.RequireFeatures(ctx, func(om outbound.Manager) {
|
||||
c.ohm = om
|
||||
})
|
||||
|
||||
for _, rawConfig := range config.Service {
|
||||
|
||||
+10
-14
@@ -16,7 +16,6 @@ import (
|
||||
"v2ray.com/core/common/protocol"
|
||||
"v2ray.com/core/common/session"
|
||||
"v2ray.com/core/common/vio"
|
||||
"v2ray.com/core/features"
|
||||
"v2ray.com/core/features/outbound"
|
||||
"v2ray.com/core/features/policy"
|
||||
"v2ray.com/core/features/routing"
|
||||
@@ -94,23 +93,20 @@ type DefaultDispatcher struct {
|
||||
func NewDefaultDispatcher(ctx context.Context, config *Config) (*DefaultDispatcher, error) {
|
||||
d := &DefaultDispatcher{}
|
||||
|
||||
v := core.MustFromContext(ctx)
|
||||
v.RequireFeatures([]interface{}{outbound.ManagerType(), routing.RouterType(), policy.ManagerType()}, func(fs []features.Feature) {
|
||||
d.ohm = fs[0].(outbound.Manager)
|
||||
d.router = fs[1].(routing.Router)
|
||||
d.policy = fs[2].(policy.Manager)
|
||||
})
|
||||
v.RequireFeatures([]interface{}{core.ServerType()}, func([]features.Feature) {
|
||||
f := v.GetFeature(stats.ManagerType())
|
||||
if f == nil {
|
||||
return
|
||||
}
|
||||
d.stats = f.(stats.Manager)
|
||||
})
|
||||
core.RequireFeatures(ctx, d.Init)
|
||||
|
||||
return d, nil
|
||||
}
|
||||
|
||||
// Init initializes DefaultDispatcher.
|
||||
// This method is visible for testing purpose.
|
||||
func (d *DefaultDispatcher) Init(om outbound.Manager, router routing.Router, pm policy.Manager, sm stats.Manager) {
|
||||
d.ohm = om
|
||||
d.router = router
|
||||
d.policy = pm
|
||||
d.stats = sm
|
||||
}
|
||||
|
||||
// Type implements common.HasType.
|
||||
func (*DefaultDispatcher) Type() interface{} {
|
||||
return routing.DispatcherType()
|
||||
|
||||
+6
-7
@@ -7,14 +7,13 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"v2ray.com/core/features/routing"
|
||||
|
||||
"v2ray.com/core"
|
||||
"v2ray.com/core/common"
|
||||
"v2ray.com/core/common/net"
|
||||
"v2ray.com/core/common/strmatcher"
|
||||
"v2ray.com/core/features"
|
||||
"v2ray.com/core/features/dns"
|
||||
"v2ray.com/core/features/routing"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
@@ -43,8 +42,6 @@ func New(ctx context.Context, config *Config) (*Server, error) {
|
||||
}
|
||||
server.hosts = hosts
|
||||
|
||||
v := core.MustFromContext(ctx)
|
||||
|
||||
addNameServer := func(endpoint *net.Endpoint) int {
|
||||
address := endpoint.Address.AsAddress()
|
||||
if address.Family().IsDomain() && address.Domain() == "localhost" {
|
||||
@@ -57,9 +54,9 @@ func New(ctx context.Context, config *Config) (*Server, error) {
|
||||
if dest.Network == net.Network_UDP {
|
||||
idx := len(server.servers)
|
||||
server.servers = append(server.servers, nil)
|
||||
v.RequireFeatures([]interface{}{routing.DispatcherType()}, func(fs []features.Feature) {
|
||||
dispatcher := fs[0].(routing.Dispatcher)
|
||||
server.servers[idx] = NewClassicNameServer(dest, dispatcher, server.clientIP)
|
||||
|
||||
core.RequireFeatures(ctx, func(d routing.Dispatcher) {
|
||||
server.servers[idx] = NewClassicNameServer(dest, d, server.clientIP)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -102,6 +99,7 @@ func New(ctx context.Context, config *Config) (*Server, error) {
|
||||
return server, nil
|
||||
}
|
||||
|
||||
// Type implements common.HasType.
|
||||
func (*Server) Type() interface{} {
|
||||
return dns.ClientType()
|
||||
}
|
||||
@@ -123,6 +121,7 @@ func (s *Server) queryIPTimeout(server NameServerInterface, domain string) ([]ne
|
||||
return ips, err
|
||||
}
|
||||
|
||||
// LookupIP implements dns.Client.
|
||||
func (s *Server) LookupIP(domain string) ([]net.IP, error) {
|
||||
if ip := s.hosts.LookupIP(domain); len(ip) > 0 {
|
||||
return ip, nil
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
|
||||
"v2ray.com/core"
|
||||
"v2ray.com/core/common"
|
||||
"v2ray.com/core/features"
|
||||
"v2ray.com/core/features/inbound"
|
||||
"v2ray.com/core/features/outbound"
|
||||
"v2ray.com/core/proxy"
|
||||
@@ -132,9 +131,9 @@ func (s *service) Register(server *grpc.Server) {
|
||||
hs := &handlerServer{
|
||||
s: s.v,
|
||||
}
|
||||
s.v.RequireFeatures([]interface{}{inbound.ManagerType(), outbound.ManagerType()}, func(fs []features.Feature) {
|
||||
hs.ihm = fs[0].(inbound.Manager)
|
||||
hs.ohm = fs[1].(outbound.Manager)
|
||||
s.v.RequireFeatures(func(im inbound.Manager, om outbound.Manager) {
|
||||
hs.ihm = im
|
||||
hs.ohm = om
|
||||
})
|
||||
RegisterHandlerServiceServer(server, hs)
|
||||
}
|
||||
|
||||
@@ -8,8 +8,6 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"v2ray.com/core/features"
|
||||
|
||||
"v2ray.com/core"
|
||||
"v2ray.com/core/app/proxyman"
|
||||
"v2ray.com/core/common"
|
||||
@@ -307,9 +305,8 @@ type Server struct {
|
||||
// NewServer creates a new mux.Server.
|
||||
func NewServer(ctx context.Context) *Server {
|
||||
s := &Server{}
|
||||
v := core.MustFromContext(ctx)
|
||||
v.RequireFeatures([]interface{}{routing.DispatcherType()}, func(fs []features.Feature) {
|
||||
s.dispatcher = fs[0].(routing.Dispatcher)
|
||||
core.RequireFeatures(ctx, func(d routing.Dispatcher) {
|
||||
s.dispatcher = d
|
||||
})
|
||||
return s
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"v2ray.com/core/common"
|
||||
"v2ray.com/core/common/net"
|
||||
"v2ray.com/core/common/session"
|
||||
"v2ray.com/core/features"
|
||||
"v2ray.com/core/features/dns"
|
||||
"v2ray.com/core/features/routing"
|
||||
"v2ray.com/core/proxy"
|
||||
@@ -38,10 +37,10 @@ func NewRouter(ctx context.Context, config *Config) (*Router, error) {
|
||||
r.rules[idx].Condition = cond
|
||||
}
|
||||
|
||||
v := core.MustFromContext(ctx)
|
||||
v.RequireFeatures([]interface{}{dns.ClientType()}, func(fs []features.Feature) {
|
||||
r.dns = fs[0].(dns.Client)
|
||||
core.RequireFeatures(ctx, func(d dns.Client) {
|
||||
r.dns = d
|
||||
})
|
||||
|
||||
return r, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -75,17 +75,21 @@ func (s *statsServer) QueryStats(ctx context.Context, request *QueryStatsRequest
|
||||
}
|
||||
|
||||
type service struct {
|
||||
v *core.Instance
|
||||
statsManager feature_stats.Manager
|
||||
}
|
||||
|
||||
func (s *service) Register(server *grpc.Server) {
|
||||
f := s.v.GetFeature(feature_stats.ManagerType())
|
||||
RegisterStatsServiceServer(server, NewStatsServer(f.(feature_stats.Manager)))
|
||||
RegisterStatsServiceServer(server, NewStatsServer(s.statsManager))
|
||||
}
|
||||
|
||||
func init() {
|
||||
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
|
||||
s := core.MustFromContext(ctx)
|
||||
return &service{v: s}, nil
|
||||
s := new(service)
|
||||
|
||||
core.RequireFeatures(ctx, func(sm feature_stats.Manager) {
|
||||
s.statsManager = sm
|
||||
})
|
||||
|
||||
return s, nil
|
||||
}))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user