From 40164efff80bb8f2943e5f7df488930c7da288d4 Mon Sep 17 00:00:00 2001 From: Hiddify <114227601+hiddify-com@users.noreply.github.com> Date: Thu, 4 Jul 2024 21:05:38 +0200 Subject: [PATCH] add basic routing and block ads, remove old geo assets --- config/config.go | 75 ++++++++++++++++++++++++++++++++++++++---------- config/option.go | 32 +++++++++++---------- config/rules.go | 13 +++++---- 3 files changed, 84 insertions(+), 36 deletions(-) diff --git a/config/config.go b/config/config.go index c6e2802..d06fcce 100644 --- a/config/config.go +++ b/config/config.go @@ -10,6 +10,7 @@ import ( "net/netip" "net/url" "strings" + "time" C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-box/option" @@ -383,24 +384,68 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro Rules: routeRules, AutoDetectInterface: true, OverrideAndroidVPN: true, - // RuleSet: []option.RuleSet{ - // { - // Type: C.RuleSetTypeRemote, - // Tag: "geoip-" + opt, - // RemoteOptions: option.RemoteRuleSet{ - // URL: "https://raw.githubusercontent.com/Chocolate4U/Iran-sing-box-rules/rule-set/geoip-ir.srs", - // UpdateInterval: option.Duration(5 * time.day), - // }, - // }, + // RuleSet: []option.RuleSet{}, + // GeoIP: &option.GeoIPOptions{ + // Path: opt.GeoIPPath, + // }, + // Geosite: &option.GeositeOptions{ + // Path: opt.GeoSitePath, // }, - GeoIP: &option.GeoIPOptions{ - Path: opt.GeoIPPath, - }, - Geosite: &option.GeositeOptions{ - Path: opt.GeoSitePath, - }, } + fmt.Println("Region==========================", opt.Region) + if opt.Region != "other" { + options.Route.RuleSet = append(options.Route.RuleSet, option.RuleSet{ + Type: C.RuleSetTypeRemote, + Tag: "geoip-" + opt.Region, + RemoteOptions: option.RemoteRuleSet{ + URL: "https://raw.githubusercontent.com/Chocolate4U/Iran-sing-box-rules/rule-set/geoip-" + opt.Region + ".srs", + UpdateInterval: option.Duration(5 * time.Hour * 24), + }, + }) + options.Route.RuleSet = append(options.Route.RuleSet, option.RuleSet{ + Type: C.RuleSetTypeRemote, + Tag: "geosite-" + opt.Region, + RemoteOptions: option.RemoteRuleSet{ + URL: "https://raw.githubusercontent.com/Chocolate4U/Iran-sing-box-rules/rule-set/geosite-" + opt.Region + ".srs", + UpdateInterval: option.Duration(5 * time.Hour * 24), + }, + }) + + routeRuleIp := option.Rule{ + Type: C.RuleTypeDefault, + DefaultOptions: option.DefaultRule{ + RuleSet: []string{"geoip-" + opt.Region}, + Outbound: OutboundDirectTag, + }, + } + routeRuleSite := option.Rule{ + Type: C.RuleTypeDefault, + DefaultOptions: option.DefaultRule{ + RuleSet: []string{"geosite-" + opt.Region}, + Outbound: OutboundDirectTag, + }, + } + options.Route.Rules = append([]option.Rule{routeRuleIp, routeRuleSite}, options.Route.Rules...) + } + if opt.BlockAds { + options.Route.RuleSet = append(options.Route.RuleSet, option.RuleSet{ + Type: C.RuleSetTypeRemote, + Tag: "geosite-ads", + RemoteOptions: option.RemoteRuleSet{ + URL: "https://raw.githubusercontent.com/Chocolate4U/Iran-sing-box-rules/rule-set/geosite-ads.srs", + UpdateInterval: option.Duration(5 * time.Hour * 24), + }, + }) + routeRule := option.Rule{ + Type: C.RuleTypeDefault, + DefaultOptions: option.DefaultRule{ + RuleSet: []string{"geosite-ads"}, + Outbound: OutboundBlockTag, + }, + } + options.Route.Rules = append([]option.Rule{routeRule}, options.Route.Rules...) + } var outbounds []option.Outbound var tags []string OutboundMainProxyTag = OutboundSelectTag diff --git a/config/option.go b/config/option.go index 11cb9d0..6bda618 100644 --- a/config/option.go +++ b/config/option.go @@ -6,18 +6,20 @@ import ( ) type ConfigOptions struct { - EnableFullConfig bool `json:"enable-full-config"` - LogLevel string `json:"log-level"` - EnableClashApi bool `json:"enable-clash-api"` - ClashApiPort uint16 `json:"clash-api-port"` - ClashApiSecret string `json:"web-secret"` - GeoIPPath string `json:"geoip-path"` - GeoSitePath string `json:"geosite-path"` - Rules []Rule `json:"rules"` - Warp WarpOptions `json:"warp"` - Warp2 WarpOptions `json:"warp2"` - Mux MuxOptions `json:"mux"` - TLSTricks TLSTricks `json:"tls-tricks"` + EnableFullConfig bool `json:"enable-full-config"` + LogLevel string `json:"log-level"` + EnableClashApi bool `json:"enable-clash-api"` + ClashApiPort uint16 `json:"clash-api-port"` + ClashApiSecret string `json:"web-secret"` + Region string `json:"region"` + BlockAds bool `json:"block-ads"` + // GeoIPPath string `json:"geoip-path"` + // GeoSitePath string `json:"geosite-path"` + Rules []Rule `json:"rules"` + Warp WarpOptions `json:"warp"` + Warp2 WarpOptions `json:"warp2"` + Mux MuxOptions `json:"mux"` + TLSTricks TLSTricks `json:"tls-tricks"` DNSOptions InboundOptions URLTestOptions @@ -124,9 +126,9 @@ func DefaultConfigOptions() *ConfigOptions { EnableClashApi: true, ClashApiPort: 6756, ClashApiSecret: "", - GeoIPPath: "geoip.db", - GeoSitePath: "geosite.db", - Rules: []Rule{}, + // GeoIPPath: "geoip.db", + // GeoSitePath: "geosite.db", + Rules: []Rule{}, Mux: MuxOptions{ Enable: false, Padding: true, diff --git a/config/rules.go b/config/rules.go index 8ce7a91..d9be177 100644 --- a/config/rules.go +++ b/config/rules.go @@ -8,12 +8,13 @@ import ( ) type Rule struct { - Domains string `json:"domains"` - IP string `json:"ip"` - Port string `json:"port"` - Network string `json:"network"` - Protocol string `json:"protocol"` - Outbound string `json:"outbound"` + RuleSetUrl string `json:"rule-set-url"` + Domains string `json:"domains"` + IP string `json:"ip"` + Port string `json:"port"` + Network string `json:"network"` + Protocol string `json:"protocol"` + Outbound string `json:"outbound"` } func (r *Rule) MakeRule() option.DefaultRule {