Compare commits

..

3 commits

Author SHA1 Message Date
3664d06d17
chore: Handle failed banning properly
Signed-off-by: Awiteb <a@4rs.nl>
2024-11-16 11:57:55 +00:00
f3561b5efd
chore: Update README.md
Signed-off-by: Awiteb <a@4rs.nl>
2024-11-16 11:47:54 +00:00
f68ce0c5bd
feat: Checks only new users configuration
Signed-off-by: Awiteb <a@4rs.nl>
2024-11-16 14:47:00 +03:00
6 changed files with 28 additions and 10 deletions

View file

@ -21,7 +21,7 @@ You can let [cargo](https://doc.rust-lang.org/cargo/) build the binary for you,
> [!TIP]
> This will install the binary in `~/.cargo/bin/forgejo-guardian`. Make sure to add this directory to your `PATH`.
> If you want to update it, run `cargo install ...` again.
> If you want to update it, rerun the command.
```sh
cargo install --git https://git.4rs.nl/awiteb/forgejo-guardian
@ -55,17 +55,18 @@ We use `TOML` format for configuration, the default configuration file is `/app/
### Structure
In our configuration file you can have the following sections and the global sections:
In our configuration file you can have the following sections and the global section:
- `forgejo`: Forgejo instance configuration
- `expressions`: Regular expressions to match against
- `telegram`: Telegram bot configuration
#### Global sections
#### Global section
The global section is the one that doesn't have a name, and it's in the top of the configuration file, with the following fields:
- `dry_run`: If set to `true`, the guardian will not ban the users, but will only alert the admins (default: `false`)
- `only_new_users`: If set to `true`, the guardian will only check the new users, and not the existing ones (default: `false`)
#### `forgejo`

View file

@ -143,12 +143,15 @@ pub struct Exprs {
pub struct Config {
/// Dry run, without banning the users
#[serde(default)]
pub dry_run: bool,
pub dry_run: bool,
/// Only checks new users
#[serde(default)]
pub only_new_users: bool,
/// Configuration for the forgejo guard itself
pub forgejo: Forgejo,
pub forgejo: Forgejo,
/// Configuration of the telegram bot
pub telegram: Telegram,
pub telegram: Telegram,
/// The expressions, which are used to determine the actions
#[serde(default)]
pub expressions: Exprs,
pub expressions: Exprs,
}

View file

@ -14,6 +14,8 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://gnu.org/licenses/agpl.txt>.
use reqwest::StatusCode;
use crate::config::{CONFIG_PATH_ENV, DEFAULT_CONFIG_PATH};
/// Result of the guard
@ -39,4 +41,7 @@ pub enum GuardError {
/// Faild to deserialize the config file
#[error("Failed to deserialize the config: {0}")]
FaildDeserializeConfig(#[from] toml::de::Error),
/// Failed to ban the user
#[error("Failed to ban the user, status code: {0}")]
FailedToBan(StatusCode),
}

View file

@ -16,7 +16,7 @@
use reqwest::Method;
use crate::error::GuardResult;
use crate::error::{GuardError, GuardResult};
/// Ban a user from the instance, purging their data.
pub async fn ban_user(
@ -34,7 +34,10 @@ pub async fn ban_user(
))
.await?;
tracing::debug!("Ban user response: {:?}", &res);
tracing::debug!("Body: {}", res.text().await.unwrap_or_default());
if !res.status().is_success() {
return Err(GuardError::FailedToBan(res.status()));
}
Ok(())
}

View file

@ -44,6 +44,7 @@ async fn try_main() -> error::GuardResult<()> {
tracing::info!("The instance: {}", config.forgejo.instance);
tracing::info!("Dry run: {}", config.dry_run);
tracing::info!("Only new users: {}", config.only_new_users);
tracing::debug!("The config exprs: {:#?}", config.expressions);
rust_i18n::set_locale(config.telegram.lang.as_str());

View file

@ -98,6 +98,7 @@ async fn check_new_users(
sus_sender: Sender<ForgejoUser>,
ban_sender: Sender<ForgejoUser>,
) {
let is_first_fetch = last_user_id.load(Ordering::Relaxed) == 0;
match get_new_users(
&request_client,
last_user_id.load(Ordering::Relaxed),
@ -112,7 +113,11 @@ async fn check_new_users(
if let Some(uid) = new_users.iter().max_by_key(|u| u.id).map(|u| u.id) {
tracing::debug!("New last user id: {uid}");
last_user_id.store(uid, Ordering::Relaxed)
last_user_id.store(uid, Ordering::Relaxed);
}
if config.only_new_users && is_first_fetch {
return;
}
for user in new_users {