blob: 34c70dc4d9422f27f622152bafe87cc00ed91461 (
plain)
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
|
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
use crate::{
command::{Input, Output},
executable::ClientExecutableCommand,
};
#[async_trait::async_trait]
pub trait Flow {
fn commands(&self) -> Vec<Box<dyn ClientExecutableCommand>>;
async fn execute(&self, mut input: Input, mut output: Output) -> Result<(), miette::Error>;
}
pub struct WaitForConnectFlow;
#[async_trait::async_trait]
impl Flow for WaitForConnectFlow {
fn commands(&self) -> Vec<Box<dyn ClientExecutableCommand>> {
vec![Box::new(crate::executable::QuitCommand)]
}
async fn execute(&self, _input: Input, mut output: Output) -> Result<(), miette::Error> {
output
.wait_and_check(Box::new(|bytes: &[u8]| -> bool {
let connect_flags = if let Some(flags) = find_connect_flags(bytes) {
flags
} else {
return false;
};
let username_flag_set = 0 != (connect_flags & 0b1000_0000); // Username flag
let password_flag_set = 0 != (connect_flags & 0b0100_0000); // Username flag
if username_flag_set {
!password_flag_set
} else {
true
}
}))
.await
}
}
fn find_connect_flags(bytes: &[u8]) -> Option<u8> {
macro_rules! getbyte {
($n:tt) => {
if let Some(b) = bytes.get($n) {
*b
} else {
return None;
}
};
}
if getbyte!(0) != 0b0001_0000 {
return None;
}
let str_len = getbyte!(4);
let connect_flag_position = 4usize + (str_len as usize) + 2;
Some(getbyte!(connect_flag_position))
}
|