Skip to content

Commit

Permalink
Require Type() for Feature
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Oct 12, 2018
1 parent dcd26ec commit d730637
Show file tree
Hide file tree
Showing 30 changed files with 162 additions and 49 deletions.
4 changes: 2 additions & 2 deletions app/commander/commander.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
8 changes: 6 additions & 2 deletions app/dispatcher/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion app/dns/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion app/policy/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,19 @@ 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()
}
}

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 {
Expand Down
2 changes: 1 addition & 1 deletion app/proxyman/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion app/proxyman/inbound/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions app/proxyman/mux/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/proxyman/outbound/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion app/proxyman/outbound/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
16 changes: 10 additions & 6 deletions app/proxyman/outbound/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion app/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
6 changes: 5 additions & 1 deletion app/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,18 @@ 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)
}
}

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()
Expand Down
1 change: 1 addition & 0 deletions common/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Runnable interface {
// HasType is the interface for objects that knows its type.
type HasType interface {
// Type returns the type of the object.
// Usually it returns (*Type)(nil) of the object.
Type() interface{}
}

Expand Down
4 changes: 4 additions & 0 deletions dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ type syncDNSClient struct {
dns.Client
}

func (d *syncDNSClient) Type() interface{} {
return dns.ClientType()
}

func (d *syncDNSClient) LookupIP(host string) ([]net.IP, error) {
d.RLock()
defer d.RUnlock()
Expand Down
4 changes: 4 additions & 0 deletions features/dns/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ type Client interface {
features.Feature
LookupIP(host string) ([]net.IP, error)
}

func ClientType() interface{} {
return (*Client)(nil)
}
1 change: 1 addition & 0 deletions features/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import "v2ray.com/core/common"
// Feature is the interface for V2Ray features. All features must implement this interface.
// All existing features have an implementation in app directory. These features can be replaced by third-party ones.
type Feature interface {
common.HasType
common.Runnable
}
8 changes: 6 additions & 2 deletions features/inbound/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ type Manager interface {
features.Feature
// GetHandlers returns an InboundHandler for the given tag.
GetHandler(ctx context.Context, tag string) (Handler, error)
// AddHandler adds the given handler into this InboundHandlerManager.
// AddHandler adds the given handler into this Manager.
AddHandler(ctx context.Context, handler Handler) error

// RemoveHandler removes a handler from InboundHandlerManager.
// RemoveHandler removes a handler from Manager.
RemoveHandler(ctx context.Context, tag string) error
}

func ManagerType() interface{} {
return (*Manager)(nil)
}
12 changes: 8 additions & 4 deletions features/outbound/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@ type Handler interface {
Dispatch(ctx context.Context, link *vio.Link)
}

// HandlerManager is a feature that manages outbound.Handlers.
type HandlerManager interface {
// Manager is a feature that manages outbound.Handlers.
type Manager interface {
features.Feature
// GetHandler returns an outbound.Handler for the given tag.
GetHandler(tag string) Handler
// GetDefaultHandler returns the default outbound.Handler. It is usually the first outbound.Handler specified in the configuration.
GetDefaultHandler() Handler
// AddHandler adds a handler into this outbound.HandlerManager.
// AddHandler adds a handler into this outbound.Manager.
AddHandler(ctx context.Context, handler Handler) error

// RemoveHandler removes a handler from outbound.HandlerManager.
// RemoveHandler removes a handler from outbound.Manager.
RemoveHandler(ctx context.Context, tag string) error
}

func ManagerType() interface{} {
return (*Manager)(nil)
}
4 changes: 4 additions & 0 deletions features/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ type Manager interface {
ForSystem() System
}

func ManagerType() interface{} {
return (*Manager)(nil)
}

var defaultBufferSize int32

func init() {
Expand Down
4 changes: 4 additions & 0 deletions features/routing/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ type Dispatcher interface {
// Dispatch returns a Ray for transporting data for the given request.
Dispatch(ctx context.Context, dest net.Destination) (*vio.Link, error)
}

func DispatcherType() interface{} {
return (*Dispatcher)(nil)
}
4 changes: 4 additions & 0 deletions features/routing/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ type Router interface {
// PickRoute returns a tag of an OutboundHandler based on the given context.
PickRoute(ctx context.Context) (string, error)
}

func RouterType() interface{} {
return (*Router)(nil)
}
4 changes: 4 additions & 0 deletions features/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ func GetOrRegisterCounter(m Manager, name string) (Counter, error) {

return m.RegisterCounter(name)
}

func ManagerType() interface{} {
return (*Manager)(nil)
}
Loading

0 comments on commit d730637

Please sign in to comment.