@@ -125,7 +125,7 @@ def _scheduler(x):
125
125
return _scheduler
126
126
127
127
128
- class IPBlacklistingResolver :
128
+ class _IPBlacklistingResolver :
129
129
"""
130
130
A proxy for reactor.nameResolver which only produces non-blacklisted IP
131
131
addresses, preventing DNS rebinding attacks on URL preview.
@@ -199,6 +199,35 @@ def resolutionComplete() -> None:
199
199
return r
200
200
201
201
202
+ @implementer (IReactorPluggableNameResolver )
203
+ class BlacklistingReactorWrapper :
204
+ """
205
+ A Reactor wrapper which will prevent DNS resolution to blacklisted IP
206
+ addresses, to prevent DNS rebinding.
207
+ """
208
+
209
+ def __init__ (
210
+ self ,
211
+ reactor : IReactorPluggableNameResolver ,
212
+ ip_whitelist : Optional [IPSet ],
213
+ ip_blacklist : IPSet ,
214
+ ):
215
+ self ._reactor = reactor
216
+
217
+ # We need to use a DNS resolver which filters out blacklisted IP
218
+ # addresses, to prevent DNS rebinding.
219
+ self ._nameResolver = _IPBlacklistingResolver (
220
+ self ._reactor , ip_whitelist , ip_blacklist
221
+ )
222
+
223
+ def __getattr__ (self , attr : str ) -> Any :
224
+ # Passthrough to the real reactor except for the DNS resolver.
225
+ if attr == "nameResolver" :
226
+ return self ._nameResolver
227
+ else :
228
+ return getattr (self ._reactor , attr )
229
+
230
+
202
231
class BlacklistingAgentWrapper (Agent ):
203
232
"""
204
233
An Agent wrapper which will prevent access to IP addresses being accessed
@@ -260,8 +289,7 @@ def __init__(
260
289
treq_args : Dict [str , Any ] = {},
261
290
ip_whitelist : Optional [IPSet ] = None ,
262
291
ip_blacklist : Optional [IPSet ] = None ,
263
- http_proxy : Optional [bytes ] = None ,
264
- https_proxy : Optional [bytes ] = None ,
292
+ use_proxy : bool = False ,
265
293
):
266
294
"""
267
295
Args:
@@ -271,8 +299,8 @@ def __init__(
271
299
we may not request.
272
300
ip_whitelist: The whitelisted IP addresses, that we can
273
301
request if it were otherwise caught in a blacklist.
274
- http_proxy: proxy server to use for http connections. host[:port]
275
- https_proxy: proxy server to use for https connections. host[:port]
302
+ use_proxy: Whether proxy settings should be discovered and used
303
+ from conventional environment variables.
276
304
"""
277
305
self .hs = hs
278
306
@@ -292,22 +320,11 @@ def __init__(
292
320
self .user_agent = self .user_agent .encode ("ascii" )
293
321
294
322
if self ._ip_blacklist :
295
- real_reactor = hs .get_reactor ()
296
323
# If we have an IP blacklist, we need to use a DNS resolver which
297
324
# filters out blacklisted IP addresses, to prevent DNS rebinding.
298
- nameResolver = IPBlacklistingResolver (
299
- real_reactor , self ._ip_whitelist , self ._ip_blacklist
325
+ self . reactor = BlacklistingReactorWrapper (
326
+ hs . get_reactor () , self ._ip_whitelist , self ._ip_blacklist
300
327
)
301
-
302
- @implementer (IReactorPluggableNameResolver )
303
- class Reactor :
304
- def __getattr__ (_self , attr ):
305
- if attr == "nameResolver" :
306
- return nameResolver
307
- else :
308
- return getattr (real_reactor , attr )
309
-
310
- self .reactor = Reactor ()
311
328
else :
312
329
self .reactor = hs .get_reactor ()
313
330
@@ -323,11 +340,11 @@ def __getattr__(_self, attr):
323
340
324
341
self .agent = ProxyAgent (
325
342
self .reactor ,
343
+ hs .get_reactor (),
326
344
connectTimeout = 15 ,
327
345
contextFactory = self .hs .get_http_client_context_factory (),
328
346
pool = pool ,
329
- http_proxy = http_proxy ,
330
- https_proxy = https_proxy ,
347
+ use_proxy = use_proxy ,
331
348
)
332
349
333
350
if self ._ip_blacklist :
0 commit comments