sing-box/option/route.go

126 lines
4.3 KiB
Go
Raw Normal View History

2022-07-02 14:07:50 +08:00
package option
import (
2022-07-25 11:29:46 +08:00
"reflect"
2022-07-24 17:58:52 +08:00
"github.com/sagernet/sing-box/common/json"
2022-07-08 23:03:57 +08:00
C "github.com/sagernet/sing-box/constant"
2022-07-03 01:57:04 +08:00
"github.com/sagernet/sing/common"
2022-07-02 14:07:50 +08:00
E "github.com/sagernet/sing/common/exceptions"
)
2022-07-02 22:55:10 +08:00
type RouteOptions struct {
GeoIP *GeoIPOptions `json:"geoip,omitempty"`
Geosite *GeositeOptions `json:"geosite,omitempty"`
Rules []Rule `json:"rules,omitempty"`
Final string `json:"final,omitempty"`
FindProcess bool `json:"find_process,omitempty"`
AutoDetectInterface bool `json:"auto_detect_interface,omitempty"`
OverrideAndroidVPN bool `json:"override_android_vpn,omitempty"`
2022-07-15 11:51:51 +08:00
DefaultInterface string `json:"default_interface,omitempty"`
2022-07-24 17:46:25 +08:00
DefaultMark int `json:"default_mark,omitempty"`
2022-07-02 22:55:10 +08:00
}
type GeoIPOptions struct {
Path string `json:"path,omitempty"`
DownloadURL string `json:"download_url,omitempty"`
DownloadDetour string `json:"download_detour,omitempty"`
}
2022-07-05 09:05:35 +08:00
type GeositeOptions struct {
Path string `json:"path,omitempty"`
DownloadURL string `json:"download_url,omitempty"`
DownloadDetour string `json:"download_detour,omitempty"`
}
2022-07-02 14:07:50 +08:00
type _Rule struct {
Type string `json:"type,omitempty"`
DefaultOptions DefaultRule `json:"-"`
LogicalOptions LogicalRule `json:"-"`
2022-07-02 14:07:50 +08:00
}
type Rule _Rule
2022-07-03 03:42:57 +08:00
func (r Rule) MarshalJSON() ([]byte, error) {
2022-07-03 01:57:04 +08:00
var v any
2022-07-02 14:07:50 +08:00
switch r.Type {
2022-07-03 01:57:04 +08:00
case C.RuleTypeDefault:
2022-07-07 21:47:21 +08:00
r.Type = ""
2022-07-03 01:57:04 +08:00
v = r.DefaultOptions
2022-07-02 14:07:50 +08:00
case C.RuleTypeLogical:
2022-07-03 01:57:04 +08:00
v = r.LogicalOptions
2022-07-02 14:07:50 +08:00
default:
2022-07-03 01:57:04 +08:00
return nil, E.New("unknown rule type: " + r.Type)
2022-07-02 14:07:50 +08:00
}
2022-07-03 03:42:57 +08:00
return MarshallObjects((_Rule)(r), v)
2022-07-02 14:07:50 +08:00
}
func (r *Rule) UnmarshalJSON(bytes []byte) error {
err := json.Unmarshal(bytes, (*_Rule)(r))
if err != nil {
return err
}
2022-07-03 01:57:04 +08:00
var v any
2022-07-02 14:07:50 +08:00
switch r.Type {
case "", C.RuleTypeDefault:
2022-07-07 21:47:21 +08:00
r.Type = C.RuleTypeDefault
2022-07-03 01:57:04 +08:00
v = &r.DefaultOptions
2022-07-02 14:07:50 +08:00
case C.RuleTypeLogical:
2022-07-03 01:57:04 +08:00
v = &r.LogicalOptions
2022-07-02 14:07:50 +08:00
default:
2022-07-03 01:57:04 +08:00
return E.New("unknown rule type: " + r.Type)
2022-07-02 14:07:50 +08:00
}
err = UnmarshallExcluded(bytes, (*_Rule)(r), v)
if err != nil {
return E.Cause(err, "route rule")
}
return nil
2022-07-02 14:07:50 +08:00
}
type DefaultRule struct {
Inbound Listable[string] `json:"inbound,omitempty"`
IPVersion int `json:"ip_version,omitempty"`
Network string `json:"network,omitempty"`
AuthUser Listable[string] `json:"auth_user,omitempty"`
Protocol Listable[string] `json:"protocol,omitempty"`
Domain Listable[string] `json:"domain,omitempty"`
DomainSuffix Listable[string] `json:"domain_suffix,omitempty"`
DomainKeyword Listable[string] `json:"domain_keyword,omitempty"`
DomainRegex Listable[string] `json:"domain_regex,omitempty"`
Geosite Listable[string] `json:"geosite,omitempty"`
SourceGeoIP Listable[string] `json:"source_geoip,omitempty"`
GeoIP Listable[string] `json:"geoip,omitempty"`
SourceIPCIDR Listable[string] `json:"source_ip_cidr,omitempty"`
IPCIDR Listable[string] `json:"ip_cidr,omitempty"`
SourcePort Listable[uint16] `json:"source_port,omitempty"`
SourcePortRange Listable[string] `json:"source_port_range,omitempty"`
Port Listable[uint16] `json:"port,omitempty"`
PortRange Listable[string] `json:"port_range,omitempty"`
ProcessName Listable[string] `json:"process_name,omitempty"`
ProcessPath Listable[string] `json:"process_path,omitempty"`
PackageName Listable[string] `json:"package_name,omitempty"`
User Listable[string] `json:"user,omitempty"`
UserID Listable[int32] `json:"user_id,omitempty"`
2022-09-10 14:09:47 +08:00
ClashMode string `json:"clash_mode,omitempty"`
Invert bool `json:"invert,omitempty"`
Outbound string `json:"outbound,omitempty"`
2022-07-02 14:07:50 +08:00
}
2022-07-03 01:57:04 +08:00
func (r DefaultRule) IsValid() bool {
var defaultValue DefaultRule
2022-07-25 11:29:46 +08:00
defaultValue.Invert = r.Invert
2022-07-03 01:57:04 +08:00
defaultValue.Outbound = r.Outbound
2022-07-25 11:29:46 +08:00
return !reflect.DeepEqual(r, defaultValue)
2022-07-03 01:57:04 +08:00
}
2022-07-02 14:07:50 +08:00
type LogicalRule struct {
Mode string `json:"mode"`
Rules []DefaultRule `json:"rules,omitempty"`
2022-07-24 14:05:06 +08:00
Invert bool `json:"invert,omitempty"`
2022-07-02 14:07:50 +08:00
Outbound string `json:"outbound,omitempty"`
}
2022-07-03 01:57:04 +08:00
func (r LogicalRule) IsValid() bool {
return len(r.Rules) > 0 && common.All(r.Rules, DefaultRule.IsValid)
}