Compare commits

..

No commits in common. "69f202e83a7477490a58b5a1548d7d9240e82c72" and "daf53ba226c844fb5eaf64fdf7d951720050590c" have entirely different histories.

6 changed files with 58 additions and 135 deletions

View File

@ -5,7 +5,7 @@ import traceback
from typing import Dict, List, Tuple, Any, Optional, Union, Literal from typing import Dict, List, Tuple, Any, Optional, Union, Literal
import qtoml import rtoml
import socketio import socketio
from colorama import Fore, Style from colorama import Fore, Style
from nacl.signing import SigningKey from nacl.signing import SigningKey
@ -14,7 +14,7 @@ from lib_not_dr.types import Options
def get_config() -> Tuple[str, str, int]: def get_config() -> Tuple[str, str, int]:
with open('config.toml', 'r', encoding='utf-8') as f: with open('config.toml', 'r', encoding='utf-8') as f:
config = qtoml.load(f) config = rtoml.load(f)
return config['host'], config['private_key'], config['self_id'] return config['host'], config['private_key'], config['self_id']
@ -188,13 +188,13 @@ async def add_message(data: Dict[str, Any]):
room_id = data['roomId'] room_id = data['roomId']
if not is_self: if not is_self:
if content == '/bot': if data.get('message').get('content') == '/bot':
message = Message(content='icalingua bot test', message = Message(content='icalingua bot test',
room_id=data['roomId']) room_id=data['roomId'])
await sio.emit('sendMessage', message.to_json()) await sio.emit('sendMessage', message.to_json())
elif content.startswith('=='): elif data.get('message').get('content').startswith('=='):
evals: str = content[2:] evals: str = data.get('message').get('content')[2:]
# quene = multiprocessing.Queue() # quene = multiprocessing.Queue()
# def run(quene, evals): # def run(quene, evals):
@ -226,7 +226,7 @@ async def add_message(data: Dict[str, Any]):
await asyncio.sleep(random.random() * 2) await asyncio.sleep(random.random() * 2)
await sio.emit('sendMessage', message.to_json()) await sio.emit('sendMessage', message.to_json())
elif content == '!!jrrp': elif data['message']['content'] == '!!jrrp':
randomer = random.Random(f'{sender_id}-{data["message"]["date"]}-jrrp-v2') randomer = random.Random(f'{sender_id}-{data["message"]["date"]}-jrrp-v2')
result = randomer.randint(0, 50) + randomer.randint(0, 50) result = randomer.randint(0, 50) + randomer.randint(0, 50)
print(f'{sender_name} 今日人品值为 {result}') print(f'{sender_name} 今日人品值为 {result}')

53
ica-rs/Cargo.lock generated
View File

@ -545,7 +545,6 @@ dependencies = [
"hex", "hex",
"rust_socketio", "rust_socketio",
"serde_json", "serde_json",
"toml",
] ]
[[package]] [[package]]
@ -1027,15 +1026,6 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "serde_spanned"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12022b835073e5b11e90a14f86838ceb1c8fb0325b72416845c487ac0fa95e80"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "serde_urlencoded" name = "serde_urlencoded"
version = "0.7.1" version = "0.7.1"
@ -1258,40 +1248,6 @@ dependencies = [
"tracing", "tracing",
] ]
[[package]]
name = "toml"
version = "0.8.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1a195ec8c9da26928f773888e0742ca3ca1040c6cd859c919c9f59c1954ab35"
dependencies = [
"serde",
"serde_spanned",
"toml_datetime",
"toml_edit",
]
[[package]]
name = "toml_datetime"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1"
dependencies = [
"serde",
]
[[package]]
name = "toml_edit"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34d383cd00a163b4a5b85053df514d45bc330f6de7737edfe0a93311d1eaa03"
dependencies = [
"indexmap",
"serde",
"serde_spanned",
"toml_datetime",
"winnow",
]
[[package]] [[package]]
name = "tower-service" name = "tower-service"
version = "0.3.2" version = "0.3.2"
@ -1657,15 +1613,6 @@ version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04"
[[package]]
name = "winnow"
version = "0.5.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c830786f7720c2fd27a1a0e27a709dbd3c4d009b56d098fc742d4f4eab91fe2"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "winreg" name = "winreg"
version = "0.50.0" version = "0.50.0"

View File

@ -11,7 +11,6 @@ ed25519-dalek = "2.1.0"
hex = "0.4.3" hex = "0.4.3"
rust_socketio = "0.4.4" rust_socketio = "0.4.4"
serde_json = "1.0.108" serde_json = "1.0.108"
toml = "0.8.8"
[patch.crates-io] [patch.crates-io]
rust_socketio = { path = "../../../rust-socketio/socketio" } rust_socketio = { path = "V:/githubs/rust-socketio/socketio" }

