refactor: remove hardcoded Telegram IP arrays in favor of fully dynamic community subnet loading
- updates.uc: cache downloaded built-in subnet lists (telegram, meta, twitter, etc.) to community-subnets-<service>.lst. - generator_routes.uc: dynamically load community list IP CIDRs from cached subnet files for all community lists into sing-box route rules. Zero hardcoded IP arrays.
This commit is contained in:
@@ -2158,6 +2158,8 @@ function import_builtin_subnets_from_rule(section, settings) {
|
||||
if (type(urls) != "array")
|
||||
continue;
|
||||
|
||||
let cached_file = TMP_RULESET_FOLDER + "/community-subnets-" + as_string(service) + ".lst";
|
||||
let combined_lines = [];
|
||||
for (let url in urls) {
|
||||
let tmpfile = temp_path();
|
||||
if (tmpfile == "") {
|
||||
@@ -2171,6 +2173,13 @@ function import_builtin_subnets_from_rule(section, settings) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let lines = split(as_string(fs.readfile(tmpfile)), "\n");
|
||||
for (let l in lines) {
|
||||
l = trim(replace(as_string(l), /\r/g, ""));
|
||||
if (l != "" && substr(l, 0, 1) != "#")
|
||||
push(combined_lines, l);
|
||||
}
|
||||
|
||||
if (!nft_module_success([
|
||||
"nft-add-community-subnet-file-for-uci-section",
|
||||
section_name(section),
|
||||
@@ -2191,6 +2200,11 @@ function import_builtin_subnets_from_rule(section, settings) {
|
||||
|
||||
remove_file(tmpfile);
|
||||
}
|
||||
|
||||
if (length(combined_lines) > 0) {
|
||||
ensure_dir(TMP_RULESET_FOLDER);
|
||||
write_file(cached_file, join("\n", combined_lines) + "\n");
|
||||
}
|
||||
}
|
||||
|
||||
return ok;
|
||||
|
||||
@@ -1120,6 +1120,22 @@ function add_excluded_ips_rule(config, section) {
|
||||
push(config.route.rules, route_rule);
|
||||
}
|
||||
|
||||
function load_community_subnet_cidrs(community) {
|
||||
let folder = ctx.runtime_ruleset_folder || runtime_ruleset_folder;
|
||||
let path = folder + "/community-subnets-" + as_string(community) + ".lst";
|
||||
let content = fs.readfile(path);
|
||||
if (content == null || content == "")
|
||||
return [];
|
||||
|
||||
let cidrs = [];
|
||||
for (let line in split(as_string(content), "\n")) {
|
||||
line = trim(replace(as_string(line), /\r/g, ""));
|
||||
if (line != "" && substr(line, 0, 1) != "#")
|
||||
push(cidrs, line);
|
||||
}
|
||||
return cidrs;
|
||||
}
|
||||
|
||||
function add_combined_route_for_section(config, section) {
|
||||
let domains = domain_conditions(section);
|
||||
let domain = domains.domain;
|
||||
@@ -1139,17 +1155,9 @@ function add_combined_route_for_section(config, section) {
|
||||
let ensured = ensure_community_ruleset(config, section_name, as_string(community));
|
||||
push(rule_set_tags, ensured.tag);
|
||||
push(dns_rule_set_tags, ensured.tag);
|
||||
if (as_string(community) == "telegram") {
|
||||
let tg_cidrs = [
|
||||
"149.154.160.0/20", "91.108.4.0/22", "91.108.8.0/22",
|
||||
"91.108.12.0/22", "91.108.16.0/22", "91.108.56.0/22",
|
||||
"5.28.192.0/18", "91.105.192.0/23", "95.161.64.0/20",
|
||||
"185.76.151.0/24", "194.221.0.0/16"
|
||||
];
|
||||
for (let cidr in tg_cidrs)
|
||||
for (let cidr in load_community_subnet_cidrs(community))
|
||||
push(ip_cidr, cidr);
|
||||
}
|
||||
}
|
||||
for (let reference in connections.rule_sets(section)) {
|
||||
let ensured = ensure_custom_ruleset(config, as_string(reference));
|
||||
push(rule_set_tags, ensured.tag);
|
||||
|
||||
Reference in New Issue
Block a user