2023-07-18 14:38:32 +02:00
|
|
|
"""Security checks for the API. Checks if the IP is masked etc."""
|
|
|
|
|
2023-06-23 02:18:28 +02:00
|
|
|
import os
|
2023-07-19 23:51:28 +02:00
|
|
|
import asyncio
|
2023-06-23 02:18:28 +02:00
|
|
|
|
|
|
|
from rich import print
|
2023-07-19 23:51:28 +02:00
|
|
|
from tests import check_proxy
|
2023-06-28 15:21:14 +02:00
|
|
|
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
load_dotenv()
|
2023-07-19 23:51:28 +02:00
|
|
|
is_proxy_enabled = bool(os.getenv('PROXY_HOST', None))
|
2023-06-23 02:18:28 +02:00
|
|
|
|
|
|
|
class InsecureIPError(Exception):
|
|
|
|
"""Raised when the IP address of the server is not secure."""
|
|
|
|
|
2023-07-19 23:51:28 +02:00
|
|
|
async def ip_protection_check():
|
2023-06-23 02:18:28 +02:00
|
|
|
"""Makes sure that the actual server IP address is not exposed to the public."""
|
|
|
|
|
2023-07-19 23:51:28 +02:00
|
|
|
if not is_proxy_enabled:
|
|
|
|
print('[yellow]WARN: The proxy is not enabled. \
|
|
|
|
Skipping IP protection check.[/yellow]')
|
|
|
|
return True
|
|
|
|
|
2023-06-23 02:18:28 +02:00
|
|
|
actual_ips = os.getenv('ACTUAL_IPS', '').split()
|
|
|
|
|
|
|
|
if actual_ips:
|
2023-07-19 23:51:28 +02:00
|
|
|
# run the async function check_proxy() and get its result
|
|
|
|
response_ip = await check_proxy()
|
2023-06-23 02:18:28 +02:00
|
|
|
|
|
|
|
for actual_ip in actual_ips:
|
2023-07-19 23:51:28 +02:00
|
|
|
if actual_ip in response_ip:
|
2023-06-28 15:21:14 +02:00
|
|
|
raise InsecureIPError(f'IP pattern "{actual_ip}" is in the values of ACTUAL_IPS of the\
|
2023-06-23 02:18:28 +02:00
|
|
|
.env file. Enable a VPN or proxy to continue.')
|
|
|
|
|
|
|
|
if is_proxy_enabled:
|
2023-07-19 23:51:28 +02:00
|
|
|
print(f'[green]GOOD: The IP "{response_ip}" was detected, which seems to be a proxy. Great![/green]')
|
|
|
|
return True
|
2023-06-23 02:18:28 +02:00
|
|
|
|
|
|
|
else:
|
2023-07-19 23:51:28 +02:00
|
|
|
print('[yellow]WARN: ACTUAL_IPS is not set in the .env file or empty.\
|
2023-06-23 02:18:28 +02:00
|
|
|
This means that the real IP of the server could be exposed. If you\'re using something\
|
|
|
|
like Cloudflare or Repl.it, you can ignore this warning.[/yellow]')
|
2023-06-28 15:21:14 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
ip_protection_check()
|