-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_2_http_proxy_v2.py
185 lines (163 loc) · 6.94 KB
/
proxy_2_http_proxy_v2.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# qinhuawei@outlook.com
import logging,threading,socket,getpass,argparse,select,os
from urllib.parse import urlparse
logging.basicConfig(level=logging.DEBUG, format="%(asctime)-15s %(levelname).4s %(filename)s:%(lineno)-4d %(message)s")
try:
import socks
except:
logging.warning('Please install pysocks, as "python -m pip install pysocks"')
os._exit(1)
def getaddrinfo(*args):
if not args[0]:
return socket_getaddrinfo(*args)
return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]
socket.socket = socks.socksocket
socket_getaddrinfo = socket.getaddrinfo
socket.getaddrinfo = getaddrinfo
class https_proxy():
def __init__(self, client_sock, client_addr):
headers= {}
header=''
while True:
header = client_sock.recv(65535).decode()
index=header.find('\n')
if index >0:
break
first_row = header[:index]
headers['method'], headers['path'], headers['protocol'] = first_row.split()
url=urlparse(headers['path'])
if url.scheme == 'http':
#logging.info(url)
if url.path.count(':'):
ip, port = url.netloc.split(':')
else:
ip = url.netloc
port = 80
proxy_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
proxy_sock.connect((ip, int(port)))
proxy_sock.sendall(header.encode())
inputs = [proxy_sock, client_sock]
outputs = []
while True:
readable, writable, exceptional = select.select(inputs, outputs, inputs)
try:
for s in readable:
if proxy_sock == s:
client_sock.sendall(proxy_sock.recv(512))
else:
proxy_sock.sendall(client_sock.recv(512))
except:
logging.exception('------http------')
break
return
else:
logging.info(url.path)
ip, port = url.path.split(':')
try:
proxy_sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
proxy_sock.connect((ip, int(port)))
client_sock.send('HTTP/1.1 200 Connection established\r\nDate: Thu, 08 Oct 2020 08:28:59 CST\r\nVia: 1.1 NEO.QIN-PROXY\r\n\r\n'.encode())
except:
logging.exception('------------')
def client_sock_recv():
while True:
try:
buff = client_sock.recv(65535)
if buff:
proxy_sock.send(buff)
else:
return
except:
logging.exception('client_sock_recv')
return
def proxy_sock_recv():
while True:
try:
buff = proxy_sock.recv(65535)
if buff:
client_sock.send(buff)
else:
return
except:
logging.exception('proxy_sock_recv')
return
t_recv = threading.Thread(target=client_sock_recv, args=())
t_send = threading.Thread(target=proxy_sock_recv, args=())
t_recv.start()
t_send.start()
t_send.join()
t_recv.join()
client_sock.close()
proxy_sock.close()
logging.info(f'---- {client_addr} end ----')
class tcp_server():
def __init__(self, host, port, client_ips, handler=https_proxy):
server_sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server_sock.bind((host,port))
server_sock.listen()
while True:
try:
client_sock, client_addr = server_sock.accept()
client_ip, _ = client_addr
if not client_ip in client_ips:
client_sock.close()
logging.warning('{} not in {}'.format(client_ip, client_ips))
continue
logging.info(f'---- {client_addr} connected ----')
threading.Thread(target=handler, args=(client_sock, client_addr)).start()
except KeyboardInterrupt:
return
except:
logging.exception('------------')
def get_local_ip(ip, port):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
s.connect((ip, port))
return s.getsockname()[0]
except:
logging.exception(f'Connect proxy {ip}:{port} failed!')
os._exit(1)
finally:
s.close()
if __name__ == "__main__":
logging.info("--------------start--------------")
description = '''proxy_2_http_proxy: forward http(s) to http_proxy server. qinhuawei@outlook.com
Client_PC ----> Laptop ----> HTTP_PROXY ----> Internet
10.0.0.2 20.0.0.2 30.0.0.2:8888
-c 10.0.0.2 -l 0.0.0.0:1234 -p 30.0.0.2:8888
EXAMPLE: python proxy_2_http_proxy_v2.py -c 10.0.0.2,10.0.0.3 -p 30.0.0.2:8888 -u proxy_username
On client_PC:
export https_proxy=http://20.0.0.2:1234
HTTPS cert issue, please visit https://github.com/qin-neo/proxy_2_http_proxy
'''
parser_t = argparse.ArgumentParser(description=description)
parser_t.add_argument('-l', dest='local_ip_port', type=str, required=False, default='0.0.0.0:1234', help='local IP Port for client visit, default 0.0.0.0:1234')
parser_t.add_argument('-c', dest='client_ip', type=str, required=True, help='client IP, example: 10.0.0.2,10.0.0.3')
parser_t.add_argument('-p', dest='proxy_ip_port', type=str, required=True, help='The Real HTTP_PROXY IP:PORT, example:30.0.0.2:8888')
parser_t.add_argument('-u', dest='user_pass', type=str, required=False, help='Username with password input or username:password')
args = parser_t.parse_args()
local_ip, local_port = args.local_ip_port.split(':')
local_port = int(local_port)
proxy_ip, proxy_port = args.proxy_ip_port.split(':')
proxy_port = int(proxy_port)
if local_ip == '0.0.0.0':
ip_2_client = get_local_ip(proxy_ip, proxy_port)
else:
ip_2_client = local_ip
client_ips = args.client_ip.split(',')
if not '127.0.0.1' in client_ips:
client_ips.append('127.0.0.1')
if not ip_2_client in client_ips:
client_ips.append(ip_2_client)
if args.user_pass.count(':'):
usename, password = args.user_pass.split(':')
else:
usename = args.user_pass
password = getpass.getpass(prompt='Password: ', stream=None)
logging.info('============== START ==============')
socks.set_default_proxy(socks.HTTP, addr=proxy_ip, port=proxy_port, username=usename, password=password)
logging.info("Local {}:{}, remote {}".format(local_ip, local_port, args.proxy_ip_port))
if ip_2_client:
logging.info("export https_proxy=http://{}:{}".format(ip_2_client, local_port))
tcp_server(local_ip, local_port, client_ips)