mirror of
https://github.com/v2ray/v2ray-core.git
synced 2026-11-02 16:21:05 +00:00
Require Type() for Feature
This commit is contained in:
30 files changed
+162
-49
No files matched your search
@@ -21,7 +21,7 @@ type Commander struct {
|
||||
server *grpc.Server
|
||||
config Config
|
||||
v *core.Instance
|
||||
ohm outbound.HandlerManager
|
||||
ohm outbound.Manager
|
||||
}
|
||||
|
||||
// NewCommander creates a new Commander based on the given config.
|
||||
@@ -32,7 +32,7 @@ func NewCommander(ctx context.Context, config *Config) (*Commander, error) {
|
||||
ohm: v.OutboundHandlerManager(),
|
||||
v: v,
|
||||
}
|
||||
if err := v.RegisterFeature((*Commander)(nil), c); err != nil {
|
||||
if err := v.RegisterFeature(c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c, nil
|
||||
|
||||
@@ -84,7 +84,7 @@ func (r *cachedReader) CloseError() {
|
||||
|
||||
// DefaultDispatcher is a default implementation of Dispatcher.
|
||||
type DefaultDispatcher struct {
|
||||
ohm outbound.HandlerManager
|
||||
ohm outbound.Manager
|
||||
router routing.Router
|
||||
policy policy.Manager
|
||||
stats feature_stats.Manager
|
||||
@@ -100,12 +100,16 @@ func NewDefaultDispatcher(ctx context.Context, config *Config) (*DefaultDispatch
|
||||
stats: v.Stats(),
|
||||
}
|
||||
|
||||
if err := v.RegisterFeature((*routing.Dispatcher)(nil), d); err != nil {
|
||||
if err := v.RegisterFeature(d); err != nil {
|
||||
return nil, newError("unable to register Dispatcher").Base(err)
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func (*DefaultDispatcher) Type() interface{} {
|
||||
return routing.DispatcherType()
|
||||
}
|
||||
|
||||
// Start implements common.Runnable.
|
||||
func (*DefaultDispatcher) Start() error {
|
||||
return nil
|
||||
|
||||
+5
-1
@@ -41,7 +41,7 @@ func New(ctx context.Context, config *Config) (*Server, error) {
|
||||
server.hosts = hosts
|
||||
|
||||
v := core.MustFromContext(ctx)
|
||||
if err := v.RegisterFeature((*dns.Client)(nil), server); err != nil {
|
||||
if err := v.RegisterFeature(server); err != nil {
|
||||
return nil, newError("unable to register DNSClient.").Base(err)
|
||||
}
|
||||
|
||||
@@ -97,6 +97,10 @@ func New(ctx context.Context, config *Config) (*Server, error) {
|
||||
return server, nil
|
||||
}
|
||||
|
||||
func (*Server) Type() interface{} {
|
||||
return dns.ClientType()
|
||||
}
|
||||
|
||||
// Start implements common.Runnable.
|
||||
func (s *Server) Start() error {
|
||||
return nil
|
||||
|
||||
+1
-1
@@ -30,7 +30,7 @@ func New(ctx context.Context, config *Config) (*Instance, error) {
|
||||
|
||||
v := core.FromContext(ctx)
|
||||
if v != nil {
|
||||
common.Must(v.RegisterFeature((*log.Handler)(nil), g))
|
||||
common.Must(v.RegisterFeature(g))
|
||||
}
|
||||
|
||||
return g, nil
|
||||
|
||||
@@ -30,7 +30,7 @@ func New(ctx context.Context, config *Config) (*Instance, error) {
|
||||
|
||||
v := core.FromContext(ctx)
|
||||
if v != nil {
|
||||
if err := v.RegisterFeature((*policy.Manager)(nil), m); err != nil {
|
||||
if err := v.RegisterFeature(m); err != nil {
|
||||
return nil, newError("unable to register PolicyManager in core").Base(err).AtError()
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,11 @@ func New(ctx context.Context, config *Config) (*Instance, error) {
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Type implements common.HasType.
|
||||
func (*Instance) Type() interface{} {
|
||||
return policy.ManagerType()
|
||||
}
|
||||
|
||||
// ForLevel implements policy.Manager.
|
||||
func (m *Instance) ForLevel(level uint32) policy.Session {
|
||||
if p, ok := m.levels[level]; ok {
|
||||
|
||||
@@ -64,7 +64,7 @@ func (op *RemoveUserOperation) ApplyInbound(ctx context.Context, handler inbound
|
||||
type handlerServer struct {
|
||||
s *core.Instance
|
||||
ihm inbound.Manager
|
||||
ohm outbound.HandlerManager
|
||||
ohm outbound.Manager
|
||||
}
|
||||
|
||||
func (s *handlerServer) AddInbound(ctx context.Context, request *AddInboundRequest) (*AddInboundResponse, error) {
|
||||
|
||||
@@ -28,12 +28,17 @@ func New(ctx context.Context, config *proxyman.InboundConfig) (*Manager, error)
|
||||
taggedHandlers: make(map[string]inbound.Handler),
|
||||
}
|
||||
v := core.MustFromContext(ctx)
|
||||
if err := v.RegisterFeature((*inbound.Manager)(nil), m); err != nil {
|
||||
if err := v.RegisterFeature(m); err != nil {
|
||||
return nil, newError("unable to register InboundHandlerManager").Base(err)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Type implements common.HasType.
|
||||
func (*Manager) Type() interface{} {
|
||||
return inbound.ManagerType()
|
||||
}
|
||||
|
||||
// AddHandler implements inbound.Manager.
|
||||
func (m *Manager) AddHandler(ctx context.Context, handler inbound.Handler) error {
|
||||
m.access.Lock()
|
||||
|
||||
@@ -310,6 +310,10 @@ func NewServer(ctx context.Context) *Server {
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Server) Type() interface{} {
|
||||
return s.dispatcher.Type()
|
||||
}
|
||||
|
||||
func (s *Server) Dispatch(ctx context.Context, dest net.Destination) (*vio.Link, error) {
|
||||
if dest.Address != muxCoolAddress {
|
||||
return s.dispatcher.Dispatch(ctx, dest)
|
||||
|
||||
@@ -21,7 +21,7 @@ type Handler struct {
|
||||
senderSettings *proxyman.SenderConfig
|
||||
streamSettings *internet.MemoryStreamConfig
|
||||
proxy proxy.Outbound
|
||||
outboundManager outbound.HandlerManager
|
||||
outboundManager outbound.Manager
|
||||
mux *mux.ClientManager
|
||||
}
|
||||
|
||||
|
||||
@@ -12,5 +12,5 @@ func TestInterfaces(t *testing.T) {
|
||||
assert := With(t)
|
||||
|
||||
assert((*Handler)(nil), Implements, (*outbound.Handler)(nil))
|
||||
assert((*Manager)(nil), Implements, (*outbound.HandlerManager)(nil))
|
||||
assert((*Manager)(nil), Implements, (*outbound.Manager)(nil))
|
||||
}
|
||||
@@ -27,12 +27,16 @@ func New(ctx context.Context, config *proxyman.OutboundConfig) (*Manager, error)
|
||||
taggedHandler: make(map[string]outbound.Handler),
|
||||
}
|
||||
v := core.MustFromContext(ctx)
|
||||
if err := v.RegisterFeature((*outbound.HandlerManager)(nil), m); err != nil {
|
||||
return nil, newError("unable to register OutboundHandlerManager").Base(err)
|
||||
if err := v.RegisterFeature(m); err != nil {
|
||||
return nil, newError("unable to register outbound.Manager").Base(err)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m *Manager) Type() interface{} {
|
||||
return outbound.ManagerType()
|
||||
}
|
||||
|
||||
// Start implements core.Feature
|
||||
func (m *Manager) Start() error {
|
||||
m.access.Lock()
|
||||
@@ -73,7 +77,7 @@ func (m *Manager) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetDefaultHandler implements outbound.HandlerManager.
|
||||
// GetDefaultHandler implements outbound.Manager.
|
||||
func (m *Manager) GetDefaultHandler() outbound.Handler {
|
||||
m.access.RLock()
|
||||
defer m.access.RUnlock()
|
||||
@@ -84,7 +88,7 @@ func (m *Manager) GetDefaultHandler() outbound.Handler {
|
||||
return m.defaultHandler
|
||||
}
|
||||
|
||||
// GetHandler implements outbound.HandlerManager.
|
||||
// GetHandler implements outbound.Manager.
|
||||
func (m *Manager) GetHandler(tag string) outbound.Handler {
|
||||
m.access.RLock()
|
||||
defer m.access.RUnlock()
|
||||
@@ -94,7 +98,7 @@ func (m *Manager) GetHandler(tag string) outbound.Handler {
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddHandler implements outbound.HandlerManager.
|
||||
// AddHandler implements outbound.Manager.
|
||||
func (m *Manager) AddHandler(ctx context.Context, handler outbound.Handler) error {
|
||||
m.access.Lock()
|
||||
defer m.access.Unlock()
|
||||
@@ -117,7 +121,7 @@ func (m *Manager) AddHandler(ctx context.Context, handler outbound.Handler) erro
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveHandler implements outbound.HandlerManager.
|
||||
// RemoveHandler implements outbound.Manager.
|
||||
func (m *Manager) RemoveHandler(ctx context.Context, tag string) error {
|
||||
if len(tag) == 0 {
|
||||
return common.ErrNoClue
|
||||
|
||||
@@ -39,7 +39,7 @@ func NewRouter(ctx context.Context, config *Config) (*Router, error) {
|
||||
r.rules[idx].Condition = cond
|
||||
}
|
||||
|
||||
if err := v.RegisterFeature((*routing.Router)(nil), r); err != nil {
|
||||
if err := v.RegisterFeature(r); err != nil {
|
||||
return nil, newError("unable to register Router").Base(err)
|
||||
}
|
||||
return r, nil
|
||||
@@ -124,6 +124,11 @@ func (*Router) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Type implement common.HasType.
|
||||
func (*Router) Type() interface{} {
|
||||
return routing.RouterType()
|
||||
}
|
||||
|
||||
func init() {
|
||||
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
||||
return NewRouter(ctx, config.(*Config))
|
||||
|
||||
+5
-1
@@ -44,7 +44,7 @@ func NewManager(ctx context.Context, config *Config) (*Manager, error) {
|
||||
|
||||
v := core.FromContext(ctx)
|
||||
if v != nil {
|
||||
if err := v.RegisterFeature((*stats.Manager)(nil), m); err != nil {
|
||||
if err := v.RegisterFeature(m); err != nil {
|
||||
return nil, newError("failed to register StatManager").Base(err)
|
||||
}
|
||||
}
|
||||
@@ -52,6 +52,10 @@ func NewManager(ctx context.Context, config *Config) (*Manager, error) {
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (*Manager) Type() interface{} {
|
||||
return stats.ManagerType()
|
||||
}
|
||||
|
||||
func (m *Manager) RegisterCounter(name string) (stats.Counter, error) {
|
||||
m.access.Lock()
|
||||
defer m.access.Unlock()
|
||||
|
||||
Reference in new issue
Block a user