Initial commit

This commit is contained in:
椰子 2023-11-08 23:38:57 +08:00
commit 8358a18de3
4 changed files with 1250 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.idea
/target
chn_list.rsc

1176
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

11
Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "cn_cidr"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
reqwest = "0.10"
futures = "0.3.29"
tokio = "0.2.25"

59
src/main.rs Normal file
View File

@ -0,0 +1,59 @@
use std::error::Error;
use std::fs::File;
use std::io::Write;
use tokio::runtime::Runtime;
use reqwest;
fn main() {
Runtime::new().unwrap()
.block_on(create())
.unwrap();
println!("1. Disable mangle rule: {}", ":foreach i in=[/ip firewall mangle find comment=\"breakwall\"] do={ /ip firewall mangle disable $i }");
println!("2. Remove old CIDR list: {}", ":foreach i in=[ find list=$address_list ] do={ remove number=$i }");
println!("3. Load new CIDR list: {}", "/import chn_list.rsc")
}
async fn create() -> Result<(), Box<dyn Error>> {
let url = "https://ispip.clang.cn/all_cn.txt";
let dest = "chn_list.rsc";
let mut dest = File::create(dest).unwrap();
// address list name
let list = "chn_list";
let clean_command = format!(":foreach i in=[/ip/firewall/address-list/find list=\"{}\"] do={{remove numbers=$i}}\n", list);
dest.write(clean_command.as_bytes()).unwrap();
// private address list
let class_a = vec!["10.0.0.0/8", "127.0.0.0/8", "0.0.0.0/8"];
let class_b = vec!["172.16.0.0/12", "169.254.0.0/16"];
let class_c = vec!["192.168.0.0/16"];
write_to_file(&mut dest, list, &class_a);
write_to_file(&mut dest, list, &class_b);
write_to_file(&mut dest, list, &class_c);
// isp private address block
let isp = vec!["100.64.0.0/10"];
write_to_file(&mut dest, list, &isp);
// request china cidr from network
let text = reqwest::get(url).await?
.text().await?;
let lines = text.lines();
let lines = lines.collect::<Vec<&str>>();
write_to_file(&mut dest, list, &lines);
// Apple push notifications
// entire block
let apns = vec!["17.0.0.0/8"];
// network ranges
// let apns = vec!["17.249.0.0/16", "17.252.0.0/16", "17.57.144.0/22", "17.188.128.0/18", "17.188.20.0/23"];
write_to_file(&mut dest, list, &apns);
Ok(())
}
fn write_to_file(file: &mut File, list: &str, address_list: &Vec<&str>) {
for x in address_list {
let line = format!("/ip/firewall/address-list add list={} address={}\n", list, x);
file.write(line.as_bytes()).unwrap();
}
}