First commit
This commit is contained in:
103
plugins/proxy.py
Normal file
103
plugins/proxy.py
Normal file
@@ -0,0 +1,103 @@
|
||||
# plugins/proxy.py
|
||||
|
||||
import os
|
||||
import logging
|
||||
import random
|
||||
import requests
|
||||
import socket
|
||||
import asyncio
|
||||
import time
|
||||
import simplematrixbotlib as botlib
|
||||
|
||||
# Function to test SOCKS4 proxies
|
||||
async def test_socks4_proxy(proxy):
|
||||
try:
|
||||
start_time = time.time()
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.settimeout(5)
|
||||
s.connect((proxy.split(':')[0], int(proxy.split(':')[1])))
|
||||
latency = round((time.time() - start_time) * 1000, 2)
|
||||
logging.info(f"Tested SOCKS4 proxy {proxy}. Latency: {latency} ms")
|
||||
return True, latency
|
||||
except Exception as e:
|
||||
logging.error(f"Error testing SOCKS4 proxy {proxy}: {e}")
|
||||
return False, None
|
||||
|
||||
# Function to test SOCKS5 proxies
|
||||
async def test_socks5_proxy(proxy):
|
||||
try:
|
||||
start_time = time.time()
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.settimeout(5)
|
||||
s.connect((proxy.split(':')[0], int(proxy.split(':')[1])))
|
||||
latency = round((time.time() - start_time) * 1000, 2)
|
||||
logging.info(f"Tested SOCKS5 proxy {proxy}. Latency: {latency} ms")
|
||||
return True, latency
|
||||
except Exception as e:
|
||||
logging.error(f"Error testing SOCKS5 proxy {proxy}: {e}")
|
||||
return False, None
|
||||
|
||||
# Function to test HTTP proxies
|
||||
async def test_http_proxy(proxy):
|
||||
local_ip = requests.get("https://api.ipify.org").text
|
||||
try:
|
||||
response = requests.get("https://api.ipify.org", proxies={"http": proxy}, timeout=5).text
|
||||
if response.strip() != local_ip.strip():
|
||||
logging.info(f"Tested anonymous HTTP proxy {proxy}")
|
||||
return True
|
||||
else:
|
||||
logging.info(f"HTTP proxy {proxy} is not anonymous")
|
||||
return False
|
||||
except Exception as e:
|
||||
logging.error(f"Error testing HTTP proxy {proxy}: {e}")
|
||||
return False
|
||||
|
||||
async def handle_command(room, message, bot, PREFIX):
|
||||
"""
|
||||
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")
|
||||
try:
|
||||
# Fetch SOCKS4 proxy
|
||||
socks4_proxies = requests.get('https://raw.githubusercontent.com/TheSpeedX/SOCKS-List/master/socks4.txt', timeout=5).text.splitlines()
|
||||
random.shuffle(socks4_proxies)
|
||||
for proxy in socks4_proxies:
|
||||
is_working, latency = await test_socks4_proxy(proxy)
|
||||
if is_working:
|
||||
await bot.api.send_text_message(room.room_id, f"SOCKS4 Proxy: {proxy} - Latency: {latency} ms")
|
||||
break
|
||||
|
||||
# Fetch SOCKS5 proxy
|
||||
socks5_proxies = requests.get('https://raw.githubusercontent.com/TheSpeedX/SOCKS-List/master/socks5.txt', timeout=5).text.splitlines()
|
||||
random.shuffle(socks5_proxies)
|
||||
for proxy in socks5_proxies:
|
||||
is_working, latency = await test_socks5_proxy(proxy)
|
||||
if is_working:
|
||||
await bot.api.send_text_message(room.room_id, f"SOCKS5 Proxy: {proxy} - Latency: {latency} ms")
|
||||
break
|
||||
|
||||
# Fetch HTTP proxy
|
||||
http_proxies = requests.get('https://raw.githubusercontent.com/TheSpeedX/SOCKS-List/master/http.txt', timeout=5).text.splitlines()
|
||||
random.shuffle(http_proxies)
|
||||
for proxy in http_proxies:
|
||||
is_working = await test_http_proxy(proxy)
|
||||
if is_working:
|
||||
await bot.api.send_text_message(room.room_id, f"HTTP Proxy: {proxy}")
|
||||
break
|
||||
|
||||
logging.info("Sent proxies to the room")
|
||||
except asyncio.TimeoutError:
|
||||
await bot.api.send_text_message(room.room_id, "Failed to fetch or test proxies. The operation timed out.")
|
||||
logging.error("Proxy fetch and test operation timed out")
|
||||
except Exception as e:
|
||||
await bot.api.send_text_message(room.room_id, f"An error occurred: {e}")
|
||||
logging.error(f"Error fetching or testing proxies: {e}")
|
Reference in New Issue
Block a user