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
use chrono::prelude::*;
use futures::sink::SinkExt;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
pub(super) mod tls_client;
pub(super) async fn write_msg_log(
msg: &str,
msg_type: &str,
root: &std::path::Path,
) -> tokio::io::Result<()> {
let now = Utc::now();
let date = now.format("%F").to_string();
let time = now.format("%H-%M-%S-%f").to_string();
let dir = root.join(date);
let file_path = dir.join(format!("{}_{}.xml", time, msg_type));
tokio::fs::create_dir_all(&dir).await?;
let mut file = tokio::fs::File::create(file_path).await?;
file.write_all(msg.as_bytes()).await?;
Ok(())
}
pub(super) async fn recv_length_msg<R: std::marker::Unpin + tokio::io::AsyncRead>(
sock: &mut R,
host: &str,
root: &std::path::Path,
) -> Result<String, bool> {
let data_len = match sock.read_u32().await {
Ok(l) => l - 4,
Err(err) => {
return Err(match err.kind() {
std::io::ErrorKind::UnexpectedEof => {
warn!("{} has closed the connection", host);
true
}
_ => {
error!("Error reading next data unit length from {}: {}", host, err);
false
}
});
}
};
let mut data_buf = vec![0u8; data_len as usize];
match sock.read_exact(&mut data_buf).await {
Ok(n) => {
if n != data_len as usize {
error!("Read less data than expected from {}", host);
return Err(false);
}
}
Err(err) => {
error!("Error reading next data from {}: {}", host, err);
return Err(matches!(err.kind(), std::io::ErrorKind::UnexpectedEof));
}
}
let data = match String::from_utf8(data_buf) {
Ok(s) => s,
Err(err) => {
error!("Invalid UTF8 from {}: {}", host, err);
return Err(false);
}
};
debug!("Received message from {} with contents: {}", host, data);
match write_msg_log(&data, "recv", root).await {
Ok(_) => {}
Err(e) => {
error!("Failed writing received message to message log: {}", e);
}
}
Ok(data)
}
pub(super) async fn recv_msg<T, R: std::marker::Unpin + tokio::io::AsyncRead>(
sock: &mut R,
host: &str,
root: &std::path::Path,
decode_fn: fn(data: String, host: &str) -> Result<T, ()>,
) -> Result<T, bool> {
let data = recv_length_msg(sock, host, root).await?;
let msg = decode_fn(data, host).map_err(|_| false)?;
Ok(msg)
}
pub(super) async fn send_msg<T, W: std::marker::Unpin + tokio::io::AsyncWrite>(
host: &str,
sock: &mut W,
root: &std::path::Path,
encode_fn: fn(message: &T, host: &str) -> Result<String, ()>,
message: &T,
) -> Result<(), ()> {
let encoded_msg = encode_fn(message, host)?;
debug!("Sending message to {} with contents: {}", host, encoded_msg);
let msg_bytes = encoded_msg.as_bytes();
let msg_len = msg_bytes.len() + 4;
match sock.write_u32(msg_len as u32).await {
Ok(_) => {}
Err(err) => {
error!("Error writing data unit length to {}: {}", &host, err);
return Err(());
}
};
match sock.write(msg_bytes).await {
Ok(_) => {}
Err(err) => {
error!("Error writing data unit to {}: {}", &host, err);
return Err(());
}
}
match write_msg_log(&encoded_msg, "send", root).await {
Ok(_) => {}
Err(e) => {
error!("Failed writing sent message to message log: {}", e);
}
}
Ok(())
}
pub(super) struct ClientReceiver<T, R: std::marker::Unpin + tokio::io::AsyncRead> {
pub host: String,
pub reader: R,
pub root: std::path::PathBuf,
pub decode_fn: fn(data: String, host: &str) -> Result<T, ()>,
}
impl<
T: 'static + std::marker::Send,
R: 'static + std::marker::Unpin + tokio::io::AsyncRead + std::marker::Send,
> ClientReceiver<T, R>
{
pub fn run(mut self) -> futures::channel::mpsc::Receiver<Result<T, bool>> {
let (mut sender, receiver) = futures::channel::mpsc::channel::<Result<T, bool>>(16);
tokio::spawn(async move {
loop {
let msg = recv_msg(&mut self.reader, &self.host, &self.root, self.decode_fn).await;
let is_close = if let Err(c) = &msg { *c } else { false };
match sender.send(msg).await {
Ok(_) => {}
Err(_) => break,
}
if is_close {
break;
}
}
});
receiver
}
}
pub(super) async fn make_tcp_socket(
host: &str,
source_addr: &Option<std::net::IpAddr>,
) -> Result<tokio::net::TcpStream, ()> {
let addr = match tokio::net::lookup_host(host).await {
Ok(mut s) => match s.next() {
Some(s) => s,
None => {
error!("Resolving {} returned no records", host);
return Err(());
}
},
Err(err) => {
error!("Failed to resolve {}: {}", host, err);
return Err(());
}
};
trace!("Setting up TCP socket for {}", host);
let socket = match if addr.is_ipv4() {
tokio::net::TcpSocket::new_v4()
} else if addr.is_ipv6() {
tokio::net::TcpSocket::new_v6()
} else {
unreachable!()
} {
Ok(s) => s,
Err(err) => {
error!("Unable to create TCP socket for {}: {}", host, err);
return Err(());
}
};
match socket.set_reuseaddr(true) {
Ok(()) => {}
Err(err) => {
error!("Unable to setup TCP socket for {}: {}", host, err);
return Err(());
}
}
if let Some(bind_addr) = source_addr {
trace!("Setting source address to {} for {}", bind_addr, host);
match socket.bind(std::net::SocketAddr::new(bind_addr.to_owned(), 0)) {
Ok(()) => {}
Err(err) => {
error!("Unable to setup TCP socket for {}: {}", host, err);
return Err(());
}
}
}
Ok(match socket.connect(addr).await {
Ok(s) => s,
Err(err) => {
error!("Unable to connect to {}: {}", host, err);
return Err(());
}
})
}