Skip to content

Commit

Permalink
feat: IP address filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
GyulyVGC committed Dec 15, 2023
1 parent b196444 commit f18bb2d
Show file tree
Hide file tree
Showing 14 changed files with 435 additions and 83 deletions.
11 changes: 1 addition & 10 deletions resources/fonts/full/subset_characters.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
!
"
#
$
%
&
'
Expand Down Expand Up @@ -142,7 +141,6 @@ z
Ț
ț
ʼ
Ά
Έ
Ή
Ό
Expand Down Expand Up @@ -291,6 +289,7 @@ z
Expand Down Expand Up @@ -437,7 +436,6 @@ z
Expand All @@ -457,7 +455,6 @@ z
Expand Down Expand Up @@ -485,9 +482,7 @@ z
Expand All @@ -500,7 +495,6 @@ z
Expand Down Expand Up @@ -590,7 +584,6 @@ z
Expand Down Expand Up @@ -765,7 +758,6 @@ z
Expand Down Expand Up @@ -803,7 +795,6 @@ z
Expand Down
Binary file modified resources/fonts/subset/sarasa-mono-sc-bold.subset.ttf
Binary file not shown.
Binary file modified resources/fonts/subset/sarasa-mono-sc-regular.subset.ttf
Binary file not shown.
66 changes: 60 additions & 6 deletions src/gui/pages/initial_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
use iced::alignment::{Horizontal, Vertical};
use iced::widget::scrollable::Direction;
use iced::widget::text::Shaping;
use iced::widget::tooltip::Position;
use iced::widget::{
button, horizontal_space, vertical_space, Button, Column, Container, Row, Scrollable, Text,
Tooltip,
TextInput, Tooltip,
};
use iced::Length::FillPortion;
use iced::{alignment, Font, Length, Renderer};
Expand All @@ -19,9 +20,11 @@ use crate::gui::styles::container::ContainerType;
use crate::gui::styles::scrollbar::ScrollbarType;
use crate::gui::styles::style_constants::{get_font, FONT_SIZE_SUBTITLE, FONT_SIZE_TITLE};
use crate::gui::styles::text::TextType;
use crate::gui::styles::text_input::TextInputType;
use crate::gui::styles::types::gradient_type::GradientType;
use crate::gui::types::message::Message;
use crate::gui::types::sniffer::Sniffer;
use crate::networking::types::ip_collection::IpCollection;
use crate::translations::translations::{
address_translation, addresses_translation, choose_adapters_translation,
ip_version_translation, select_filters_translation, start_translation,
Expand All @@ -45,6 +48,12 @@ pub fn initial_page(sniffer: &Sniffer) -> Container<Message, Renderer<StyleType>
let transport_active = &sniffer.filters.transport;
let col_transport_buttons = col_transport_buttons(transport_active, font, language);

let address_active = &sniffer.filters.address_str;
let col_address_filter = col_address_input(address_active, font, language);

let port_active = &sniffer.filters.address_str;
let col_port_filter = col_address_input(port_active, font, language);

let filters_pane = Column::new()
.width(FillPortion(6))
.padding(10)
Expand All @@ -57,10 +66,16 @@ pub fn initial_page(sniffer: &Sniffer) -> Container<Message, Renderer<StyleType>
)
.push(
Row::new()
.spacing(10)
.spacing(20)
.push(col_ip_buttons)
.push(col_transport_buttons),
)
.push(
Row::new()
.spacing(20)
.push(col_address_filter)
.push(col_port_filter),
)
.push(button_start(font, language, color_gradient));

