manager.go 1.4 KB

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