Refactor client.rs and main.rs

This commit is contained in:
shenjack 2023-12-11 18:17:55 +08:00
parent d1b27d3825
commit 27db3d5ef0
Signed by: shenjack
GPG Key ID: 7B1134A979775551
2 changed files with 11 additions and 30 deletions

View File

@ -1,5 +1,4 @@
use ed25519_dalek::{Signature, Signer, SigningKey}; use ed25519_dalek::{Signature, Signer, SigningKey};
use rust_socketio::{ClientBuilder, Event, Payload, RawClient};
pub struct IcalinguaSinger { pub struct IcalinguaSinger {
pub host: String, pub host: String,

View File

@ -1,29 +1,29 @@
mod client; mod client;
use ed25519_dalek::{Signature, Signer, SigningKey}; use ed25519_dalek::{Signature, Signer, SigningKey};
use rust_socketio::{ClientBuilder, Payload, RawClient, Event}; use rust_socketio::{ClientBuilder, Event, Payload, RawClient};
use std::time::Duration; use std::time::Duration;
static KEY: &str = "";
static HOST: &str = "";
#[allow(unused)] #[allow(unused)]
fn require_auth_callback(payload: Payload, client: RawClient) { fn require_auth_callback(payload: Payload, client: RawClient) {
let key = std::env::args().nth(2).expect("No key given");
let auth_key = match payload { let auth_key = match payload {
Payload::String(str) => Some(str), Payload::String(str) => Some(str),
Payload::Binary(_) => None, Payload::Binary(_) => None,
} }
.unwrap(); .expect("Payload should be String");
// 去除前后的 "
let auth_key = &auth_key[1..auth_key.len() - 1]; let auth_key = &auth_key[1..auth_key.len() - 1];
println!("auth_key: {}", auth_key); println!("auth_key: {}", auth_key);
let array_key: [u8; 32] = hex::decode(KEY).unwrap().try_into().unwrap(); let array_key: [u8; 32] = hex::decode(key)
.expect("Key should be hex")
.try_into()
.expect("Key should be 32 bytes");
let signing_key: SigningKey = SigningKey::from_bytes(&array_key); let signing_key: SigningKey = SigningKey::from_bytes(&array_key);
let salt = hex::decode(auth_key).unwrap(); let salt = hex::decode(auth_key).expect("Got an invalid salt from the server");
let signature: Signature = signing_key.sign(salt.as_slice()); let signature: Signature = signing_key.sign(salt.as_slice());
// let sign = hex::encode(signature.to_bytes()); // let sign = hex::encode(signature.to_bytes());
let sign = signature.to_bytes().to_vec(); let sign = signature.to_bytes().to_vec();
@ -46,10 +46,12 @@ fn ws_main() {
let connect_call_back = |payload: Payload, _client: RawClient| { let connect_call_back = |payload: Payload, _client: RawClient| {
println!("Connect callback: {:#?}", payload); println!("Connect callback: {:#?}", payload);
}; };
// 从命令行获取 host 和 key
let host = std::env::args().nth(1).expect("No host given");
// get a socket that is connected to the admin namespace // get a socket that is connected to the admin namespace
let socket = ClientBuilder::new(HOST) let socket = ClientBuilder::new(host)
// .namespace("/admin") // .namespace("/admin")
.on_any(any_event) .on_any(any_event)
.on("connect", connect_call_back) .on("connect", connect_call_back)
@ -62,26 +64,6 @@ fn ws_main() {
socket.disconnect().expect("Disconnect failed") socket.disconnect().expect("Disconnect failed")
} }
fn sign_main() {
// 生成 SingningKey
let array_key: [u8; 32] = hex::decode(KEY).unwrap().try_into().unwrap();
let signing_key: SigningKey = SigningKey::from_bytes(&array_key);
// 要签名的东西
let data = "187d0b21becfa7a49e97afc00646e169";
let data = hex::decode(data).unwrap();
// 签名
let signature: Signature = signing_key.sign(data.as_slice());
// 生成签名
let sign = hex::encode(signature.to_bytes());
println!("sign: {} {:?} {}", sign, signature.to_bytes(), signature.to_bytes().len());
// println!("hex: {}", hex::encode());
}
fn main() { fn main() {
sign_main();
ws_main(); ws_main();
} }