-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminecraft.rs
286 lines (249 loc) · 8.97 KB
/
minecraft.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
use std::{
collections::VecDeque,
fmt::Write,
sync::LazyLock,
time::{SystemTime, UNIX_EPOCH},
};
use azalea::{
app::{App, Plugin, Update},
chat::{handle_send_chat_event, handler::SendChatKindEvent, ChatKind, ChatReceivedEvent},
ecs::prelude::*,
TabList,
};
use ncr::{
encoding::{
Base64Encoding,
Base64rEncoding,
Encoding,
Mc256Encoding,
NewBase64rEncoding,
Sus16Encoding,
},
encryption::{Cfb8Encryption, EcbEncryption, Encryption, GcmEncryption},
utils::{prepend_header, trim_header},
AesKey,
NcrError,
};
use crate::prelude::*;
/// Minecraft chat command parsing integration
pub struct MinecraftParserPlugin;
impl Plugin for MinecraftParserPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(CmdCooldown::default())
.add_event::<CmdEvent>()
.add_event::<MsgEvent>()
.add_systems(
Update,
(
Self::handle_chat_received_events,
Self::handle_send_msg_events.before(handle_send_chat_event),
)
.chain(),
);
}
}
impl MinecraftParserPlugin {
pub fn handle_chat_received_events(
mut chat_received_events: EventReader<ChatReceivedEvent>,
mut cmd_events: EventWriter<CmdEvent>,
mut cooldown: ResMut<CmdCooldown>,
query: Query<&TabList>,
settings: Res<GlobalSettings>,
) {
let mut events = Vec::new();
for event in chat_received_events.read() {
let (username, content) = event.packet.split_sender_and_content();
let (username, content, message) = if let Some(username) = username {
(username, content, event.packet.is_whisper()) /* Vanilla Server Format */
} else if let Some((_whole, username, whisper, content)) = regex_captures!(
r"^(?:\[.+\] )?([a-zA-Z_0-9]{1,16}) (?:> )?(whispers: |-> me] )?(.+)$",
&content /* Custom Server Formats */
) {
(str!(username), str!(content), !whisper.is_empty())
} else {
continue;
};
let Ok(tab_list) = query.get(event.entity) else {
return; /* Not Connected */
};
let Some((uuid, _)) = tab_list.iter().find(|(_, i)| i.profile.name == username) else {
continue; /* Not Online */
};
if settings.whitelist_only && !settings.users.contains_key(uuid) {
continue; /* Not Whitelisted */
}
let key = AesKey::decode_base64(&settings.chat.key).unwrap_or_else(|_| KEY.clone());
let (encryption, content) = find_encryption(&content, &key);
let mut args = content
.split(' ')
.map(String::from)
.collect::<VecDeque<_>>();
let Some(alias) = args.pop_front() else {
continue; /* Command Missing */
};
if !alias.starts_with(&settings.command_prefix) {
continue; /* Command Invalid */
}
let Some(command) = Cmds::find(&alias.replace(&settings.command_prefix, "")) else {
continue; /* Command Invalid */
};
if cooldown.check(&username, settings.command_cooldown) {
info!("Command on cooldown");
continue; /* Command Cooldown */
}
events.push(CmdEvent {
args,
cmd: command,
entity: Some(event.entity),
message,
sender: CmdSender::Minecraft(*uuid),
source: CmdSource::Minecraft(encryption),
});
}
cmd_events.send_batch(events);
}
pub fn handle_send_msg_events(
mut chat_kind_events: EventWriter<SendChatKindEvent>,
mut msg_events: EventReader<MsgEvent>,
query: Query<(&TabList, &LocalSettings)>,
settings: Res<GlobalSettings>,
) {
for mut event in msg_events.read().cloned() {
#[rustfmt::skip]
let (
Some(entity),
CmdSource::Minecraft(type_encryption),
CmdSender::Minecraft(uuid)
) = (event.entity, event.source, event.sender) else {
continue;
};
let Ok((tab_list, local_settings)) = query.get(entity) else {
return;
};
if local_settings.disable_responses {
continue; /* Responses Disabled */
}
let Some(username) = tab_list
.iter()
.find(|(_, info)| info.profile.uuid == uuid)
.map(|(_, info)| info.profile.name.clone())
else {
continue; /* Player Offline */
};
info!("Command Response: {}", event.content);
if local_settings.anti_spam.enabled {
if let Ok(duration) = SystemTime::now().duration_since(UNIX_EPOCH) {
let _ = write!(event.content, " [{}]", duration.as_secs());
}
}
try_encrypt(&mut event.content, &settings.chat, type_encryption);
chat_kind_events.send(SendChatKindEvent {
content: format!("w {username} {}", event.content),
entity,
kind: ChatKind::Command,
});
}
}
}
/* No Chat Reports Mod */
pub static KEY: LazyLock<AesKey> = LazyLock::new(|| {
AesKey::from([
110, 87, 235, 158, 0, 43, 147, 119, 33, 27, 172, 51, 157, 195, 153, 228,
])
});
static ENCODERS: LazyLock<[EncodingType; 3]> = LazyLock::new(|| {
[
EncodingType::NewBase64r,
EncodingType::Base64r,
EncodingType::Base64,
]
});
#[derive(Clone, Copy, Debug)]
pub enum EncodingType {
Base64,
Base64r,
NewBase64r,
Mc256,
Sus16,
}
impl Encoding for EncodingType {
fn encode(self, text: &[u8]) -> String {
match self {
Self::NewBase64r => NewBase64rEncoding.encode(text),
Self::Base64r => Base64rEncoding.encode(text),
Self::Base64 => Base64Encoding.encode(text),
Self::Mc256 => Mc256Encoding.encode(text),
Self::Sus16 => Sus16Encoding.encode(text),
}
}
fn decode(self, text: &str) -> Result<Vec<u8>, NcrError> {
match self {
Self::NewBase64r => NewBase64rEncoding.decode(text),
Self::Base64r => Base64rEncoding.decode(text),
Self::Base64 => Base64Encoding.decode(text),
Self::Mc256 => Mc256Encoding.decode(text),
Self::Sus16 => Sus16Encoding.decode(text),
}
}
}
#[derive(Clone, Copy, Debug)]
pub enum EncryptionType {
CFB(EncodingType),
ECB(EncodingType),
GCM(EncodingType),
}
impl Encryption for EncryptionType {
type KeyType = AesKey;
fn encrypt(self, plaintext: &str, key: &Self::KeyType) -> Result<String, NcrError> {
match self {
Self::CFB(encryption) => Cfb8Encryption(encryption).encrypt(plaintext, key),
Self::ECB(encryption) => EcbEncryption(encryption).encrypt(plaintext, key),
Self::GCM(encryption) => GcmEncryption(encryption).encrypt(plaintext, key),
}
}
fn decrypt(self, ciphertext: &str, key: &Self::KeyType) -> Result<String, NcrError> {
match self {
Self::CFB(encryption) => Cfb8Encryption(encryption).decrypt(ciphertext, key),
Self::ECB(encryption) => EcbEncryption(encryption).decrypt(ciphertext, key),
Self::GCM(encryption) => GcmEncryption(encryption).decrypt(ciphertext, key),
}
}
}
#[must_use]
pub fn find_encryption(content: &str, key: &AesKey) -> (Option<EncryptionType>, String) {
for &encoder in ENCODERS.iter() {
let encryptors = [
EncryptionType::CFB(encoder),
EncryptionType::ECB(encoder),
EncryptionType::GCM(encoder),
];
for encryptor in encryptors {
if let Ok(plaintext) = encryptor.decrypt(content, key) {
if let Ok(trimmed) = trim_header(&plaintext) {
return (Some(encryptor), String::from(trimmed));
}
}
}
}
(None, String::from(content))
}
pub fn try_encrypt(
content: &mut String,
chat_encryption: &ChatEncryption,
type_encryption: Option<EncryptionType>,
) {
if chat_encryption.mode == EncryptionMode::Never {
return;
}
let key = AesKey::decode_base64(&chat_encryption.key).unwrap_or_else(|_| KEY.clone());
let plaintext = prepend_header(content);
if let Some(encryption) = type_encryption {
if let Ok(ciphertext) = encryption.encrypt(&plaintext, &key) {
*content = ciphertext;
}
} else if chat_encryption.mode == EncryptionMode::Always {
if let Ok(ciphertext) = Cfb8Encryption(NewBase64rEncoding).encrypt(&plaintext, &key) {
*content = ciphertext;
}
}
}