webtorrent-rs 1.0.2

Streaming torrent client for Rust - 1:1 port of webtorrent
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
use crate::error::{Result, WebTorrentError};
use std::collections::HashMap;
use std::net::SocketAddrV4;
use std::net::Ipv4Addr;
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{debug, error, info, warn};

/// Port mapping information
#[derive(Debug, Clone)]
struct PortMapping {
    public_port: u16,
    private_port: u16,
    protocol: String,
    description: String,
    mapping_type: MappingType,
}

#[derive(Debug, Clone)]
enum MappingType {
    Upnp,
    Pmp,
}

/// NAT traversal using UPnP and NAT-PMP
pub struct NatTraversal {
    upnp_enabled: bool,
    pmp_enabled: bool,
    destroyed: Arc<RwLock<bool>>,
    mappings: Arc<RwLock<HashMap<String, PortMapping>>>,
}

impl NatTraversal {
    /// Create a new NAT traversal instance
    pub async fn new(upnp_enabled: bool, pmp_enabled: bool) -> Result<Self> {
        Ok(Self {
            upnp_enabled,
            pmp_enabled,
            destroyed: Arc::new(RwLock::new(false)),
            mappings: Arc::new(RwLock::new(HashMap::new())),
        })
    }

    /// Map a port via UPnP or NAT-PMP
    pub async fn map_port(
        &self,
        public_port: u16,
        private_port: u16,
        protocol: &str,
        description: &str,
    ) -> Result<()> {
        if *self.destroyed.read().await {
            return Err(WebTorrentError::Nat("NAT traversal destroyed".to_string()));
        }

        // Check if already mapped
        let key = format!("{}:{}:{}", protocol, public_port, private_port);
        {
            let mappings = self.mappings.read().await;
            if mappings.contains_key(&key) {
                debug!("Port mapping already exists: {}", key);
                return Ok(());
            }
        }

        // Try UPnP first if enabled
        if self.upnp_enabled {
            match self.map_upnp(public_port, private_port, protocol, description).await {
                Ok(_) => {
                    let mapping = PortMapping {
                        public_port,
                        private_port,
                        protocol: protocol.to_string(),
                        description: description.to_string(),
                        mapping_type: MappingType::Upnp,
                    };
                    self.mappings.write().await.insert(key, mapping);
                    info!("UPnP port mapping successful: {} -> {} ({})", public_port, private_port, protocol);
                    return Ok(());
                }
                Err(e) => {
                    warn!("UPnP port mapping failed: {}", e);
                    // Fall through to try NAT-PMP if enabled
                }
            }
        }

        // Try NAT-PMP if enabled
        if self.pmp_enabled {
            match self.map_pmp(public_port, private_port, protocol, description).await {
                Ok(_) => {
                    let mapping = PortMapping {
                        public_port,
                        private_port,
                        protocol: protocol.to_string(),
                        description: description.to_string(),
                        mapping_type: MappingType::Pmp,
                    };
                    self.mappings.write().await.insert(key, mapping);
                    info!("NAT-PMP port mapping successful: {} -> {} ({})", public_port, private_port, protocol);
                    return Ok(());
                }
                Err(e) => {
                    error!("NAT-PMP port mapping failed: {}", e);
                    return Err(e);
                }
            }
        }

        Err(WebTorrentError::Nat("No NAT traversal method enabled or available".to_string()))
    }

    /// Unmap a port
    pub async fn unmap_port(
        &self,
        public_port: u16,
        private_port: u16,
        protocol: &str,
    ) -> Result<()> {
        if *self.destroyed.read().await {
            return Err(WebTorrentError::Nat("NAT traversal destroyed".to_string()));
        }

        let key = format!("{}:{}:{}", protocol, public_port, private_port);
        let mapping = {
            let mut mappings = self.mappings.write().await;
            mappings.remove(&key)
        };

        if let Some(mapping) = mapping {
            match mapping.mapping_type {
                MappingType::Upnp => {
                    if let Err(e) = self.unmap_upnp(public_port, private_port, &mapping.protocol).await {
                        warn!("UPnP port unmapping failed: {}", e);
                        // Continue anyway - mapping removed from tracking
                    } else {
                        debug!("UPnP port unmapping successful: {} -> {}", public_port, private_port);
                    }
                }
                MappingType::Pmp => {
                    if let Err(e) = self.unmap_pmp(public_port, private_port, &mapping.protocol).await {
                        warn!("NAT-PMP port unmapping failed: {}", e);
                        // Continue anyway - mapping removed from tracking
                    } else {
                        debug!("NAT-PMP port unmapping successful: {} -> {}", public_port, private_port);
                    }
                }
            }
        }

        Ok(())
    }

