manager.go 1.4 KB

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