hypertor 0.2.2

Tor HTTP client and onion service library with Python bindings
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//! Distributed tracing support.
//!
//! Provides request tracing with spans for observability and debugging.
//! Compatible with OpenTelemetry concepts.

use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant, SystemTime};

use parking_lot::RwLock;

/// Unique trace identifier.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TraceId(u64);

impl TraceId {
    /// Generate a new trace ID.
    pub fn generate() -> Self {
        static COUNTER: AtomicU64 = AtomicU64::new(1);
        let id = COUNTER.fetch_add(1, Ordering::Relaxed);
        // Mix in timestamp for better distribution
        let now = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .map(|d| d.as_nanos() as u64)
            .unwrap_or(0);
        Self(id ^ (now & 0xFFFF_FFFF_0000_0000))
    }

    /// Create from a raw value.
    pub fn from_raw(value: u64) -> Self {
        Self(value)
    }

    /// Get the raw value.
    pub fn as_raw(&self) -> u64 {
        self.0
    }

    /// Convert to hex string for propagation.
    pub fn to_hex(&self) -> String {
        format!("{:016x}", self.0)
    }

    /// Parse from hex string.
    pub fn from_hex(s: &str) -> Option<Self> {
        u64::from_str_radix(s, 16).ok().map(Self)
    }
}

impl std::fmt::Display for TraceId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:016x}", self.0)
    }
}

/// Unique span identifier.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SpanId(u64);

impl SpanId {
    /// Generate a new span ID.
    pub fn generate() -> Self {
        static COUNTER: AtomicU64 = AtomicU64::new(1);
        Self(COUNTER.fetch_add(1, Ordering::Relaxed))
    }

    /// Create from a raw value.
    pub fn from_raw(value: u64) -> Self {
        Self(value)
    }

    /// Get the raw value.
    pub fn as_raw(&self) -> u64 {
        self.0
    }

    /// Convert to hex string.
    pub fn to_hex(&self) -> String {
        format!("{:016x}", self.0)
    }
}

impl std::fmt::Display for SpanId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:016x}", self.0)
    }
}

/// Span status.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SpanStatus {
    /// Span completed successfully
    Ok,
    /// Span completed with an error
    Error,
    /// Span is still in progress
    InProgress,
    /// Span was cancelled
    Cancelled,
}

/// Span kind (type of operation).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SpanKind {
    /// Internal operation
    #[default]
    Internal,
    /// Outgoing request (client)
    Client,
    /// Incoming request (server)
    Server,
    /// Async producer
    Producer,
    /// Async consumer
    Consumer,
}

/// A span representing a unit of work.
#[derive(Debug)]
pub struct Span {
    /// Trace this span belongs to
    pub trace_id: TraceId,
    /// Unique span identifier
    pub span_id: SpanId,
    /// Parent span (if any)
    pub parent_id: Option<SpanId>,
    /// Operation name
    pub name: String,
    /// Span kind
    pub kind: SpanKind,
    /// Start time
    pub start_time: Instant,
    /// End time (if finished)
    pub end_time: Option<Instant>,
    /// Status
    pub status: SpanStatus,
    /// Attributes/tags
    pub attributes: HashMap<String, AttributeValue>,
    /// Events/logs
    pub events: Vec<SpanEvent>,
}

impl Span {
    /// Create a new span.
    pub fn new(trace_id: TraceId, name: impl Into<String>) -> Self {
        Self {
            trace_id,
            span_id: SpanId::generate(),
            parent_id: None,
            name: name.into(),
            kind: SpanKind::default(),
            start_time: Instant::now(),
            end_time: None,
            status: SpanStatus::InProgress,
            attributes: HashMap::new(),
            events: Vec::new(),
        }
    }

    /// Create a child span.
    pub fn child(&self, name: impl Into<String>) -> Self {
        Self {
            trace_id: self.trace_id,
            span_id: SpanId::generate(),
            parent_id: Some(self.span_id),
            name: name.into(),
            kind: SpanKind::default(),
            start_time: Instant::now(),
            end_time: None,
            status: SpanStatus::InProgress,
            attributes: HashMap::new(),
            events: Vec::new(),
        }
    }

    /// Set the span kind.
    #[must_use]
    pub fn with_kind(mut self, kind: SpanKind) -> Self {
        self.kind = kind;
        self
    }

    /// Set an attribute.
    pub fn set_attribute(&mut self, key: impl Into<String>, value: impl Into<AttributeValue>) {
        self.attributes.insert(key.into(), value.into());
    }

    /// Add an event.
    pub fn add_event(&mut self, name: impl Into<String>) {
        self.events.push(SpanEvent {
            name: name.into(),
            timestamp: Instant::now(),
            attributes: HashMap::new(),
        });
    }

