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
//! Filesystem tree refresh and parsing.
use std::collections::HashMap;
use serde_json::{Value, json};
use crate::base64::base64url_decode;
use crate::crypto::aes::{aes128_cbc_decrypt, aes128_ecb_decrypt};
use crate::error::{MegaError, Result};
use crate::fs::node::{Node, NodeType};
use crate::session::Session;
impl Session {
/// Refresh the filesystem tree from the server.
///
/// This fetches all nodes (SDK-style `f`), share keys, and public links, then decrypts
/// attributes and rebuilds cached paths.
/// Must be called before using `list()`, `stat()`, etc.
/// Cloud Drive paths are rooted at `/Root`.
///
/// # Example
/// ```no_run
/// # use megalib::Session;
/// # async fn example() -> megalib::error::Result<()> {
/// let mut session = Session::login("user@example.com", "password").await?;
/// session.refresh().await?;
/// let nodes = session.list("/Root", false)?;
/// # Ok(())
/// # }
/// ```
pub async fn refresh(&mut self) -> Result<()> {
// Match SDK behavior: ensure keys are initialized before fetching nodes.
self.ensure_keys_attribute().await?;
// Fetch filesystem data
let response = self
.api_mut()
.request(json!({"a": "f", "c": 1, "r": 1, "ca": 1}))
.await?;
if let Some(sn) = response.get("sn").and_then(|v| v.as_str()) {
self.scsn = Some(sn.to_string());
self.wsc_url = None;
self.sc_catchup = true;
self.current_seqtag = None;
self.current_seqtag_seen = false;
self.alerts_catchup_pending = true;
}
// Parse share keys from "ok" array
if let Some(ok_array) = response.get("ok").and_then(|v| v.as_array()) {
self.parse_share_keys(ok_array);
}
// Parse outgoing shares (and pending shares) to seed sharee tracking.
if let Some(s_array) = response.get("s").and_then(|v| v.as_array()) {
self.ingest_outshares_from_fetch(s_array);
}
// Parse public links from "ph" array (if present)
let public_links = response
.get("ph")
.and_then(|v| v.as_array())
.map(|arr| Self::parse_public_links(arr))
.unwrap_or_default();
// Parse nodes from "f" array
let nodes_array = response
.get("f")
.and_then(|v| v.as_array())
.ok_or(MegaError::InvalidResponse)?;
// Preload share keys for our own folders so children can decrypt when no ok entry is usable.
for node_json in nodes_array {
if let Some(1) = node_json.get("t").and_then(|v| v.as_i64()) {
if let (Some(handle), Some(kstr)) = (
node_json.get("h").and_then(|v| v.as_str()),
node_json.get("k").and_then(|v| v.as_str()),
) {
for part in kstr.split('/') {
if let Some((key_handle, encrypted_key)) = part.split_once(':') {
if key_handle == self.user_handle {
if let Ok(enc) = base64url_decode(encrypted_key) {
let dec = aes128_ecb_decrypt(&enc, self.master_key());
if dec.len() >= 16 {
let mut key = [0u8; 16];
key.copy_from_slice(&dec[..16]);
self.share_keys.entry(handle.to_string()).or_insert(key);
// Also populate key_manager for upgraded flows.
if self.key_manager.is_ready() {
self.key_manager.add_share_key_from_str(handle, &key);
}
}
}
}
}
}
}
}
}
let mut nodes = Vec::new();
for node_json in nodes_array {
if let Some(mut node) = self.parse_node(node_json) {
if let Some(link) = public_links.get(&node.handle) {
node.link = Some(link.clone());
}
nodes.push(node);
}
}
// Build node paths
Self::build_node_paths(&mut nodes);
// Store nodes
self.nodes = nodes;
// Clear in-use flags for share keys no longer present, persist if changed.
if self.clear_inuse_flags_for_missing_shares() {
let _ = self.persist_keys_with_retry().await;
}
Ok(())
}
/// Parse share keys from the "ok" array response.
///
/// Share keys can be encrypted with:
/// - AES (master key) - for your own shares (key length <= 22 base64 chars)
/// - RSA (private key) - for shares from other users (key length > 22 base64 chars)
fn parse_share_keys(&mut self, ok_array: &[Value]) {
for ok in ok_array {
if let (Some(h), Some(k)) = (
ok.get("h").and_then(|v| v.as_str()),
ok.get("k").and_then(|v| v.as_str()),
) {
// Determine if RSA or AES based on key length (megatools heuristic)
if k.len() > 22 {
// RSA-encrypted share key (from another user)
if let Ok(encrypted) = base64url_decode(k) {
if let Some(decrypted) = self.rsa_key().decrypt(&encrypted) {
if decrypted.len() >= 16 {
let mut key = [0u8; 16];
key.copy_from_slice(&decrypted[..16]);
self.share_keys.entry(h.to_string()).or_insert(key);
}
}
}
} else {
// AES-encrypted share key (your own share)
if let Ok(encrypted) = base64url_decode(k) {
let decrypted = aes128_ecb_decrypt(&encrypted, self.master_key());
if decrypted.len() >= 16 {
let mut key = [0u8; 16];
key.copy_from_slice(&decrypted[..16]);
self.share_keys.entry(h.to_string()).or_insert(key);
}
}
}
}
}
}
/// Parse public link handles from the "ph" array response.
///
/// Returns a map of node handle -> public link handle.
fn parse_public_links(ph_array: &[Value]) -> HashMap<String, String> {
let mut links = HashMap::new();
for ph in ph_array {
if let (Some(h), Some(ph_handle)) = (
ph.get("h").and_then(|v| v.as_str()),
ph.get("ph").and_then(|v| v.as_str()),
) {
links.insert(h.to_string(), ph_handle.to_string());
}
}
links
}
/// Parse a single node from JSON.
pub(crate) fn parse_node(&self, json: &Value) -> Option<Node> {
let handle = json.get("h")?.as_str()?.to_string();
let parent_handle = json
.get("p")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
let node_type_int = json.get("t")?.as_i64()?;
let node_type = NodeType::from_i64(node_type_int)?;
let size = json.get("s").and_then(|v| v.as_u64()).unwrap_or(0);
let timestamp = json.get("ts").and_then(|v| v.as_i64()).unwrap_or(0);
let file_attr = json
.get("fa")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
let (name, node_key) = match node_type {
NodeType::Root => ("Root".to_string(), Vec::new()),
NodeType::Inbox => ("Inbox".to_string(), Vec::new()),
NodeType::Trash => ("Trash".to_string(), Vec::new()),
_ => {
// Decrypt attributes and node key
let attrs_b64 = json.get("a")?.as_str()?;
let key_str = json.get("k")?.as_str()?;
let node_key = self.decrypt_node_key(key_str)?;
match self.decrypt_node_attrs(attrs_b64, &node_key) {
Some(name) => (name, node_key),
None => return None,
}
}
};
Some(Node {
name,
handle,
parent_handle,
node_type,
size,
timestamp,
key: node_key,
path: None,
link: None,
file_attr,
})
}
/// Decrypt a node key from the "k" field.
fn decrypt_node_key(&self, key_str: &str) -> Option<Vec<u8>> {
// Key format: "handle:encrypted_key" or "handle:encrypted_key/handle2:key2"
for part in key_str.split('/') {
if let Some((key_handle, encrypted_key)) = part.split_once(':') {
// Try master key first (if key_handle matches user_handle)
let decrypt_key = if key_handle == self.user_handle {
Some(self.master_key())
} else {
self.share_keys.get(key_handle).map(|k| k as &[u8; 16])
};
if let Some(key) = decrypt_key {
if let Ok(encrypted) = base64url_decode(encrypted_key) {
let decrypted = aes128_ecb_decrypt(&encrypted, key);
return Some(decrypted);
}
}
}
}
None
}
pub(crate) fn decrypt_node_attrs(&self, attrs_b64: &str, node_key: &[u8]) -> Option<String> {
let encrypted = base64url_decode(attrs_b64).ok()?;
// Use first 16 bytes of node key for attribute decryption
let aes_key: [u8; 16] = if node_key.len() >= 32 {
// File key: XOR first and second 16-byte halves
let mut key = [0u8; 16];
for i in 0..16 {
key[i] = node_key[i] ^ node_key[i + 16];
}
key
} else if node_key.len() >= 16 {
node_key[..16].try_into().ok()?
} else {
return None;
};
let decrypted = aes128_cbc_decrypt(&encrypted, &aes_key);
// Attributes are JSON prefixed with "MEGA"
let text = String::from_utf8_lossy(&decrypted);
if !text.starts_with("MEGA") {
return None;
}
// Parse JSON after "MEGA" prefix
let json_str = text.trim_start_matches("MEGA").trim_end_matches('\0');
let attrs: Value = serde_json::from_str(json_str).ok()?;
attrs
.get("n")
.and_then(|v| v.as_str())
.map(|s| s.to_string())
}
/// Build full paths for all nodes.
pub(crate) fn build_node_paths(nodes: &mut [Node]) {
// Create handle -> node index map
let handle_map: HashMap<&str, usize> = nodes
.iter()
.enumerate()
.map(|(i, n)| (n.handle.as_str(), i))
.collect();
// First, compute all paths
let paths: Vec<String> = (0..nodes.len())
.map(|i| Self::build_node_path(nodes, i, &handle_map, 0))
.collect();
// Then assign them
for (i, path) in paths.into_iter().enumerate() {
nodes[i].path = Some(path);
}
}
/// Recursively build a node's path.
fn build_node_path(
nodes: &[Node],
idx: usize,
handle_map: &HashMap<&str, usize>,
depth: usize,
) -> String {
// Prevent infinite loops
if depth > 100 {
return format!("/{}", nodes[idx].name);
}
let node = &nodes[idx];
// Root nodes have path "/"
if matches!(
node.node_type,
NodeType::Root | NodeType::Inbox | NodeType::Trash | NodeType::Network
) {
return format!("/{}", node.name);
}
// Find parent and prepend its path
if let Some(parent_handle) = &node.parent_handle {
if let Some(&parent_idx) = handle_map.get(parent_handle.as_str()) {
let parent_path = Self::build_node_path(nodes, parent_idx, handle_map, depth + 1);
return format!("{}/{}", parent_path.trim_end_matches('/'), node.name);
}
}
format!("/{}", node.name)
}
}