trident-client 0.13.0-rc.1

Trident is Rust based fuzzing framework for Solana programs written in Anchor.
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
use crate::error::Error;
use axum::extract::Path;
use axum::extract::State;
use axum::http::header;
use axum::http::StatusCode;
use axum::response::Html;
use axum::response::Response;
use axum::routing::get;
use axum::Router;
use notify::Config;
use notify::RecommendedWatcher;
use notify::RecursiveMode;
use notify::Watcher;
use serde::Serialize;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::Mutex;
use std::time::SystemTime;
use tokio::fs;
use tower_http::cors::CorsLayer;
use tower_http::services::ServeDir;

#[derive(Debug, Clone, Serialize)]
pub struct FileInfo {
    pub name: String,
    pub path: String,
    pub modified: SystemTime,
    pub size: u64,
    pub file_type: FileType,
}

#[derive(Debug, Clone, Serialize)]
pub enum FileType {
    Dashboard,
    Log,
}

#[derive(Clone)]
struct AppState {
    files: Arc<Mutex<HashMap<String, FileInfo>>>,
    base_path: PathBuf,
}

impl AppState {
    fn new(base_path: PathBuf) -> Self {
        Self {
            files: Arc::new(Mutex::new(HashMap::new())),
            base_path,
        }
    }

    fn update_files(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let mut files = self.files.lock().unwrap();
        files.clear();

        if !self.base_path.exists() {
            return Ok(());
        }

        for entry in std::fs::read_dir(&self.base_path)? {
            let entry = entry?;
            let path = entry.path();

            if path.is_file() {
                if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
                    if let Some(file_type) = determine_file_type(&path) {
                        let metadata = entry.metadata()?;

                        let info = FileInfo {
                            name: name.to_string(),
                            path: path.to_string_lossy().to_string(),
                            modified: metadata.modified()?,
                            size: metadata.len(),
                            file_type,
                        };
                        files.insert(name.to_string(), info);
                    }
                }
            }
        }

        Ok(())
    }

    fn get_files(&self) -> Vec<FileInfo> {
        let files = self.files.lock().unwrap();
        let mut sorted: Vec<_> = files.values().cloned().collect();
        sorted.sort_by(|a, b| b.modified.cmp(&a.modified));
        sorted
    }
}

fn determine_file_type(path: &std::path::Path) -> Option<FileType> {
    if let Some(extension) = path.extension().and_then(|e| e.to_str()) {
        match extension.to_lowercase().as_str() {
            "html" | "htm" => Some(FileType::Dashboard),
            "log" => Some(FileType::Log),
            _ => None,
        }
    } else {
        None
    }
}

async fn file_list(State(state): State<AppState>) -> Result<Html<String>, StatusCode> {
    let _ = state.update_files();
    let files = state.get_files();

    let html = generate_file_list_html(files);
    Ok(Html(html))
}

async fn serve_file(
    State(state): State<AppState>,
    Path(filename): Path<String>,
) -> Result<Response, StatusCode> {
    let file_path = state.base_path.join(&filename);

    if !file_path.exists() || !file_path.is_file() {
        return Err(StatusCode::NOT_FOUND);
    }

    if let Some(file_type) = determine_file_type(&file_path) {
        match fs::read_to_string(&file_path).await {
            Ok(content) => {
                let (content_type, formatted_content) = match file_type {
                    FileType::Dashboard => ("text/html; charset=utf-8", content),
                    FileType::Log => (
                        "text/html; charset=utf-8",
                        format_log_as_html(&content, &filename),
                    ),
                };

                let response = Response::builder()
                    .header(header::CONTENT_TYPE, content_type)
                    .body(formatted_content.into())
                    .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
                Ok(response)
            }
            Err(_) => Err(StatusCode::INTERNAL_SERVER_ERROR),
        }
    } else {
        Err(StatusCode::FORBIDDEN)
    }
}

