Security fixes
This commit is contained in:
+10
-72
@@ -2,8 +2,6 @@
|
||||
This plugin provides a command to get random SOCKS5 proxies.
|
||||
"""
|
||||
|
||||
# plugins/proxy.py
|
||||
|
||||
import os
|
||||
import logging
|
||||
import random
|
||||
@@ -14,10 +12,11 @@ from datetime import datetime, timedelta
|
||||
import concurrent.futures
|
||||
import simplematrixbotlib as botlib
|
||||
import sqlite3
|
||||
import ipaddress
|
||||
|
||||
from plugins.utils import is_public_destination
|
||||
|
||||
# Constants
|
||||
SOCKS5_LIST_URL = 'https://raw.githubusercontent.com/TheSpeedX/SOCKS-List/master/socks5.txt'
|
||||
# SOCKS5_LIST_URL = 'https://raw.githubusercontent.com/proxifly/free-proxy-list/main/proxies/protocols/socks5/data.txt'
|
||||
MAX_TRIES = 64
|
||||
PROXY_LIST_FILENAME = 'socks5.txt'
|
||||
PROXY_LIST_EXPIRATION = timedelta(hours=8)
|
||||
@@ -25,19 +24,10 @@ MAX_THREADS = 128
|
||||
PROXIES_DB_FILE = 'proxies.db'
|
||||
MAX_PROXIES_IN_DB = 10
|
||||
|
||||
# Setup verbose logging
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||
|
||||
def test_proxy(proxy):
|
||||
"""
|
||||
Test a SOCKS5 proxy and return the outcome.
|
||||
|
||||
Args:
|
||||
proxy (str): The SOCKS5 proxy address in the format 'ip:port'.
|
||||
|
||||
Returns:
|
||||
tuple: (bool: success, str: proxy, int: latency)
|
||||
"""
|
||||
"""Test a SOCKS5 proxy and return the outcome."""
|
||||
try:
|
||||
ip, port = proxy.split(':')
|
||||
logging.info(f"Testing SOCKS5 proxy: {ip}:{port}")
|
||||
@@ -53,14 +43,7 @@ def test_proxy(proxy):
|
||||
except Exception as e:
|
||||
return False, proxy, None
|
||||
|
||||
|
||||
async def download_proxy_list():
|
||||
"""
|
||||
Download the SOCKS5 proxy list file if it doesn't already exist or if it's expired.
|
||||
|
||||
Returns:
|
||||
bool: True if the proxy list is downloaded or up-to-date, False otherwise.
|
||||
"""
|
||||
try:
|
||||
if not os.path.exists(PROXY_LIST_FILENAME) or \
|
||||
datetime.now() - datetime.fromtimestamp(os.path.getctime(PROXY_LIST_FILENAME)) > PROXY_LIST_EXPIRATION:
|
||||
@@ -77,15 +60,7 @@ async def download_proxy_list():
|
||||
logging.error(f"Error downloading proxy list: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def check_db_for_proxy():
|
||||
"""
|
||||
Check the proxies database for a working proxy.
|
||||
If found, test the proxy and remove it from the database if it doesn't work.
|
||||
|
||||
Returns:
|
||||
str or None: The working proxy if found, None otherwise.
|
||||
"""
|
||||
try:
|
||||
with sqlite3.connect(PROXIES_DB_FILE) as conn:
|
||||
cursor = conn.cursor()
|
||||
@@ -115,15 +90,7 @@ def check_db_for_proxy():
|
||||
logging.error(f"Error checking proxies database: {e}")
|
||||
return None, None
|
||||
|
||||
|
||||
def save_proxy_to_db(proxy, latency):
|
||||
"""
|
||||
Save a working proxy to the proxies database.
|
||||
|
||||
Args:
|
||||
proxy (str): The working proxy to be saved.
|
||||
latency (int): Latency of the proxy.
|
||||
"""
|
||||
try:
|
||||
with sqlite3.connect(PROXIES_DB_FILE) as conn:
|
||||
cursor = conn.cursor()
|
||||
@@ -140,44 +107,25 @@ def save_proxy_to_db(proxy, latency):
|
||||
except Exception as e:
|
||||
logging.error(f"Error saving proxy to database: {e}")
|
||||
|
||||
|
||||
async def handle_command(room, message, bot, prefix, config):
|
||||
"""
|
||||
Function to handle the !proxy command.
|
||||
|
||||
Args:
|
||||
room (Room): The Matrix room where the command was invoked.
|
||||
message (RoomMessage): The message object containing the command.
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
match = botlib.MessageMatch(room, message, bot, prefix)
|
||||
if match.is_not_from_this_bot() and match.prefix() and match.command("proxy"):
|
||||
logging.info("Received !proxy command")
|
||||
|
||||
# Check database for a working proxy
|
||||
working_proxy, latency = check_db_for_proxy()
|
||||
if working_proxy:
|
||||
await bot.api.send_markdown_message(room.room_id,
|
||||
f"✅ Using cached working SOCKS5 Proxy: **{working_proxy}** - Latency: **{latency} ms**")
|
||||
logging.info(f"Using cached working SOCKS5 proxy {working_proxy}")
|
||||
return
|
||||
|
||||
# Download proxy list if needed
|
||||
else:
|
||||
if not await download_proxy_list():
|
||||
await bot.api.send_markdown_message(room.room_id, "Error downloading proxy list")
|
||||
logging.error("Error downloading proxy list")
|
||||
return
|
||||
|
||||
try:
|
||||
# Read proxies from file
|
||||
with open(PROXY_LIST_FILENAME, 'r') as f:
|
||||
socks5_proxies = [line.replace("socks5://", "") for line in f.read().splitlines()]
|
||||
# Filter out private/internal proxies before testing
|
||||
socks5_proxies = [p for p in socks5_proxies if is_public_destination(p.split(':')[0])]
|
||||
random.shuffle(socks5_proxies)
|
||||
|
||||
# Test proxies concurrently
|
||||
tested_proxies = 0
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=MAX_THREADS) as executor:
|
||||
futures = []
|
||||
@@ -188,36 +136,26 @@ async def handle_command(room, message, bot, prefix, config):
|
||||
if success:
|
||||
await bot.api.send_markdown_message(room.room_id,
|
||||
f"✅ Anonymous SOCKS5 Proxy: **{proxy}** - Latency: **{latency} ms**")
|
||||
logging.info(f"Sent SOCKS5 proxy {proxy} to the room")
|
||||
save_proxy_to_db(proxy, latency) # Save working proxy to the database
|
||||
save_proxy_to_db(proxy, latency)
|
||||
tested_proxies += 1
|
||||
if tested_proxies >= MAX_PROXIES_IN_DB:
|
||||
break # Stop testing proxies once MAX_PROXIES_IN_DB are saved to the database
|
||||
|
||||
# Check database for a working proxy after testing
|
||||
break
|
||||
working_proxy, latency = check_db_for_proxy()
|
||||
if working_proxy:
|
||||
await bot.api.send_markdown_message(room.room_id,
|
||||
f"✅ Using cached working SOCKS5 Proxy: **{working_proxy}** - Latency: **{latency} ms**")
|
||||
logging.info(f"Using cached working SOCKS5 proxy {working_proxy}")
|
||||
|
||||
else:
|
||||
# If no working proxy found after testing
|
||||
await bot.api.send_markdown_message(room.room_id, "❌ No working anonymous SOCKS5 proxy found")
|
||||
logging.info("No working anonymous SOCKS5 proxy found")
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Error handling !proxy command: {e}")
|
||||
await bot.api.send_markdown_message(room.room_id, "❌ Error handling !proxy command")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Plugin Metadata
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
__version__ = "1.0.0"
|
||||
__version__ = "1.0.1"
|
||||
__author__ = "Funguy Bot"
|
||||
__description__ = "Working SOCKS5 proxy finder"
|
||||
__description__ = "Working SOCKS5 proxy finder (SSRF‑safe)"
|
||||
__help__ = """
|
||||
<details>
|
||||
<summary><strong>!proxy</strong> – Random working SOCKS5 proxy</summary>
|
||||
|
||||
Reference in New Issue
Block a user