Skip to content

Instantly share code, notes, and snippets.

@eyedeekay
Last active May 21, 2023 21:18
Show Gist options
  • Save eyedeekay/176c04bfc97d3b0dd9ab87bfeeae9480 to your computer and use it in GitHub Desktop.
Save eyedeekay/176c04bfc97d3b0dd9ab87bfeeae9480 to your computer and use it in GitHub Desktop.
#! /usr/bin/env bash
# This is a gitlab anti-bot script for selfhosted instances.
# It was used on i2pgit.org after a bot attack.
# You **WILL** need to modify it for other instances.
# You **WILL** need to set up an API key with very dangerous perms to use it.
# It **WILL** break your shit in terrible, unfixable ways if you aren't very very careful.
# You **SHOULD NEVER RUN THIS SCRIPT WITHOUT READING IT TWICE FIRST**
# FOR REAL this is like a BIG RED BUTTON UNDER A BULLETPROOF GLASS CASE WITH TWO KEYS.
# IN CASE OF BOT ARMY consider NOT using this thing but maybe if you really have to, then ask for help, then use it.
# IT'S WEEDEATING WITH A FLAMETHROWER. IT'S CHAINSAW SURGERY. I CANNOT EMPHASIZE THAT ENOUGH.
# I HAVE NO DIRECT KNOWLEDGE OF IT KILLING ANY KITTENS BUT I CANNOT RULE IT OUT.
# That said, it did take care of the bot problem.
gitlab-api-v4 users --all | jq -c '.[]' |
while read -r row
do
echo "begin loop"
LAO=$(echo "$row" | jq .last_activity_on)
USERID=$(echo "$row" | jq .id)
IS_FOLLOWED=$(echo "$row" | jq .is_followed)
CAN_CREATE_PROJECT=$(echo "$row" | jq .can_create_project)
if [ "$IS_FOLLOWED" = "true" ]; then
continue
fi
if [ "$LAO" = "null" ]; then
echo "$USERID"
echo "Has activity: $LAO, deleting"
gitlab-api-v4 delete_user "$USERID"
continue
fi
if [ "$CAN_CREATE_PROJECT" = "false" ]; then
echo "$USERID"
echo "can create project: $CAN_CREATE_PROJECT, deleting"
gitlab-api-v4 delete_user "$USERID"
continue
fi
USER_MEMBERSHIPS=$(gitlab-api-v4 user_memberships "$USERID")
if [ "$USER_MEMBERSHIPS" = "[]" ]; then
echo "$USERID"
echo "has memberships $USER_MEMBERSHIPS, deleting"
gitlab-api-v4 delete_user "$USERID"
continue
else
echo "$USER_MEMBERSHIPS" | jq -c '.[]' |
while read -r row2
do
#echo "$row2" | jq
IS_PROJECT=$(echo "$row2" | jq .source_type)
echo "project is: $IS_PROJECT"
if [ "$IS_PROJECT" = "\"Project\"" ]; then
NAME=$(echo "$row2" | jq .source_name)
echo "project name is: $NAME"
I2P_RELATED=$(echo "$NAME" | grep -i i2p)
if [ -n "$I2P_RELATED" ]; then
echo "$NAME contained an I2P related term" | tee i2p.tmp
break
fi
fi
if [ "$IS_PROJECT" = "\"Namespace\"" ]; then
NAME=$(echo "$row2" | jq .source_name)
echo "group name is: $NAME"
PRID=$(echo "$row2" | jq .source_id)
I2P_RELATED=$(echo "$NAME" | grep -i i2p)
if [ -z "$I2P_RELATED" ]; then
echo "Deleting non-I2P related group"
gitlab-api-v4 delete_group "$PRID"
break
fi
fi
done
if [ -f i2p.tmp ]; then
echo "$USERID"
echo "has i2p related project."
rm i2p.tmp
else
echo "$USERID"
echo "has no i2p related projects, deleting"
gitlab-api-v4 delete_user "$USERID"
fi
fi
echo ""
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment