openproxyherder/scripts/dronebl/dronebl_submitter.py

98 lines
2.9 KiB
Python

import yaml
import psycopg2
import psycopg2.extras
import requests
import sys
from xml.etree import ElementTree
class DroneBL(object):
def __init__(self):
self.config = yaml.full_load(open("config.yaml"))
self.conn = psycopg2.connect(dbname=self.config["dbname"], user=self.config["dbuser"], password=self.config["dbpassword"], host=self.config["dbhost"])
def make_request(self, text):
key = self.config["dronebl_key"]
request_string = f'<?xml version="1.0"?>\n<request key="{key}">\n{text}\n</request>'
headers = {
"Content-Type": "text/xml",
"Content-Length": str(len(request_string)),
}
try:
r = requests.post("https://mirror1.dronebl.org/RPC2", data=request_string, headers=headers)
print(r.text)
except requests.exceptions.SSLError as e:
print(str(e))
sys.exit(1)
return r
def sync_db(self):
with self.conn:
with self.conn.cursor() as curs:
curs.execute(f"""select ip, exit_ip, proxy_type from proxies
where not exists (select ip from dronebl WHERE ip = proxies.ip) and
not exists (select ip from dronebl WHERE ip = proxies.exit_ip) and
status='active' limit 200""")
new_proxies = curs.fetchall()
proxies_to_test = []
for data in new_proxies:
ip = data[0]
exit_ip = data[1]
proxy_type = data[2]
if proxy_type == "http":
dbl_type = 9
elif proxy_type == "vpngate":
dbl_type = 19
elif proxy_type == "socks4" or proxy_type == "socks5":
dbl_type = 8
else:
continue
proxies_to_test.append((ip, dbl_type, "listed"))
if ip != exit_ip:
proxies_to_test.append((exit_ip, dbl_type, "listed"))
ip_addresses = []
while len(proxies_to_test) > 0:
xml_add_tags = []
xml_lookup_tags = []
if len(proxies_to_test) > 40:
proxies_to_check = proxies_to_test[:40]
proxies_to_test = proxies_to_test[40:]
else:
proxies_to_check = proxies_to_test
proxies_to_test = []
for proxy in proxies_to_check:
ip = proxy[0]
if ip not in ip_addresses:
ip_addresses.append(ip)
xml_lookup_tags.append(f"<lookup ip='{ip}' listed='1' />")
lookup_request = self.make_request("\n".join(xml_lookup_tags))
ip_address_tuple = ((ip,) for ip in ip_addresses)
insert_query = "insert into dronebl (ip) values %s"
psycopg2.extras.execute_values(curs, insert_query, ip_address_tuple)
self.conn.commit()
try:
tree = ElementTree.fromstring(lookup_request.text)
except:
tree = []
for child in tree:
if child.tag != "result":
continue
if child.attrib["listed"] == "1":
ip = child.attrib["ip"]
if ip in ip_addresses:
ip_addresses.remove(ip)
for proxy in proxies_to_check:
if proxy[0] in ip_addresses:
xml_add_tags.append(f"<add ip='{proxy[0]}' type='{proxy[1]}' />")
self.make_request("\n".join(xml_add_tags))
if __name__ == "__main__":
DroneBL().sync_db()