fn generate_file_list_html(files: Vec<FileInfo>) -> String {
    // Load the template
    let template = include_str!("server/dashboard_list_template.html");

    // Separate files by type
    let mut dashboards: Vec<&FileInfo> = files
        .iter()
        .filter(|f| matches!(f.file_type, FileType::Dashboard))
        .collect();
    let mut logs: Vec<&FileInfo> = files
        .iter()
        .filter(|f| matches!(f.file_type, FileType::Log))
        .collect();

    // Sort each type by modified time
    dashboards.sort_by(|a, b| b.modified.cmp(&a.modified));
    logs.sort_by(|a, b| b.modified.cmp(&a.modified));

    let sections = if files.is_empty() {
        r#"
        <div class="no-dashboards">
            <div class="no-dashboards-icon">📊</div>
            <h3>No Files Available</h3>
            <p>No fuzzing files found in the monitored directory.</p>
            <p style="font-size: 0.9em; margin-top: 8px;">Start fuzzing to see results here.</p>
        </div>
        "#
        .to_string()
    } else {
        let mut sections = String::new();

        // Dashboard section
        if !dashboards.is_empty() {
            sections.push_str(&format!(
                r#"
                <div class="section-header">
                    <h2>📊 Fuzzing Dashboards <span class="count">({} files)</span></h2>
                </div>
                <div class="dashboards-grid">
                "#,
                dashboards.len()
            ));

            for dashboard in dashboards {
                let time_str = format_time(dashboard.modified);
                sections.push_str(&format!(
                    r#"
                    <div class="file-item dashboard-type">
                        <div class="file-header">
                            <h3 class="file-title">📊 {}</h3>
                            <div class="file-meta">
                                <span class="file-time">{}</span>
                            </div>
                        </div>
                        <div class="file-actions">
                            <a href="/file/{}" class="btn btn-primary">
                                View Dashboard
                            </a>
                        </div>
                    </div>
                    "#,
                    dashboard.name, time_str, dashboard.name
                ));
            }

            sections.push_str("</div>");
        }

        // Debug logs section
        if !logs.is_empty() {
            sections.push_str(&format!(
                r#"
                <div class="section-header">
                    <h2>📋 Debug Logs <span class="count">({} files)</span></h2>
                </div>
                <div class="dashboards-grid">
                "#,
                logs.len()
            ));

            for log in logs {
                let time_str = format_time(log.modified);
                sections.push_str(&format!(
                    r#"
                    <div class="file-item log-type">
                        <div class="file-header">
                            <h3 class="file-title">📋 {}</h3>
                            <div class="file-meta">
                                <span class="file-time">{}</span>
                            </div>
                        </div>
                        <div class="file-actions">
                            <a href="/file/{}" class="btn btn-secondary">
                                View Log
                            </a>
                        </div>
                    </div>
                    "#,
                    log.name, time_str, log.name
                ));
            }

            sections.push_str("</div>");
        }

        sections
    };

    // Replace template variables
    template.replace("{{DASHBOARD_ITEMS}}", &sections)
}

fn format_time(time: SystemTime) -> String {
    use std::time::UNIX_EPOCH;

    match time.duration_since(UNIX_EPOCH) {
        Ok(duration) => {
            let secs = duration.as_secs();
            let now_secs = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs();

            let diff = now_secs.saturating_sub(secs);

            if diff < 60 {
                "Just now".to_string()
            } else if diff < 3600 {
                format!("{} min ago", diff / 60)
            } else if diff < 86400 {
                format!("{} hr ago", diff / 3600)
            } else {
                format!("{} days ago", diff / 86400)
            }
        }
        Err(_) => "Unknown".to_string(),
    }
}

pub struct DashboardServer {
    directory: PathBuf,
    host: String,
    port: u16,
}

impl DashboardServer {
    pub fn new(directory: impl Into<PathBuf>, host: String, port: u16) -> Self {
        Self {
            directory: directory.into(),
            host,
            port,
        }
    }

