dmc-dsg 0.1.4

DMC DSG Client
Documentation
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
#![macro_use]
#![allow(unused)]

use cyfs_lib::*;
use std::ops::{Deref};
use cyfs_base::*;
use async_trait::async_trait;
use std::sync::{Arc, Weak, Mutex};
use async_std::future::Future;
use cyfs_util::EventListenerAsyncRoutine;

pub(crate) const APP_ERROR_FAILED: u16 = 2000;

macro_rules! app_err {
    ( $err: expr) => {
    cyfs_base::BuckyError::new(cyfs_base::BuckyErrorCodeEx::DecError($err as u16), format!("{}:{} app_code_err:{}", file!(), line!(), stringify!($err)))
    };
}

macro_rules! app_err2 {
    ( $err: expr, $msg: expr) => {
    cyfs_base::BuckyError::new(cyfs_base::BuckyErrorCodeEx::DecError($err as u16), format!("{}:{} app_code_err:{} msg:{}", file!(), line!(), stringify!($err), $msg))
    };
}

trait ArcWeakHelper<T: ?Sized> {
    fn to_rc(&self) -> BuckyResult<Arc<T>>;
}

impl <T: ?Sized> ArcWeakHelper<T> for std::sync::Weak<T> {
    fn to_rc(&self) -> BuckyResult<Arc<T>> {
        match self.upgrade() {
            Some(v) => {
                Ok(v)
            },
            None => {
                Err(app_err!(APP_ERROR_FAILED))
            }
        }
    }
}

macro_rules! cyfs_err {
    ( $err: expr, $($arg:tt)*) => {
        {
            log::error!("{}", format!($($arg)*));
            cyfs_base::BuckyError::new($err, format!("{}:{} msg:{}", file!(), line!(), format!($($arg)*)))
        }
    };
}

pub enum SharedCyfsStackExEndpointResult {
    Pass,
    Accepted((ObjectId, Vec<u8>))
}

#[async_trait]
pub trait SharedCyfsStackExEndpoint: Send + Sync + 'static {
    async fn call(&self, param: &RouterHandlerPostObjectRequest) -> BuckyResult<SharedCyfsStackExEndpointResult>;
}

#[async_trait]
impl<F, Fut> SharedCyfsStackExEndpoint for F
    where
        F: Send + Sync + 'static + Fn(&RouterHandlerPostObjectRequest) -> Fut,
        Fut: Send + 'static + Future<Output = BuckyResult<SharedCyfsStackExEndpointResult>>,
{
    async fn call(&self, param: &RouterHandlerPostObjectRequest) -> BuckyResult<SharedCyfsStackExEndpointResult> {
        let fut = (self)(param);
        fut.await
    }
}

pub struct SharedCyfsStackServer {
    stack: Arc<SharedCyfsStack>,
    name: String,
    ep: Mutex<Option<Arc<dyn SharedCyfsStackExEndpoint>>>,
    req_path: String,
    dec_id: ObjectId,
    access: AccessString,
}
pub type SharedCyfsStackServerRef = Arc<SharedCyfsStackServer>;
pub type SharedCyfsStackServerWeakRef = Weak<SharedCyfsStackServer>;

impl Deref for SharedCyfsStackServer {
    type Target = Arc<SharedCyfsStack>;

    fn deref(&self) -> &Self::Target {
        &self.stack
    }
}

struct OnPutHandle {
    stackex: SharedCyfsStackServerWeakRef
}

#[async_trait]
impl EventListenerAsyncRoutine<RouterHandlerPostObjectRequest, RouterHandlerPostObjectResult> for OnPutHandle {
    async fn call(&self, param: &RouterHandlerPostObjectRequest) -> BuckyResult<RouterHandlerPostObjectResult> {
        self.stackex.to_rc()?.on_recv_obj(param).await
    }
}

