Refined Proxy Module + Some Cleanup in Main File

This commit is contained in:
Lukas 2020-11-01 19:04:27 +01:00
parent 1f655deeb8
commit bf3e38eaa2
2 changed files with 55 additions and 28 deletions

36
main.py
View File

@ -18,11 +18,12 @@ def initialize():
init(autoreset=True) init(autoreset=True)
def readsites(p=None): def readsites(sitepath=None):
print(f"Checking Sitedata...\n{tools.spacer}")
dod = {} dod = {}
if p: if sitepath:
sitedir = p sitedir = sitepath
else: else:
rootdir = os.path.dirname(os.path.abspath(__file__)) rootdir = os.path.dirname(os.path.abspath(__file__))
sitedir = rootdir + r"\sites" sitedir = rootdir + r"\sites"
@ -35,10 +36,12 @@ def readsites(p=None):
data = json.load(f) data = json.load(f)
dod[site] = data dod[site] = data
print(dod) # print(f"DEBUG: {dod}")
print(tools.spacer)
def proxytest(): def proxytest():
print(f"Creating Proxys...\n{tools.spacer}")
one = proxy.Proxy one = proxy.Proxy
one.name = "TEST PROXY" one.name = "TEST PROXY"
one.usr = os.getenv("PROXYUSR") one.usr = os.getenv("PROXYUSR")
@ -47,22 +50,25 @@ def proxytest():
one.port = os.getenv("PROXYPORT") one.port = os.getenv("PROXYPORT")
proxy.test(one) proxy.test(one)
# proxy.testurl(one, "http://www.whatismyproxy.com/")
print(tools.spacer)
if __name__ == "__main__": def monitortest(n=1):
print(f"Creating {tools.keyword(n)} new Monitor(s)...\n{tools.spacer}")
num_monitors = 10 for i in range(0, n):
initialize()
readsites()
proxytest()
tools.clear()
print(f"Creating {num_monitors} new Monitors...\n{tools.spacer}")
for i in range(0, num_monitors):
mon = monitor.Monitor() mon = monitor.Monitor()
mon.name = f"Monitor {i}" mon.name = f"Monitor {i}"
mon.url = "test" mon.url = "test"
print(f"{Fore.MAGENTA}{mon.name}{Style.RESET_ALL} |" print(f"{Fore.MAGENTA}{mon.name}{Style.RESET_ALL} |"
f" {tools.keyword('URL')}({mon.url}) {tools.keyword('PROXY')}({mon.proxy})") f" {tools.keyword('URL')}({mon.url}) {tools.keyword('PROXY')}({mon.proxy})")
print(tools.spacer) print(tools.spacer)
if __name__ == "__main__":
tools.clear()
initialize()
readsites()
proxytest()
monitortest()

View File

@ -1,34 +1,53 @@
import requests import requests
from requests.auth import HTTPProxyAuth
def testurl(proxy, url):
if proxy.isworking:
r = proxy.session.get(url)
data = r.text
print(data)
def test(proxy): def test(proxy):
# print(f'http://{proxy.usr}:{proxy.pwd}@{proxy.url}:{proxy.port}') # print(f'http://{proxy.usr}:{proxy.pwd}@{proxy.url}:{proxy.port}')
s = requests.Session() proxy.session = requests.Session()
url = "http://example.com" url = "https://api.ipify.org/"
proxies = {
'http': f'http://{proxy.url}:{proxy.port}',
'https': f'http://{proxy.url}:{proxy.port}'
}
auth = HTTPProxyAuth(proxy.usr, proxy.pwd)
s.proxies = proxies proxies = {
s.auth = auth 'http': f'http://{proxy.usr}:{proxy.pwd}@{proxy.url}:{proxy.port}',
'https': f'http://{proxy.usr}:{proxy.pwd}@{proxy.url}:{proxy.port}'
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
' Chrome/86.0.4240.111 Safari/537.36'
}
try: try:
r = s.get(url) proxy.session.headers = headers
# Check incoming ip
r = proxy.session.get(url)
incoming = r.text
# Check proxied ip
proxy.session.proxies = proxies
r = proxy.session.get(url)
if r.status_code == 200: if r.status_code == 200:
proxy.ip = r.text
print(f"Proxied {incoming} to {proxy.ip}")
proxy.isworking = True proxy.isworking = True
else: else:
print(r.text) print(r.text)
proxy.isworking = False proxy.isworking = False
print(f"{r.status_code} -> {proxy.isworking}") print(f"Statuscode: {r.status_code}\nGood Proxy: {proxy.isworking}")
except Exception as e: except Exception as e:
print(f"Connection error! (Check proxy)\n\n{e}") print(f"Connection Error! (check Proxy settings)\n\n{e}")
proxy.isworking = False proxy.isworking = False
@ -41,4 +60,6 @@ class Proxy:
self.port = None self.port = None
self.usr = None self.usr = None
self.pwd = None self.pwd = None
self.ip = None
self.session = None
self.isworking = None self.isworking = None