forked from pir2/oprahproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.py
146 lines (122 loc) · 4.68 KB
/
proxy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
import asyncio
import logging
import random
import os.path
import base64
import csv
import oprahproxy
#from vpn import get_proxy
log = logging.getLogger(__name__)
mycountries = ['CA','US']
auth = proxies = proxy = port = country = None
pool = asyncio.Queue()
psize = 0
logging.basicConfig(format='%(asctime)s %(message)s')
log = logging.getLogger()
async def process_client(client_reader, client_writer, *, CHUNK=4096):
global psize
client_name = client_writer.get_extra_info('peername')
print('Client connected:', client_name)
log.info('Client connected: %s', client_name)
try:
remote_reader, remote_writer = pool.get_nowait()
except asyncio.QueueEmpty:
if True: #psize < pool.maxsize:
psize += 1
print('new remote connection:', psize)
log.info('new remote connection: %s', psize)
random_int = random.randint(1,len(proxies))-1
country, proxy, port = proxies[random_int]
print('COUNTRY %s IP %s PORT %s' % (country, proxy, port))
log.info('COUNTRY %s IP %s PORT %s' % (country, proxy, port))
remote_reader, remote_writer = await asyncio.open_connection(
host=proxy, port=port, ssl=True, server_hostname=country+'.opera-proxy.net')
else:
remote_reader, remote_writer = await pool.get()
headers = []
content_length = 0
while True:
line = await client_reader.readline()
if line.startswith(b'Content-Length'):
content_length = int(line.split(b' ')[1])
if line == b'\r\n':
break
headers.append(line)
headers = b''.join(headers) + auth + b'\r\n'
#print(headers)
remote_writer.write(headers)
# HTTPS tunnel
if headers.startswith(b'CONNECT'):
async def forward():
while True:
req = await client_reader.read(CHUNK)
#print('> ', req)
if not req:
break
remote_writer.write(req)
async def backward():
while True:
res = await remote_reader.read(CHUNK)
#print('< ', res)
if not res:
break
client_writer.write(res)
await asyncio.wait([asyncio.ensure_future(forward()),
asyncio.ensure_future(backward())],timeout=30)
print('tunnel done:', client_name)
else: # plain HTTP data
sent = 0
while sent < content_length:
req = await client_reader.read(CHUNK)
sent += len(req)
#print('> ', req)
remote_writer.write(req)
await remote_writer.drain()
while True:
res = await remote_reader.read(CHUNK)
#print('< ', res)
client_writer.write(res)
if len(res) < CHUNK:
break
await client_writer.drain()
client_writer.close()
pool.put((remote_reader, remote_writer))
print('Client finished:', client_name)
log.info('Client finished: %s', client_name)
def client_handler(client_reader, client_writer):
asyncio.ensure_future(process_client(client_reader, client_writer))
def check_proxy():
global proxies, auth
if os.path.isfile('proxylist.csv'):
with open('proxylist.csv', newline='') as csvfile:
i = csv.reader(csvfile, delimiter=',')
proxylist = [proxylist for proxylist in i]
proxies = [p for p in proxylist if p[0] in mycountries]
else:
proxies = [['68.71.61.22','443'],['162.253.131.60','80']]
if os.path.exists('secret') and os.path.exists('creds'):
#print('secret exists')
device_id, device_password = open('secret').read().split()
email, password = open('creds').read().split()
didp = device_id + ":" + device_password
auth = base64.b64encode(didp.encode('ascii')).decode('ascii')
else:
auth = None
return auth
def get_proxy():
print('Attempting to get proxies.')
you_get_a_proxy = oprahproxy.OprahProxy('se0306',
'7502E43F3381C82E571733A350099BB5D449DD48311839C099ADC4631BA0CC04')
you_get_a_proxy.everybody_gets_a_proxy()
print('Proxies retrieved.')
if __name__ == '__main__':
while not check_proxy():
print('No proxy information found.')
get_proxy()
auth = 'Proxy-Authorization: BASIC {}\r\n'.format(auth).encode('ascii')
loop = asyncio.get_event_loop()
server = loop.run_until_complete(asyncio.start_server(client_handler, port=8888))
print('Started HTTP proxy at', server.sockets[0].getsockname())
log.info('Started HTTP proxy at %s', server.sockets[0].getsockname())
loop.run_forever()