    /// Add an event with attributes.
    pub fn add_event_with_attrs(
        &mut self,
        name: impl Into<String>,
        attrs: HashMap<String, AttributeValue>,
    ) {
        self.events.push(SpanEvent {
            name: name.into(),
            timestamp: Instant::now(),
            attributes: attrs,
        });
    }

    /// Record an error.
    pub fn record_error(&mut self, error: &str) {
        self.status = SpanStatus::Error;
        self.set_attribute("error", true);
        self.set_attribute("error.message", error.to_string());
        self.add_event("exception");
    }

    /// Mark span as successful.
    pub fn set_ok(&mut self) {
        self.status = SpanStatus::Ok;
    }

    /// End the span.
    pub fn end(&mut self) {
        self.end_time = Some(Instant::now());
        if self.status == SpanStatus::InProgress {
            self.status = SpanStatus::Ok;
        }
    }

    /// Get span duration.
    pub fn duration(&self) -> Duration {
        match self.end_time {
            Some(end) => end.duration_since(self.start_time),
            None => self.start_time.elapsed(),
        }
    }

    /// Check if span is still active.
    pub fn is_active(&self) -> bool {
        self.end_time.is_none()
    }
}

/// Attribute value types.
#[derive(Debug, Clone, PartialEq)]
pub enum AttributeValue {
    /// String value
    String(String),
    /// Integer value
    Int(i64),
    /// Float value
    Float(f64),
    /// Boolean value
    Bool(bool),
    /// String array
    StringArray(Vec<String>),
}

impl From<&str> for AttributeValue {
    fn from(s: &str) -> Self {
        Self::String(s.to_string())
    }
}

impl From<String> for AttributeValue {
    fn from(s: String) -> Self {
        Self::String(s)
    }
}

impl From<i64> for AttributeValue {
    fn from(i: i64) -> Self {
        Self::Int(i)
    }
}

impl From<i32> for AttributeValue {
    fn from(i: i32) -> Self {
        Self::Int(i64::from(i))
    }
}

impl From<f64> for AttributeValue {
    fn from(f: f64) -> Self {
        Self::Float(f)
    }
}

impl From<bool> for AttributeValue {
    fn from(b: bool) -> Self {
        Self::Bool(b)
    }
}

/// Event within a span.
#[derive(Debug, Clone)]
pub struct SpanEvent {
    /// Event name
    pub name: String,
    /// When the event occurred
    pub timestamp: Instant,
    /// Event attributes
    pub attributes: HashMap<String, AttributeValue>,
}

/// Trace context for propagation.
#[derive(Debug, Clone)]
pub struct TraceContext {
    /// Trace identifier
    pub trace_id: TraceId,
    /// Current span identifier
    pub span_id: SpanId,
    /// Trace flags (sampling, etc.)
    pub flags: u8,
}

impl TraceContext {
    /// Create a new trace context.
    pub fn new(trace_id: TraceId, span_id: SpanId) -> Self {
        Self {
            trace_id,
            span_id,
            flags: 0x01, // Sampled by default
        }
    }

    /// Check if the trace is sampled.
    pub fn is_sampled(&self) -> bool {
        (self.flags & 0x01) != 0
    }

    /// Format for W3C traceparent header.
    pub fn to_traceparent(&self) -> String {
        format!(
            "00-{:032x}-{:016x}-{:02x}",
            self.trace_id.0 as u128, self.span_id.0, self.flags
        )
    }

    /// Parse from W3C traceparent header.
    pub fn from_traceparent(header: &str) -> Option<Self> {
        let parts: Vec<&str> = header.split('-').collect();
        if parts.len() != 4 || parts[0] != "00" {
            return None;
        }

        let trace_id = u128::from_str_radix(parts[1], 16).ok()? as u64;
        let span_id = u64::from_str_radix(parts[2], 16).ok()?;
        let flags = u8::from_str_radix(parts[3], 16).ok()?;

        Some(Self {
            trace_id: TraceId(trace_id),
            span_id: SpanId(span_id),
            flags,
        })
    }
}

/// Request tracer for collecting spans.
#[derive(Debug, Default)]
pub struct Tracer {
    spans: Arc<RwLock<Vec<Span>>>,
    active_traces: Arc<RwLock<HashMap<TraceId, Vec<SpanId>>>>,
}

impl Tracer {
    /// Create a new tracer.
    pub fn new() -> Self {
        Self::default()
    }

    /// Start a new trace.
    pub fn start_trace(&self, name: impl Into<String>) -> Span {
        let trace_id = TraceId::generate();
        let span = Span::new(trace_id, name).with_kind(SpanKind::Client);

        let mut active = self.active_traces.write();
        active.entry(trace_id).or_default().push(span.span_id);

        span
    }

