Compare commits
No commits in common. "3664d06d170f264ea1aba908567800a19f7c0a31" and "9b533e7ea37741808e63500e6f7b3273cfcb8e5a" have entirely different histories.
3664d06d17
...
9b533e7ea3
6 changed files with 10 additions and 28 deletions
|
@ -21,7 +21,7 @@ You can let [cargo](https://doc.rust-lang.org/cargo/) build the binary for you,
|
||||||
|
|
||||||
> [!TIP]
|
> [!TIP]
|
||||||
> This will install the binary in `~/.cargo/bin/forgejo-guardian`. Make sure to add this directory to your `PATH`.
|
> 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, rerun the command.
|
> If you want to update it, run `cargo install ...` again.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cargo install --git https://git.4rs.nl/awiteb/forgejo-guardian
|
cargo install --git https://git.4rs.nl/awiteb/forgejo-guardian
|
||||||
|
@ -55,18 +55,17 @@ We use `TOML` format for configuration, the default configuration file is `/app/
|
||||||
|
|
||||||
### Structure
|
### Structure
|
||||||
|
|
||||||
In our configuration file you can have the following sections and the global section:
|
In our configuration file you can have the following sections and the global sections:
|
||||||
|
|
||||||
- `forgejo`: Forgejo instance configuration
|
- `forgejo`: Forgejo instance configuration
|
||||||
- `expressions`: Regular expressions to match against
|
- `expressions`: Regular expressions to match against
|
||||||
- `telegram`: Telegram bot configuration
|
- `telegram`: Telegram bot configuration
|
||||||
|
|
||||||
#### Global section
|
#### Global sections
|
||||||
|
|
||||||
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:
|
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`)
|
- `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`
|
#### `forgejo`
|
||||||
|
|
||||||
|
|
|
@ -143,15 +143,12 @@ pub struct Exprs {
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
/// Dry run, without banning the users
|
/// Dry run, without banning the users
|
||||||
#[serde(default)]
|
#[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
|
/// Configuration for the forgejo guard itself
|
||||||
pub forgejo: Forgejo,
|
pub forgejo: Forgejo,
|
||||||
/// Configuration of the telegram bot
|
/// Configuration of the telegram bot
|
||||||
pub telegram: Telegram,
|
pub telegram: Telegram,
|
||||||
/// The expressions, which are used to determine the actions
|
/// The expressions, which are used to determine the actions
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub expressions: Exprs,
|
pub expressions: Exprs,
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,8 +14,6 @@
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// 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>.
|
// 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};
|
use crate::config::{CONFIG_PATH_ENV, DEFAULT_CONFIG_PATH};
|
||||||
|
|
||||||
/// Result of the guard
|
/// Result of the guard
|
||||||
|
@ -41,7 +39,4 @@ pub enum GuardError {
|
||||||
/// Faild to deserialize the config file
|
/// Faild to deserialize the config file
|
||||||
#[error("Failed to deserialize the config: {0}")]
|
#[error("Failed to deserialize the config: {0}")]
|
||||||
FaildDeserializeConfig(#[from] toml::de::Error),
|
FaildDeserializeConfig(#[from] toml::de::Error),
|
||||||
/// Failed to ban the user
|
|
||||||
#[error("Failed to ban the user, status code: {0}")]
|
|
||||||
FailedToBan(StatusCode),
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
use reqwest::Method;
|
use reqwest::Method;
|
||||||
|
|
||||||
use crate::error::{GuardError, GuardResult};
|
use crate::error::GuardResult;
|
||||||
|
|
||||||
/// Ban a user from the instance, purging their data.
|
/// Ban a user from the instance, purging their data.
|
||||||
pub async fn ban_user(
|
pub async fn ban_user(
|
||||||
|
@ -34,10 +34,7 @@ pub async fn ban_user(
|
||||||
))
|
))
|
||||||
.await?;
|
.await?;
|
||||||
tracing::debug!("Ban user response: {:?}", &res);
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,7 +44,6 @@ async fn try_main() -> error::GuardResult<()> {
|
||||||
|
|
||||||
tracing::info!("The instance: {}", config.forgejo.instance);
|
tracing::info!("The instance: {}", config.forgejo.instance);
|
||||||
tracing::info!("Dry run: {}", config.dry_run);
|
tracing::info!("Dry run: {}", config.dry_run);
|
||||||
tracing::info!("Only new users: {}", config.only_new_users);
|
|
||||||
tracing::debug!("The config exprs: {:#?}", config.expressions);
|
tracing::debug!("The config exprs: {:#?}", config.expressions);
|
||||||
|
|
||||||
rust_i18n::set_locale(config.telegram.lang.as_str());
|
rust_i18n::set_locale(config.telegram.lang.as_str());
|
||||||
|
|
|
@ -98,7 +98,6 @@ async fn check_new_users(
|
||||||
sus_sender: Sender<ForgejoUser>,
|
sus_sender: Sender<ForgejoUser>,
|
||||||
ban_sender: Sender<ForgejoUser>,
|
ban_sender: Sender<ForgejoUser>,
|
||||||
) {
|
) {
|
||||||
let is_first_fetch = last_user_id.load(Ordering::Relaxed) == 0;
|
|
||||||
match get_new_users(
|
match get_new_users(
|
||||||
&request_client,
|
&request_client,
|
||||||
last_user_id.load(Ordering::Relaxed),
|
last_user_id.load(Ordering::Relaxed),
|
||||||
|
@ -113,11 +112,7 @@ async fn check_new_users(
|
||||||
|
|
||||||
if let Some(uid) = new_users.iter().max_by_key(|u| u.id).map(|u| u.id) {
|
if let Some(uid) = new_users.iter().max_by_key(|u| u.id).map(|u| u.id) {
|
||||||
tracing::debug!("New last user id: {uid}");
|
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 {
|
for user in new_users {
|
||||||
|
|
Loading…
Reference in a new issue