    async fn map_upnp(
        &self,
        public_port: u16,
        private_port: u16,
        protocol: &str,
        description: &str,
    ) -> Result<()> {
        use igd::*;

        // search_gateway is blocking, so run it in a blocking task
        let gateway = match tokio::task::spawn_blocking(|| {
            search_gateway(Default::default())
        }).await {
            Ok(Ok(gateway)) => gateway,
            Ok(Err(e)) => {
                return Err(WebTorrentError::Nat(format!("UPnP gateway search failed: {}", e)));
            }
            Err(e) => {
                return Err(WebTorrentError::Nat(format!("UPnP gateway search task failed: {}", e)));
            }
        };

        let protocol_enum = match protocol.to_lowercase().as_str() {
            "tcp" => PortMappingProtocol::TCP,
            "udp" => PortMappingProtocol::UDP,
            _ => {
                return Err(WebTorrentError::Nat(format!("Invalid protocol: {}", protocol)));
            }
        };

        // Map port with 3600 second (1 hour) lease time
        let lease_duration = 3600u32;
        let private_addr = SocketAddrV4::new(Ipv4Addr::LOCALHOST, private_port);
        let description = description.to_string();
        let result = tokio::task::spawn_blocking(move || {
            gateway.add_port(protocol_enum, public_port, private_addr, lease_duration, &description)
        }).await.map_err(|e| {
            WebTorrentError::Nat(format!("UPnP port mapping task failed: {}", e))
        })?;
        
        match result {
            Ok(_) => {
                debug!("UPnP port mapping successful: {} -> {} ({})", public_port, private_port, protocol);
                Ok(())
            }
            Err(e) => Err(WebTorrentError::Nat(format!("UPnP port mapping failed: {}", e))),
        }
    }

    async fn unmap_upnp(
        &self,
        public_port: u16,
        _private_port: u16,
        protocol: &str,
    ) -> Result<()> {
        use igd::*;

        let gateway = match tokio::task::spawn_blocking(|| {
            search_gateway(Default::default())
        }).await {
            Ok(Ok(gateway)) => gateway,
            Ok(Err(e)) => {
                return Err(WebTorrentError::Nat(format!("UPnP gateway search failed: {}", e)));
            }
            Err(e) => {
                return Err(WebTorrentError::Nat(format!("UPnP gateway search task failed: {}", e)));
            }
        };

        let protocol_enum = match protocol.to_lowercase().as_str() {
            "tcp" => PortMappingProtocol::TCP,
            "udp" => PortMappingProtocol::UDP,
            _ => {
                return Err(WebTorrentError::Nat(format!("Invalid protocol: {}", protocol)));
            }
        };

        let result = tokio::task::spawn_blocking(move || {
            gateway.remove_port(protocol_enum, public_port)
        }).await.map_err(|e| {
            WebTorrentError::Nat(format!("UPnP port unmapping task failed: {}", e))
        })?;

        match result {
            Ok(_) => {
                debug!("UPnP port unmapping successful: {} ({})", public_port, protocol);
                Ok(())
            }
            Err(e) => Err(WebTorrentError::Nat(format!("UPnP port unmapping failed: {}", e))),
        }
    }