let body = Column::new().push(vertical_space(Length::Fixed(5.0))).push(
Expand All @@ -81,14 +96,16 @@ fn col_ip_buttons(
let mut buttons_row = Row::new().spacing(5).padding([0, 0, 0, 5]);
for option in IpVersion::ALL {
let is_active = active_ip_filters.contains(&option);
let check_simbol = if is_active { "✔" } else { "✘" };
buttons_row = buttons_row.push(
Button::new(
Text::new(option.to_string())
Text::new(format!("{check_simbol} {option}"))
.shaping(Shaping::Advanced)
.horizontal_alignment(Horizontal::Center)
.vertical_alignment(Vertical::Center)
.font(font),
)
.width(Length::Fixed(80.0))
.width(Length::Fixed(90.0))
.height(Length::Fixed(35.0))
.style(if is_active {
ButtonType::BorderedRoundSelected
Expand Down Expand Up @@ -119,14 +136,16 @@ fn col_transport_buttons(
let mut buttons_row = Row::new().spacing(5).padding([0, 0, 0, 5]);
for option in TransProtocol::ALL {
let is_active = active_transport_filters.contains(&option);
let check_simbol = if is_active { "✔" } else { "✘" };
buttons_row = buttons_row.push(
Button::new(
Text::new(option.to_string())
Text::new(format!("{check_simbol} {option}"))
.shaping(Shaping::Advanced)
.horizontal_alignment(Horizontal::Center)
.vertical_alignment(Vertical::Center)
.font(font),
)
.width(Length::Fixed(80.0))
.width(Length::Fixed(90.0))
.height(Length::Fixed(35.0))
.style(if is_active {
ButtonType::BorderedRoundSelected
Expand All @@ -149,6 +168,41 @@ fn col_transport_buttons(
.push(buttons_row)
}

fn col_address_input(
value: &String,
font: Font,
language: Language,
) -> Column<'static, Message, Renderer<StyleType>> {
let is_error = if value.is_empty() {
false
} else {
IpCollection::new(value).is_none()
};
let input_row = Row::new().padding([0, 0, 0, 5]).push(
TextInput::new(IpCollection::PLACEHOLDER_STR, value)
.padding([0, 5])
.on_input(Message::AddressFilter)
.font(font)
.width(Length::Fixed(310.0))
.style(if is_error {
TextInputType::Error
} else {
TextInputType::Standard
}),
);

Column::new()
.width(Length::Fill)
.spacing(7)
.push(
Text::new(address_translation(language))
.font(font)
.style(TextType::Subtitle)
.size(FONT_SIZE_SUBTITLE),
)
.push(input_row)
}

fn button_start(
font: Font,
language: Language,
Expand Down
4 changes: 2 additions & 2 deletions src/gui/styles/types/color_remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ mod tests {
const CATPPUCCIN_PINK_NO_OCTO: &str = "%f5c2e7";
const CATPPUCCIN_PINK_TRUNCATED: &str = "#c2e7";
const CATPPUCCIN_PINK_TOO_LONG: &str = "#f5c2e7f5c2e7f5";
const INVALID_COLOR: &str = "#ca🐈";
const INVALID_COLOR: &str = "#cal✘";

// Test if deserializing and serializing a color works.
#[test]
Expand Down Expand Up @@ -205,7 +205,7 @@ mod tests {
fn test_invalid_hex_color_de() {
assert_de_tokens_error::<DelegateTest>(
&[Token::Str(INVALID_COLOR)],
"invalid value: string \"#ca🐈\", expected valid hexadecimal",
"invalid value: string \"#cal✘\", expected valid hexadecimal",
);
}
}
2 changes: 2 additions & 0 deletions src/gui/types/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub enum Message {
IpVersionSelection(IpVersion, bool),
/// Select transport filter
TransportProtocolSelection(TransProtocol, bool),
/// Changed address filter
AddressFilter(String),
/// Select chart type to be displayed
ChartSelection(ChartType),
/// Select report type to be displayed
Expand Down
70 changes: 35 additions & 35 deletions src/gui/types/sniffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::mmdb::types::mmdb_reader::MmdbReader;
use crate::networking::manage_packets::get_capture_result;
use crate::networking::types::filters::Filters;
use crate::networking::types::host::Host;
use crate::networking::types::ip_collection::IpCollection;
use crate::networking::types::my_device::MyDevice;
use crate::networking::types::search_parameters::SearchParameters;
use crate::notifications::notify_and_log::notify_and_log;
Expand Down Expand Up @@ -147,6 +148,12 @@ impl Sniffer {
self.filters.transport.remove(&protocol);
}
}
Message::AddressFilter(value) => {
if let Some(collection) = IpCollection::new(&value) {
self.filters.address_collection = collection;
}
self.filters.address_str = value;
}
Message::ChartSelection(unit) => self.traffic_chart.change_kind(unit),
Message::ReportSortSelection(sort) => self.report_sort_type = sort,
Message::OpenWebPage(web_page) => Self::open_web(&web_page),
Expand Down Expand Up @@ -549,53 +556,46 @@ mod tests {
};
use crate::notifications::types::sound::Sound;
use crate::{
AppProtocol, ByteMultiple, ChartType, Configs, IpVersion, Language, ReportSortType,
RunningPage, Sniffer, StyleType, TransProtocol,
ByteMultiple, ChartType, Configs, IpVersion, Language, ReportSortType, RunningPage,
Sniffer, StyleType, TransProtocol,
};

#[test]
fn test_correctly_update_ip_version() {
let mut sniffer = Sniffer::new(&Configs::default(), Arc::new(Mutex::new(None)));

assert_eq!(sniffer.filters.ip, IpVersion::Other);
sniffer.update(Message::IpVersionSelection(IpVersion::IPv6));
assert_eq!(sniffer.filters.ip, IpVersion::IPv6);
sniffer.update(Message::IpVersionSelection(IpVersion::IPv4));
assert_eq!(sniffer.filters.ip, IpVersion::IPv4);
sniffer.update(Message::IpVersionSelection(IpVersion::IPv4));
assert_eq!(sniffer.filters.ip, IpVersion::IPv4);
sniffer.update(Message::IpVersionSelection(IpVersion::Other));
assert_eq!(sniffer.filters.ip, IpVersion::Other);
assert_eq!(sniffer.filters.ip, HashSet::from(IpVersion::ALL));
sniffer.update(Message::IpVersionSelection(IpVersion::IPv6, true));
assert_eq!(sniffer.filters.ip, HashSet::from(IpVersion::ALL));
sniffer.update(Message::IpVersionSelection(IpVersion::IPv4, false));
assert_eq!(sniffer.filters.ip, HashSet::from([IpVersion::IPv6]));
sniffer.update(Message::IpVersionSelection(IpVersion::IPv6, false));
assert_eq!(sniffer.filters.ip, HashSet::new());
}

#[test]
fn test_correctly_update_transport_protocol() {
let mut sniffer = Sniffer::new(&Configs::default(), Arc::new(Mutex::new(None)));

assert_eq!(sniffer.filters.transport, TransProtocol::Other);
sniffer.update(Message::TransportProtocolSelection(TransProtocol::UDP));
assert_eq!(sniffer.filters.transport, TransProtocol::UDP);
sniffer.update(Message::TransportProtocolSelection(TransProtocol::UDP));
assert_eq!(sniffer.filters.transport, TransProtocol::UDP);
sniffer.update(Message::TransportProtocolSelection(TransProtocol::TCP));
assert_eq!(sniffer.filters.transport, TransProtocol::TCP);
sniffer.update(Message::TransportProtocolSelection(TransProtocol::Other));
assert_eq!(sniffer.filters.transport, TransProtocol::Other);
}

#[test]
fn test_correctly_update_application_protocol() {
let mut sniffer = Sniffer::new(&Configs::default(), Arc::new(Mutex::new(None)));

assert_eq!(sniffer.filters.application, AppProtocol::Other);
sniffer.update(Message::AppProtocolSelection(AppProtocol::HTTPS));
assert_eq!(sniffer.filters.application, AppProtocol::HTTPS);
sniffer.update(Message::AppProtocolSelection(AppProtocol::HTTP));
assert_eq!(sniffer.filters.application, AppProtocol::HTTP);
sniffer.update(Message::AppProtocolSelection(AppProtocol::HTTP));
assert_eq!(sniffer.filters.application, AppProtocol::HTTP);
sniffer.update(Message::AppProtocolSelection(AppProtocol::XMPP));
assert_eq!(sniffer.filters.application, AppProtocol::XMPP);
assert_eq!(sniffer.filters.transport, HashSet::from(TransProtocol::ALL));
sniffer.update(Message::TransportProtocolSelection(
TransProtocol::UDP,
true,
));
assert_eq!(sniffer.filters.transport, HashSet::from(TransProtocol::ALL));
sniffer.update(Message::TransportProtocolSelection(
TransProtocol::UDP,
false,
));
assert_eq!(
sniffer.filters.transport,
HashSet::from([TransProtocol::TCP])
);
sniffer.update(Message::TransportProtocolSelection(
TransProtocol::TCP,
false,
));
assert_eq!(sniffer.filters.transport, HashSet::new());
}

#[test]
Expand Down
Loading

0 comments on commit f18bb2d

Please sign in to comment.