|
1 | | -# 🚀 Discord Bot IP Whitelister - Linux Service Setup |
2 | 1 |
|
3 | | -This guide explains how to **install, configure, and run** the Discord bot as a **systemd service** on a Linux server. |
| 2 | +# Discord Bot IP Whitelister |
4 | 3 |
|
| 4 | +A Discord bot that allows users to manage their IP whitelisting and bans directly from Discord. With the `/whitelist` command, players can securely add their IP addresses to the UFW firewall of a game server, preventing unauthorized access and mitigating DDoS attacks. |
| 5 | +Admins have access to the `/ban` command, which allows them to ban a user by their Discord ID, preventing them from whitelisting their IP. This bot is designed for servers using IP-based whitelisting, such as FiveM, Minecraft, Rust, and more. |
5 | 6 |
|
6 | | -## 📌 Prerequisites |
7 | | -Before proceeding, ensure you have: |
8 | | -- A **Linux server** (Ubuntu, Debian, CentOS, etc.) |
9 | | -- **Go installed** (for building the binary) |
10 | | -- **Systemd** (default in most modern Linux distributions) |
11 | | -- **A valid `.env` file** with bot credentials |
12 | 7 |
|
| 8 | +## Features |
| 9 | +- **Slash command `/whitelist (ip)`**: Players can whitelist their IPs securely. |
| 10 | +- **Slash command `/ban (user_id)`**: Admins can prevent specific users from adding their IP. |
| 11 | +- **Prevents unauthorized access**: Only approved users can connect. |
| 12 | +- **DDoS Mitigation**: Reduces attack surface by limiting access to verified players. |
| 13 | +- **Multi-Game Support**: Works with FiveM, Minecraft, Rust, and any server using UFW. |
| 14 | +- **SQLite Database**: Keeps a log of whitelisted users and their IPs for traceability. |
| 15 | +- **Systemd Service Support**: Runs as a background service for automatic startup and reliability. |
| 16 | +- **Secure & Configurable**: Uses environment variables for easy customization. |
13 | 17 |
|
14 | 18 |
|
15 | | -## 🛠️ Step 1: Build the Go Application |
16 | 19 |
|
17 | | -If you haven’t already compiled the bot, run: |
| 20 | +## Installation |
18 | 21 |
|
19 | | -```sh |
20 | | -go build -o bot-app |
21 | | -``` |
22 | | - |
23 | | -Move the binary to a suitable directory: |
| 22 | +### 1. One-Line Install Command |
| 23 | +Run this command to **automatically download and install** the bot: |
24 | 24 |
|
25 | 25 | ```sh |
26 | | -sudo mv bot-app /usr/local/bin/ |
| 26 | +curl -sSL https://raw.githubusercontent.com/geekloper/discord-bot-ip-whitelister/main/install.sh | sudo bash |
27 | 27 | ``` |
28 | 28 |
|
29 | | -Ensure the binary has execution permissions: |
30 | | - |
31 | | -```sh |
32 | | -sudo chmod +x /usr/local/bin/bot-app |
33 | | -``` |
| 29 | +This script will: |
| 30 | +- Checks if **UFW** and **SQLite3** are installed (prompts if missing). |
| 31 | +- Creates a dedicated system user (`whitelistbot`). |
| 32 | +- Downloads the latest bot binary from GitHub. |
| 33 | +- Installs the bot to `/usr/local/bin/`. |
| 34 | +- Sets up configuration files in `/etc/ip_whitelister_bot/`. |
| 35 | +- Creates a SQLite database in `/var/lib/ip_whitelister_bot/`. |
| 36 | +- Configures and enables a **systemd service** for auto-restart. |
34 | 37 |
|
35 | 38 | --- |
36 | 39 |
|
37 | | -## 📁 Step 2: Create an Environment Variables File |
38 | | - |
39 | | -To securely store environment variables, create a dedicated environment file: |
| 40 | +### 2. Configure the Bot |
| 41 | +Edit the `.env` file to add your Discord bot credentials: |
40 | 42 |
|
41 | 43 | ```sh |
42 | | -sudo nano /etc/default/discord-bot |
| 44 | +sudo vi /etc/whitelist_bot/.env |
43 | 45 | ``` |
44 | 46 |
|
45 | | -Add your required environment variables: |
46 | | - |
| 47 | +Example `.env` file: |
47 | 48 | ```ini |
48 | | -BOT_TOKEN=your_bot_token_here |
49 | | -BOT_GUILD_ID=your_guild_id |
50 | | -SERVICE_PORTS=80,443 |
51 | | -DELETE_COMMADS=true |
52 | | -``` |
53 | | - |
54 | | -Ensure the file is readable by the service user: |
55 | | - |
56 | | -```sh |
57 | | -sudo chmod 600 /etc/default/discord-bot |
| 49 | +BOT_TOKEN=your-bot-token |
| 50 | +BOT_GUILD_ID=your-guild-id |
| 51 | +ADMIN_IDS=1234567890,0987654321 |
| 52 | +DELETE_COMMANDS=true |
| 53 | +SERVICES=80/tcp,443/tcp |
| 54 | +DEBUG=false |
| 55 | +UFW_PATH=/usr/sbin/ufw |
| 56 | +DB_PATH=/var/lib/ip_whitelister_bot/whitelist.db |
58 | 57 | ``` |
59 | 58 |
|
60 | 59 | --- |
61 | 60 |
|
62 | | -## ⚙️ Step 3: Create a Systemd Service File |
63 | | - |
64 | | -To manage the bot as a Linux service, create a `systemd` unit file: |
| 61 | +### 3. Start the Bot |
| 62 | +Once configured, start and enable the bot: |
65 | 63 |
|
66 | 64 | ```sh |
67 | | -sudo nano /etc/systemd/system/discord-bot.service |
68 | | -``` |
69 | | - |
70 | | -Paste the following configuration: |
71 | | - |
72 | | -```ini |
73 | | -[Unit] |
74 | | -Description=Discord Bot IP Whitelister |
75 | | -After=network.target |
76 | | - |
77 | | -[Service] |
78 | | -Type=simple |
79 | | -User=your_linux_user # Change this to the appropriate user |
80 | | -Group=your_linux_user |
81 | | -WorkingDirectory=/usr/local/bin |
82 | | -ExecStart=/usr/local/bin/bot-app |
83 | | -Restart=always |
84 | | -RestartSec=5 |
85 | | -EnvironmentFile=/etc/default/discord-bot # Load environment variables |
86 | | - |
87 | | -[Install] |
88 | | -WantedBy=multi-user.target |
| 65 | +sudo systemctl restart ip_whitelister_bot |
| 66 | +sudo systemctl enable ip_whitelister_bot |
89 | 67 | ``` |
90 | 68 |
|
91 | | -Save and exit. |
92 | | - |
93 | | -Reload `systemd` to apply the changes: |
| 69 | +Check if the bot is running: |
94 | 70 |
|
95 | 71 | ```sh |
96 | | -sudo systemctl daemon-reload |
| 72 | +sudo systemctl status ip_whitelister_bot |
97 | 73 | ``` |
98 | 74 |
|
99 | | ---- |
100 | | - |
101 | | -## 🚀 Step 4: Start and Enable the Service |
102 | | - |
103 | | -Start the bot service: |
| 75 | +View logs: |
104 | 76 |
|
105 | 77 | ```sh |
106 | | -sudo systemctl start discord-bot |
| 78 | +sudo journalctl -u ip_whitelister_bot -f |
107 | 79 | ``` |
108 | 80 |
|
109 | | -Enable it to run on boot: |
| 81 | +## Usage |
110 | 82 |
|
111 | | -```sh |
112 | | -sudo systemctl enable discord-bot |
113 | | -``` |
| 83 | +## 🛠️ Usage |
114 | 84 |
|
115 | | -Check if the service is running: |
| 85 | +### ✅ Whitelisting an IP |
| 86 | +Users can whitelist their IP via the Discord command: |
116 | 87 |
|
117 | | -```sh |
118 | | -sudo systemctl status discord-bot |
119 | 88 | ``` |
120 | | - |
121 | | -You should see output similar to: |
122 | | - |
123 | | -```sh |
124 | | -● discord-bot.service - Discord Bot IP Whitelister |
125 | | - Loaded: loaded (/etc/systemd/system/discord-bot.service; enabled; vendor preset: enabled) |
126 | | - Active: active (running) since Sun 2025-02-08 14:00:00 UTC; 2s ago |
127 | | - Main PID: 12345 (bot-app) |
128 | | - CGroup: /system.slice/discord-bot.service |
| 89 | +/whitelist 192.168.1.1 |
129 | 90 | ``` |
130 | 91 |
|
131 | | ---- |
132 | | - |
133 | | -## 📊 Step 5: Viewing Logs |
| 92 | +This will automatically add the IP to UFW and store it in the database. |
134 | 93 |
|
135 | | -To monitor logs in real time: |
| 94 | +### ❌ Banning a User |
| 95 | +Admins can **ban a user by Discord ID** to prevent them from using `/whitelist`: |
136 | 96 |
|
137 | | -```sh |
138 | | -sudo journalctl -u discord-bot -f |
139 | 97 | ``` |
140 | | - |
141 | | -To view logs for the last hour: |
142 | | - |
143 | | -```sh |
144 | | -sudo journalctl -u discord-bot --since "1 hour ago" |
| 98 | +/ban 1234567890 |
145 | 99 | ``` |
146 | 100 |
|
147 | | ---- |
148 | | - |
149 | | -## 🔄 Managing the Service |
| 101 | +Banning removes their ability to whitelist IPs & deny their ips in UFW. |
150 | 102 |
|
151 | | -Restart the bot: |
152 | 103 |
|
153 | | -```sh |
154 | | -sudo systemctl restart discord-bot |
155 | | -``` |
156 | 104 |
|
157 | | -Stop the bot: |
| 105 | +## Uninstallation |
| 106 | +To remove the bot completely: |
158 | 107 |
|
159 | 108 | ```sh |
160 | | -sudo systemctl stop discord-bot |
| 109 | +sudo systemctl stop ip_whitelister_bot |
| 110 | +sudo systemctl disable ip_whitelister_bot |
| 111 | +sudo rm -rf /usr/local/bin/ip_whitelister_bot /etc/ip_whitelister_bot /var/lib/ip_whitelister_bot /etc/systemd/system/ip_whitelister_bot.service |
| 112 | +sudo systemctl daemon-reload |
161 | 113 | ``` |
162 | 114 |
|
163 | | -Disable it from starting on boot: |
| 115 | +--- |
164 | 116 |
|
165 | | -```sh |
166 | | -sudo systemctl disable discord-bot |
167 | | -``` |
| 117 | +## Contributing |
| 118 | +Contributions are welcome! If you find a bug or want to improve the bot, feel free to submit an issue or pull request. |
168 | 119 |
|
169 | 120 | --- |
170 | 121 |
|
171 | | -## 🛠️ Troubleshooting |
172 | | - |
173 | | -### ❌ The service doesn’t start |
174 | | -- Check logs using: |
175 | | - ```sh |
176 | | - sudo journalctl -u discord-bot -xe |
177 | | - ``` |
178 | | -- Ensure the binary is executable: |
179 | | - ```sh |
180 | | - sudo chmod +x /usr/local/bin/bot-app |
181 | | - ``` |
182 | | -- Make sure the environment file exists and has correct permissions: |
183 | | - ```sh |
184 | | - ls -l /etc/default/discord-bot |
185 | | - ``` |
186 | | - |
187 | | -### 🔄 Changes not applying after updating `.env` |
188 | | -- Restart the service: |
189 | | - ```sh |
190 | | - sudo systemctl restart discord-bot |
191 | | - ``` |
192 | | -- If still not working, reload `systemd`: |
193 | | - ```sh |
194 | | - sudo systemctl daemon-reload |
195 | | - sudo systemctl restart discord-bot |
196 | | - ``` |
197 | | - |
| 122 | +## License |
| 123 | +This project is open-source under the MIT License. |
0 commit comments