Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Ignore HELP spam #25

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ All notable changes to this project will be documented in this file.

- Try to improve performance by calling `madvise` to inform Kernel we are reading sequentially ([#24])

### Changed
- Ignore repeated `HELP` requests ([#25])
- Only the first 2 requests of any `parse` patch are answered
- Answers `Stop spamming HELP!` on the third request
- Doesn't respond to any further requests

[#24]: https://github.com/sbernauer/breakwater/pull/24
[#25]: https://github.com/sbernauer/breakwater/pull/25

## [0.14.0] - 2024-05-30 at GPN 22 :)

Expand Down
2 changes: 2 additions & 0 deletions breakwater-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ if cfg!(feature = "alpha") {
"PX x y rrggbbaa: Color the pixel (x,y) with the given hexadecimal color rrggbb. The alpha part is discarded for performance reasons, as breakwater was compiled without the alpha feature"
}
).as_bytes();

pub const ALT_HELP_TEXT: &[u8] = b"Stop spamming HELP!\n";
21 changes: 16 additions & 5 deletions breakwater-parser/src/original.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
sync::Arc,
};

use breakwater_core::{framebuffer::FrameBuffer, HELP_TEXT};
use breakwater_core::{framebuffer::FrameBuffer, ALT_HELP_TEXT, HELP_TEXT};
use tokio::io::AsyncWriteExt;

use crate::{Parser, ParserError};
Expand Down Expand Up @@ -38,6 +38,7 @@ impl Parser for OriginalParser {
mut stream: impl AsyncWriteExt + Send + Unpin,
) -> Result<usize, ParserError> {
let mut last_byte_parsed = 0;
let mut help_count = 0;

let mut i = 0; // We can't use a for loop here because Rust don't lets use skip characters by incrementing i
let loop_end = buffer.len().saturating_sub(PARSER_LOOKAHEAD); // Let's extract the .len() call and the subtraction into it's own variable so we only compute it once
Expand Down Expand Up @@ -178,10 +179,20 @@ impl Parser for OriginalParser {
i += 4;
last_byte_parsed = i - 1;

stream
.write_all(HELP_TEXT)
.await
.expect("Failed to write bytes to tcp socket");
#[allow(clippy::comparison_chain)]
if help_count < 3 {
stream
.write_all(HELP_TEXT)
.await
.expect("Failed to write bytes to tcp socket");
help_count += 1;
} else if help_count == 3 {
stream
.write_all(ALT_HELP_TEXT)
.await
.expect("Failed to write bytes to tcp socket");
help_count += 1;
}
continue;
}

Expand Down
Loading