This repository has been archived by the owner on Oct 23, 2018. It is now read-only.
/
proxy.py
297 lines (230 loc) · 9.7 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env python3
"""
HTTP proxy server that supports CONNECT requests and adds support for
DNS caching.
Copyright (c) 2015 TecArt GmbH. All rights reserved.
This source code is licensed under the license found in the LICENSE file
in the root directory of this source tree.
"""
import argparse
from urllib import parse as urlparse
import twisted.web.http
import twisted.internet
from twisted.internet.protocol import Protocol, ClientFactory
from twisted.internet import address, error
from twisted.internet.endpoints import HostnameEndpoint
from twisted.web.proxy import Proxy, ProxyRequest, ProxyClientFactory
from twisted.python import log, syslog
from dnscache import DnsCache
import config
class ConnectProxyClientFactory(ProxyClientFactory):
pass
class ConnectProxyRequest(ProxyRequest):
"""ConnectProxyRequest is a factory for for HTTP proxy requests
that supports the "CONNECT" keyword for tunneling of
SSL/TLS-enabled connections.
@ivar connectedProtocol:
@ivar noisy: Controls whether this class will emit debug noise
"""
connectedProtocol = None
noisy = False
protocols = {
b'http': ProxyClientFactory,
b'https': ConnectProxyClientFactory
}
ports = {b'http': 80, b'https': 443}
def process(self):
"""Checkes the request for the CONNECT keyword and redirects
to specific functions accordingly."""
if self.method == b'CONNECT':
self.process_connect()
else:
ProxyRequest.process(self)
def fail(self, message, body):
"""Returns the HTTP 501 status code if an error occurs
@param message: Short description that will be appended to the
HTTP status code
@type message: str
@param body: The error full message that will be send to the
user
@type body: str
"""
self.setResponseCode(501, message)
self.responseHeaders.addRawHeader("Content-Type", "text/html")
self.write(body)
self.finish()
def split_hostport(self, hostport, port=443):
"""Splits the host and port that where received and sets a
default port if non was submitted
@param hostport: String containing host and port
@type hostport: str
@param port: The default port that is used if hostport contains
no port
@type port: int
@return: A tuple containing host and port
"""
parts = hostport.split(b':', 1)
if len(parts) == 2:
try:
port = int(parts[1])
except ValueError:
pass
return parts[0], port
def process_connect(self):
"""Process a client CONNECT request and connect the requested
server with the client.
"""
parsed = urlparse.urlparse(self.uri)
default_port = self.ports.get(parsed.scheme)
host, port = self.split_hostport(parsed.netloc or parsed.path,
default_port)
if port is None:
self.fail("Bad CONNECT Request",
"Unable to parse port from URI: %s" % repr(self.uri))
return
clientFactory = ConnectProxyProtocolFactory(host, port, self)
clientFactory.noisy = False
self.ip = self.reactor.dns_cache.get(host, port)
self.reactor.connectTCP(self.ip, port, clientFactory,
timeout=config.REQUEST_TIMEOUT)
class ConnectProxy(Proxy):
"""Twisted protocol that can process HTTP proxy requests with and
without the CONNECT keyword
@ivar requestFactory: L{ConnectProxyRequest} for the request to the
remote the client requested
@ivar connectedRemote: C{None} or the remote the client connects to
@ivar noisy: Controls whether this class will emit debug noise
"""
requestFactory = ConnectProxyRequest
connectedRemote = None
noisy = False
def requestDone(self, request):
"""requestDone Fires when the first client request is done.
@param request: The finished request from a client
@type request:
"""
if request.method == b'CONNECT' and self.connectedRemote is not None:
self.connectedRemote.connectedClient = self
self._handlingRequest = False
self.resumeProducing()
data = b''.join(self._dataBuffer)
self._dataBuffer = []
self.setLineMode(data)
else:
Proxy.requestDone(self, request)
def connectionLost(self, reason):
"""Fired after a connection is closed or lost. Calls
loseConnection() on the transport as well.
@param reason: Holds the reason, why the connection was lost
"""
if self.connectedRemote is not None:
self.connectedRemote.transport.loseConnection()
Proxy.connectionLost(self, reason)
def dataReceived(self, data):
"""Fires when data is received from the remote. If a CONNECT
request was issued, the data is sent directly to the client,
else it will be processed by the Proxy class from Twisted.
@param data: Data received from the remote
"""
if self.connectedRemote is None:
Proxy.dataReceived(self, data)
else:
# Once proxy is connected, forward all bytes received
# from the original client to the remote server.
self.connectedRemote.transport.write(data)
class ConnectProxyProtocol(Protocol):
"""Protocol that connects to a proxy client, client meaning the
user who will request HTTP ressources.
@ivar connectedClient: Object holding the transport and other client
information
@ivar noisy: Controls whether this class will emit debug noise
"""
connectedClient = None
noisy = False
def connectionMade(self):
"""Fires, when a client connects to the proxy server. The
method will set appropriate headers to let the client to it can
continue with it's CONNECTed channel."""
try:
self.factory.request.channel.connectedRemote = self
self.factory.request.setResponseCode(200, b"CONNECT OK")
self.factory.request.setHeader('X-Connected-IP',
self.transport.realAddress[0])
self.factory.request.finish()
except Exception as e:
log.err('ConnectProxyProtocol::connectionMade failed with %s' % e)
pass
def connectionLost(self, reason):
"""Fires, when the connection to the client is lost or closed.
Will close the transport connection to the client, if the client
object is still available.
"""
if self.connectedClient is not None:
self.connectedClient.transport.loseConnection()
def dataReceived(self, data):
"""Fired, when data from the client is received. Will
pass-through the data for CONNECTed ends, if a connection is
present"""
if self.connectedClient is not None:
# Forward all bytes from the remote server back to the
# original connected client
self.connectedClient.transport.write(data)
class ConnectProxyProtocolFactory(ClientFactory):
"""Factory for L{ConnectProxyProtocol} objects
@ivar protocol: Used protocol for connections. L{ConnectProxyProtocol}
@ivar retries: How many times a connection will time out before the
server will give up
@ivar noisy: Controls whether this class will emit debug noise
"""
protocol = ConnectProxyProtocol
retries = 5
noisy = False
def __init__(self, host, port, request):
"""Creates an instance of L{ConnectProxyProtocolFactory}
@param host: Hostname of the server to connect to
@type host: str
@param port: Port of the server to connect to
@type port: int
@param request: A request object containing information what the
client requested
"""
self.request = request
self.host = host
self.port = port
def clientConnectionFailed(self, connector, reason):
"""Fires when the connection was lost during a connection
attempt to the external ressource. If the reason for the failed
connection was a timeout, the server will try a reconnect.
@param connector: The twisted Connector object
@type connector: L{twisted.internet.tcp.Connector}
@param reason: The reason for the failed connection
"""
if reason.check(error.TimeoutError) or reason.check(error.ConnectError):
twisted.internet.reactor.dns_cache.mark_hostport_down(self.host,
self.request.ip, self.port)
if self.retries > 0:
self.retries -= 1
connector.host = twisted.internet.reactor.dns_cache.get(
self.host, self.port)
connector.connect()
return
log.err(str(reason))
self.request.fail(b"Gateway Error", str.encode(str(reason)))
class ProxyFactory(twisted.web.http.HTTPFactory):
protocol = ConnectProxy
noisy = False
if __name__ == '__main__':
import sys
if config.LOG_TYPE is 'syslog':
syslog.startLogging(prefix='tecproxy', facility=config.LOG_FACILITY)
else:
log.startLogging(sys.stderr)
factory = ProxyFactory()
twisted.internet.reactor.dns_cache = DnsCache(ttl=config.DNS_TTL)
for ip in config.LISTEN_IP:
twisted.internet.reactor.listenTCP(
config.LISTEN_PORT,
factory,
interface=ip
)
twisted.internet.reactor.run()