    pub async fn start(&self) -> Result<(), Error> {
        let base_path = self.directory.clone();

        // Create directory if it doesn't exist
        if !base_path.exists() {
            tokio::fs::create_dir_all(&base_path).await?;
        }

        println!("🚀 Starting Trident Dashboard Server");
        println!("📁 Serving dashboards from: {}", base_path.display());
        println!("🌐 Server running at: http://{}:{}", self.host, self.port);
        println!("📊 Dashboard list: http://{}:{}/", self.host, self.port);
        println!("🔄 Web page auto-refreshes every 3 seconds");
        println!();

        let state = AppState::new(base_path.clone());

        // Initial scan for files
        let _ = state.update_files();

        // Set up file watcher for real-time updates
        let watch_state = state.clone();
        let watch_path = base_path.clone();
        tokio::spawn(async move {
            let (tx, mut rx) = tokio::sync::mpsc::channel(100);

            let mut watcher = RecommendedWatcher::new(
                move |res| {
                    let _ = tx.blocking_send(res);
                },
                Config::default(),
            )
            .unwrap();

            let _ = watcher.watch(&watch_path, RecursiveMode::NonRecursive);

            while let Some(_event) = rx.recv().await {
                let _ = watch_state.update_files();
            }
        });

        // Build the router
        let app = Router::new()
            .route("/", get(file_list))
            .route("/file/:filename", get(serve_file))
            .route("/dashboard/:filename", get(serve_file)) // Backward compatibility
            .nest_service("/static", ServeDir::new(&base_path))
            .layer(CorsLayer::permissive())
            .with_state(state);

        // Start the server
        let addr = format!("{}:{}", self.host, self.port);
        let listener = tokio::net::TcpListener::bind(&addr).await?;

        println!("✅ Server started successfully!");
        println!("Press Ctrl+C to stop the server");
        println!();

        axum::serve(listener, app).await?;

        Ok(())
    }
}

fn format_log_as_html(content: &str, filename: &str) -> String {
    let highlighted_content = content
        .lines()
        .map(|line| {
            let escaped = html_escape(line);
            if line.contains("ERROR") {
                format!("<div class=\"log-error\">{}</div>", escaped)
            } else if line.contains("DEBUG") {
                format!("<div class=\"log-debug\">{}</div>", escaped)
            } else if line.contains("Program") && line.contains("invoke") {
                format!("<div class=\"log-invoke\">{}</div>", escaped)
            } else if line.contains("Program") && line.contains("success") {
                format!("<div class=\"log-success\">{}</div>", escaped)
            } else if line.contains("Program") && line.contains("failed") {
                format!("<div class=\"log-failed\">{}</div>", escaped)
            } else if line.contains("PANICKED") {
                format!("<div class=\"log-panic\">{}</div>", escaped)
            } else {
                format!("<div class=\"log-line\">{}</div>", escaped)
            }
        })
        .collect::<Vec<_>>()
        .join("");

    format!(
        r#"<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{} - Trident Debug Log</title>
    <style>
        body {{
            font-family: 'Courier New', monospace;
            margin: 0;
            padding: 20px;
            background: #0f172a;
            color: #e2e8f0;
            font-size: 14px;
            line-height: 1.4;
        }}
        .header {{
            background: #1e293b;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 20px;
            border-left: 4px solid #3b82f6;
        }}
        .content {{
            background: #1e293b;
            padding: 20px;
            border-radius: 8px;
            max-height: 80vh;
            overflow-y: auto;
            border: 1px solid #334155;
        }}
        .log-line {{ color: #cbd5e1; }}
        .log-error {{ color: #f87171; font-weight: bold; }}
        .log-debug {{ color: #94a3b8; }}
        .log-invoke {{ color: #22c55e; }}
        .log-success {{ color: #22c55e; font-weight: bold; }}
        .log-failed {{ color: #f87171; font-weight: bold; }}
        .log-panic {{ color: #fbbf24; font-weight: bold; background: rgba(251, 191, 36, 0.1); padding: 2px 4px; border-radius: 4px; }}
        .nav {{ margin-bottom: 20px; }}
        .nav a {{ color: #38bdf8; text-decoration: none; }}
        .nav a:hover {{ text-decoration: underline; }}
    </style>
</head>
<body>
    <div class="nav">
        <a href="/">← Back to File List</a>
    </div>
    <div class="header">
        <h1>📋 Debug Log: {}</h1>
        <p>Trident SVM execution log with syntax highlighting</p>
    </div>
    <div class="content">
        {}
    </div>
</body>
</html>"#,
        filename, filename, highlighted_content
    )
}

fn html_escape(text: &str) -> String {
    text.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
        .replace('\'', "&#x27;")
}