mirror of
http://shenjack.top:5100/shenjack/icalingua-python-bot.git
synced 2024-11-23 12:41:05 +08:00
Refactor authentication handling in main.rs
This commit is contained in:
parent
daf53ba226
commit
9e5a2c726c
|
@ -9,15 +9,12 @@ use std::time::Duration;
|
|||
fn require_auth_callback(payload: Payload, client: RawClient, _id: Option<i32>) {
|
||||
let key = std::env::args().nth(2).expect("No key given");
|
||||
let require_data = match payload {
|
||||
Payload::String(_) => None,
|
||||
Payload::Binary(_) => None,
|
||||
Payload::Text(json_value) => Some(json_value),
|
||||
_ => None,
|
||||
}
|
||||
.expect("Payload should be Json data");
|
||||
|
||||
println!("require_data: {:?}", require_data);
|
||||
|
||||
// let (auth_key, version) = (&require_data[0], &require_data[1]);
|
||||
let (auth_key, version) = (&require_data[0], &require_data[1]);
|
||||
// println!("auth_key: {:?}, version: {:?}", auth_key, version);
|
||||
let auth_key = match &require_data.get(0) {
|
||||
Some(Value::String(auth_key)) => Some(auth_key),
|
||||
|
@ -30,7 +27,7 @@ fn require_auth_callback(payload: Payload, client: RawClient, _id: Option<i32>)
|
|||
.try_into()
|
||||
.expect("Key should be 32 bytes");
|
||||
|
||||
let signing_key: SigningKey = SigningKey::from_bytes(&array_key);
|
||||
let signing_key = SigningKey::from_bytes(&array_key);
|
||||
|
||||
let salt = hex::decode(auth_key).expect("Got an invalid salt from the server");
|
||||
let signature: Signature = signing_key.sign(salt.as_slice());
|
||||
|
@ -41,12 +38,7 @@ fn require_auth_callback(payload: Payload, client: RawClient, _id: Option<i32>)
|
|||
|
||||
#[allow(unused)]
|
||||
fn any_event(event: Event, payload: Payload, _client: RawClient, id: Option<i32>) {
|
||||
// println!("event: {} | {:#?}", event, payload);
|
||||
match payload {
|
||||
Payload::Binary(bin) => println!("event: {}|id:{:?}|bin: {:?}", event, id, bin),
|
||||
Payload::String(str) => println!("event: {}|id:{:?}|str: {:?}", event, id, str),
|
||||
Payload::Text(txt) => println!("event: {}|id:{:?}|txt: {:?}", event, id, txt),
|
||||
}
|
||||
println!("event: {} |{:?}|id{:?}", event, payload, id)
|
||||
}
|
||||
|
||||
fn ws_main() {
|
||||
|
@ -54,7 +46,15 @@ fn ws_main() {
|
|||
// 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| {
|
||||
println!("Connect callback: {:#?}", payload);
|
||||
match payload {
|
||||
Payload::Text(values) => {
|
||||
if values.first() == Some(&Value::String("authSucceed".to_string())) {
|
||||
// 一个绿色的 "已经连接到 icalingua 服务器"
|
||||
println!("\x1b[32m已经登录到 icalingua!\x1b[0m");
|
||||
}
|
||||
},
|
||||
_ => ()
|
||||
}
|
||||
};
|
||||
// 从命令行获取 host 和 key
|
||||
let host = std::env::args().nth(1).expect("No host given");
|
||||
|
@ -64,7 +64,7 @@ fn ws_main() {
|
|||
let socket = ClientBuilder::new(host)
|
||||
// .namespace("/admin")
|
||||
.on_any(any_event)
|
||||
.on("connect", connect_call_back)
|
||||
.on("message", connect_call_back)
|
||||
.on("requireAuth", require_auth_callback)
|
||||
.connect()
|
||||
.expect("Connection failed");
|
||||
|
|
Loading…
Reference in New Issue
Block a user