impl SharedCyfsStackServer {
    pub fn new(name: String, stack: Arc<SharedCyfsStack>, dec_id: ObjectId, req_path: String, access: AccessString) -> SharedCyfsStackServerRef {
        SharedCyfsStackServerRef::new(Self {
            stack,
            name,
            ep: Mutex::new(None),
            req_path,
            dec_id,
            access
        })
    }

    pub fn get_stack(&self) -> &Arc<SharedCyfsStack> {
        &self.stack
    }

    pub fn set_end_point(&self, ep: impl SharedCyfsStackExEndpoint) {
        let mut self_ep = self.ep.lock().unwrap();
        *self_ep = Some(Arc::new(ep))
    }

    pub async fn listen(self: &SharedCyfsStackServerRef) -> BuckyResult<()> {
        let listener = OnPutHandle {
            stackex: SharedCyfsStackServerRef::downgrade(self)
        };

        self.stack.root_state_meta_stub(None, None).add_access(GlobalStatePathAccessItem {
            path: self.req_path.clone(),
            access: GlobalStatePathGroupAccess::Default(self.access.value())
        }).await?;

        self.stack.router_handlers().add_handler(RouterHandlerChain::Handler,
                                                 self.name.clone().as_str(),
                                                 0,
                                                 None,
                                                 Some(self.req_path.clone()),
                                                 RouterHandlerAction::Default,
                                                 Some(Box::new(listener)))?;
        Ok(())
    }

    pub async fn stop(&self) -> BuckyResult<bool> {
        self.stack.router_handlers().remove_handler(RouterHandlerChain::Handler,
                                                    RouterHandlerCategory::PostObject,
                                                    self.name.as_str()).await?;

        Ok(true)
    }

    pub(crate) async fn on_recv_obj(self: &SharedCyfsStackServerRef, param: &RouterHandlerPostObjectRequest) -> BuckyResult<RouterHandlerPostObjectResult> {
        let ep = {
            let ep = self.ep.lock().unwrap();
            if ep.is_some() {
                Some(ep.as_ref().unwrap().clone())
            } else {
                None
            }
        };

        if ep.is_some() {
            match ep.unwrap().call(param).await {
                Ok(ret) => {
                    match ret {
                        SharedCyfsStackExEndpointResult::Accepted((object_id, object_raw)) => {
                            Ok(RouterHandlerPostObjectResult {
                                action: RouterHandlerAction::Response,
                                request: None,
                                response: Some(Ok(NONPostObjectInputResponse {
                                    object: Some(NONObjectInfo {
                                        object_id,
                                        object_raw,
                                        object: None,
                                    })
                                })),
                            })
                        },
                        SharedCyfsStackExEndpointResult::Pass => {
                            Ok(
                                RouterHandlerPostObjectResult {
                                    action: RouterHandlerAction::Pass,
                                    request: None,
                                    response: None,
                                })
                        }
                    }
                },
                Err(e) => {
                    log::error!("handle err {}", &e);
                    Ok(RouterHandlerPostObjectResult {
                        action: RouterHandlerAction::Response,
                        request: None,
                        response: Some(Err(e))
                    })
                }
            }
        } else {
            Ok(
                RouterHandlerPostObjectResult {
                    action: RouterHandlerAction::Pass,
                    request: None,
                    response: None,
                })
        }
    }

    // pub async fn verify_object(&self, object_id: ObjectId, object_raw: Vec<u8>) -> BuckyResult<bool> {
    //     let req = NONVerifyBySignRequest {
    //         target: None,
    //         object_id,
    //         object_raw,
    //         desc_signs: None,
    //         body_signs: None
    //     };
    // }

