@@ -101,7 +101,7 @@ class HTTP20Connection(object):
101101
102102 def __init__ (self , host , port = None , secure = None , window_manager = None ,
103103 enable_push = False , ssl_context = None , proxy_host = None ,
104- proxy_port = None , force_proto = None , proxy_headers = None ,
104+ proxy_port = None , proxy_type = None , force_proto = None , proxy_headers = None ,
105105 timeout = None , ** kwargs ):
106106 """
107107 Creates an HTTP/2 connection to a specific server.
@@ -126,11 +126,13 @@ def __init__(self, host, port=None, secure=None, window_manager=None,
126126 self .proxy_host , self .proxy_port = to_host_port_tuple (
127127 proxy_host , default_port = 8080
128128 )
129+ self .proxy_type = proxy_type
129130 elif proxy_host :
130- self .proxy_host , self .proxy_port = proxy_host , proxy_port
131+ self .proxy_host , self .proxy_port , self . proxy_type = proxy_host , proxy_port , proxy_type
131132 else :
132133 self .proxy_host = None
133134 self .proxy_port = None
135+ self .proxy_type = None
134136 self .proxy_headers = proxy_headers
135137
136138 #: The size of the in-memory buffer used to store data from the
@@ -353,22 +355,49 @@ def connect(self):
353355 connect_timeout = self ._timeout
354356 read_timeout = self ._timeout
355357
356- if self .proxy_host and self .secure :
357- # Send http CONNECT method to a proxy and acquire the socket
358- sock = _create_tunnel (
359- self .proxy_host ,
360- self .proxy_port ,
361- self .host ,
362- self .port ,
363- proxy_headers = self .proxy_headers ,
364- timeout = self ._timeout
365- )
366- elif self .proxy_host :
367- # Simple http proxy
368- sock = socket .create_connection (
369- (self .proxy_host , self .proxy_port ),
370- timeout = connect_timeout
371- )
358+ if self .proxy_host :
359+ if self .proxy_type .startswith ("socks" ):
360+ import socks
361+ rdns = (self .proxy_type [- 1 ]== "h" )
362+ # any error will result in silently connecting without a proxy.
363+ # IDK why it is done this way.
364+ if not rdns :
365+ raise ValueError ("RDNS is disabled. Proxying dns queries is disabled. NSA is spying you." )
366+ if rdns and self .proxy_type .startswith ("socks4" ):
367+ raise ValueError ("RDNS is not supported for socks4. socks.create_connection ignores it silently." )
368+ if not isinstance (self .proxy_host , str ):
369+ raise ValueError ("self.proxy_host" , repr (self .proxy_host ), "is not str" )
370+ if not isinstance (self .proxy_port , int ):
371+ raise ValueError ("self.proxy_port" , repr (self .proxy_port ), "is not int" )
372+ socks_version_char = self .proxy_type [5 ]
373+ sock = socks .create_connection (
374+ (self .host , self .port ),
375+ proxy_type = getattr (socks , "PROXY_TYPE_SOCKS" + socks_version_char ),
376+ proxy_addr = self .proxy_host ,
377+ proxy_port = self .proxy_port ,
378+ #proxy_username=username,
379+ #proxy_password=password,
380+ proxy_rdns = rdns ,
381+ )
382+ #sock.getsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
383+ elif self .proxy_host and self .secure :
384+ # Send http CONNECT method to a proxy and acquire the socket
385+ sock = _create_tunnel (
386+ self .proxy_host ,
387+ self .proxy_port ,
388+ self .host ,
389+ self .port ,
390+ proxy_headers = self .proxy_headers ,
391+ timeout = self ._timeout
392+ )
393+ elif self .proxy_host :
394+ # Simple http proxy
395+ sock = socket .create_connection (
396+ (self .proxy_host , self .proxy_port ),
397+ timeout = connect_timeout
398+ )
399+ else :
400+ raise Exception ("Unsupported proxy type: " + repr (proxy_type ))
372401 else :
373402 sock = socket .create_connection ((self .host , self .port ),
374403 timeout = connect_timeout )
@@ -403,7 +432,7 @@ def _connect_upgrade(self, sock):
403432 with self ._conn as conn :
404433 conn .initiate_upgrade_connection ()
405434 conn .update_settings (
406- {h2 .settings .ENABLE_PUSH : int (self ._enable_push )}
435+ {h2 .settings .SettingCodes . ENABLE_PUSH : int (self ._enable_push )}
407436 )
408437 self ._send_outstanding_data ()
409438
@@ -424,7 +453,7 @@ def _send_preamble(self):
424453 with self ._conn as conn :
425454 conn .initiate_connection ()
426455 conn .update_settings (
427- {h2 .settings .ENABLE_PUSH : int (self ._enable_push )}
456+ {h2 .settings .SettingCodes . ENABLE_PUSH : int (self ._enable_push )}
428457 )
429458 self ._send_outstanding_data ()
430459
0 commit comments