    async fn map_pmp(
        &self,
        public_port: u16,
        private_port: u16,
        protocol: &str,
        _description: &str,
    ) -> Result<()> {
        // NAT-PMP is synchronous, so run it in a blocking task
        let public_port = public_port;
        let private_port = private_port;
        let protocol = protocol.to_string();
        
        tokio::task::spawn_blocking(move || {
            use natpmp::Natpmp;

            // Create NAT-PMP client
            let mut nat = match Natpmp::new() {
                Ok(nat) => nat,
                Err(e) => {
                    return Err(WebTorrentError::Nat(format!("NAT-PMP client creation failed: {}", e)));
                }
            };

            // Get public IP address first
            match nat.send_public_address_request() {
                Ok(_) => {}
                Err(e) => {
                    return Err(WebTorrentError::Nat(format!("NAT-PMP public address request failed: {}", e)));
                }
            }

            // Read public address response
            let _public_addr = match nat.read_response_or_retry() {
                Ok(resp) => {
                    debug!("NAT-PMP public address response: {:?}", resp);
                    resp
                }
                Err(e) => {
                    return Err(WebTorrentError::Nat(format!("NAT-PMP response read failed: {}", e)));
                }
            };

            // Determine protocol - natpmp::Protocol enum
            // Based on natpmp 0.3 crate documentation, Protocol has TCP and UDP variants
            let protocol_enum = match protocol.to_lowercase().as_str() {
                "tcp" => natpmp::Protocol::TCP,
                "udp" => natpmp::Protocol::UDP,
                _ => {
                    return Err(WebTorrentError::Nat(format!("Invalid protocol: {}", protocol)));
                }
            };

            // Request port mapping with 3600 second (1 hour) lease time
            let lease_duration = 3600u32;
            match nat.send_port_mapping_request(protocol_enum, private_port, public_port, lease_duration) {
                Ok(_) => {}
                Err(e) => {
                    return Err(WebTorrentError::Nat(format!("NAT-PMP port mapping request failed: {}", e)));
                }
            }

            // Read port mapping response
            let _port_mapping_response = match nat.read_response_or_retry() {
                Ok(resp) => {
                    debug!("NAT-PMP port mapping response: {:?}", resp);
                    resp
                }
                Err(e) => {
                    return Err(WebTorrentError::Nat(format!("NAT-PMP port mapping response read failed: {}", e)));
                }
            };

            debug!("NAT-PMP port mapping successful: {} -> {} ({})", 
                public_port, 
                private_port,
                protocol
            );
            Ok(())
        }).await.map_err(|e| {
            WebTorrentError::Nat(format!("NAT-PMP task failed: {}", e))
        })?
    }

    async fn unmap_pmp(
        &self,
        public_port: u16,
        private_port: u16,
        protocol: &str,
    ) -> Result<()> {
        let public_port = public_port;
        let private_port = private_port;
        let protocol = protocol.to_string();

        tokio::task::spawn_blocking(move || {
            use natpmp::Natpmp;

            let mut nat = match Natpmp::new() {
                Ok(nat) => nat,
                Err(e) => {
                    return Err(WebTorrentError::Nat(format!("NAT-PMP client creation failed: {}", e)));
                }
            };

            // Determine protocol - use natpmp::Protocol enum
            let protocol_enum = match protocol.to_lowercase().as_str() {
                "tcp" => natpmp::Protocol::TCP,
                "udp" => natpmp::Protocol::UDP,
                _ => {
                    return Err(WebTorrentError::Nat(format!("Invalid protocol: {}", protocol)));
                }
            };

            // Remove port mapping by setting lease time to 0
            match nat.send_port_mapping_request(protocol_enum, private_port, public_port, 0) {
                Ok(_) => {}
                Err(e) => {
                    return Err(WebTorrentError::Nat(format!("NAT-PMP port unmapping request failed: {}", e)));
                }
            }

            // Read response (may fail for unmapping, but that's okay)
            let _ = nat.read_response_or_retry();

            debug!("NAT-PMP port unmapping successful: {} ({})", public_port, protocol);
            Ok(())
        }).await.map_err(|e| {
            WebTorrentError::Nat(format!("NAT-PMP unmapping task failed: {}", e))
        })?
    }

    /// Get all active port mappings
    pub async fn get_mappings(&self) -> Vec<(u16, u16, String, String)> {
        let mappings = self.mappings.read().await;
        mappings.values()
            .map(|m| (m.public_port, m.private_port, m.protocol.clone(), m.description.clone()))
            .collect()
    }

    /// Check if a port is mapped
    pub async fn is_mapped(&self, public_port: u16, private_port: u16, protocol: &str) -> bool {
        let key = format!("{}:{}:{}", protocol, public_port, private_port);
        self.mappings.read().await.contains_key(&key)
    }

    /// Destroy the NAT traversal instance and unmap all ports
    pub async fn destroy(&self) -> Result<()> {
        if *self.destroyed.read().await {
            return Ok(());
        }

        *self.destroyed.write().await = true;

        // Unmap all ports
        let mappings = {
            let mut mappings = self.mappings.write().await;
            mappings.drain().collect::<Vec<_>>()
        };

        for (_, mapping) in mappings {
            let _ = match mapping.mapping_type {
                MappingType::Upnp => {
                    self.unmap_upnp(mapping.public_port, mapping.private_port, &mapping.protocol).await
                }
                MappingType::Pmp => {
                    self.unmap_pmp(mapping.public_port, mapping.private_port, &mapping.protocol).await
                }
            };
        }

        info!("NAT traversal destroyed and all ports unmapped");
        Ok(())
    }
}