-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.rs
228 lines (192 loc) · 6.37 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#![feature(
once_cell_try,
lazy_cell,
maybe_uninit_uninit_array,
maybe_uninit_slice,
io_error_other,
iter_intersperse
)]
#![warn(unsafe_op_in_unsafe_fn)]
#![allow(clippy::module_inception, non_snake_case)]
#![windows_subsystem = "windows"]
use anyhow::{anyhow, Context};
use dll_syringe::process::{OwnedProcess, Process};
use log::{debug, error, info, trace, warn};
use winapi::{
shared::minwindef::FALSE,
um::{processthreadsapi::OpenProcess, synchapi::WaitForSingleObject, winnt::PROCESS_TERMINATE},
};
use std::{env, io, os::windows::prelude::FromRawHandle, time::Duration};
use crate::{
args::{LogLevel, ARGS},
blocker::SpotifyAdBlocker,
logger::{Console, FileLog},
named_mutex::NamedMutex,
};
mod args;
mod blocker;
mod logger;
mod named_mutex;
mod resolver;
mod rpc;
mod spotify_process_scanner;
mod tray;
mod update;
const APP_NAME: &str = "BurntSushi";
const APP_AUTHOR: &str = "OpenByteDev";
const APP_VERSION: &str = env!("CARGO_PKG_VERSION");
const APP_NAME_WITH_VERSION: &str = concat!("BurntSushi v", env!("CARGO_PKG_VERSION"));
const DEFAULT_BLOCKER_FILE_NAME: &str = "BurntSushiBlocker_x64.dll";
const DEFAULT_FILTER_FILE_NAME: &str = "filter.toml";
#[tokio::main(flavor = "current_thread")]
async fn main() {
logger::global::init();
log::set_max_level(ARGS.log_level.into_level_filter());
if let Some(console) = Console::attach() {
logger::global::get().console = Some(console);
debug!("Attached to console");
}
if ARGS.console {
if let Some(console) = Console::alloc() {
logger::global::get().console = Some(console);
debug!("Allocated new console");
}
}
let mut log_file = ARGS.log_file.clone();
if log_file.is_none() && ARGS.log_level == LogLevel::Debug {
let mut auto_log_file = dirs::data_dir();
if let Some(ref mut log_file) = auto_log_file {
log_file.push("OpenByte");
log_file.push("BurntSushi");
log_file.push("BurntSushi.log");
}
log_file = auto_log_file;
}
if let Some(log_file) = log_file {
logger::global::get().file = Some(FileLog::new(log_file));
}
info!("{}", APP_NAME_WITH_VERSION);
trace!(
"Running from {}",
env::current_exe()
.unwrap_or_else(|_| "<unknown>".into())
.display()
);
if ARGS.install {
match handle_install().await {
Ok(()) => info!("App successfully installed."),
Err(e) => error!("Failed to install application: {e}"),
}
return;
}
if let Some(old_bin_path) = &ARGS.update_old_bin {
tokio::task::spawn(tokio::fs::remove_file(old_bin_path));
}
if ARGS.force_restart {
match terminate_other_instances() {
Ok(_) => debug!("Killed previously running instances"),
Err(err) => {
error!("Failed to open previously running instance (err={err})");
return;
}
}
}
if ARGS.ignore_singleton {
run().await;
} else {
let lock = NamedMutex::new(&format!("{APP_NAME} SINGLETON MUTEX")).unwrap();
let mut guard_result = lock.try_lock();
if ARGS.singleton_wait_for_shutdown {
while matches!(guard_result, Ok(None)) {
tokio::time::sleep(Duration::from_millis(100)).await;
guard_result = lock.try_lock();
}
}
match guard_result {
Ok(Some(_guard)) => run().await,
Ok(None) => {
error!("App is already running. (use --ignore-singleton to ignore)\nExiting...")
}
Err(e) => error!(
"Failed to lock singleton mutex: {} (use --ignore-singleton to ignore) ",
e
),
};
}
logger::global::unset();
}
async fn run() {
let mut system_tray = tray::SystemTrayManager::build_and_run().await.unwrap();
let mut app = SpotifyAdBlocker::new();
let (update_restart_tx, update_restart_rx) = tokio::sync::oneshot::channel();
tokio::task::spawn(async move {
match update::update().await {
Ok(true) => update_restart_tx.send(()).unwrap(),
Ok(false) => {}
Err(e) => error!("App update failed: {e:#}"),
}
});
tokio::select! {
_ = app.run() => {
}
_ = wait_for_ctrl_c() => {
debug!("Ctrl-C received");
}
_ = system_tray.wait_for_exit() => {
debug!("System tray exited");
}
Ok(_) = update_restart_rx => {
debug!("Shutting down due to update");
}
}
info!("Shutting down...");
app.stop().await;
system_tray.exit().await;
info!("Exiting...");
}
async fn wait_for_ctrl_c() -> Result<(), ctrlc::Error> {
let (tx, rx) = tokio::sync::oneshot::channel();
let mut handler = Some(move || tx.send(()).unwrap());
ctrlc::set_handler(move || {
if let Some(h) = handler.take() {
h()
}
})?;
rx.await.unwrap();
Ok(())
}
async fn handle_install() -> anyhow::Result<()> {
if !is_elevated::is_elevated() {
return Err(anyhow!("Must be run as administrator"));
}
let current_location = env::current_exe().context("Failed to locate current executable")?;
let blocker_location = current_location
.parent()
.ok_or_else(|| anyhow!("Failed to determine parent directory"))?
.join(DEFAULT_BLOCKER_FILE_NAME);
resolver::resolve_blocker(Some(&blocker_location))
.await
.context("Failed to write blocker to disk")?;
Ok(())
}
fn terminate_other_instances() -> anyhow::Result<()> {
let other_processes = OwnedProcess::find_all_by_name(APP_NAME)
.into_iter()
.filter(|p| !p.is_current());
for process in other_processes {
let handle = unsafe {
OpenProcess(
PROCESS_TERMINATE,
FALSE,
process.pid().map_or(0, |v| v.get()),
)
};
if handle.is_null() {
Err(io::Error::last_os_error()).context("Failed to access other running instances")?;
}
let process = unsafe { OwnedProcess::from_raw_handle(handle) };
process.kill().context("Failed to kill process.")?;
let _ = unsafe { WaitForSingleObject(handle, Duration::from_secs(5).as_millis() as _) };
}
Ok(())
}