    /// Start a span with an existing trace context.
    pub fn start_span(&self, ctx: &TraceContext, name: impl Into<String>) -> Span {
        let mut span = Span::new(ctx.trace_id, name);
        span.parent_id = Some(ctx.span_id);

        let mut active = self.active_traces.write();
        active.entry(ctx.trace_id).or_default().push(span.span_id);

        span
    }

    /// End and record a span.
    pub fn end_span(&self, mut span: Span) {
        span.end();

        // Remove from active
        let mut active = self.active_traces.write();
        if let Some(spans) = active.get_mut(&span.trace_id) {
            spans.retain(|&id| id != span.span_id);
            if spans.is_empty() {
                active.remove(&span.trace_id);
            }
        }

        // Store completed span
        self.spans.write().push(span);
    }

    /// Get all completed spans.
    pub fn get_spans(&self) -> Vec<Span> {
        // Can't clone Span easily, so return count for now
        let spans = self.spans.read();
        spans
            .iter()
            .map(|s| Span {
                trace_id: s.trace_id,
                span_id: s.span_id,
                parent_id: s.parent_id,
                name: s.name.clone(),
                kind: s.kind,
                start_time: s.start_time,
                end_time: s.end_time,
                status: s.status,
                attributes: s.attributes.clone(),
                events: s.events.clone(),
            })
            .collect()
    }

    /// Get span count.
    pub fn span_count(&self) -> usize {
        self.spans.read().len()
    }

    /// Clear all spans.
    pub fn clear(&self) {
        self.spans.write().clear();
    }

    /// Get active trace count.
    pub fn active_trace_count(&self) -> usize {
        self.active_traces.read().len()
    }
}

/// HTTP-specific span attributes.
pub mod http {
    use super::*;

    /// Set standard HTTP request attributes.
    pub fn set_request_attrs(span: &mut Span, method: &str, url: &str, host: &str) {
        span.set_attribute("http.method", method.to_string());
        span.set_attribute("http.url", url.to_string());
        span.set_attribute("http.host", host.to_string());
        span.set_attribute(
            "http.scheme",
            if url.starts_with("https") {
                "https"
            } else {
                "http"
            },
        );
    }

    /// Set standard HTTP response attributes.
    pub fn set_response_attrs(span: &mut Span, status_code: u16, content_length: Option<u64>) {
        span.set_attribute("http.status_code", i64::from(status_code));
        if let Some(len) = content_length {
            span.set_attribute("http.response_content_length", len as i64);
        }
        if status_code >= 400 {
            span.status = SpanStatus::Error;
        }
    }

    /// Set Tor-specific attributes.
    pub fn set_tor_attrs(span: &mut Span, circuit_id: Option<&str>, is_onion: bool) {
        span.set_attribute("tor.is_onion", is_onion);
        if let Some(id) = circuit_id {
            span.set_attribute("tor.circuit_id", id.to_string());
        }
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)]
    use super::*;

    #[test]
    fn test_trace_id() {
        let id1 = TraceId::generate();
        let id2 = TraceId::generate();
        assert_ne!(id1, id2);

        let hex = id1.to_hex();
        let parsed = TraceId::from_hex(&hex).unwrap();
        assert_eq!(id1, parsed);
    }

    #[test]
    fn test_span() {
        let trace_id = TraceId::generate();
        let mut span = Span::new(trace_id, "test_operation");
        span.set_attribute("key", "value");
        span.add_event("started");

        assert!(span.is_active());
        span.end();
        assert!(!span.is_active());
        assert_eq!(span.status, SpanStatus::Ok);
    }

    #[test]
    fn test_child_span() {
        let trace_id = TraceId::generate();
        let parent = Span::new(trace_id, "parent");
        let child = parent.child("child");

        assert_eq!(child.trace_id, parent.trace_id);
        assert_eq!(child.parent_id, Some(parent.span_id));
    }

    #[test]
    fn test_trace_context() {
        let ctx = TraceContext::new(TraceId::generate(), SpanId::generate());
        let header = ctx.to_traceparent();
        let parsed = TraceContext::from_traceparent(&header).unwrap();

        assert_eq!(ctx.trace_id, parsed.trace_id);
        assert_eq!(ctx.span_id, parsed.span_id);
    }

    #[test]
    fn test_tracer() {
        let tracer = Tracer::new();

        let span = tracer.start_trace("request");
        assert_eq!(tracer.active_trace_count(), 1);

        tracer.end_span(span);
        assert_eq!(tracer.active_trace_count(), 0);
        assert_eq!(tracer.span_count(), 1);
    }
}