platform.go 2.5 KB

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