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>) {
|
fn require_auth_callback(payload: Payload, client: RawClient, _id: Option<i32>) {
|
||||||
let key = std::env::args().nth(2).expect("No key given");
|
let key = std::env::args().nth(2).expect("No key given");
|
||||||
let require_data = match payload {
|
let require_data = match payload {
|
||||||
Payload::String(_) => None,
|
|
||||||
Payload::Binary(_) => None,
|
|
||||||
Payload::Text(json_value) => Some(json_value),
|
Payload::Text(json_value) => Some(json_value),
|
||||||
|
_ => None,
|
||||||
}
|
}
|
||||||
.expect("Payload should be Json data");
|
.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);
|
// println!("auth_key: {:?}, version: {:?}", auth_key, version);
|
||||||
let auth_key = match &require_data.get(0) {
|
let auth_key = match &require_data.get(0) {
|
||||||
Some(Value::String(auth_key)) => Some(auth_key),
|
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()
|
.try_into()
|
||||||
.expect("Key should be 32 bytes");
|
.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 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());
|
||||||
@ -41,12 +38,7 @@ fn require_auth_callback(payload: Payload, client: RawClient, _id: Option<i32>)
|
|||||||
|
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
fn any_event(event: Event, payload: Payload, _client: RawClient, id: Option<i32>) {
|
fn any_event(event: Event, payload: Payload, _client: RawClient, id: Option<i32>) {
|
||||||
// println!("event: {} | {:#?}", event, payload);
|
println!("event: {} |{:?}|id{:?}", event, payload, id)
|
||||||
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),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ws_main() {
|
fn ws_main() {
|
||||||
@ -54,7 +46,15 @@ fn ws_main() {
|
|||||||
// this callback gets the payload as well as an instance of the
|
// this callback gets the payload as well as an instance of the
|
||||||
// socket to communicate with the server
|
// socket to communicate with the server
|
||||||
let connect_call_back = |payload: Payload, _client: RawClient, _id| {
|
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
|
// 从命令行获取 host 和 key
|
||||||
let host = std::env::args().nth(1).expect("No host given");
|
let host = std::env::args().nth(1).expect("No host given");
|
||||||
@ -64,7 +64,7 @@ fn ws_main() {
|
|||||||
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("message", connect_call_back)
|
||||||
.on("requireAuth", require_auth_callback)
|
.on("requireAuth", require_auth_callback)
|
||||||
.connect()
|
.connect()
|
||||||
.expect("Connection failed");
|
.expect("Connection failed");
|
||||||
|
Loading…
Reference in New Issue
Block a user