2023-12-11 01:17:25 +08:00
|
|
|
mod client;
|
2024-01-24 18:14:50 +08:00
|
|
|
mod config;
|
|
|
|
mod py;
|
2023-12-11 01:17:25 +08:00
|
|
|
|
2024-01-24 18:14:50 +08:00
|
|
|
use colored::Colorize;
|
2023-12-11 18:17:55 +08:00
|
|
|
use rust_socketio::{ClientBuilder, Event, Payload, RawClient};
|
2023-12-11 00:00:01 +08:00
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
#[allow(unused)]
|
2024-01-24 18:14:50 +08:00
|
|
|
fn any_event(event: Event, payload: Payload, _client: RawClient) {
|
2023-12-24 17:27:15 +08:00
|
|
|
match payload {
|
|
|
|
Payload::Binary(ref data) => {
|
2024-01-24 18:14:50 +08:00
|
|
|
println!("event: {} |{:?}", event, data)
|
2023-12-24 17:27:15 +08:00
|
|
|
}
|
|
|
|
Payload::Text(ref data) => {
|
2024-01-24 18:14:50 +08:00
|
|
|
print!("event: {}", event.as_str().purple());
|
2023-12-24 17:27:15 +08:00
|
|
|
for value in data {
|
2024-01-24 18:14:50 +08:00
|
|
|
println!("|{}", value.to_string());
|
2023-12-24 17:27:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
// println!("event: {} |{:?}|id{:?}", event, payload, id)
|
2023-12-11 00:00:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ws_main() {
|
2024-02-10 21:26:57 +08:00
|
|
|
py::init_py();
|
2023-12-11 00:00:01 +08:00
|
|
|
// define a callback which is called when a payload is received
|
|
|
|
// this callback gets the payload as well as an instance of the
|
|
|
|
// socket to communicate with the server
|
2024-01-24 18:14:50 +08:00
|
|
|
let connect_call_back = |payload: Payload, _client: RawClient| match payload {
|
2023-12-24 17:27:15 +08:00
|
|
|
Payload::Text(values) => {
|
|
|
|
if let Some(value) = values.first() {
|
2024-01-25 22:08:02 +08:00
|
|
|
// if let Some("authSucceed") = value.as_str() {
|
|
|
|
// println!("{}", "已经登录到 icalingua!".green());
|
|
|
|
// }
|
|
|
|
match value.as_str() {
|
2024-02-10 21:26:57 +08:00
|
|
|
Some("authSucceed") => {
|
|
|
|
py::run();
|
|
|
|
println!("{}", "已经登录到 icalingua!".green())
|
|
|
|
}
|
2024-01-25 22:08:02 +08:00
|
|
|
Some("authFailed") => {
|
|
|
|
println!("{}", "登录到 icalingua 失败!".red());
|
|
|
|
panic!("登录失败")
|
|
|
|
}
|
|
|
|
Some("authRequired") => println!("{}", "需要登录到 icalingua!".yellow()),
|
|
|
|
_ => (),
|
2023-12-12 01:42:09 +08:00
|
|
|
}
|
2023-12-24 17:27:15 +08:00
|
|
|
}
|
2023-12-12 01:42:09 +08:00
|
|
|
}
|
2023-12-24 17:27:15 +08:00
|
|
|
_ => (),
|
2023-12-11 00:00:01 +08:00
|
|
|
};
|
2023-12-11 18:17:55 +08:00
|
|
|
// 从命令行获取 host 和 key
|
2023-12-24 17:27:15 +08:00
|
|
|
// 从命令行获取配置文件路径
|
2024-01-24 18:14:50 +08:00
|
|
|
let ica_config = config::IcaConfig::new_from_cli();
|
|
|
|
let ica_singer = client::IcalinguaSinger::new_from_config(ica_config);
|
2023-12-11 00:00:01 +08:00
|
|
|
|
|
|
|
// get a socket that is connected to the admin namespace
|
|
|
|
|
2023-12-24 17:27:15 +08:00
|
|
|
let socket = ClientBuilder::new(ica_singer.host.clone())
|
2024-02-10 12:18:09 +08:00
|
|
|
.transport_type(rust_socketio::TransportType::Websocket)
|
2023-12-11 00:00:01 +08:00
|
|
|
.on_any(any_event)
|
2023-12-12 01:42:09 +08:00
|
|
|
.on("message", connect_call_back)
|
2024-01-24 18:14:50 +08:00
|
|
|
.on("requireAuth", move |a, b| ica_singer.sign_callback(a, b))
|
2023-12-11 00:00:01 +08:00
|
|
|
.connect()
|
|
|
|
.expect("Connection failed");
|
|
|
|
|
|
|
|
std::thread::sleep(Duration::from_secs(10));
|
|
|
|
|
|
|
|
socket.disconnect().expect("Disconnect failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2024-02-10 12:18:09 +08:00
|
|
|
tracing_subscriber::fmt()
|
|
|
|
.with_max_level(tracing::Level::DEBUG)
|
|
|
|
.init();
|
2023-12-11 00:00:01 +08:00
|
|
|
ws_main();
|
|
|
|
}
|