    async fn sign_object(&self, object_id: ObjectId, object_raw: Vec<u8>) -> BuckyResult<Vec<u8>> {
        let flags = CRYPTO_REQUEST_FLAG_SIGN_BY_DEVICE | CRYPTO_REQUEST_FLAG_SIGN_PUSH_DESC;
        let resp = self.stack.crypto().sign_object(CryptoSignObjectRequest {
            common: CryptoOutputRequestCommon {
                req_path: None,
                dec_id: Some(self.dec_id.clone()),
                target: None,
                flags
            },
            flags,
            object: NONObjectInfo {
                object_id,
                object_raw,
                object: None
            }
        }).await?;

        Ok(resp.object.unwrap().object_raw)
    }
}

pub struct CyfsPath {
    target: ObjectId,
    target_dec_id: ObjectId,
    req_path: String,
}

impl CyfsPath {
    pub fn new(target: ObjectId, target_dec_id: ObjectId, req_path: &str) -> Self {
        Self {
            target,
            target_dec_id,
            req_path: req_path.to_string()
        }
    }

    pub fn to_path(&self) -> String {
        format!("/{}/{}/{}", self.target.to_string(), self.target_dec_id.to_string(), self.req_path)
    }

    pub fn parse(path: &str) -> BuckyResult<Self> {
        if !path.starts_with("/") {
            return Err(cyfs_err!(BuckyErrorCode::InvalidFormat, "parse {} err", path));
        }
        let path_ref = &path[1..];
        let pos = path_ref.find("/");
        if pos.is_none() {
            return Err(cyfs_err!(BuckyErrorCode::InvalidFormat, "parse {} err", path));
        }
        let target = &path_ref[..pos.unwrap()];

        let path_ref = &path_ref[pos.unwrap() + 1..];
        let pos = path_ref.find("/");
        if pos.is_none() {
            return Err(cyfs_err!(BuckyErrorCode::InvalidFormat, "parse {} err", path));
        }
        let target_dec_id = &path_ref[..pos.unwrap()];

        let req_path = path_ref[pos.unwrap() + 1..].to_string();
        Ok(Self {
            target: ObjectId::from_base58(target)?,
            target_dec_id: ObjectId::from_base58(target_dec_id)?,
            req_path
        })
    }
}

pub type SharedCyfsStackRef = Arc<SharedCyfsStack>;

#[async_trait::async_trait]
pub trait SharedCyfsStackEx {
    async fn sign_object(&self, object_id: ObjectId, object_raw: Vec<u8>) -> BuckyResult<Vec<u8>>;
    async fn sign_object2<T: ObjectType + Sync + Send, O: for <'a> RawDecode<'a>>(&self, obj: &NamedObjectBase<T>) -> BuckyResult<O>
        where <T as cyfs_base::ObjectType>::ContentType: cyfs_base::BodyContent + cyfs_base::RawEncode,
              <T as cyfs_base::ObjectType>::DescType: RawEncodeWithContext<cyfs_base::NamedObjectContext>;
    async fn resolve_ood(&self, object_id: ObjectId) -> BuckyResult<ObjectId>;
    async fn resolve_ood_list(&self, object_id: ObjectId) -> BuckyResult<Vec<DeviceId>>;
    async fn get_object_from_noc<T: for <'a> RawDecode<'a>>(&self, object_id: ObjectId) -> BuckyResult<T>;
    async fn put_object_to_noc<T: ObjectType + Sync + Send>(&self, obj: &NamedObjectBase<T>) -> BuckyResult<ObjectId>
        where <T as cyfs_base::ObjectType>::ContentType: cyfs_base::BodyContent + cyfs_base::RawEncode,
              <T as cyfs_base::ObjectType>::DescType: RawEncodeWithContext<cyfs_base::NamedObjectContext>;
    async fn get_object<T: for <'a> RawDecode<'a>>(
        &self,
        target: Option<ObjectId>,
        object_id: ObjectId
    ) -> BuckyResult<T>;
    async fn put_object_with_resp(
        &self,
        req_path: &str,
        object_id: ObjectId,
        object_raw: Vec<u8>
    ) -> BuckyResult<Vec<u8>>;
    async fn put_object_with_resp2<T: RawEncode + for <'a> RawDecode<'a>>(
        &self,
        req_path: &str,
        object_id: ObjectId,
        object_raw: Vec<u8>
    ) -> BuckyResult<T>;
    async fn gen_aes_key_and_encrypt(&self) -> BuckyResult<(AesKey, Vec<u8>)>;
    async fn decrypt_aes_key(&self, encrypt_aes_key: Vec<u8>) -> BuckyResult<AesKey>;
}

