github.com/xraypb/xray-core@v1.6.6/app/policy/manager.go (about)

     1  package policy
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/xraypb/xray-core/common"
     7  	"github.com/xraypb/xray-core/features/policy"
     8  )
     9  
    10  // Instance is an instance of Policy manager.
    11  type Instance struct {
    12  	levels map[uint32]*Policy
    13  	system *SystemPolicy
    14  }
    15  
    16  // New creates new Policy manager instance.
    17  func New(ctx context.Context, config *Config) (*Instance, error) {
    18  	m := &Instance{
    19  		levels: make(map[uint32]*Policy),
    20  		system: config.System,
    21  	}
    22  	if len(config.Level) > 0 {
    23  		for lv, p := range config.Level {
    24  			pp := defaultPolicy()
    25  			pp.overrideWith(p)
    26  			m.levels[lv] = pp
    27  		}
    28  	}
    29  
    30  	return m, nil
    31  }
    32  
    33  // Type implements common.HasType.
    34  func (*Instance) Type() interface{} {
    35  	return policy.ManagerType()
    36  }
    37  
    38  // ForLevel implements policy.Manager.
    39  func (m *Instance) ForLevel(level uint32) policy.Session {
    40  	if p, ok := m.levels[level]; ok {
    41  		return p.ToCorePolicy()
    42  	}
    43  	return policy.SessionDefault()
    44  }
    45  
    46  // ForSystem implements policy.Manager.
    47  func (m *Instance) ForSystem() policy.System {
    48  	if m.system == nil {
    49  		return policy.System{}
    50  	}
    51  	return m.system.ToCorePolicy()
    52  }
    53  
    54  // Start implements common.Runnable.Start().
    55  func (m *Instance) Start() error {
    56  	return nil
    57  }
    58  
    59  // Close implements common.Closable.Close().
    60  func (m *Instance) Close() error {
    61  	return nil
    62  }
    63  
    64  func init() {
    65  	common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
    66  		return New(ctx, config.(*Config))
    67  	}))
    68  }