add basic routing and block ads, remove old geo assets

This commit is contained in:
Hiddify
2024-07-04 21:05:38 +02:00
parent 4bd3392f27
commit 40164efff8
3 changed files with 84 additions and 36 deletions

View File

@@ -10,6 +10,7 @@ import (
"net/netip" "net/netip"
"net/url" "net/url"
"strings" "strings"
"time"
C "github.com/sagernet/sing-box/constant" C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
@@ -383,24 +384,68 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro
Rules: routeRules, Rules: routeRules,
AutoDetectInterface: true, AutoDetectInterface: true,
OverrideAndroidVPN: true, OverrideAndroidVPN: true,
// RuleSet: []option.RuleSet{ // RuleSet: []option.RuleSet{},
// { // GeoIP: &option.GeoIPOptions{
// Type: C.RuleSetTypeRemote, // Path: opt.GeoIPPath,
// 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),
// }, // },
// Geosite: &option.GeositeOptions{
// Path: opt.GeoSitePath,
// }, // },
// }, }
GeoIP: &option.GeoIPOptions{ fmt.Println("Region==========================", opt.Region)
Path: opt.GeoIPPath, 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),
}, },
Geosite: &option.GeositeOptions{ })
Path: opt.GeoSitePath, 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 outbounds []option.Outbound
var tags []string var tags []string
OutboundMainProxyTag = OutboundSelectTag OutboundMainProxyTag = OutboundSelectTag

View File

@@ -11,8 +11,10 @@ type ConfigOptions struct {
EnableClashApi bool `json:"enable-clash-api"` EnableClashApi bool `json:"enable-clash-api"`
ClashApiPort uint16 `json:"clash-api-port"` ClashApiPort uint16 `json:"clash-api-port"`
ClashApiSecret string `json:"web-secret"` ClashApiSecret string `json:"web-secret"`
GeoIPPath string `json:"geoip-path"` Region string `json:"region"`
GeoSitePath string `json:"geosite-path"` BlockAds bool `json:"block-ads"`
// GeoIPPath string `json:"geoip-path"`
// GeoSitePath string `json:"geosite-path"`
Rules []Rule `json:"rules"` Rules []Rule `json:"rules"`
Warp WarpOptions `json:"warp"` Warp WarpOptions `json:"warp"`
Warp2 WarpOptions `json:"warp2"` Warp2 WarpOptions `json:"warp2"`
@@ -124,8 +126,8 @@ func DefaultConfigOptions() *ConfigOptions {
EnableClashApi: true, EnableClashApi: true,
ClashApiPort: 6756, ClashApiPort: 6756,
ClashApiSecret: "", ClashApiSecret: "",
GeoIPPath: "geoip.db", // GeoIPPath: "geoip.db",
GeoSitePath: "geosite.db", // GeoSitePath: "geosite.db",
Rules: []Rule{}, Rules: []Rule{},
Mux: MuxOptions{ Mux: MuxOptions{
Enable: false, Enable: false,

View File

@@ -8,6 +8,7 @@ import (
) )
type Rule struct { type Rule struct {
RuleSetUrl string `json:"rule-set-url"`
Domains string `json:"domains"` Domains string `json:"domains"`
IP string `json:"ip"` IP string `json:"ip"`
Port string `json:"port"` Port string `json:"port"`