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
use std::{net::{IpAddr, Ipv4Addr, SocketAddr}, path::Path, str::FromStr, time::{Duration, Instant}};
use async_std::{
sync::Arc,
task,
net::{TcpListener, TcpStream},
stream::StreamExt,
future,
io::prelude::*,
fs::File,
};
use cyfs_base::*;
use crate::{
stack::{WeakStack, Stack},
tunnel::{BuildTunnelParams},
datagram::{self, DatagramOptions},
download::*,
DownloadTaskControl,
TaskControlState,
types::*,
ChunkDownloadConfig,
};
use super::command::*;
use super::super::sn::client::SnStatus;
struct DebugStubImpl {
stack: WeakStack,
listener: TcpListener,
}
#[derive(Clone)]
pub struct Config {
pub local: String,
pub port: u16
}
#[derive(Clone)]
pub struct DebugStub(Arc<DebugStubImpl>);
impl DebugStub {
pub async fn open(weak_stack: WeakStack) -> BuckyResult<Self> {
let stack = Stack::from(&weak_stack);
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::from_str(stack.config().debug.as_ref().unwrap().local.as_str()).unwrap()),
stack.config().debug.as_ref().unwrap().port);
let listener = TcpListener::bind(addr).await?;
Ok(Self(Arc::new(DebugStubImpl {
stack: weak_stack,
listener
})))
}
pub fn listen(&self) {
const READ_CMD_TIMEOUT: u64 = 30;
let stub = self.clone();
task::spawn(async move {
let mut incoming = stub.0.listener.incoming();
loop {
if let Some(stream) = incoming.next().await {
if let Ok(stream) = stream {
let stub = stub.clone();
task::spawn(async move {
let mut stream = stream;
let mut command = String::new();
match future::timeout(Duration::from_secs(READ_CMD_TIMEOUT), stream.read_to_string(&mut command)).await {
Err(_) => {
error!("read cmd timeout!");
},
Ok(_) => {
stub.handle_command(command, stream).await;
}
}
});
}
}
}
});
let stack = Stack::from(&self.0.stack);
let debug_tunnel = stack.datagram_manager().bind_reserved(datagram::ReservedVPort::Debug).unwrap();
task::spawn(async move {
loop {
match debug_tunnel.recv_v().await {
Ok(datagrams) => {
for datagram in datagrams {
let resp = b"debug";
let mut options = datagram.options.clone();
let _ = debug_tunnel.send_to(
resp.as_ref(),
&mut options,
&datagram.source.remote,
datagram.source.vport);
}
},
Err(_err) => {
}
}
}
});
}
async fn handle_command(&self, command: String, tunnel: TcpStream) {
println!("command:{}", command);
let stack = Stack::from(&self.0.stack);
let mut tunnel = tunnel;
match DebugCommand::from_str(&stack, &command).await {
Ok(command) => {
if let Err(err) = match command {
DebugCommand::Test(_) => self.test(tunnel.clone()).await,
DebugCommand::Ping(command) => self.ping(tunnel.clone(), command).await,
DebugCommand::Nc(command) => self.nc(tunnel.clone(), command).await,
DebugCommand::GetChunk(command) => self.get_chunk(tunnel.clone(), command).await,
DebugCommand::GetFile(command) => self.get_file(tunnel.clone(), command).await,
DebugCommand::PutChunk(command) => self.put_chunk(tunnel.clone(), command).await,
DebugCommand::PutFile(command) => self.put_file(tunnel.clone(), command).await,
DebugCommand::SnConnStatus(command) => self.sn_conn_status(tunnel.clone(), command).await,
} {
let _ = tunnel.write_all(err.as_ref()).await;
}
},
Err(err) => {
let _ = tunnel.write_all(err.as_ref()).await;
}
}
}
async fn test(&self, tunnel: TcpStream) -> Result<(), String> {
let mut tunnel = tunnel;
let _ = tunnel.write_all(format!("hello\r\n").as_bytes()).await;
let _ = tunnel.write_all(format!("bdt\r\n").as_bytes()).await;
Ok(())
}
async fn sn_conn_status(&self, tunnel: TcpStream, command: DebugCommandSnConnStatus) -> Result<(), String> {
let mut tunnel = tunnel;
let stack = Stack::from(&self.0.stack);
let dev_id_option = {
if command.sn.is_none() {
let sn_list = stack.sn_client().sn_list();
let sn = sn_list.get(0);
match sn {
Some(sn) => {
let sn = sn.clone();
Some(sn)
},
_ => None,
}
} else {
command.sn
}
};
if dev_id_option.is_none() {
let _ = tunnel.write_all("Err: sn is none\r\n".as_ref()).await;
return Ok(())
}
let sn_dev_id = dev_id_option.unwrap();
let timeout = {
if command.timeout_sec == 0 {
6
} else {
command.timeout_sec
}
};
let sleep_ms = 200;
let mut counter = timeout*(1000/sleep_ms);
loop {
let sn_status = stack.sn_client().status_of(&sn_dev_id);
match sn_status {
Some(st) => {
if st == SnStatus::Online {
let _ = tunnel.write_all("Ok: sn connected\r\n".as_ref()).await;
return Ok(())
}
},
_ => {}
}
counter -= 1;
if counter == 0 {
break ;
}
task::sleep(Duration::from_millis(sleep_ms)).await;
}
let _ = tunnel.write_all("Err: sn connect timeout\r\n".as_ref()).await;
Ok(())
}
async fn ping(&self, tunnel: TcpStream, command: DebugCommandPing) -> Result<(), String> {
let mut tunnel = tunnel;
let stack = Stack::from(&self.0.stack);
let datagram = stack.datagram_manager().bind(0)
.map_err(|err| format!("deamon bind datagram tunnel failed for {}\r\n", err))?;
for _ in 0..command.count {
let mut options = DatagramOptions::default();
let _ = tunnel.write_all("send ping.\r\n".as_ref()).await;
let ts = cyfs_base::bucky_time_now();
options.sequence = Some(TempSeq::from(ts as u32));
let _ = datagram.send_to(
"debug".as_ref(),
&mut options,
&command.remote.desc().device_id(),
datagram::ReservedVPort::Debug.into());
match future::timeout(command.timeout, datagram.recv_v()).await {
Err(_err) => {
let _ = tunnel.write_all("timeout\r\n".as_ref()).await;
},
Ok(res) => {
let datagrams = res.unwrap();
for datagram in datagrams {
if let Some(opt) = datagram.options.sequence {
if opt == options.sequence.unwrap() {
let s = format!("respose. time: {:.1} ms\r\n",
(cyfs_base::bucky_time_now() - ts) as f64 / 1000.0);
let _ = tunnel.write_all(s.as_bytes()).await;
break ;
}
}
}
}
}
}
Ok(())
}
async fn nc(&self, tunnel: TcpStream, command: DebugCommandNc) -> Result<(), String> {
let mut tunnel = tunnel;
let stack = Stack::from(&self.0.stack);
let _ = tunnel.write_all("connecting stream\r\n".as_ref()).await;
let _ = stack.stream_manager().connect(command.port, vec![], BuildTunnelParams {
remote_const: command.remote.desc().clone(),
remote_sn: vec![],
remote_desc: Some(command.remote.clone())
}).await.map_err(|err| format!("Err: {}\r\n", err.msg().to_string()))?;
let _ = tunnel.write_all("Ok: stream connected\r\n".as_ref()).await;
Ok(())
}
async fn get_chunk(&self, tunnel: TcpStream, command: DebugCommandGetChunk) -> Result<(), String> {
let mut tunnel = tunnel;
let stack = Stack::from(&self.0.stack);
let chunk_id = command.chunk_id;
let remotes = command.remotes;
let timeout = command.timeout;
let local_path = command.local_path;
let _ = tunnel.write_all("start downloading chunk..\r\n".as_ref()).await;
let task = download_chunk_to_path(&stack,
chunk_id,
ChunkDownloadConfig::from(remotes),
&local_path).await
.map_err(|e| format!("download err: {}\r\n", e))?;
let _ = tunnel.write_all("waiting..\r\n".as_ref()).await;
let task_start_time = Instant::now();
let ret = watchdog_download_finished(task, timeout).await;
if ret.is_ok() {
let size = get_filesize(&local_path);
let cost = Instant::now() - task_start_time;
let cost_sec = (cost.as_millis() as f64) / 1000.0;
let speed = (size as f64) * 8.0 / cost_sec / 1000000.0;
let _ = tunnel.write_all(format!("download chunk finish.\r\nsize: {:.1} MB\r\ncost: {:.1} s\r\nspeed: {:.1} Mbps\r\n",
size/1024/1024, cost_sec, speed).as_bytes()).await;
}
ret
}
async fn get_file(&self, tunnel: TcpStream, command: DebugCommandGetFile) -> Result<(), String> {
let mut tunnel = tunnel;
let stack = Stack::from(&self.0.stack);
let file_id = command.file_id;
let remotes = command.remotes;
let timeout = command.timeout;
let local_path = command.local_path;
let _ = tunnel.write_all("start downloading file..\r\n".as_ref()).await;
let task = download_file_to_path(&stack, file_id,
ChunkDownloadConfig::from(remotes),
&local_path).await.map_err(|e| {
format!("download err: {}\r\n", e)
})?;
let _ = tunnel.write_all("waitting..\r\n".as_ref()).await;
let ret = watchdog_download_finished(task, timeout).await;
if ret.is_ok() {
let _ = tunnel.write_all("download file finish.\r\n".as_ref()).await;
}
ret
}
async fn put_chunk(&self, tunnel: TcpStream, command: DebugCommandPutChunk) -> Result<(), String> {
let mut tunnel = tunnel;
let stack = Stack::from(&self.0.stack);
let local_path = command.local_path;
if local_path.as_path().exists() {
let mut file = async_std::fs::File::open(local_path.as_path()).await.map_err(|e| {
format!("open file err: {}\r\n", e)
})?;
let mut content = Vec::<u8>::new();
let _ = file.read_to_end(&mut content).await.map_err(|e| {
format!("read file err: {}\r\n", e)
})?;
if content.len() == 0 {
return Err(format!("file size is zero\r\n"));
}
match ChunkId::calculate(content.as_slice()).await {
Ok(chunk_id) => {
let _ = track_chunk_in_path(&stack, &chunk_id, local_path).await
.map_err(|e| format!("put chunk err: {}\r\n", e))?;
let _ = tunnel.write_all(format!("put chunk success. chunk_id: {}\r\n",
chunk_id.to_string()).as_bytes()).await;
Ok(())
},
Err(e) => {
Err(format!("calculate chunk id err: {}\r\n", e))
}
}
} else {
Err(format!("file not exists: {}\r\n", local_path.to_str().unwrap()))
}
}
async fn put_file(&self, tunnel: TcpStream, command: DebugCommandPutFile) -> Result<(), String> {
let mut tunnel = tunnel;
let stack = Stack::from(&self.0.stack);
let local_path = command.local_path;
if local_path.as_path().exists() {
let chunkids = {
let _ = tunnel.write_all("calculate chunkid by file..\r\n".as_ref()).await;
let chunk_size: usize = 10 * 1024 * 1024;
let mut chunkids = Vec::new();
let mut file = File::open(local_path.as_path()).await.map_err(|e| {
format!("open file err: {}\r\n", e)
})?;
loop {
let mut buf = vec![0u8; chunk_size];
let len = file.read(&mut buf).await.map_err(|e| {
format!("read file err: {}\r\n", e)
})?;
if len > 0 {
if len < chunk_size {
buf.truncate(len);
}
let hash = hash_data(&buf[..]);
let chunkid = ChunkId::new(&hash, buf.len() as u32);
chunkids.push(chunkid);
}
if len < chunk_size {
break ;
}
}
chunkids
};
let (hash, len) = hash_file(local_path.as_path()).await.map_err(|e| {
format!("hash file err: {}\r\n", e)
})?;
let file = cyfs_base::File::new(
ObjectId::default(),
len,
hash,
ChunkList::ChunkInList(chunkids)
).no_create_time().build();
let buf_len_ret = file.raw_measure(&None);
if buf_len_ret.is_err() {
return Err(format!("raw_measure err\r\n"));
}
let mut buf = vec![0u8; buf_len_ret.unwrap()];
let encode_ret = file.raw_encode(buf.as_mut_slice(), &None);
if encode_ret.is_err() {
return Err(format!("raw_encode err\r\n"));
}
track_file_in_path(&stack, file, local_path).await
.map_err(|e| format!("track file err {}\r\n", e))?;
let _ = tunnel.write_all(format!("put file sucess. file_id: {}\r\n",
hex::encode(buf)).as_bytes()).await;
Ok(())
} else {
Err(format!("{} not exists\r\n", &local_path.to_str().unwrap()))
}
}
}
async fn watchdog_download_finished(task: Box<dyn DownloadTaskControl>, timeout: u32) -> Result<(), String> {
let mut _timeout = 1800;
let mut i = 0;
loop {
match task.control_state() {
TaskControlState::Finished => {
break Ok(());
},
TaskControlState::Downloading(speed, _) => {
if speed > 0 {
i = 0;
if _timeout == 1800 {
_timeout = timeout;
}
} else {
i += 1;
}
},
TaskControlState::Canceled => {
break Err(format!("download canceled\r\n"));
},
TaskControlState::Paused => {
},
TaskControlState::Err(e) => {
break Err(format!("download err, code: {:?}\r\n", e));
},
}
if i >= _timeout {
let _ = task.cancel();
break Err(format!("download timeout\r\n"));
}
task::sleep(Duration::from_secs(1)).await;
}
}
fn get_filesize(path: &Path) -> u64 {
let res = std::fs::File::open(path);
if res.is_ok() {
let file = res.unwrap();
return file.metadata().unwrap().len();
}
0
}