fix: Resolve 44 compilation errors in ghost-core

This commit is contained in:
pandaadir05
2025-11-17 22:26:53 +02:00
parent b1f098571d
commit 34007d11c1
16 changed files with 566 additions and 310 deletions

View File

@@ -30,7 +30,7 @@ mod colors {
use colors::*;
pub fn draw<B: Backend>(f: &mut Frame<B>, app: &App) {
pub fn draw<B: Backend>(f: &mut Frame, app: &App) {
let size = f.size();
// Create main layout
@@ -59,7 +59,7 @@ pub fn draw<B: Backend>(f: &mut Frame<B>, app: &App) {
draw_footer(f, chunks[2], app);
}
fn draw_header<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
fn draw_header<B: Backend>(f: &mut Frame, area: Rect, app: &App) {
let titles = app.get_tab_titles();
let tabs = Tabs::new(titles)
.block(
@@ -81,7 +81,7 @@ fn draw_header<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
f.render_widget(tabs, area);
}
fn draw_footer<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
fn draw_footer<B: Backend>(f: &mut Frame, area: Rect, app: &App) {
let help_text = match app.current_tab {
TabIndex::Overview => "Up/Down: Navigate | Tab: Switch tabs | R: Refresh | C: Clear | Q: Quit",
TabIndex::Processes => "Up/Down: Select process | Enter: View details | Tab: Switch tabs | Q: Quit",
@@ -102,7 +102,7 @@ fn draw_footer<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
f.render_widget(footer, area);
}
fn draw_overview<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
fn draw_overview<B: Backend>(f: &mut Frame, area: Rect, app: &App) {
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
@@ -122,7 +122,7 @@ fn draw_overview<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
draw_recent_detections(f, chunks[2], app);
}
fn draw_stats_panel<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
fn draw_stats_panel<B: Backend>(f: &mut Frame, area: Rect, app: &App) {
let stats_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([
@@ -194,7 +194,7 @@ fn draw_stats_panel<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
f.render_widget(perf_gauge, stats_chunks[3]);
}
fn draw_threat_gauge<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
fn draw_threat_gauge<B: Backend>(f: &mut Frame, area: Rect, app: &App) {
let threat_level = if app.stats.malicious_processes > 0 {
100
} else if app.stats.suspicious_processes > 0 {
@@ -229,7 +229,7 @@ fn draw_threat_gauge<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
f.render_widget(threat_gauge, area);
}
fn draw_recent_detections<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
fn draw_recent_detections<B: Backend>(f: &mut Frame, area: Rect, app: &App) {
let items: Vec<ListItem> = app
.detections
.iter()
@@ -267,7 +267,7 @@ fn draw_recent_detections<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
f.render_widget(list, area);
}
fn draw_processes<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
fn draw_processes<B: Backend>(f: &mut Frame, area: Rect, app: &App) {
let chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(70), Constraint::Percentage(30)])
@@ -323,7 +323,7 @@ fn draw_processes<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
draw_process_details(f, chunks[1], app);
}
fn draw_process_details<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
fn draw_process_details<B: Backend>(f: &mut Frame, area: Rect, app: &App) {
let details = if let Some(ref process) = app.selected_process {
format!(
"PID: {}\nPPID: {}\nName: {}\nPath: {}\nThreads: {}",
@@ -350,7 +350,7 @@ fn draw_process_details<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
f.render_widget(paragraph, area);
}
fn draw_detections<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
fn draw_detections<B: Backend>(f: &mut Frame, area: Rect, app: &App) {
let items: Vec<ListItem> = app
.detections
.iter()
@@ -403,7 +403,7 @@ fn draw_detections<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
f.render_stateful_widget(list, area, &mut state);
}
fn draw_memory<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
fn draw_memory<B: Backend>(f: &mut Frame, area: Rect, app: &App) {
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Length(8), Constraint::Min(0)])
@@ -442,7 +442,7 @@ fn draw_memory<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
f.render_widget(memory_info, chunks[1]);
}
fn draw_logs<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
fn draw_logs<B: Backend>(f: &mut Frame, area: Rect, app: &App) {
let items: Vec<ListItem> = app
.logs
.iter()

463
ghost-tui/src/ui.rs.bak Normal file
View File

@@ -0,0 +1,463 @@
//! Terminal User Interface rendering for Ghost detection system.
//!
//! This module provides all the drawing functions for the TUI components,
//! including the main dashboard, process list, detection history, and system logs.
use crate::app::{App, TabIndex};
use ghost_core::ThreatLevel;
use ratatui::{
backend::Backend,
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Color, Modifier, Style},
text::{Line, Span, Text},
widgets::{Block, Borders, Cell, Gauge, List, ListItem, Paragraph, Row, Table, Tabs, Wrap},
Frame,
};
/// Color scheme for consistent theming across the application.
mod colors {
use ratatui::style::Color;
pub const PRIMARY: Color = Color::Cyan;
pub const SECONDARY: Color = Color::Magenta;
pub const SUCCESS: Color = Color::Green;
pub const WARNING: Color = Color::Yellow;
pub const DANGER: Color = Color::Red;
pub const BACKGROUND: Color = Color::Black;
pub const TEXT: Color = Color::White;
pub const MUTED: Color = Color::Gray;
}
use colors::*;
pub fn draw<B: Backend>(f: &mut Frame<B>, app: &App) {
let size = f.size();
// Create main layout
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(3), // Header
Constraint::Min(0), // Content
Constraint::Length(3), // Footer
])
.split(size);
// Draw header
draw_header(f, chunks[0], app);
// Draw main content based on selected tab
match app.current_tab {
TabIndex::Overview => draw_overview(f, chunks[1], app),
TabIndex::Processes => draw_processes(f, chunks[1], app),
TabIndex::Detections => draw_detections(f, chunks[1], app),
TabIndex::Memory => draw_memory(f, chunks[1], app),
TabIndex::Logs => draw_logs(f, chunks[1], app),
}
// Draw footer
draw_footer(f, chunks[2], app);
}
fn draw_header<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
let titles = app.get_tab_titles();
let tabs = Tabs::new(titles)
.block(
Block::default()
.borders(Borders::ALL)
.title("Ghost - Process Injection Detection")
.title_style(Style::default().fg(PRIMARY).add_modifier(Modifier::BOLD))
.border_style(Style::default().fg(PRIMARY)),
)
.select(app.current_tab as usize)
.style(Style::default().fg(TEXT))
.highlight_style(
Style::default()
.fg(BACKGROUND)
.bg(PRIMARY)
.add_modifier(Modifier::BOLD),
);
f.render_widget(tabs, area);
}
fn draw_footer<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
let help_text = match app.current_tab {
TabIndex::Overview => "Up/Down: Navigate | Tab: Switch tabs | R: Refresh | C: Clear | Q: Quit",
TabIndex::Processes => "Up/Down: Select process | Enter: View details | Tab: Switch tabs | Q: Quit",
TabIndex::Detections => "Up/Down: Navigate detections | C: Clear history | Tab: Switch tabs | Q: Quit",
TabIndex::Memory => "Up/Down: Navigate | Tab: Switch tabs | R: Refresh | Q: Quit",
TabIndex::Logs => "Up/Down: Navigate logs | C: Clear logs | Tab: Switch tabs | Q: Quit",
};
let footer = Paragraph::new(help_text)
.block(
Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(SECONDARY)),
)
.style(Style::default().fg(TEXT))
.alignment(Alignment::Center);
f.render_widget(footer, area);
}
fn draw_overview<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(8), // Stats
Constraint::Length(8), // Threat level gauge
Constraint::Min(0), // Recent detections
])
.split(area);
// Statistics panel
draw_stats_panel(f, chunks[0], app);
// Threat level gauge
draw_threat_gauge(f, chunks[1], app);
// Recent detections
draw_recent_detections(f, chunks[2], app);
}
fn draw_stats_panel<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
let stats_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Percentage(25),
Constraint::Percentage(25),
Constraint::Percentage(25),
Constraint::Percentage(25),
])
.split(area);
let total_processes = Gauge::default()
.block(
Block::default()
.borders(Borders::ALL)
.title("Total Processes")
.border_style(Style::default().fg(PRIMARY)),
)
.gauge_style(Style::default().fg(PRIMARY))
.percent(std::cmp::min(app.stats.total_processes.saturating_mul(100) / 500, 100) as u16)
.label(format!("{}", app.stats.total_processes));
f.render_widget(total_processes, stats_chunks[0]);
let suspicious_gauge = Gauge::default()
.block(
Block::default()
.borders(Borders::ALL)
.title("Suspicious")
.border_style(Style::default().fg(WARNING)),
)
.gauge_style(Style::default().fg(WARNING))
.percent(if app.stats.total_processes > 0 {
(app.stats.suspicious_processes.saturating_mul(100) / app.stats.total_processes) as u16
} else {
0
})
.label(format!("{}", app.stats.suspicious_processes));
f.render_widget(suspicious_gauge, stats_chunks[1]);
let malicious_gauge = Gauge::default()
.block(
Block::default()
.borders(Borders::ALL)
.title("Malicious")
.border_style(Style::default().fg(DANGER)),
)
.gauge_style(Style::default().fg(DANGER))
.percent(if app.stats.total_processes > 0 {
(app.stats.malicious_processes.saturating_mul(100) / app.stats.total_processes) as u16
} else {
0
})
.label(format!("{}", app.stats.malicious_processes));
f.render_widget(malicious_gauge, stats_chunks[2]);
let perf_gauge = Gauge::default()
.block(
Block::default()
.borders(Borders::ALL)
.title("Scan Time (ms)")
.border_style(Style::default().fg(SUCCESS)),
)
.gauge_style(Style::default().fg(SUCCESS))
.percent(std::cmp::min(app.stats.scan_time_ms as u16 / 10, 100))
.label(format!("{}ms", app.stats.scan_time_ms));
f.render_widget(perf_gauge, stats_chunks[3]);
}
fn draw_threat_gauge<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
let threat_level = if app.stats.malicious_processes > 0 {
100
} else if app.stats.suspicious_processes > 0 {
60
} else {
20
};
let color = if threat_level > 80 {
DANGER
} else if threat_level > 40 {
WARNING
} else {
SUCCESS
};
let threat_gauge = Gauge::default()
.block(
Block::default()
.borders(Borders::ALL)
.title("System Threat Level")
.title_style(Style::default().fg(color).add_modifier(Modifier::BOLD))
.border_style(Style::default().fg(color)),
)
.gauge_style(Style::default().fg(color))
.percent(threat_level)
.label(format!(
"{}% - {} Detection(s)",
threat_level, app.stats.total_detections
));
f.render_widget(threat_gauge, area);
}
fn draw_recent_detections<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
let items: Vec<ListItem> = app
.detections
.iter()
.take(10)
.map(|detection| {
let (level_marker, style) = match detection.threat_level {
ThreatLevel::Malicious => ("[!]", Style::default().fg(DANGER)),
ThreatLevel::Suspicious => ("[?]", Style::default().fg(WARNING)),
ThreatLevel::Clean => ("[+]", Style::default().fg(SUCCESS)),
};
let time = detection.timestamp.format("%H:%M:%S");
let content = format!(
"{} [{}] {} (PID: {}) - {:.1}%",
level_marker,
time,
detection.process.name,
detection.process.pid,
detection.confidence * 100.0
);
ListItem::new(content).style(style)
})
.collect();
let list = List::new(items)
.block(
Block::default()
.borders(Borders::ALL)
.title("Recent Detections")
.border_style(Style::default().fg(SECONDARY)),
)
.style(Style::default().fg(TEXT));
f.render_widget(list, area);
}
fn draw_processes<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
let chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(70), Constraint::Percentage(30)])
.split(area);
// Process table
let header_cells = ["PID", "PPID", "Name", "Threads", "Status"]
.iter()
.map(|h| Cell::from(*h).style(Style::default().fg(PRIMARY).add_modifier(Modifier::BOLD)));
let header = Row::new(header_cells).height(1).bottom_margin(1);
let rows: Vec<Row> = app.processes.iter().map(|proc| {
let status = match app.detections.iter().find(|d| d.process.pid == proc.pid) {
Some(detection) => match detection.threat_level {
ThreatLevel::Malicious => "MALICIOUS",
ThreatLevel::Suspicious => "SUSPICIOUS",
ThreatLevel::Clean => "CLEAN",
},
None => "CLEAN",
};
Row::new(vec![
Cell::from(proc.pid.to_string()),
Cell::from(proc.ppid.to_string()),
Cell::from(proc.name.as_str()),
Cell::from(proc.thread_count.to_string()),
Cell::from(status),
])
}).collect();
let table = Table::new(rows)
.header(header)
.block(
Block::default()
.borders(Borders::ALL)
.title("System Processes")
.border_style(Style::default().fg(PRIMARY)),
)
.highlight_style(Style::default().bg(PRIMARY).fg(BACKGROUND))
.widths(&[
Constraint::Length(8),
Constraint::Length(8),
Constraint::Min(20),
Constraint::Length(8),
Constraint::Length(15),
]);
let mut state = app.processes_state.clone();
f.render_stateful_widget(table, chunks[0], &mut state);
// Process details panel
draw_process_details(f, chunks[1], app);
}
fn draw_process_details<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
let details = if let Some(ref process) = app.selected_process {
format!(
"PID: {}\nPPID: {}\nName: {}\nPath: {}\nThreads: {}",
process.pid,
process.ppid,
process.name,
process.path.as_deref().unwrap_or("Unknown"),
process.thread_count
)
} else {
"Select a process to view details".to_string()
};
let paragraph = Paragraph::new(details)
.block(
Block::default()
.borders(Borders::ALL)
.title("Process Details")
.border_style(Style::default().fg(SECONDARY)),
)
.style(Style::default().fg(TEXT))
.wrap(Wrap { trim: true });
f.render_widget(paragraph, area);
}
fn draw_detections<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
let items: Vec<ListItem> = app
.detections
.iter()
.map(|detection| {
let level_style = match detection.threat_level {
ThreatLevel::Malicious => Style::default().fg(DANGER),
ThreatLevel::Suspicious => Style::default().fg(WARNING),
ThreatLevel::Clean => Style::default().fg(SUCCESS),
};
let content = vec![
Line::from(vec![
Span::styled(
format!("[{}] ", detection.timestamp.format("%Y-%m-%d %H:%M:%S")),
Style::default().fg(MUTED),
),
Span::styled(
format!("{:?}", detection.threat_level),
level_style.add_modifier(Modifier::BOLD),
),
]),
Line::from(format!(
"Process: {} (PID: {})",
detection.process.name, detection.process.pid
)),
Line::from(format!("Confidence: {:.1}%", detection.confidence * 100.0)),
Line::from("Indicators:"),
];
let mut all_lines = content;
for indicator in &detection.indicators {
all_lines.push(Line::from(format!(" - {}", indicator)));
}
all_lines.push(Line::from(""));
ListItem::new(Text::from(all_lines))
})
.collect();
let list = List::new(items)
.block(
Block::default()
.borders(Borders::ALL)
.title(format!("Detection History ({} total)", app.detections.len()))
.border_style(Style::default().fg(DANGER)),
)
.style(Style::default().fg(TEXT));
let mut state = app.detections_state.clone();
f.render_stateful_widget(list, area, &mut state);
}
fn draw_memory<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Length(8), Constraint::Min(0)])
.split(area);
let memory_gauge = Gauge::default()
.block(
Block::default()
.borders(Borders::ALL)
.title("Memory Usage")
.border_style(Style::default().fg(PRIMARY)),
)
.gauge_style(Style::default().fg(PRIMARY))
.percent((app.stats.memory_usage_mb * 10.0) as u16)
.label(format!("{:.2} MB", app.stats.memory_usage_mb));
f.render_widget(memory_gauge, chunks[0]);
let memory_info = Paragraph::new(
"Memory Analysis:\n\n\
- Process memory regions scanned\n\
- RWX regions monitored\n\
- Suspicious allocations detected\n\
- Memory layout anomalies tracked\n\n\
Advanced memory analysis features coming soon...",
)
.block(
Block::default()
.borders(Borders::ALL)
.title("Memory Analysis")
.border_style(Style::default().fg(SECONDARY)),
)
.style(Style::default().fg(TEXT))
.wrap(Wrap { trim: true });
f.render_widget(memory_info, chunks[1]);
}
fn draw_logs<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
let items: Vec<ListItem> = app
.logs
.iter()
.map(|log| ListItem::new(log.as_str()).style(Style::default().fg(TEXT)))
.collect();
let list = List::new(items)
.block(
Block::default()
.borders(Borders::ALL)
.title(format!("System Logs ({} entries)", app.logs.len()))
.border_style(Style::default().fg(SUCCESS)),
)
.style(Style::default().fg(TEXT));
let mut state = app.logs_state.clone();
f.render_stateful_widget(list, area, &mut state);
}