2022-07-02 06:07:50 +00:00
|
|
|
package option
|
|
|
|
|
|
|
|
import (
|
2022-07-08 15:03:57 +00:00
|
|
|
C "github.com/sagernet/sing-box/constant"
|
2022-07-02 17:57:04 +00:00
|
|
|
"github.com/sagernet/sing/common"
|
2022-07-02 06:07:50 +00:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2022-07-06 07:01:09 +00:00
|
|
|
|
|
|
|
"github.com/goccy/go-json"
|
2022-07-02 06:07:50 +00:00
|
|
|
)
|
|
|
|
|
2022-07-02 14:55:10 +00:00
|
|
|
type RouteOptions struct {
|
2022-07-10 00:18:52 +00:00
|
|
|
GeoIP *GeoIPOptions `json:"geoip,omitempty"`
|
|
|
|
Geosite *GeositeOptions `json:"geosite,omitempty"`
|
|
|
|
Rules []Rule `json:"rules,omitempty"`
|
|
|
|
Final string `json:"final,omitempty"`
|
2022-07-23 11:01:41 +00:00
|
|
|
FindProcess bool `json:"find_process,omitempty"`
|
2022-07-10 00:18:52 +00:00
|
|
|
AutoDetectInterface bool `json:"auto_detect_interface,omitempty"`
|
2022-07-15 03:51:51 +00:00
|
|
|
DefaultInterface string `json:"default_interface,omitempty"`
|
2022-07-02 14:55:10 +00:00
|
|
|
}
|
|
|
|
|
2022-07-02 17:57:04 +00:00
|
|
|
func (o RouteOptions) Equals(other RouteOptions) bool {
|
|
|
|
return common.ComparablePtrEquals(o.GeoIP, other.GeoIP) &&
|
2022-07-05 01:05:35 +00:00
|
|
|
common.ComparablePtrEquals(o.Geosite, other.Geosite) &&
|
2022-07-02 17:57:04 +00:00
|
|
|
common.SliceEquals(o.Rules, other.Rules)
|
|
|
|
}
|
|
|
|
|
2022-07-02 14:55:10 +00:00
|
|
|
type GeoIPOptions struct {
|
|
|
|
Path string `json:"path,omitempty"`
|
|
|
|
DownloadURL string `json:"download_url,omitempty"`
|
|
|
|
DownloadDetour string `json:"download_detour,omitempty"`
|
|
|
|
}
|
|
|
|
|
2022-07-05 01:05:35 +00:00
|
|
|
type GeositeOptions struct {
|
|
|
|
Path string `json:"path,omitempty"`
|
|
|
|
DownloadURL string `json:"download_url,omitempty"`
|
|
|
|
DownloadDetour string `json:"download_detour,omitempty"`
|
|
|
|
}
|
|
|
|
|
2022-07-02 06:07:50 +00:00
|
|
|
type _Rule struct {
|
2022-07-03 15:23:18 +00:00
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
DefaultOptions DefaultRule `json:"-"`
|
|
|
|
LogicalOptions LogicalRule `json:"-"`
|
2022-07-02 06:07:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Rule _Rule
|
|
|
|
|
2022-07-02 17:57:04 +00:00
|
|
|
func (r Rule) Equals(other Rule) bool {
|
|
|
|
return r.Type == other.Type &&
|
2022-07-03 15:23:18 +00:00
|
|
|
r.DefaultOptions.Equals(other.DefaultOptions) &&
|
|
|
|
r.LogicalOptions.Equals(other.LogicalOptions)
|
2022-07-02 17:57:04 +00:00
|
|
|
}
|
|
|
|
|
2022-07-02 19:42:57 +00:00
|
|
|
func (r Rule) MarshalJSON() ([]byte, error) {
|
2022-07-02 17:57:04 +00:00
|
|
|
var v any
|
2022-07-02 06:07:50 +00:00
|
|
|
switch r.Type {
|
2022-07-02 17:57:04 +00:00
|
|
|
case C.RuleTypeDefault:
|
2022-07-07 13:47:21 +00:00
|
|
|
r.Type = ""
|
2022-07-02 17:57:04 +00:00
|
|
|
v = r.DefaultOptions
|
2022-07-02 06:07:50 +00:00
|
|
|
case C.RuleTypeLogical:
|
2022-07-02 17:57:04 +00:00
|
|
|
v = r.LogicalOptions
|
2022-07-02 06:07:50 +00:00
|
|
|
default:
|
2022-07-02 17:57:04 +00:00
|
|
|
return nil, E.New("unknown rule type: " + r.Type)
|
2022-07-02 06:07:50 +00:00
|
|
|
}
|
2022-07-02 19:42:57 +00:00
|
|
|
return MarshallObjects((_Rule)(r), v)
|
2022-07-02 06:07:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Rule) UnmarshalJSON(bytes []byte) error {
|
|
|
|
err := json.Unmarshal(bytes, (*_Rule)(r))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-02 17:57:04 +00:00
|
|
|
var v any
|
2022-07-02 06:07:50 +00:00
|
|
|
switch r.Type {
|
2022-07-10 08:41:38 +00:00
|
|
|
case "", C.RuleTypeDefault:
|
2022-07-07 13:47:21 +00:00
|
|
|
r.Type = C.RuleTypeDefault
|
2022-07-02 17:57:04 +00:00
|
|
|
v = &r.DefaultOptions
|
2022-07-02 06:07:50 +00:00
|
|
|
case C.RuleTypeLogical:
|
2022-07-02 17:57:04 +00:00
|
|
|
v = &r.LogicalOptions
|
2022-07-02 06:07:50 +00:00
|
|
|
default:
|
2022-07-02 17:57:04 +00:00
|
|
|
return E.New("unknown rule type: " + r.Type)
|
2022-07-02 06:07:50 +00:00
|
|
|
}
|
2022-07-03 03:28:15 +00:00
|
|
|
err = UnmarshallExcluded(bytes, (*_Rule)(r), v)
|
|
|
|
if err != nil {
|
|
|
|
return E.Cause(err, "route rule")
|
|
|
|
}
|
|
|
|
return nil
|
2022-07-02 06:07:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultRule struct {
|
2022-07-24 08:21:06 +00:00
|
|
|
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"`
|
|
|
|
PackageName Listable[string] `json:"package_name,omitempty"`
|
|
|
|
User Listable[string] `json:"user,omitempty"`
|
|
|
|
UserID Listable[int32] `json:"user_id,omitempty"`
|
|
|
|
Invert bool `json:"invert,omitempty"`
|
|
|
|
Outbound string `json:"outbound,omitempty"`
|
2022-07-02 06:07:50 +00:00
|
|
|
}
|
|
|
|
|
2022-07-02 17:57:04 +00:00
|
|
|
func (r DefaultRule) IsValid() bool {
|
|
|
|
var defaultValue DefaultRule
|
|
|
|
defaultValue.Outbound = r.Outbound
|
|
|
|
return !r.Equals(defaultValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r DefaultRule) Equals(other DefaultRule) bool {
|
|
|
|
return common.ComparableSliceEquals(r.Inbound, other.Inbound) &&
|
|
|
|
r.IPVersion == other.IPVersion &&
|
|
|
|
r.Network == other.Network &&
|
2022-07-17 07:11:26 +00:00
|
|
|
common.ComparableSliceEquals(r.User, other.User) &&
|
2022-07-02 17:57:04 +00:00
|
|
|
common.ComparableSliceEquals(r.Protocol, other.Protocol) &&
|
|
|
|
common.ComparableSliceEquals(r.Domain, other.Domain) &&
|
|
|
|
common.ComparableSliceEquals(r.DomainSuffix, other.DomainSuffix) &&
|
|
|
|
common.ComparableSliceEquals(r.DomainKeyword, other.DomainKeyword) &&
|
2022-07-04 07:51:41 +00:00
|
|
|
common.ComparableSliceEquals(r.DomainRegex, other.DomainRegex) &&
|
2022-07-05 01:05:35 +00:00
|
|
|
common.ComparableSliceEquals(r.Geosite, other.Geosite) &&
|
2022-07-02 17:57:04 +00:00
|
|
|
common.ComparableSliceEquals(r.SourceGeoIP, other.SourceGeoIP) &&
|
|
|
|
common.ComparableSliceEquals(r.GeoIP, other.GeoIP) &&
|
|
|
|
common.ComparableSliceEquals(r.SourceIPCIDR, other.SourceIPCIDR) &&
|
|
|
|
common.ComparableSliceEquals(r.IPCIDR, other.IPCIDR) &&
|
|
|
|
common.ComparableSliceEquals(r.SourcePort, other.SourcePort) &&
|
2022-07-24 08:21:06 +00:00
|
|
|
common.ComparableSliceEquals(r.SourcePortRange, other.SourcePortRange) &&
|
2022-07-02 17:57:04 +00:00
|
|
|
common.ComparableSliceEquals(r.Port, other.Port) &&
|
2022-07-24 08:21:06 +00:00
|
|
|
common.ComparableSliceEquals(r.PortRange, other.PortRange) &&
|
2022-07-23 11:01:41 +00:00
|
|
|
common.ComparableSliceEquals(r.ProcessName, other.ProcessName) &&
|
|
|
|
common.ComparableSliceEquals(r.PackageName, other.PackageName) &&
|
|
|
|
common.ComparableSliceEquals(r.User, other.User) &&
|
|
|
|
common.ComparableSliceEquals(r.UserID, other.UserID) &&
|
2022-07-24 05:44:26 +00:00
|
|
|
r.Invert == other.Invert &&
|
2022-07-02 17:57:04 +00:00
|
|
|
r.Outbound == other.Outbound
|
|
|
|
}
|
|
|
|
|
2022-07-02 06:07:50 +00:00
|
|
|
type LogicalRule struct {
|
|
|
|
Mode string `json:"mode"`
|
|
|
|
Rules []DefaultRule `json:"rules,omitempty"`
|
2022-07-24 06:05:06 +00:00
|
|
|
Invert bool `json:"invert,omitempty"`
|
2022-07-02 06:07:50 +00:00
|
|
|
Outbound string `json:"outbound,omitempty"`
|
|
|
|
}
|
2022-07-02 17:57:04 +00:00
|
|
|
|
|
|
|
func (r LogicalRule) IsValid() bool {
|
|
|
|
return len(r.Rules) > 0 && common.All(r.Rules, DefaultRule.IsValid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r LogicalRule) Equals(other LogicalRule) bool {
|
|
|
|
return r.Mode == other.Mode &&
|
|
|
|
common.SliceEquals(r.Rules, other.Rules) &&
|
2022-07-24 06:05:06 +00:00
|
|
|
r.Invert == other.Invert &&
|
2022-07-02 17:57:04 +00:00
|
|
|
r.Outbound == other.Outbound
|
|
|
|
}
|