diff --git a/Cargo.lock b/Cargo.lock index ac725ad6..aae50637 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3188,6 +3188,15 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3" +[[package]] +name = "signal-hook-registry" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1" +dependencies = [ + "libc", +] + [[package]] name = "simd-adler32" version = "0.3.4" @@ -3265,6 +3274,7 @@ dependencies = [ "iced_native", "indexmap", "maxminddb", + "once_cell", "pcap", "plotters", "plotters-iced", @@ -3273,6 +3283,7 @@ dependencies = [ "rstest", "serde", "thousands", + "tokio", ] [[package]] @@ -3525,11 +3536,25 @@ dependencies = [ "libc", "mio", "num_cpus", + "parking_lot 0.12.1", "pin-project-lite", + "signal-hook-registry", "socket2", + "tokio-macros", "windows-sys 0.45.0", ] +[[package]] +name = "tokio-macros" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61a573bdc87985e9d6ddeed1b3d864e8a302c847e40d647746df2f1de209d1ce" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.4", +] + [[package]] name = "tokio-native-tls" version = "0.3.1" diff --git a/Cargo.toml b/Cargo.toml index 0a3d45ef..d94f1719 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,18 @@ repository = "https://github.com/GyulyVGC/sniffnet" license = "MIT OR Apache-2.0" keywords = ["filter", "network", "packet", "sniffer", "gui"] categories = ["visualization", "gui", "network-programming"] -include = ["src/**/*", "LICENSE-*", "README.md", "CHANGELOG.md", "resources/countries_flags/**/*", "resources/DB/GeoLite2-Country.mmdb", "resources/palettes/*", "resources/fonts/subset/*", "resources/sounds/*"] +include = [ + "src/**/*", + "LICENSE-*", + "README.md", + "CHANGELOG.md", + "resources/countries_flags/**/*", + "resources/DB/GeoLite2-Country.mmdb", + "resources/palettes/*", + "resources/fonts/icons.ttf", + "resources/fonts/sarasa/subset/*", + "resources/sounds/*" +] [package.metadata.bundle] @@ -40,6 +51,8 @@ confy = "0.5.1" serde = { version = "1.0.160", default_features = false, features = ["derive"] } rodio = { version = "0.17.1", default_features = false, features = ["mp3"] } reqwest = { version = "0.11.16", features = ["json", "blocking"] } +tokio = { version = "1.27.0", features = ["full"] } +once_cell = "1.17.1" [dev-dependencies] rstest = "0.17.0" diff --git a/resources/fonts/noto-naskh-arabic-bold.ttf b/resources/fonts/noto/NotoNaskhArabic-Bold.ttf similarity index 100% rename from resources/fonts/noto-naskh-arabic-bold.ttf rename to resources/fonts/noto/NotoNaskhArabic-Bold.ttf diff --git a/resources/fonts/noto-naskh-arabic-regular.ttf b/resources/fonts/noto/NotoNaskhArabic-Regular.ttf similarity index 100% rename from resources/fonts/noto-naskh-arabic-regular.ttf rename to resources/fonts/noto/NotoNaskhArabic-Regular.ttf diff --git a/resources/fonts/noto-sans-bold.ttf b/resources/fonts/noto/NotoSans-Bold.ttf similarity index 100% rename from resources/fonts/noto-sans-bold.ttf rename to resources/fonts/noto/NotoSans-Bold.ttf diff --git a/resources/fonts/noto-sans-regular.ttf b/resources/fonts/noto/NotoSans-Regular.ttf similarity index 100% rename from resources/fonts/noto-sans-regular.ttf rename to resources/fonts/noto/NotoSans-Regular.ttf diff --git a/resources/fonts/noto-sans-korean-bold.otf b/resources/fonts/noto/NotoSansKR-Bold.otf similarity index 100% rename from resources/fonts/noto-sans-korean-bold.otf rename to resources/fonts/noto/NotoSansKR-Bold.otf diff --git a/resources/fonts/noto-sans-korean-regular.otf b/resources/fonts/noto/NotoSansKR-Regular.otf similarity index 100% rename from resources/fonts/noto-sans-korean-regular.otf rename to resources/fonts/noto/NotoSansKR-Regular.otf diff --git a/resources/fonts/noto-sans-chinese-bold.otf b/resources/fonts/noto/NotoSansSC-Bold.otf similarity index 100% rename from resources/fonts/noto-sans-chinese-bold.otf rename to resources/fonts/noto/NotoSansSC-Bold.otf diff --git a/resources/fonts/noto-sans-chinese-regular.otf b/resources/fonts/noto/NotoSansSC-Regular.otf similarity index 100% rename from resources/fonts/noto-sans-chinese-regular.otf rename to resources/fonts/noto/NotoSansSC-Regular.otf diff --git a/resources/fonts/noto-sans-symbols.ttf b/resources/fonts/noto/NotoSansSymbols2-Regular.ttf similarity index 100% rename from resources/fonts/noto-sans-symbols.ttf rename to resources/fonts/noto/NotoSansSymbols2-Regular.ttf diff --git a/resources/fonts/sarasa/full/sarasa-mono-bold.ttf b/resources/fonts/sarasa/full/sarasa-mono-bold.ttf new file mode 100644 index 00000000..0995ee2a Binary files /dev/null and b/resources/fonts/sarasa/full/sarasa-mono-bold.ttf differ diff --git a/resources/fonts/sarasa/full/sarasa-mono-regular.ttf b/resources/fonts/sarasa/full/sarasa-mono-regular.ttf new file mode 100644 index 00000000..67ebe054 Binary files /dev/null and b/resources/fonts/sarasa/full/sarasa-mono-regular.ttf differ diff --git a/resources/fonts/sarasa/full/subset_characters.txt b/resources/fonts/sarasa/full/subset_characters.txt new file mode 100644 index 00000000..04138b46 --- /dev/null +++ b/resources/fonts/sarasa/full/subset_characters.txt @@ -0,0 +1,567 @@ + +! +" +# +$ +% +& +' +( +) +* ++ +, +- +. +/ +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +: +; +< += +> +? +@ +A +B +C +D +E +F +G +H +I +J +K +L +M +N +O +P +Q +R +S +T +U +V +W +X +Y +Z +[ +\ +] +^ +_ +` +a +b +c +d +e +f +g +h +i +j +k +l +m +n +o +p +q +r +s +t +u +v +w +x +y +z +{ +| +} +¡ +¿ +Ê +Î +Ö +Ü +à +á +â +ã +ä +ç +è +é +ê +ì +í +î +ñ +ó +ô +õ +ö +ù +ú +û +ü +ă +ą +ć +ę +ğ +İ +ı +ł +ń +ś +Ş +ş +ź +ż +Ș +ș +ț +А +В +Г +Д +Е +З +И +К +Л +М +Н +О +П +Р +С +Т +У +Ф +Ч +Ш +Щ +Я +а +б +в +г +д +е +ж +з +и +й +к +л +м +н +о +п +р +с +т +у +ф +х +ц +ч +ш +щ +ъ +ы +ь +э +ю +я +ё +є +і +ї +⏎ +❤ +一 +中 +主 +义 +了 +互 +些 +交 +仅 +从 +他 +以 +们 +任 +会 +传 +但 +体 +何 +使 +侧 +信 +值 +入 +其 +内 +出 +击 +分 +到 +前 +功 +动 +包 +协 +占 +即 +发 +可 +右 +后 +吗 +含 +启 +告 +哦 +喜 +器 +图 +在 +地 +址 +士 +夹 +如 +始 +字 +它 +完 +定 +家 +将 +小 +尝 +层 +已 +并 +应 +开 +当 +待 +志 +总 +您 +情 +想 +成 +我 +所 +打 +报 +择 +指 +按 +换 +据 +接 +控 +收 +数 +整 +文 +新 +无 +日 +时 +星 +是 +显 +暂 +暗 +最 +有 +未 +本 +条 +果 +标 +梦 +概 +欢 +正 +此 +每 +没 +洋 +活 +流 +浅 +测 +海 +清 +滤 +潜 +点 +版 +率 +生 +用 +的 +皆 +监 +目 +知 +确 +示 +秒 +空 +站 +等 +简 +类 +络 +网 +置 +联 +自 +色 +节 +藏 +要 +览 +言 +计 +议 +设 +试 +详 +语 +误 +请 +贴 +超 +输 +达 +过 +近 +还 +这 +连 +退 +适 +选 +通 +速 +配 +里 +量 +错 +间 +阈 +除 +隐 +需 +面 +音 +页 +题 +黑 +가 +감 +값 +개 +검 +것 +게 +겠 +겨 +결 +계 +고 +과 +관 +교 +국 +그 +근 +기 +까 +네 +넷 +눌 +는 +능 +니 +다 +당 +대 +댑 +데 +뎁 +도 +되 +된 +될 +됩 +두 +둘 +드 +들 +따 +때 +라 +랑 +래 +러 +레 +려 +련 +로 +료 +류 +륨 +른 +를 +리 +림 +링 +마 +만 +말 +면 +모 +몽 +문 +바 +받 +발 +버 +별 +보 +본 +볼 +부 +분 +사 +삭 +새 +생 +서 +석 +선 +설 +성 +세 +션 +셨 +소 +속 +송 +수 +숨 +스 +습 +시 +신 +아 +않 +알 +았 +애 +어 +언 +없 +었 +에 +여 +연 +열 +였 +오 +올 +요 +용 +우 +운 +워 +위 +유 +으 +을 +음 +의 +이 +인 +일 +임 +있 +자 +장 +재 +적 +전 +정 +제 +종 +주 +줄 +중 +즐 +지 +직 +찰 +찾 +체 +초 +총 +최 +추 +케 +콜 +크 +킷 +타 +태 +택 +터 +테 +토 +트 +패 +페 +표 +프 +플 +픽 +필 +하 +한 +할 +합 +해 +현 +화 +확 +환 +활 +효 +, diff --git a/resources/fonts/sarasa/subset/sarasa-mono-subset-bold.ttf b/resources/fonts/sarasa/subset/sarasa-mono-subset-bold.ttf new file mode 100644 index 00000000..226a73cb Binary files /dev/null and b/resources/fonts/sarasa/subset/sarasa-mono-subset-bold.ttf differ diff --git a/resources/fonts/sarasa/subset/sarasa-mono-subset-regular.ttf b/resources/fonts/sarasa/subset/sarasa-mono-subset-regular.ttf new file mode 100644 index 00000000..f7aae9f6 Binary files /dev/null and b/resources/fonts/sarasa/subset/sarasa-mono-subset-regular.ttf differ diff --git a/src/chart/types/traffic_chart.rs b/src/chart/types/traffic_chart.rs index faad2007..d903151f 100644 --- a/src/chart/types/traffic_chart.rs +++ b/src/chart/types/traffic_chart.rs @@ -8,13 +8,13 @@ use iced::Element; use plotters::style::RGBColor; use plotters_iced::{Chart, ChartBuilder, ChartWidget, DrawingBackend}; -use crate::gui::styles::fonts; +use crate::gui::styles::fonts::get_language_font; use crate::gui::styles::style_constants::{get_color_mix_chart, CHARTS_LINE_BORDER}; use crate::gui::styles::types::palette::to_rgb_color; use crate::gui::types::message::Message; use crate::translations::translations::{incoming_translation, outgoing_translation}; use crate::utils::formatted_strings::get_formatted_bytes_string; -use crate::{font_selector, get_colors, ChartType, Language, StyleType}; +use crate::{get_colors, ChartType, Language, StyleType}; /// Struct defining the chart to be displayed in gui run page pub struct TrafficChart { @@ -70,7 +70,7 @@ impl TrafficChart { let language = self.language; Container::new( Column::new().push( - ChartWidget::new(self).resolve_font(move |_, _| font_selector!(color_font, language)), + ChartWidget::new(self).resolve_font(move |_, _| get_language_font(color_font, language)), ), ) .align_x(Horizontal::Left) diff --git a/src/gui/components/footer.rs b/src/gui/components/footer.rs index f7d78fd0..1e253876 100644 --- a/src/gui/components/footer.rs +++ b/src/gui/components/footer.rs @@ -8,7 +8,7 @@ use iced::{Alignment, Font, Length}; use iced_native::widget::horizontal_space; use iced_native::widget::tooltip::Position; -use crate::gui::styles::fonts; +use crate::gui::styles::fonts::get_symbols_font; use crate::gui::styles::style_constants::{get_font, get_font_headers, FONT_SIZE_FOOTER, ICONS}; use crate::gui::styles::types::element_type::ElementType; use crate::gui::styles::types::style_tuple::StyleTuple; @@ -48,7 +48,7 @@ pub fn footer( Text::new("❤") .width(Length::Fixed(14.0)) .size(FONT_SIZE_FOOTER + 4.0) - .font(fonts::NOTO_SANS_SYMBOLS) + .font(get_symbols_font()) ) .push( Text::new(" by Giuliano Bellini") diff --git a/src/gui/styles/fonts.rs b/src/gui/styles/fonts.rs index 8a3d345d..73f830e5 100644 --- a/src/gui/styles/fonts.rs +++ b/src/gui/styles/fonts.rs @@ -1,67 +1,85 @@ use iced::Font; +use once_cell::sync::OnceCell; +use std::collections::HashMap; +use plotters::style::RGBColor; +use crate::translations::types::language::Language; -pub const NOTO_SANS_SYMBOLS: Font = Font::External { - name: "noto-sans-symbols", - bytes: include_bytes!("../../../resources/fonts/noto-sans-symbols.ttf"), -}; +pub type FontsMap = HashMap<&'static str, Font>; -pub const NOTO_SANS_REGULAR: Font = Font::External { - name: "noto-sans-regular", - bytes: include_bytes!("../../../resources/fonts/noto-sans-regular.ttf"), -}; +pub static FONTS: OnceCell = OnceCell::new(); -pub const NOTO_SANS_BOLD: Font = Font::External { - name: "noto-sans-bold", - bytes: include_bytes!("../../../resources/fonts/noto-sans-bold.ttf"), -}; +struct FontMetadata { + pub name: &'static str, + pub file: &'static str, +} -pub const NOTO_NASKH_ARABIC_REGULAR: Font = Font::External { - name: "noto-naskh-arabic-regular", - bytes: include_bytes!("../../../resources/fonts/noto-naskh-arabic-regular.ttf"), -}; +const FONT_METADATA: [FontMetadata; 9] = [ + FontMetadata { name: "noto-sans-symbols", file: "NotoSansSymbols2-Regular.ttf" }, + FontMetadata { name: "noto-sans-regular", file: "NotoSans-Regular.ttf" }, + FontMetadata { name: "noto-sans-bold", file: "NotoSans-Bold.ttf" }, + FontMetadata { name: "noto-naskh-arabic-regular", file: "NotoNaskhArabic-Regular.ttf" }, + FontMetadata { name: "noto-naskh-arabic-bold", file: "NotoNaskhArabic-Bold.ttf" }, + FontMetadata { name: "noto-sans-korean-regular", file: "NotoSansKR-Regular.otf" }, + FontMetadata { name: "noto-sans-korean-bold", file: "NotoSansKR-Bold.otf" }, + FontMetadata { name: "noto-sans-chinese-regular", file: "NotoSansSC-Regular.otf" }, + FontMetadata { name: "noto-sans-chinese-bold", file: "NotoSansSC-Bold.otf" }, +]; -pub const NOTO_NASKH_ARABIC_BOLD: Font = Font::External { - name: "noto-naskh-arabic-bold", - bytes: include_bytes!("../../../resources/fonts/noto-naskh-arabic-bold.ttf"), -}; +async fn download_font(base_url: &str, metadata: &FontMetadata) -> Result { + let url = format!("{}/{}", base_url, metadata.file); + let response = reqwest::get(url).await?; -pub const NOTO_SANS_KOREAN_REGULAR: Font = Font::External { - name: "noto-sans-korean-regular", - bytes: include_bytes!("../../../resources/fonts/noto-sans-korean-regular.otf"), -}; + if response.status() == reqwest::StatusCode::NOT_FOUND { + return Ok(Font::Default); + } -pub const NOTO_SANS_KOREAN_BOLD: Font = Font::External { - name: "noto-sans-korean-bold", - bytes: include_bytes!("../../../resources/fonts/noto-sans-korean-bold.otf"), -}; + let bytes = response.bytes().await?; + let font_data = Box::new(bytes); + let font = Font::External { + name: metadata.name, + bytes: &*Box::leak(font_data), + }; + Ok(font) +} + +#[tokio::main] +pub async fn load_fonts() { + let base_url = String::from("https://raw.githubusercontent.com/GyulyVGC/sniffnet/main/resources/fonts/noto"); -pub const NOTO_SANS_CHINESE_REGULAR: Font = Font::External { - name: "noto-sans-chinese-regular", - bytes: include_bytes!("../../../resources/fonts/noto-sans-chinese-regular.otf"), -}; + let mut download_futures = Vec::new(); -pub const NOTO_SANS_CHINESE_BOLD: Font = Font::External { - name: "noto-sans-chinese-bold", - bytes: include_bytes!("../../../resources/fonts/noto-sans-chinese-bold.otf"), -}; + for metadata in &FONT_METADATA { + download_futures.push(download_font(&base_url, metadata)); + } -#[macro_export] -macro_rules! font_selector { - ($color:expr, $language:expr) => { - match $color { - // if white non-bold - RGBColor(255, 255, 255) => match $language { - Language::FA => fonts::NOTO_NASKH_ARABIC_REGULAR, - Language::KO => fonts::NOTO_SANS_KOREAN_REGULAR, - Language::ZH => fonts::NOTO_SANS_CHINESE_REGULAR, - _ => fonts::NOTO_SANS_REGULAR, - }, - _ => match $language { - Language::FA => fonts::NOTO_NASKH_ARABIC_BOLD, - Language::KO => fonts::NOTO_SANS_KOREAN_BOLD, - Language::ZH => fonts::NOTO_SANS_CHINESE_BOLD, - _ => fonts::NOTO_SANS_BOLD, - } + let mut fonts_map = HashMap::new(); + for (i, font_result) in download_futures.into_iter().enumerate() { + if let Ok(font) = font_result.await { + fonts_map.insert(FONT_METADATA[i].name, font); } - }; + } + + FONTS.set(fonts_map).ok(); +} + +pub fn get_language_font(color: RGBColor, language: Language) -> Font { + match color { + // if white non-bold + RGBColor(255, 255, 255) => match language { + Language::FA => FONTS.get().and_then(|f| f.get("noto-naskh-arabic-regular")).unwrap_or(&Font::Default).clone(), + Language::KO => FONTS.get().and_then(|f| f.get("noto-sans-korean-regular")).unwrap_or(&Font::Default).clone(), + Language::ZH => FONTS.get().and_then(|f| f.get("noto-sans-chinese-regular")).unwrap_or(&Font::Default).clone(), + _ => FONTS.get().and_then(|f| f.get("noto-sans-regular")).unwrap_or(&Font::Default).clone(), + }, + _ => match language { + Language::FA => FONTS.get().and_then(|f| f.get("noto-naskh-arabic-bold")).unwrap_or(&Font::Default).clone(), + Language::KO => FONTS.get().and_then(|f| f.get("noto-sans-korean-bold")).unwrap_or(&Font::Default).clone(), + Language::ZH => FONTS.get().and_then(|f| f.get("noto-sans-chinese-bold")).unwrap_or(&Font::Default).clone(), + _ => FONTS.get().and_then(|f| f.get("noto-sans-bold")).unwrap_or(&Font::Default).clone(), + }, + } +} + +pub fn get_symbols_font() -> Font { + FONTS.get().and_then(|f| f.get("noto-sans-symbols")).unwrap_or(&Font::Default).clone() } \ No newline at end of file diff --git a/src/gui/styles/style_constants.rs b/src/gui/styles/style_constants.rs index 15799589..d75dfc2c 100644 --- a/src/gui/styles/style_constants.rs +++ b/src/gui/styles/style_constants.rs @@ -1,11 +1,10 @@ //! Module defining the constants used for aesthetic purposes (colors, borders...) use iced::{Color, Font}; -use plotters::style::RGBColor; -use crate::gui::styles::fonts; +use crate::gui::styles::fonts::get_language_font; use crate::gui::styles::types::palette::{to_rgb_color, Palette}; -use crate::{font_selector, get_colors, StyleType}; +use crate::{get_colors, StyleType}; use crate::translations::types::language::Language; // night theme @@ -165,12 +164,12 @@ pub const MON_AMOUR_STYLE: Palette = Palette { pub fn get_font(style: StyleType, language: Language) -> Font { let color_font = to_rgb_color(get_colors(style).text_body); - font_selector!(color_font, language) + get_language_font(color_font, language) } pub fn get_font_headers(style: StyleType, language: Language) -> Font { let color_font = to_rgb_color(get_colors(style).text_headers); - font_selector!(color_font, language) + get_language_font(color_font, language) } pub fn get_color_mix_chart(style: StyleType) -> f64 { diff --git a/src/main.rs b/src/main.rs index e6551681..3976c241 100644 --- a/src/main.rs +++ b/src/main.rs @@ -91,6 +91,8 @@ pub fn main() -> iced::Result { }) .unwrap(); + gui::styles::fonts::load_fonts(); + print_cli_welcome_message(); Sniffer::run(Settings { @@ -117,7 +119,7 @@ pub fn main() -> iced::Result { newer_release_available1, ), default_font: Some(include_bytes!( - "../resources/fonts/noto-sans-regular.ttf" + "../resources/fonts/sarasa/subset/sarasa-mono-subset-bold.ttf" )), default_text_size: FONT_SIZE_BODY, text_multithreading: true,