platform.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package option
  2. import (
  3. E "github.com/sagernet/sing/common/exceptions"
  4. "github.com/sagernet/sing/common/json"
  5. "github.com/sagernet/sing/common/json/badoption"
  6. )
  7. type OnDemandOptions struct {
  8. Enabled bool `json:"enabled,omitempty"`
  9. Rules []OnDemandRule `json:"rules,omitempty"`
  10. }
  11. type OnDemandRule struct {
  12. Action *OnDemandRuleAction `json:"action,omitempty"`
  13. DNSSearchDomainMatch badoption.Listable[string] `json:"dns_search_domain_match,omitempty"`
  14. DNSServerAddressMatch badoption.Listable[string] `json:"dns_server_address_match,omitempty"`
  15. InterfaceTypeMatch *OnDemandRuleInterfaceType `json:"interface_type_match,omitempty"`
  16. SSIDMatch badoption.Listable[string] `json:"ssid_match,omitempty"`
  17. ProbeURL string `json:"probe_url,omitempty"`
  18. }
  19. type OnDemandRuleAction int
  20. func (r *OnDemandRuleAction) MarshalJSON() ([]byte, error) {
  21. if r == nil {
  22. return nil, nil
  23. }
  24. value := *r
  25. var actionName string
  26. switch value {
  27. case 1:
  28. actionName = "connect"
  29. case 2:
  30. actionName = "disconnect"
  31. case 3:
  32. actionName = "evaluate_connection"
  33. default:
  34. return nil, E.New("unknown action: ", value)
  35. }
  36. return json.Marshal(actionName)
  37. }
  38. func (r *OnDemandRuleAction) UnmarshalJSON(bytes []byte) error {
  39. var actionName string
  40. if err := json.Unmarshal(bytes, &actionName); err != nil {
  41. return err
  42. }
  43. var actionValue int
  44. switch actionName {
  45. case "connect":
  46. actionValue = 1
  47. case "disconnect":
  48. actionValue = 2
  49. case "evaluate_connection":
  50. actionValue = 3
  51. case "ignore":
  52. actionValue = 4
  53. default:
  54. return E.New("unknown action name: ", actionName)
  55. }
  56. *r = OnDemandRuleAction(actionValue)
  57. return nil
  58. }
  59. type OnDemandRuleInterfaceType int
  60. func (r *OnDemandRuleInterfaceType) MarshalJSON() ([]byte, error) {
  61. if r == nil {
  62. return nil, nil
  63. }
  64. value := *r
  65. var interfaceTypeName string
  66. switch value {
  67. case 1:
  68. interfaceTypeName = "any"
  69. case 2:
  70. interfaceTypeName = "wifi"
  71. case 3:
  72. interfaceTypeName = "cellular"
  73. default:
  74. return nil, E.New("unknown interface type: ", value)
  75. }
  76. return json.Marshal(interfaceTypeName)
  77. }
  78. func (r *OnDemandRuleInterfaceType) UnmarshalJSON(bytes []byte) error {
  79. var interfaceTypeName string
  80. if err := json.Unmarshal(bytes, &interfaceTypeName); err != nil {
  81. return err
  82. }
  83. var interfaceTypeValue int
  84. switch interfaceTypeName {
  85. case "any":
  86. interfaceTypeValue = 1
  87. case "wifi":
  88. interfaceTypeValue = 2
  89. case "cellular":
  90. interfaceTypeValue = 3
  91. default:
  92. return E.New("unknown interface type name: ", interfaceTypeName)
  93. }
  94. *r = OnDemandRuleInterfaceType(interfaceTypeValue)
  95. return nil
  96. }