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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
use crate::handler::EventSender;
use crate::DeviceSource::{JsonFile, JsonString};
use crate::{
Authentication, ClientHandler, DeviceLockVerification, DeviceSource, EventResultHandler,
Module, SessionStore, ShowQR, ShowSlider,
};
use anyhow::{anyhow, Context, Result};
use bytes::{Buf, BufMut, Bytes, BytesMut};
use futures::future::BoxFuture;
use futures::FutureExt;
use rand::prelude::IteratorRandom;
use ricq::ext::common::after_login;
use ricq_core::binary::{BinaryReader, BinaryWriter};
use ricq_core::command::wtlogin::{
LoginDeviceLocked, LoginNeedCaptcha, LoginResponse, LoginSuccess, LoginUnknownStatus,
QRCodeConfirmed, QRCodeImageFetch, QRCodeState,
};
use ricq_core::protocol::device::Device;
use ricq_core::protocol::version::{Version, ANDROID_PHONE};
use ricq_core::{RQError, RQResult, Token};
use std::cmp::min;
use std::sync::Arc;
use std::time::Duration;
use tokio::net::TcpStream;
use tokio::task::JoinHandle;
use tokio::time::sleep;
#[cfg(feature = "connect_handler")]
use crate::features::connect_handler::ConnectionHandler;
#[cfg(feature = "connect_handler")]
use std::ops::Deref;
use std::path::Path;
#[cfg(feature = "connect_handler")]
use std::pin::Pin;
pub struct Client {
pub rq_client: Arc<ricq::Client>,
pub authentication: Authentication,
pub session_store: Arc<Option<Box<dyn SessionStore + Sync + Send>>>,
pub(crate) modules: Arc<Vec<Module>>,
pub(crate) result_handlers: Arc<Vec<EventResultHandler>>,
pub show_qr: ShowQR,
pub show_slider: ShowSlider,
pub shutting: bool,
pub device_lock_verification: DeviceLockVerification,
#[cfg(feature = "connect_handler")]
pub connection_handler: Arc<Option<Box<dyn ConnectionHandler + Sync + Send>>>,
pub reconnect_duration: Duration,
}
impl Client {
pub async fn write_token_to_store(&self) -> Result<()> {
if let Some(session_store) = self.session_store.as_deref() {
session_store
.save_session(token_to_bytes(&self.rq_client.gen_token().await).to_vec())
.await
} else {
Ok(())
}
}
}
pub async fn run_client(c: Arc<Client>) -> Result<()> {
let mut handle = connection(c.clone()).await?;
if !token_login(c.as_ref()).await {
login_authentication(&c).await?;
c.write_token_to_store().await?;
}
let event_sender = EventSender {
modules: c.modules.clone(),
result_handlers: c.result_handlers.clone(),
};
loop {
after_login(&c.rq_client.clone()).await;
tracing::info!("开始接收消息");
let err = match loop_events(handle, &event_sender).await {
Ok(_) => {
tracing::warn!("连接已断开");
anyhow::Error::msg("what's up")
}
Err(err) => {
tracing::warn!("连接已断开 {:?}", err);
err.into()
}
};
handle = re_connection(c.clone()).await?;
tracing::info!("恢复连接");
if token_login(c.as_ref()).await {
tracing::info!("恢复会话");
} else {
tracing::warn!("未能恢复会话");
let login = match c.authentication {
Authentication::UinPassword(_, _) => {
tracing::info!("使用账号密码重新登录");
login_authentication(&c)
}
Authentication::UinPasswordMd5(_, _) => {
tracing::info!("使用账号密码重新登录");
login_authentication(&c)
}
_ => {
tracing::error!("当前登录方式不支持重新登录");
return Err(err);
}
};
login.await?;
}
}
}
pub async fn run_client_once(client: Arc<Client>) -> Result<()> {
let handle = connection(client.clone()).await?;
tokio::task::yield_now().await;
if !token_login(&client).await {
login_authentication(&client).await?;
}
after_login(&client.rq_client.clone()).await;
client.write_token_to_store().await?;
let event_sender = EventSender {
modules: client.modules.clone(),
result_handlers: client.result_handlers.clone(),
};
loop_events(handle, &event_sender).await
}
async fn re_connection(client: Arc<Client>) -> Result<JoinHandle<()>> {
let mut times = 0;
loop {
times += 1;
let d = client.reconnect_duration + Duration::from_secs(min(5, times - 1));
tracing::info!("{}秒后进行{}次重连", d.as_secs(), times);
sleep(d).await;
let res = connection(client.clone()).await;
match res {
Ok(jh) => return Ok(jh),
Err(_) => (),
}
}
}
async fn connection(client: Arc<Client>) -> Result<JoinHandle<()>> {
let addresses = client.rq_client.get_address_list().await;
let address = addresses
.into_iter()
.choose_stable(&mut rand::thread_rng())
.unwrap();
#[cfg(not(feature = "connect_handler"))]
let handle = {
let stream = TcpStream::connect(address)
.await
.with_context(|| "连接到服务器出错")?;
tokio::spawn(async move { client.rq_client.start(stream).await })
};
#[cfg(feature = "connect_handler")]
let handle = if let Some(handler) = client.connection_handler.deref() {
let stream = handler
.connect(address)
.await
.with_context(|| "连接到服务器出错")?;
tokio::spawn(async move { client.rq_client.start(Pin::new(stream)).await })
} else {
let stream = TcpStream::connect(address)
.await
.with_context(|| "连接到服务器出错")?;
tokio::spawn(async move { client.rq_client.start(stream).await })
};
Ok(handle)
}
async fn loop_events(handle: JoinHandle<()>, event_sender: &EventSender) -> Result<()> {
let _ = event_sender.send_connected_and_online().await;
let result = handle.await;
let _ = event_sender.send_disconnected_and_offline().await;
result.with_context(|| "事件轮询中断")?;
Ok(())
}
async fn token_login(client: &Client) -> bool {
if let Some(session_file) = client.session_store.as_deref() {
let session_data = match session_file.load_session().await {
Ok(data) => data,
Err(err) => {
tracing::info!("{:?}", err);
return false;
}
};
if let Some(session_data) = session_data {
let result = client
.rq_client
.token_login(bytes_to_token(session_data))
.await;
match result {
Ok(_) => true,
Err(err) => match err {
RQError::TokenLoginFailed => {
let _ = session_file.remove_session().await;
false
}
_ => false,
},
}
} else {
false
}
} else {
false
}
}
async fn login_authentication(client: &Client) -> Result<()> {
authenticate(&client.authentication, client).await
}
fn authenticate<'a>(
authentication: &'a Authentication,
client: &'a Client,
) -> BoxFuture<'a, Result<()>> {
async move {
let rq_client = client.rq_client.clone();
match authentication.clone() {
Authentication::QRCode => qr_login(client, client.show_qr.clone()).await,
Authentication::UinPassword(uin, password) => {
let first = rq_client.password_login(uin, &password).await;
loop_login(client, first).await
}
Authentication::UinPasswordMd5(uin, password) => {
let first = rq_client.password_md5_login(uin, &password).await;
loop_login(client, first).await
}
Authentication::CustomUinPassword(cup) => {
let uin = cup.input_uin().await?;
let password = cup.input_password().await?;
let first = rq_client.password_login(uin, &password).await;
loop_login(client, first).await
}
Authentication::CustomUinPasswordMd5(cup) => {
let uin = cup.input_uin().await?;
let password = cup.input_password_md5().await?;
let first = rq_client.password_md5_login(uin, &password).await;
loop_login(client, first).await
}
Authentication::CallBack(wrapper) => {
let callback_authentication = (wrapper.clone().callback)(rq_client);
match callback_authentication {
Authentication::CallBack(_) => {
Err(anyhow::Error::msg("登录失败: 嵌套的回调函数"))
}
Authentication::Abandon => Err(anyhow::Error::msg("放弃登录")),
_ => authenticate(&authentication, client).await,
}
}
Authentication::Abandon => Err(anyhow::Error::msg("放弃登录")),
}
}
.boxed()
}
async fn qr_login(client: &Client, show_qr: ShowQR) -> Result<()> {
let rq_client = client.rq_client.clone();
let mut image_sig = Bytes::new();
let mut resp = rq_client
.fetch_qrcode()
.await
.with_context(|| "二维码加载失败")?;
loop {
match resp {
QRCodeState::ImageFetch(QRCodeImageFetch {
ref image_data,
ref sig,
}) => {
image_sig = sig.clone();
match show_qr {
ShowQR::OpenBySystem => {
tokio::fs::write("qrcode.png", &image_data)
.await
.with_context(|| "文件写入出错 qrcode.png")?;
tracing::info!("二维码被写入文件: qrcode.png");
#[cfg(any(
target_os = "windows",
target_os = "linux",
target_os = "macos"
))]
match opener::open("qrcode.png") {
Ok(_) => tracing::info!("已打开二维码图片, 请扫码"),
Err(_) => tracing::warn!("未能打开二维码图片, 请手动扫码"),
}
#[cfg(not(any(
target_os = "windows",
target_os = "linux",
target_os = "macos"
)))]
tracing::info!("当前环境不支持打开图片, 请手动扫码");
}
#[cfg(feature = "console_qr")]
ShowQR::PrintToConsole => {
if let Err(err) = print_qr_to_console(image_data) {
return Err(anyhow!("二维码打印到控制台时出现误 : {}", err));
}
tracing::info!("请扫码");
}
ShowQR::Custom(ref func) => {
tracing::info!("使用自定义二维码打印");
func(image_data.clone()).await?;
}
ShowQR::SaveToFile => {
tokio::fs::write("qrcode.png", &image_data)
.await
.with_context(|| "文件写入出错 qrcode.png")?;
tracing::info!("二维码被写入文件: qrcode.png, 请扫码");
}
}
}
QRCodeState::WaitingForScan => {
}
QRCodeState::WaitingForConfirm => {
}
QRCodeState::Timeout => {
tracing::info!("二维码已超时,重新获取");
resp = rq_client
.fetch_qrcode()
.await
.with_context(|| "二维码加载失败")?;
continue;
}
QRCodeState::Confirmed(QRCodeConfirmed {
ref tmp_pwd,
ref tmp_no_pic_sig,
ref tgt_qr,
..
}) => {
tracing::info!("二维码已确认");
let first = rq_client
.qrcode_login(tmp_pwd, tmp_no_pic_sig, tgt_qr)
.await;
return loop_login(client, first).await;
}
QRCodeState::Canceled => {
return Err(anyhow::Error::msg("二维码已取消"));
}
}
sleep(Duration::from_secs(5)).await;
resp = rq_client
.query_qrcode_result(&image_sig)
.await
.with_context(|| "二维码状态加载失败")?;
}
}
async fn loop_login(client: &Client, first: RQResult<LoginResponse>) -> Result<()> {
let rq_client = client.rq_client.clone();
let mut resp = first?;
loop {
match resp {
LoginResponse::Success(LoginSuccess {
ref account_info, ..
}) => {
tracing::info!("登录成功: {:?}", account_info);
return Ok(());
}
LoginResponse::DeviceLocked(LoginDeviceLocked {
ref sms_phone,
ref verify_url,
ref message,
..
}) => {
tracing::info!("设备锁 : {:?}", message);
tracing::info!("密保手机 : {:?}", sms_phone);
tracing::info!("验证地址 : {:?}", verify_url);
match client.device_lock_verification.clone() {
DeviceLockVerification::Url => {
qr2term::print_qr(
verify_url
.clone()
.with_context(|| "未能取得设备锁验证地址")?
.as_str(),
)?;
tracing::info!("验证地址 : {:?}", verify_url);
tracing::info!("手机扫码或者打开url,处理完成后重启程序");
std::process::exit(0);
}
DeviceLockVerification::Sms(st) => {
rq_client.request_sms().await?;
resp = rq_client
.submit_sms_code(st.clone().get().await?.as_str())
.await?;
}
}
}
LoginResponse::NeedCaptcha(LoginNeedCaptcha {
ref verify_url,
image_captcha: ref _image_captcha,
..
}) => match client.show_slider {
ShowSlider::AndroidHelper => {
tracing::info!("滑动条 (原URL) : {:?}", verify_url);
let helper_url = verify_url
.clone()
.unwrap()
.replace("ssl.captcha.qq.com", "txhelper.glitch.me");
tracing::info!("滑动条 (改URL) : {:?}", helper_url);
let mut txt = http_get(&helper_url)
.await
.with_context(|| "http请求失败")?;
tracing::info!("您需要使用该仓库 提供的APP进行滑动 , 滑动后请等待, https://github.com/mzdluo123/TxCaptchaHelper : {}", txt);
loop {
sleep(Duration::from_secs(5)).await;
let rsp = http_get(&helper_url)
.await
.with_context(|| "http请求失败")?;
if !rsp.eq(&txt) {
txt = rsp;
break;
}
}
tracing::info!("获取到ticket : {}", txt);
resp = rq_client.submit_ticket(&txt).await.expect("发送ticket失败");
}
#[cfg(all(any(target_os = "windows"), feature = "pop_window_slider"))]
ShowSlider::PopWindow => {
if let Some(ticket) =
crate::captcha_window::ticket(verify_url.as_ref().unwrap())
{
resp = rq_client
.submit_ticket(&ticket)
.await
.expect("failed to submit ticket");
} else {
panic!("not slide");
}
}
},
LoginResponse::DeviceLockLogin { .. } => {
resp = rq_client
.device_lock_login()
.await
.with_context(|| "设备锁登录失败")?;
}
LoginResponse::AccountFrozen => {
return Err(anyhow::Error::msg("账户被冻结"));
}
LoginResponse::TooManySMSRequest => {
return Err(anyhow::Error::msg("短信请求过于频繁"));
}
LoginResponse::UnknownStatus(LoginUnknownStatus {
ref status,
ref tlv_map,
message,
..
}) => {
return Err(anyhow::Error::msg(format!(
"不能解析的登录响应: {:?}, {:?}, {:?}",
status, tlv_map, message,
)));
}
}
}
}
async fn http_get(url: &str) -> Result<String> {
Ok(reqwest::ClientBuilder::new().build().unwrap().get(url).header(
"user-agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Mobile Safari/537.36",
).send().await?
.text()
.await?)
}
pub fn token_to_bytes(t: &Token) -> Bytes {
let mut token = BytesMut::with_capacity(1024);
token.put_i64(t.uin);
token.write_bytes_short(&t.d2);
token.write_bytes_short(&t.d2key);
token.write_bytes_short(&t.tgt);
token.write_bytes_short(&t.srm_token);
token.write_bytes_short(&t.t133);
token.write_bytes_short(&t.encrypted_a1);
token.write_bytes_short(&t.wt_session_ticket_key);
token.write_bytes_short(&t.out_packet_session_id);
token.write_bytes_short(&t.tgtgt_key);
token.freeze()
}
pub fn bytes_to_token(token: Vec<u8>) -> Token {
let mut t = Bytes::from(token);
Token {
uin: t.get_i64(),
d2: t.read_bytes_short().to_vec(),
d2key: t.read_bytes_short().to_vec(),
tgt: t.read_bytes_short().to_vec(),
srm_token: t.read_bytes_short().to_vec(),
t133: t.read_bytes_short().to_vec(),
encrypted_a1: t.read_bytes_short().to_vec(),
wt_session_ticket_key: t.read_bytes_short().to_vec(),
out_packet_session_id: t.read_bytes_short().to_vec(),
tgtgt_key: t.read_bytes_short().to_vec(),
}
}
pub struct ClientBuilder {
device_source: DeviceSource,
version: &'static Version,
authentication: Option<Authentication>,
session_store: Arc<Option<Box<dyn SessionStore + Sync + Send>>>,
modules_vec: Arc<Vec<Module>>,
result_handlers_vec: Arc<Vec<EventResultHandler>>,
show_qr: Option<ShowQR>,
show_slider: Option<ShowSlider>,
device_lock_verification: Option<DeviceLockVerification>,
#[cfg(feature = "connect_handler")]
connect_handler_arc: Arc<Option<Box<dyn ConnectionHandler + Sync + Send>>>,
reconnect_duration: Duration,
}
impl ClientBuilder {
pub fn new() -> Self {
Self {
device_source: DeviceSource::default(),
version: &ANDROID_PHONE,
authentication: None,
session_store: Arc::new(None),
modules_vec: Arc::new(vec![]),
result_handlers_vec: Arc::new(vec![]),
show_qr: None,
show_slider: None,
device_lock_verification: None,
#[cfg(feature = "connect_handler")]
connect_handler_arc: None.into(),
reconnect_duration: Duration::from_millis(100),
}
}
pub fn modules<S: Into<Arc<Vec<Module>>>>(mut self, h: S) -> Self {
self.modules_vec = h.into();
self
}
pub fn result_handlers<E: Into<Arc<Vec<EventResultHandler>>>>(mut self, e: E) -> Self {
self.result_handlers_vec = e.into();
self
}
pub fn show_rq<E: Into<Option<ShowQR>>>(mut self, show_qr: E) -> Self {
self.show_qr = show_qr.into();
self
}
pub fn show_slider<E: Into<Option<ShowSlider>>>(mut self, show_slider: E) -> Self {
self.show_slider = show_slider.into();
self
}
#[cfg(all(any(target_os = "windows"), feature = "pop_window_slider"))]
pub fn show_slider_pop_menu_if_possible(self) -> Self {
self.show_slider(ShowSlider::PopWindow)
}
#[cfg(not(all(any(target_os = "windows"), feature = "pop_window_slider")))]
pub fn show_slider_pop_menu_if_possible(self) -> Self {
self
}
pub fn device_lock_verification<E: Into<Option<DeviceLockVerification>>>(
mut self,
device_lock_verification: E,
) -> Self {
self.device_lock_verification = device_lock_verification.into();
self
}
pub async fn build(&self) -> Result<Client, anyhow::Error> {
Ok(Client {
rq_client: Arc::new(ricq::Client::new(
match &self.device_source {
JsonFile(file_name) => {
if Path::new(file_name).exists() {
parse_device_json(
&tokio::fs::read_to_string(file_name)
.await
.with_context(|| format!("读取文件失败 : {}", file_name))?,
)?
} else {
let device = Device::random();
tokio::fs::write(file_name, serde_json::to_string(&device).unwrap())
.await
.with_context(|| format!("写入文件失败 : {}", file_name))?;
device
}
}
JsonString(json_string) => parse_device_json(json_string)?,
},
self.version.clone(),
ClientHandler {
modules: self.modules_vec.clone(),
result_handlers: self.result_handlers_vec.clone(),
},
)),
authentication: self
.authentication
.clone()
.with_context(|| "您必须设置验证方式 (调用authentication)")?,
session_store: self.session_store.clone(),
modules: self.modules_vec.clone(),
result_handlers: self.result_handlers_vec.clone(),
show_qr: if self.show_qr.is_some() {
self.show_qr.clone().unwrap()
} else {
#[cfg(feature = "console_qr")]
let show_qr = ShowQR::PrintToConsole;
#[cfg(not(feature = "console_qr"))]
let show_qr = ShowQR::SaveToFile;
show_qr
},
show_slider: if self.show_slider.is_some() {
self.show_slider.clone().unwrap()
} else {
ShowSlider::AndroidHelper
},
device_lock_verification: if self.device_lock_verification.is_some() {
self.device_lock_verification.clone().unwrap()
} else {
DeviceLockVerification::Url
},
shutting: false,
#[cfg(feature = "connect_handler")]
connection_handler: self.connect_handler_arc.clone(),
reconnect_duration: self.reconnect_duration,
})
}
pub fn device(mut self, device_source: DeviceSource) -> Self {
self.device_source = device_source;
self
}
pub fn version(mut self, version: &'static Version) -> Self {
self.version = version;
self
}
pub fn session_store(mut self, session_store: Box<dyn SessionStore + Sync + Send>) -> Self {
self.session_store = Arc::new(Some(session_store));
self
}
pub fn authentication(mut self, authentication: Authentication) -> Self {
self.authentication = Some(authentication);
self
}
#[cfg(feature = "connect_handler")]
pub fn connect_handler(
mut self,
connect_handler: Box<dyn ConnectionHandler + Sync + Send>,
) -> Self {
self.connect_handler_arc = Arc::new(Some(connect_handler));
self
}
pub fn reconnect_duration(mut self, reconnect_duration: Duration) -> Self {
self.reconnect_duration = reconnect_duration;
self
}
}
fn parse_device_json(json: &str) -> Result<Device, anyhow::Error> {
Ok(serde_json::from_str(json).with_context(|| format!("DeviceJson解析失败"))?)
}
#[cfg(feature = "console_qr")]
fn print_qr_to_console(buff: &Bytes) -> Result<()> {
let img = image::load_from_memory(buff)?.into_luma8();
let mut img = rqrr::PreparedImage::prepare(img);
let grids = img.detect_grids();
let (_, content) = grids.get(0).with_context(|| "未能识别出二维码")?.decode()?;
qr2term::print_qr(content.as_str())?;
Ok(())
}