Skip to content

Commit a8b98e0

Browse files
committed
Add 'force update' and 'reload engine' requests, minor changes and fixes
1 parent f9b9f3a commit a8b98e0

File tree

1 file changed

+56
-8
lines changed

1 file changed

+56
-8
lines changed

src/main.rs

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ use adblock::lists::{FilterFormat, FilterSet};
1313

1414
use attohttpc;
1515

16-
fn handle_client(mut stream: UnixStream, blocker: Arc<Engine>) {
16+
enum InitType {
17+
Default,
18+
Reload,
19+
Update
20+
}
21+
22+
fn handle_client(mut stream: UnixStream, mut blocker: Arc<Engine>) {
1723
let reader = BufReader::new(stream.try_clone().unwrap());
1824
for line in reader.lines() {
1925
let line = line.unwrap();
@@ -54,6 +60,16 @@ fn handle_client(mut stream: UnixStream, blocker: Arc<Engine>) {
5460

5561
res.push_str(&style);
5662
},
63+
"r" => {
64+
// reload engine request
65+
blocker = Arc::new(setup_blocker(InitType::Reload));
66+
res.push('0');
67+
},
68+
"u" => {
69+
// force update request
70+
blocker = Arc::new(setup_blocker(InitType::Update));
71+
res.push('0');
72+
},
5773
_ => {
5874
res.push_str("Unknown code supplied");
5975
}
@@ -87,11 +103,11 @@ fn update_list(url: &str, lists_dir: &str) -> String {
87103
return url.to_string() + " " + &stamp.as_secs().to_string();
88104
}
89105
}
90-
106+
91107
return url.to_string();
92108
}
93109

94-
fn parse_urls(urls_file: &str, lists_dir: &str) -> bool {
110+
fn parse_urls(urls_file: &str, lists_dir: &str, force_update: bool) -> bool {
95111
fs::create_dir_all(&lists_dir).unwrap();
96112
let mut updated = false;
97113

@@ -110,7 +126,7 @@ fn parse_urls(urls_file: &str, lists_dir: &str) -> bool {
110126
let mut parts = line.split(' ');
111127
let url = parts.next().unwrap();
112128

113-
if parts.clone().count() == 0 || parts.next().unwrap().parse::<u64>().unwrap() < timestamp.as_secs() {
129+
if !line.starts_with('#') && (force_update || parts.clone().count() == 0 || parts.next().unwrap().parse::<u64>().unwrap() < timestamp.as_secs()) {
114130
// list needs to be updated
115131
updated = true;
116132
out.push_str(&update_list(&url, &lists_dir));
@@ -123,6 +139,9 @@ fn parse_urls(urls_file: &str, lists_dir: &str) -> bool {
123139

124140
file.seek(std::io::SeekFrom::Start(0)).unwrap();
125141
file.write_all(&out.into_bytes()).unwrap();
142+
} else {
143+
let mut file = fs::File::create(urls_file).unwrap();
144+
file.write(b"# Add your filter list urls here; lines starting with # will be ignored; timestamps right after urls determine the expiration time\n").unwrap();
126145
}
127146

128147
return updated;
@@ -181,20 +200,49 @@ fn start_server(socket_path: &str, blocker: Engine) {
181200
thread::spawn(move || handle_client(stream, blocker));
182201
}
183202
Err(err) => {
184-
println!("Error: {}", err);
203+
eprintln!("Error: {}", err);
185204
}
186205
}
187206
}
188207
}
189208

190-
fn main() {
209+
fn setup_blocker(init_type: InitType) -> Engine {
191210
let home_dir = var("HOME").expect("Can't find environment variable $HOME");
192211
let config_dir = home_dir.to_owned() + "/.config/ars";
193212
let lists_dir = config_dir.to_owned() + "/lists";
194213
let engine_file = config_dir.to_owned() + "/engine";
195214
let urls_file = config_dir.to_owned() + "/urls";
215+
let custom_filters_file = lists_dir.to_owned() + "/custom";
216+
217+
let updated = match init_type {
218+
InitType::Default => {
219+
parse_urls(&urls_file, &lists_dir, false)
220+
},
221+
InitType::Reload => {
222+
parse_urls(&urls_file, &lists_dir, false);
223+
true
224+
},
225+
InitType::Update => {
226+
parse_urls(&urls_file, &lists_dir, true)
227+
}
228+
};
196229

197-
let updated = parse_urls(&urls_file, &lists_dir);
198-
let blocker = init_engine(&engine_file, &lists_dir, updated);
230+
let custom_file = fs::OpenOptions::new().write(true).create_new(true).open(&custom_filters_file);
231+
match custom_file {
232+
Ok(mut file) => {
233+
file.write(b"# Add your custom network and cosmetic filters here, lines starting with # will be ignored\n").unwrap();
234+
}
235+
Err(err) => {
236+
if err.kind() != std::io::ErrorKind::AlreadyExists {
237+
eprintln!("Can't create custom filters file: {}", err);
238+
}
239+
}
240+
}
241+
242+
return init_engine(&engine_file, &lists_dir, updated);
243+
}
244+
245+
fn main() {
246+
let blocker = setup_blocker(InitType::Default);
199247
start_server("/tmp/ars", blocker);
200248
}

0 commit comments

Comments
 (0)