#[async_trait::async_trait]
impl SharedCyfsStackEx for SharedCyfsStack {
    async fn sign_object(&self, object_id: ObjectId, object_raw: Vec<u8>) -> BuckyResult<Vec<u8>> {
        let flags = CRYPTO_REQUEST_FLAG_SIGN_BY_DEVICE | CRYPTO_REQUEST_FLAG_SIGN_PUSH_DESC;
        let resp = self.crypto().sign_object(CryptoSignObjectRequest {
            common: CryptoOutputRequestCommon {
                req_path: None,
                dec_id: None,
                target: None,
                flags
            },
            flags,
            object: NONObjectInfo {
                object_id,
                object_raw,
                object: None
            }
        }).await?;

        Ok(resp.object.unwrap().object_raw)
    }

    async fn sign_object2<T: ObjectType + Sync + Send, O: for<'a> RawDecode<'a>>(&self, obj: &NamedObjectBase<T>) -> BuckyResult<O>
        where <T as ObjectType>::ContentType: BodyContent + RawEncode, <T as ObjectType>::DescType: RawEncodeWithContext<NamedObjectContext> {
        let object_id = obj.desc().calculate_id();
        let signed = self.sign_object(object_id, obj.to_vec()?).await?;
        O::clone_from_slice(signed.as_slice())
    }

    async fn resolve_ood(&self, object_id: ObjectId) -> BuckyResult<ObjectId> {
        let resp = self.util().resolve_ood(UtilResolveOODRequest {
            common: UtilOutputRequestCommon {
                req_path: None,
                dec_id: None,
                target: None,
                flags: 0
            },
            object_id,
            owner_id: None
        }).await?;

        let ood_id = resp.device_list[0].object_id().clone();
        Ok(ood_id)
    }

    async fn resolve_ood_list(&self, object_id: ObjectId) -> BuckyResult<Vec<DeviceId>> {
        let resp = self.util().resolve_ood(UtilResolveOODRequest {
            common: UtilOutputRequestCommon {
                req_path: None,
                dec_id: None,
                target: None,
                flags: 0
            },
            object_id,
            owner_id: None
        }).await?;

        Ok(resp.device_list)
    }

    async fn get_object_from_noc<T: for<'a> RawDecode<'a>>(&self, object_id: ObjectId) -> BuckyResult<T> {
        self.get_object(None, object_id).await
    }

    async fn put_object_to_noc<T: ObjectType + Sync + Send>(&self, obj: &NamedObjectBase<T>) -> BuckyResult<ObjectId>
        where <T as cyfs_base::ObjectType>::ContentType: cyfs_base::BodyContent + cyfs_base::RawEncode,
              <T as cyfs_base::ObjectType>::DescType: RawEncodeWithContext<cyfs_base::NamedObjectContext> {
        let object_id = obj.desc().calculate_id();
        let object_raw = obj.to_vec()?;
        self.non_service().put_object(NONPutObjectOutputRequest { common: NONOutputRequestCommon {
            req_path: None,
            source: None,
            dec_id: None,
            level: NONAPILevel::NOC,
            target: None,
            flags: 0
        }, object: NONObjectInfo {
            object_id: object_id.clone(),
            object_raw,
            object: None
        },
            access: None
        }).await?;

        Ok(object_id)
    }

