manager.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package manager
  2. import (
  3. "context"
  4. "v2ray.com/core/app/policy"
  5. "v2ray.com/core/common"
  6. )
  7. // Instance is an instance of Policy manager.
  8. type Instance struct {
  9. levels map[uint32]*policy.Policy
  10. }
  11. // New creates new Policy manager instance.
  12. func New(ctx context.Context, config *policy.Config) (*Instance, error) {
  13. levels := config.Level
  14. if levels == nil {
  15. levels = make(map[uint32]*policy.Policy)
  16. }
  17. for _, p := range levels {
  18. g := global()
  19. g.OverrideWith(p)
  20. *p = g
  21. }
  22. return &Instance{
  23. levels: levels,
  24. }, nil
  25. }
  26. func global() policy.Policy {
  27. return policy.Policy{
  28. Timeout: &policy.Policy_Timeout{
  29. Handshake: &policy.Second{Value: 4},
  30. ConnectionIdle: &policy.Second{Value: 300},
  31. UplinkOnly: &policy.Second{Value: 5},
  32. DownlinkOnly: &policy.Second{Value: 30},
  33. },
  34. }
  35. }
  36. // GetPolicy implements policy.Manager.
  37. func (m *Instance) GetPolicy(level uint32) policy.Policy {
  38. if p, ok := m.levels[level]; ok {
  39. return *p
  40. }
  41. return global()
  42. }
  43. // Start implements app.Application.Start().
  44. func (m *Instance) Start() error {
  45. return nil
  46. }
  47. // Close implements app.Application.Close().
  48. func (m *Instance) Close() {
  49. }
  50. // Interface implement app.Application.Interface().
  51. func (m *Instance) Interface() interface{} {
  52. return (*policy.Manager)(nil)
  53. }
  54. func init() {
  55. common.Must(common.RegisterConfig((*policy.Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  56. return New(ctx, config.(*policy.Config))
  57. }))
  58. }