From 0ed685b6ec0936bcb28edb42e643ef6be4817fa2 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Wed, 24 Jan 2024 18:14:50 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A8=8D=E5=BE=AE=E9=87=8D=E6=96=B0=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E4=BA=86=E4=B8=80=E4=B8=8B=E9=85=8D=E7=BD=AE=E8=BF=99?= =?UTF-8?q?=E8=BE=B9=E7=9A=84=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ica-rs/Cargo.lock | 141 +++++++++++++++++++++++++++++++++++++++++++ ica-rs/Cargo.toml | 8 +++ ica-rs/build.rs | 2 +- ica-rs/src/client.rs | 45 ++++++-------- ica-rs/src/config.rs | 35 +++++++++++ ica-rs/src/main.rs | 25 ++++---- ica-rs/src/py.rs | 18 ++++++ 7 files changed, 234 insertions(+), 40 deletions(-) create mode 100644 ica-rs/src/config.rs create mode 100644 ica-rs/src/py.rs diff --git a/ica-rs/Cargo.lock b/ica-rs/Cargo.lock index 44beae4..67ba1d9 100644 --- a/ica-rs/Cargo.lock +++ b/ica-rs/Cargo.lock @@ -463,6 +463,12 @@ version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermit-abi" version = "0.3.3" @@ -554,7 +560,9 @@ dependencies = [ "ed25519", "ed25519-dalek", "hex", + "pyo3", "rust_socketio", + "serde", "serde_json", "toml", ] @@ -579,6 +587,12 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "indoc" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e186cfbae8084e513daff4240b4797e342f988cecda4fb6c939150f96315fd8" + [[package]] name = "instant" version = "0.1.12" @@ -627,6 +641,16 @@ version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456" +[[package]] +name = "lock_api" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "log" version = "0.4.20" @@ -639,6 +663,15 @@ version = "2.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" +[[package]] +name = "memoffset" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c" +dependencies = [ + "autocfg", +] + [[package]] name = "mime" version = "0.3.17" @@ -752,6 +785,29 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "parking_lot" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets 0.48.5", +] + [[package]] name = "percent-encoding" version = "2.3.1" @@ -807,6 +863,67 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "pyo3" +version = "0.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a89dc7a5850d0e983be1ec2a463a171d20990487c3cfcd68b5363f1ee3d6fe0" +dependencies = [ + "cfg-if", + "indoc", + "libc", + "memoffset", + "parking_lot", + "pyo3-build-config", + "pyo3-ffi", + "pyo3-macros", + "unindent", +] + +[[package]] +name = "pyo3-build-config" +version = "0.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07426f0d8fe5a601f26293f300afd1a7b1ed5e78b2a705870c5f30893c5163be" +dependencies = [ + "once_cell", + "target-lexicon", +] + +[[package]] +name = "pyo3-ffi" +version = "0.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbb7dec17e17766b46bca4f1a4215a85006b4c2ecde122076c562dd058da6cf1" +dependencies = [ + "libc", + "pyo3-build-config", +] + +[[package]] +name = "pyo3-macros" +version = "0.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f738b4e40d50b5711957f142878cfa0f28e054aa0ebdfc3fd137a843f74ed3" +dependencies = [ + "proc-macro2", + "pyo3-macros-backend", + "quote", + "syn", +] + +[[package]] +name = "pyo3-macros-backend" +version = "0.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fc910d4851847827daf9d6cdd4a823fbdaab5b8818325c5e97a86da79e8881f" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "quote" version = "1.0.33" @@ -978,6 +1095,12 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + [[package]] name = "security-framework" version = "2.9.2" @@ -1099,6 +1222,12 @@ dependencies = [ "autocfg", ] +[[package]] +name = "smallvec" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" + [[package]] name = "socket2" version = "0.4.10" @@ -1167,6 +1296,12 @@ dependencies = [ "libc", ] +[[package]] +name = "target-lexicon" +version = "0.12.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69758bda2e78f098e4ccb393021a0963bb3442eac05f135c30f61b7370bbafae" + [[package]] name = "tempfile" version = "3.8.1" @@ -1381,6 +1516,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unindent" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7de7d73e1754487cb58364ee906a499937a0dfabd86bcb980fa99ec8c8fa2ce" + [[package]] name = "url" version = "2.5.0" diff --git a/ica-rs/Cargo.toml b/ica-rs/Cargo.toml index 06582f7..5fc4013 100644 --- a/ica-rs/Cargo.toml +++ b/ica-rs/Cargo.toml @@ -11,9 +11,17 @@ ed25519 = "2.2.3" ed25519-dalek = "2.1.0" hex = "0.4.3" rust_socketio = "0.4.4" + +serde = { version = "1.0", features = ["derive"] } serde_json = "1.0.108" toml = "0.8.8" + colored = "2.1.0" +# inline-python = "0.12.0" + +[dependencies.pyo3] +version = "0.20.2" +# features = ["auto-initialize"] [patch.crates-io] rust_socketio = { path = "../../../rust-socketio/socketio" } diff --git a/ica-rs/build.rs b/ica-rs/build.rs index 8b48414..61458da 100644 --- a/ica-rs/build.rs +++ b/ica-rs/build.rs @@ -16,4 +16,4 @@ fn pyo3_config() { fn main() { pyo3_config(); -} \ No newline at end of file +} diff --git a/ica-rs/src/client.rs b/ica-rs/src/client.rs index b8dd086..b52d648 100644 --- a/ica-rs/src/client.rs +++ b/ica-rs/src/client.rs @@ -1,6 +1,8 @@ -use serde_json::Value; +use crate::config::IcaConfig; + use ed25519_dalek::{Signature, Signer, SigningKey}; -use rust_socketio::{ClientBuilder, Event, Payload, RawClient}; +use rust_socketio::{Payload, RawClient}; +use serde_json::Value; pub struct IcalinguaSinger { pub host: String, @@ -8,24 +10,17 @@ pub struct IcalinguaSinger { } impl IcalinguaSinger { - pub fn new_from_config(config_file_path: String) -> Self { - // try read config from file - let config = std::fs::read_to_string(config_file_path).expect("Failed to read config file"); - let config: toml::Value = toml::from_str(&config).expect("Failed to parse config file"); - let host = config["host"] - .as_str() - .expect("host should be string") - .to_string(); - let pub_key = config["private_key"] - .as_str() - .expect("private_key should be string") - .to_string(); - + pub fn new_from_config(config: IcaConfig) -> Self { + let host = config.host; + let pub_key = config.private_key; Self::new_from_raw(host, pub_key) } pub fn new_from_raw(host: String, pub_key: String) -> Self { - let array_key: [u8; 32] = hex::decode(pub_key).unwrap().try_into().unwrap(); + let array_key: [u8; 32] = hex::decode(pub_key) + .expect("Not a vaild pub key") + .try_into() + .expect("Not a vaild pub key"); let signing_key: SigningKey = SigningKey::from_bytes(&array_key); Self { @@ -34,14 +29,10 @@ impl IcalinguaSinger { } } - pub fn sign_for_salt(&self, salt: String) -> Vec { - let salt: Vec = hex::decode(salt).unwrap(); - let signature: Signature = self.private_key.sign(salt.as_slice()); - - signature.to_bytes().to_vec() - } - - pub fn sign_callback(&self, payload: Payload, client: RawClient, _id: Option) { + /// 最痛苦的一集 + /// + /// 签名的回调函数 + pub fn sign_callback(&self, payload: Payload, client: RawClient) { let require_data = match payload { Payload::Text(json_value) => Some(json_value), _ => None, @@ -49,7 +40,7 @@ impl IcalinguaSinger { .expect("Payload should be Json data"); let (auth_key, version) = (&require_data[0], &require_data[1]); - // println!("auth_key: {:?}, version: {:?}", auth_key, version); + println!("auth_key: {:?}, version: {:?}", auth_key, version); let auth_key = match &require_data.get(0) { Some(Value::String(auth_key)) => Some(auth_key), _ => None, @@ -60,6 +51,8 @@ impl IcalinguaSinger { let signature: Signature = self.private_key.sign(salt.as_slice()); let sign = signature.to_bytes().to_vec(); - client.emit("auth", sign).unwrap(); + client + .emit("auth", sign) + .expect("Faild to send signin data"); } } diff --git a/ica-rs/src/config.rs b/ica-rs/src/config.rs new file mode 100644 index 0000000..59e6662 --- /dev/null +++ b/ica-rs/src/config.rs @@ -0,0 +1,35 @@ +use std::env; +use std::fs; + +use serde::Deserialize; +use toml; + +#[derive(Debug, Deserialize)] +pub struct IcaConfig { + /// icalingua 私钥 + pub private_key: String, + /// icalingua 服务器地址 + pub host: String, + /// bot 的 qq + pub self_id: u64, + /// 提醒的房间 + pub notice_room: Vec, + /// 是否提醒 + pub notice_start: Option, + /// Python 插件路径 + pub py_plugin_path: Option +} + +impl IcaConfig { + pub fn new_from_path(config_file_path: String) -> Self { + // try read config from file + let config = fs::read_to_string(&config_file_path).expect("Failed to read config file"); + let ret: Self = toml::from_str(&config) + .expect(format!("Failed to parse config file {}", &config_file_path).as_str()); + ret + } + pub fn new_from_cli() -> Self { + let config_file_path = env::args().nth(1).expect("No config path given"); + Self::new_from_path(config_file_path) + } +} diff --git a/ica-rs/src/main.rs b/ica-rs/src/main.rs index 3551a3e..5884846 100644 --- a/ica-rs/src/main.rs +++ b/ica-rs/src/main.rs @@ -1,23 +1,22 @@ mod client; +mod config; +mod py; -use colored::*; -use ed25519_dalek::{Signature, Signer, SigningKey}; +use colored::Colorize; use rust_socketio::{ClientBuilder, Event, Payload, RawClient}; -use serde_json::Value; use std::time::Duration; #[allow(unused)] -fn any_event(event: Event, payload: Payload, _client: RawClient, id: Option) { +fn any_event(event: Event, payload: Payload, _client: RawClient) { match payload { Payload::Binary(ref data) => { - println!("event: {} |{:?}|id{:?}", event, data, id) + println!("event: {} |{:?}", event, data) } Payload::Text(ref data) => { - print!("\x1b[35mevent: {event}\x1b[0m"); + print!("event: {}", event.as_str().purple()); for value in data { - print!("|{}", value.to_string()); + println!("|{}", value.to_string()); } - println!("|id:{:?}|", id); } _ => (), } @@ -28,11 +27,11 @@ fn ws_main() { // 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 - let connect_call_back = |payload: Payload, _client: RawClient, _id| match payload { + let connect_call_back = |payload: Payload, _client: RawClient| match payload { Payload::Text(values) => { if let Some(value) = values.first() { if let Some("authSucceed") = value.as_str() { - println!("\x1b[32m已经登录到 icalingua!\x1b[0m"); + println!("{}", "已经登录到 icalingua!".green()); } } } @@ -40,8 +39,8 @@ fn ws_main() { }; // 从命令行获取 host 和 key // 从命令行获取配置文件路径 - let config_path = std::env::args().nth(1).expect("No config path given"); - let ica_singer = client::IcalinguaSinger::new_from_config(config_path); + let ica_config = config::IcaConfig::new_from_cli(); + let ica_singer = client::IcalinguaSinger::new_from_config(ica_config); // get a socket that is connected to the admin namespace @@ -49,7 +48,7 @@ fn ws_main() { // .namespace("/admin") .on_any(any_event) .on("message", connect_call_back) - .on("requireAuth", move |a, b, c| ica_singer.sign_callback(a, b, c)) + .on("requireAuth", move |a, b| ica_singer.sign_callback(a, b)) .connect() .expect("Connection failed"); diff --git a/ica-rs/src/py.rs b/ica-rs/src/py.rs new file mode 100644 index 0000000..6d39351 --- /dev/null +++ b/ica-rs/src/py.rs @@ -0,0 +1,18 @@ +// use inline_python::{python, Context}; + +use pyo3::prelude::*; + +#[pyclass] +#[pyo3(name = "BotStatus")] +pub struct BotStatusPy {} + +pub fn run() { + // +} + +// pub fn run() { +// let con: Context = python! { +// print("Hello, world!"); +// }; + +// }