    async fn get_object<T: for <'a> RawDecode<'a>>(&self, target: Option<ObjectId>, object_id: ObjectId) -> BuckyResult<T> {
        let mut err = None;
        for _ in 0..3 {
            let resp = match self.non_service().get_object(NONGetObjectOutputRequest {
                common: NONOutputRequestCommon {
                    req_path: None,
                    source: None,
                    dec_id: None,
                    level: if target.is_none() {NONAPILevel::NOC} else {NONAPILevel::Router},
                    target,
                    flags: 0
                },
                object_id: object_id.clone(),
                inner_path: None
            }).await {
                Ok(resp) => resp,
                Err(e) => {
                    log::error!("get_object {} err {}", object_id.to_string(), e);
                    err = Some(e);
                    continue;
                }
            };

            return T::clone_from_slice(resp.object.object_raw.as_slice());
        }
        Err(err.unwrap())
    }

    #[tracing::instrument(skip(self, object_raw), err)]
    async fn put_object_with_resp(&self, req_path: &str, object_id: ObjectId, object_raw: Vec<u8>) -> BuckyResult<Vec<u8>> {
        let cyfs_path = CyfsPath::parse(req_path)?;
        let path = RequestGlobalStatePath {
            global_state_category: None,
            global_state_root: None,
            dec_id: Some(cyfs_path.target_dec_id),
            req_path: Some(cyfs_path.req_path)
        };

        let resp = self.non_service().post_object(NONPostObjectOutputRequest {
            common: NONOutputRequestCommon {
                req_path: Some(path.to_string()),
                source: None,
                dec_id: None,
                level: NONAPILevel::Router,
                target: Some(cyfs_path.target),
                flags: 0
            },
            object: NONObjectInfo {
                object_id,
                object_raw,
                object: None
            }
        }).await?;

        if resp.object.is_none() {
            Err(cyfs_err!(BuckyErrorCode::InvalidData, "resp data is none"))
        } else {
            let object_raw = resp.object.unwrap().object_raw;
            Ok(object_raw)
        }
    }

    #[tracing::instrument(skip(self, object_raw), err)]
    async fn put_object_with_resp2<T: RawEncode + for <'a> RawDecode<'a>>(&self, req_path: &str, object_id: ObjectId, object_raw: Vec<u8>) -> BuckyResult<T> {
        let cyfs_path = CyfsPath::parse(req_path)?;
        let path = RequestGlobalStatePath {
            global_state_category: None,
            global_state_root: None,
            dec_id: Some(cyfs_path.target_dec_id),
            req_path: Some(cyfs_path.req_path)
        };

        let resp = self.non_service().post_object(NONPostObjectOutputRequest {
            common: NONOutputRequestCommon {
                req_path: Some(path.to_string()),
                source: None,
                dec_id: None,
                level: NONAPILevel::Router,
                target: Some(cyfs_path.target),
                flags: 0
            },
            object: NONObjectInfo {
                object_id,
                object_raw,
                object: None
            }
        }).await?;

        if resp.object.is_none() {
            Err(cyfs_err!(BuckyErrorCode::InvalidData, "resp data is none"))
        } else {
            let object_raw = resp.object.unwrap().object_raw;
            Ok(T::clone_from_slice(object_raw.as_slice())?)
        }
    }

    async fn gen_aes_key_and_encrypt(&self) -> BuckyResult<(AesKey, Vec<u8>)> {
        let req = CryptoEncryptDataOutputRequest::new().by_device().gen_aeskey_and_encrypt();
        let resp = self.crypto().encrypt_data(req).await?;
        Ok((resp.aes_key.unwrap(), resp.result))
    }

    async fn decrypt_aes_key(&self, encrypt_aes_key: Vec<u8>) -> BuckyResult<AesKey> {
        let req = CryptoDecryptDataOutputRequest::new(encrypt_aes_key).by_device().decrypt_aeskey();
        let resp = self.crypto().decrypt_data(req).await?;
        Ok(AesKey::from(resp.data))
    }
}