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{ // Geosite: &option.GeositeOptions{
// URL: "https://raw.githubusercontent.com/Chocolate4U/Iran-sing-box-rules/rule-set/geoip-ir.srs", // Path: opt.GeoSitePath,
// UpdateInterval: option.Duration(5 * time.day),
// },
// },
// }, // },
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 outbounds []option.Outbound
var tags []string var tags []string
OutboundMainProxyTag = OutboundSelectTag OutboundMainProxyTag = OutboundSelectTag

View File

@@ -6,18 +6,20 @@ import (
) )
type ConfigOptions struct { type ConfigOptions struct {
EnableFullConfig bool `json:"enable-full-config"` EnableFullConfig bool `json:"enable-full-config"`
LogLevel string `json:"log-level"` LogLevel string `json:"log-level"`
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"`
Rules []Rule `json:"rules"` // GeoIPPath string `json:"geoip-path"`
Warp WarpOptions `json:"warp"` // GeoSitePath string `json:"geosite-path"`
Warp2 WarpOptions `json:"warp2"` Rules []Rule `json:"rules"`
Mux MuxOptions `json:"mux"` Warp WarpOptions `json:"warp"`
TLSTricks TLSTricks `json:"tls-tricks"` Warp2 WarpOptions `json:"warp2"`
Mux MuxOptions `json:"mux"`
TLSTricks TLSTricks `json:"tls-tricks"`
DNSOptions DNSOptions
InboundOptions InboundOptions
URLTestOptions URLTestOptions
@@ -124,9 +126,9 @@ 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,
Padding: true, Padding: true,

View File

@@ -8,12 +8,13 @@ import (
) )
type Rule struct { type Rule struct {
Domains string `json:"domains"` RuleSetUrl string `json:"rule-set-url"`
IP string `json:"ip"` Domains string `json:"domains"`
Port string `json:"port"` IP string `json:"ip"`
Network string `json:"network"` Port string `json:"port"`
Protocol string `json:"protocol"` Network string `json:"network"`
Outbound string `json:"outbound"` Protocol string `json:"protocol"`
Outbound string `json:"outbound"`
} }
func (r *Rule) MakeRule() option.DefaultRule { func (r *Rule) MakeRule() option.DefaultRule {