-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthproxy.py
More file actions
190 lines (166 loc) · 7.27 KB
/
Copy pathauthproxy.py
File metadata and controls
190 lines (166 loc) · 7.27 KB
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
186
187
188
189
190
import functools
import socketserver
import socket
import select
import base64
import getpass
import configparser
import logging
import threading
logger = logging.getLogger(__name__)
#Ref:
#https://tools.ietf.org/rfc/rfc7230.txt
class ProxyHandler(socketserver.BaseRequestHandler):
timeout_relay = 3600 #Time for neither party to send any data to timeout the connection
timeout_socket = 5 # Used by create_connection() as time for new connection to timeout
#N.B. recv() also use timeout_socket, but it is protected by select()
#N.B. Besides timeout_socket, a ?system-wide connection timeout is additionally in place
def __init__(self, parent_addr, parent_port, parent_user, parent_pass, *args, **kwargs):
self.parentProxyAdr = (parent_addr, str(parent_port))
if parent_user != '':
self.authString = ('Proxy-Authorization: Basic %s\r\n' % base64.b64encode((parent_user+":"+parent_pass).encode()).decode('ascii')).encode()
else:
self.authString = ''.encode()
super().__init__(*args, **kwargs)
def handle(self):
L = self.request
try:
R = socket.create_connection(self.parentProxyAdr, timeout=self.timeout_socket)
except socket.timeout:
print('Cannot connect to parent proxy')
return
sockets = [L,R]
last = None
intercept = True
try:
while True:
rlist, wlist, xlist = select.select(sockets, [], sockets, self.timeout_relay)
if rlist:
for source in rlist:
data = source.recv(8192)
if not data: #Connection closed
return
dest = L if source is R else R
if intercept:
if source != last:
last = source
if source is L: #If this is the first packet from Local
firstCRCL = data.find(b"\n",2);
if firstCRCL == -1:
print('Error 414: ' + repr(data))
#TODO Response 414 URI Too Long
return
try:
cmd = data[:firstCRCL].decode().strip()
#print(repr(cmd))
cmd, target, version = cmd.split(" ")
except (ValueError, UnicodeDecodeError) as e:
print('Error 400: ' + repr(data))
#TODO Response 400 Bad Request
return
if cmd == "CONNECT":
intercept = False
dest.sendall(data[:firstCRCL+1])
dest.sendall(self.authString)
dest.sendall(data[firstCRCL+1:])
if target == "/open.pac":
if cmd == "GET":
with open('pac.txt', 'rb') as f:
source.sendall(b"HTTP/1.1 200 OK\r\n\r\n") #TODO Connection-close header
data = True
while data:
data = f.read(8192)
source.sendall(data)
return
raise Exception("Should NOT happen")
continue
dest.sendall(data) #Relay as-is if not intercepted
elif xlist:
print(len(rlist)) #Theoretically can happen but i have never seen
print(len(wlist))
print(len(xlist))
print(xlist[0].recv(8192))
return
else:
print('408 Timeout: %s %s' % (cmd, target)) #Relay timeout
#TODO Response 408
return
except ConnectionResetError as e: #WinError 10054: An existing connection was forcibly closed by the remote host
return
except (socket.timeout,TimeoutError): #timeout from recv; socket.timeout -> exceeded timeout_socket, TimeoutError -> exceeded ?system-wide timeout
raise Exception("Should NOT happen") #Should be protected by 'select' function
except Exception as e:
raise
finally:
L.close()
R.close()
#TODO Consider Response 408 Request Timeout with header: close
class AuthProxy():
def __init__(self, parent_addr, parent_port, parent_user, parent_pass):
# server = socketserver.TCPServer(("localhost",8080), ProxyHandler)
self.server = socketserver.ThreadingTCPServer(
("localhost", 8888),
functools.partial(ProxyHandler, parent_addr, parent_port, parent_user, parent_pass))
server_thread = threading.Thread(target=self.server.serve_forever)
server_thread.daemon = True
server_thread.start()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.server.shutdown()
self.server.server_close()
def main():
_CONFIG_FILENAME = 'authproxy.ini'
config = configparser.ConfigParser()
config.read(_CONFIG_FILENAME)
try:
if config['DEFAULT']['address'] == '' or config['DEFAULT']['port'] == '' or config['DEFAULT']['user'] == '':
raise KeyError
except KeyError:
config['DEFAULT']['address'] = ''
config['DEFAULT']['port'] = ''
config['DEFAULT']['user'] = ''
with open(_CONFIG_FILENAME, 'w') as f:
config.write(f)
print('No valid setting from authproxy.ini. Terminating.')
return
with AuthProxy(config['DEFAULT']['address'],
config['DEFAULT']['port'],
config['DEFAULT']['user'], getpass.getpass()):
input()
if __name__ == '__main__':
main()
#Experiments
##def test():
## from threading import Thread
## import time
##
## addr = ("localhost",8888)
##
## def serve_one():
## with socket.socket() as listener:
## listener.bind(addr)
## listener.listen(1)
## with listener.accept()[0] as R:
## print(len(R.recv(8192)))
## time.sleep(10)
## pass
##
## thread = Thread(target = serve_one)
## thread.start()
## try:
## with socket.create_connection(('127.0.0.1', 8888), timeout=1) as L:
## rlist, wlist, xlist = select.select([L], [], [L], 30)
## print(rlist, wlist, xlist)
## print(len(L.recv(8192)))
## #time.sleep(30)
## #L.sendall(b'Hellow World!')
## except TimeoutError:
## print('system timeout')
## raise
## except socket.timeout:
## print('socket timeout')
## raise
## #thread.join()
##
##test()