View File

@ -1,65 +1,25 @@
use serde_json::Value;
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,
pub private_key: SigningKey, pub pub_key: SigningKey,
} }
impl IcalinguaSinger { impl IcalinguaSinger {
pub fn new_from_config(config_file_path: String) -> Self { pub fn new(host: String, pub_key: &str) -> 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();
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).unwrap().try_into().unwrap();
let signing_key: SigningKey = SigningKey::from_bytes(&array_key); let signing_key: SigningKey = SigningKey::from_bytes(&array_key);
Self { Self {
host, host,
private_key: signing_key, pub_key: signing_key,
} }
} }
pub fn sign_for_salt(&self, salt: String) -> Vec<u8> { pub fn sign_for_salt(&self, salt: String) -> Vec<u8> {
let salt: Vec<u8> = hex::decode(salt).unwrap(); let salt: Vec<u8> = hex::decode(salt).unwrap();
let signature: Signature = self.private_key.sign(salt.as_slice()); let signature: Signature = self.pub_key.sign(salt.as_slice());
signature.to_bytes().to_vec() signature.to_bytes().to_vec()
} }
pub fn sign_callback(&self, payload: Payload, client: RawClient, _id: Option<i32>) {
let require_data = match payload {
Payload::Text(json_value) => Some(json_value),
_ => None,
}
.expect("Payload should be Json data");
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),
_ => None,
}
.expect("auth_key should be string");
let salt = hex::decode(auth_key).expect("Got an invalid salt from the server");
let signature: Signature = self.private_key.sign(salt.as_slice());
let sign = signature.to_bytes().to_vec();
client.emit("auth", sign).unwrap();
}
} }

View File

@ -5,50 +5,67 @@ use rust_socketio::{ClientBuilder, Event, Payload, RawClient};
use serde_json::Value; use serde_json::Value;
use std::time::Duration; use std::time::Duration;
#[allow(unused)]
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),
}
.expect("Payload should be Json data");
println!("require_data: {:?}", require_data);
// 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),
_ => None,
}
.expect("auth_key should be string");
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 salt = hex::decode(auth_key).expect("Got an invalid salt from the server");
let signature: Signature = signing_key.sign(salt.as_slice());
// let sign = hex::encode(signature.to_bytes());
let sign = signature.to_bytes().to_vec();
client.emit("auth", sign).unwrap();
}
#[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);
match payload { match payload {
Payload::Binary(ref data) => { Payload::Binary(bin) => println!("event: {}|id:{:?}|bin: {:?}", event, id, bin),
println!("event: {} |{:?}|id{:?}", event, data, id) Payload::String(str) => println!("event: {}|id:{:?}|str: {:?}", event, id, str),
Payload::Text(txt) => println!("event: {}|id:{:?}|txt: {:?}", event, id, txt),
} }
Payload::Text(ref data) => {
print!("\x1b[35mevent: {event}\x1b[0m");
for value in data {
print!("|{}", value.to_string());
}
println!("|id:{:?}|", id);
}
_ => (),
}
// println!("event: {} |{:?}|id{:?}", event, payload, id)
} }
fn ws_main() { fn ws_main() {
// define a callback which is called when a payload is received // define a callback which is called when a payload is received
// 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| match payload { let connect_call_back = |payload: Payload, _client: RawClient, _id| {
Payload::Text(values) => { println!("Connect callback: {:#?}", payload);
if let Some(value) = values.first() {
if let Some("authSucceed") = value.as_str() {
println!("\x1b[32m已经登录到 icalingua!\x1b[0m");
}
}
}
_ => (),
}; };
// 从命令行获取 host 和 key // 从命令行获取 host 和 key
// 从命令行获取配置文件路径 let host = std::env::args().nth(1).expect("No host given");
let config_path = std::env::args().nth(1).expect("No config path given");
let ica_singer = client::IcalinguaSinger::new_from_config(config_path);
// get a socket that is connected to the admin namespace // get a socket that is connected to the admin namespace
let socket = ClientBuilder::new(ica_singer.host.clone()) let socket = ClientBuilder::new(host)
// .namespace("/admin") // .namespace("/admin")
.on_any(any_event) .on_any(any_event)
.on("message", connect_call_back) .on("connect", connect_call_back)
.on("requireAuth", move |a, b, c| ica_singer.sign_callback(a, b, c)) .on("requireAuth", require_auth_callback)
.connect() .connect()
.expect("Connection failed"); .expect("Connection failed");

View File

@ -1,5 +1,5 @@
lib-not-dr lib-not-dr
colorama colorama
qtoml rtoml
pynacl pynacl
python-socketio[asyncio_client] python-socketio[asyncio_client]