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> { 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::>(); 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(); } }