github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/option/platform.go (about)

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