2022-07-06 23:11:48 +08:00
|
|
|
package option
|
|
|
|
|
2022-07-07 21:47:21 +08:00
|
|
|
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-07 21:47:21 +08:00
|
|
|
"github.com/sagernet/sing/common"
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
)
|
|
|
|
|
2022-07-06 23:11:48 +08:00
|
|
|
type DNSOptions struct {
|
2022-07-11 13:21:17 +08:00
|
|
|
Servers []DNSServerOptions `json:"servers,omitempty"`
|
|
|
|
Rules []DNSRule `json:"rules,omitempty"`
|
|
|
|
Final string `json:"final,omitempty"`
|
2022-07-06 23:39:17 +08:00
|
|
|
DNSClientOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
type DNSClientOptions struct {
|
2022-07-11 13:21:17 +08:00
|
|
|
Strategy DomainStrategy `json:"strategy,omitempty"`
|
|
|
|
DisableCache bool `json:"disable_cache,omitempty"`
|
|
|
|
DisableExpire bool `json:"disable_expire,omitempty"`
|
2022-07-06 23:11:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type DNSServerOptions struct {
|
2022-07-11 18:44:59 +08:00
|
|
|
Tag string `json:"tag,omitempty"`
|
|
|
|
Address string `json:"address"`
|
|
|
|
AddressResolver string `json:"address_resolver,omitempty"`
|
|
|
|
AddressStrategy DomainStrategy `json:"address_strategy,omitempty"`
|
|
|
|
AddressFallbackDelay Duration `json:"address_fallback_delay,omitempty"`
|
2022-08-22 12:01:50 +08:00
|
|
|
Strategy DomainStrategy `json:"strategy,omitempty"`
|
2022-07-11 18:44:59 +08:00
|
|
|
Detour string `json:"detour,omitempty"`
|
2022-07-06 23:11:48 +08:00
|
|
|
}
|
2022-07-07 21:47:21 +08:00
|
|
|
|
|
|
|
type _DNSRule struct {
|
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
DefaultOptions DefaultDNSRule `json:"-"`
|
|
|
|
LogicalOptions LogicalDNSRule `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type DNSRule _DNSRule
|
|
|
|
|
|
|
|
func (r DNSRule) MarshalJSON() ([]byte, error) {
|
|
|
|
var v any
|
|
|
|
switch r.Type {
|
|
|
|
case C.RuleTypeDefault:
|
2022-07-24 14:05:06 +08:00
|
|
|
r.Type = ""
|
2022-07-07 21:47:21 +08:00
|
|
|
v = r.DefaultOptions
|
|
|
|
case C.RuleTypeLogical:
|
|
|
|
v = r.LogicalOptions
|
|
|
|
default:
|
|
|
|
return nil, E.New("unknown rule type: " + r.Type)
|
|
|
|
}
|
|
|
|
return MarshallObjects((_DNSRule)(r), v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DNSRule) UnmarshalJSON(bytes []byte) error {
|
|
|
|
err := json.Unmarshal(bytes, (*_DNSRule)(r))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var v any
|
|
|
|
switch r.Type {
|
2022-07-26 13:53:25 +08:00
|
|
|
case "", C.RuleTypeDefault:
|
|
|
|
r.Type = C.RuleTypeDefault
|
2022-07-07 21:47:21 +08:00
|
|
|
v = &r.DefaultOptions
|
|
|
|
case C.RuleTypeLogical:
|
|
|
|
v = &r.LogicalOptions
|
|
|
|
default:
|
|
|
|
return E.New("unknown rule type: " + r.Type)
|
|
|
|
}
|
|
|
|
err = UnmarshallExcluded(bytes, (*_DNSRule)(r), v)
|
|
|
|
if err != nil {
|
|
|
|
return E.Cause(err, "dns route rule")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultDNSRule struct {
|
2022-07-24 16:21:06 +08:00
|
|
|
Inbound Listable[string] `json:"inbound,omitempty"`
|
2022-08-16 23:46:05 +08:00
|
|
|
IPVersion int `json:"ip_version,omitempty"`
|
2022-07-24 16:21:06 +08:00
|
|
|
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"`
|
|
|
|
SourceIPCIDR Listable[string] `json:"source_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"`
|
2022-08-30 10:44:40 +08:00
|
|
|
ProcessPath Listable[string] `json:"process_path,omitempty"`
|
2022-07-24 16:21:06 +08:00
|
|
|
PackageName Listable[string] `json:"package_name,omitempty"`
|
|
|
|
User Listable[string] `json:"user,omitempty"`
|
|
|
|
UserID Listable[int32] `json:"user_id,omitempty"`
|
|
|
|
Outbound Listable[string] `json:"outbound,omitempty"`
|
2022-09-10 14:09:47 +08:00
|
|
|
ClashMode string `json:"clash_mode,omitempty"`
|
2022-07-24 16:21:06 +08:00
|
|
|
Invert bool `json:"invert,omitempty"`
|
|
|
|
Server string `json:"server,omitempty"`
|
|
|
|
DisableCache bool `json:"disable_cache,omitempty"`
|
2022-07-07 21:47:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r DefaultDNSRule) IsValid() bool {
|
|
|
|
var defaultValue DefaultDNSRule
|
2022-07-25 11:29:46 +08:00
|
|
|
defaultValue.Invert = r.Invert
|
2022-07-07 21:47:21 +08:00
|
|
|
defaultValue.Server = r.Server
|
2022-07-25 11:29:46 +08:00
|
|
|
defaultValue.DisableCache = r.DisableCache
|
|
|
|
return !reflect.DeepEqual(r, defaultValue)
|
2022-07-07 21:47:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type LogicalDNSRule struct {
|
2022-07-24 14:05:06 +08:00
|
|
|
Mode string `json:"mode"`
|
|
|
|
Rules []DefaultDNSRule `json:"rules,omitempty"`
|
|
|
|
Invert bool `json:"invert,omitempty"`
|
|
|
|
Server string `json:"server,omitempty"`
|
|
|
|
DisableCache bool `json:"disable_cache,omitempty"`
|
2022-07-07 21:47:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r LogicalDNSRule) IsValid() bool {
|
|
|
|
return len(r.Rules) > 0 && common.All(r.Rules, DefaultDNSRule.IsValid)
|
|
|
|
}
|