_http.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. """
  2. _http.py
  3. websocket - WebSocket client library for Python
  4. Copyright 2021 engn33r
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. """
  15. import errno
  16. import os
  17. import socket
  18. import sys
  19. from ._exceptions import *
  20. from ._logging import *
  21. from ._socket import*
  22. from ._ssl_compat import *
  23. from ._url import *
  24. from base64 import encodebytes as base64encode
  25. __all__ = ["proxy_info", "connect", "read_headers"]
  26. try:
  27. from python_socks.sync import Proxy
  28. from python_socks._errors import *
  29. from python_socks._types import ProxyType
  30. HAVE_PYTHON_SOCKS = True
  31. except:
  32. HAVE_PYTHON_SOCKS = False
  33. class ProxyError(Exception):
  34. pass
  35. class ProxyTimeoutError(Exception):
  36. pass
  37. class ProxyConnectionError(Exception):
  38. pass
  39. class proxy_info:
  40. def __init__(self, **options):
  41. self.proxy_host = options.get("http_proxy_host", None)
  42. if self.proxy_host:
  43. self.proxy_port = options.get("http_proxy_port", 0)
  44. self.auth = options.get("http_proxy_auth", None)
  45. self.no_proxy = options.get("http_no_proxy", None)
  46. self.proxy_protocol = options.get("proxy_type", "http")
  47. # Note: If timeout not specified, default python-socks timeout is 60 seconds
  48. self.proxy_timeout = options.get("timeout", None)
  49. if self.proxy_protocol not in ['http', 'socks4', 'socks4a', 'socks5', 'socks5h']:
  50. raise ProxyError("Only http, socks4, socks5 proxy protocols are supported")
  51. else:
  52. self.proxy_port = 0
  53. self.auth = None
  54. self.no_proxy = None
  55. self.proxy_protocol = "http"
  56. def _start_proxied_socket(url, options, proxy):
  57. if not HAVE_PYTHON_SOCKS:
  58. raise WebSocketException("Python Socks is needed for SOCKS proxying but is not available")
  59. hostname, port, resource, is_secure = parse_url(url)
  60. if proxy.proxy_protocol == "socks5":
  61. rdns = False
  62. proxy_type = ProxyType.SOCKS5
  63. if proxy.proxy_protocol == "socks4":
  64. rdns = False
  65. proxy_type = ProxyType.SOCKS4
  66. # socks5h and socks4a send DNS through proxy
  67. if proxy.proxy_protocol == "socks5h":
  68. rdns = True
  69. proxy_type = ProxyType.SOCKS5
  70. if proxy.proxy_protocol == "socks4a":
  71. rdns = True
  72. proxy_type = ProxyType.SOCKS4
  73. ws_proxy = Proxy.create(
  74. proxy_type=proxy_type,
  75. host=proxy.proxy_host,
  76. port=int(proxy.proxy_port),
  77. username=proxy.auth[0] if proxy.auth else None,
  78. password=proxy.auth[1] if proxy.auth else None,
  79. rdns=rdns)
  80. sock = ws_proxy.connect(hostname, port, timeout=proxy.proxy_timeout)
  81. if is_secure and HAVE_SSL:
  82. sock = _ssl_socket(sock, options.sslopt, hostname)
  83. elif is_secure:
  84. raise WebSocketException("SSL not available.")
  85. return sock, (hostname, port, resource)
  86. def connect(url, options, proxy, socket):
  87. # Use _start_proxied_socket() only for socks4 or socks5 proxy
  88. # Use _tunnel() for http proxy
  89. # TODO: Use python-socks for http protocol also, to standardize flow
  90. if proxy.proxy_host and not socket and not (proxy.proxy_protocol == "http"):
  91. return _start_proxied_socket(url, options, proxy)
  92. hostname, port, resource, is_secure = parse_url(url)
  93. if socket:
  94. return socket, (hostname, port, resource)
  95. addrinfo_list, need_tunnel, auth = _get_addrinfo_list(
  96. hostname, port, is_secure, proxy)
  97. if not addrinfo_list:
  98. raise WebSocketException(
  99. "Host not found.: " + hostname + ":" + str(port))
  100. sock = None
  101. try:
  102. sock = _open_socket(addrinfo_list, options.sockopt, options.timeout)
  103. if need_tunnel:
  104. sock = _tunnel(sock, hostname, port, auth)
  105. if is_secure:
  106. if HAVE_SSL:
  107. sock = _ssl_socket(sock, options.sslopt, hostname)
  108. else:
  109. raise WebSocketException("SSL not available.")
  110. return sock, (hostname, port, resource)
  111. except:
  112. if sock:
  113. sock.close()
  114. raise
  115. def _get_addrinfo_list(hostname, port, is_secure, proxy):
  116. phost, pport, pauth = get_proxy_info(
  117. hostname, is_secure, proxy.proxy_host, proxy.proxy_port, proxy.auth, proxy.no_proxy)
  118. try:
  119. # when running on windows 10, getaddrinfo without socktype returns a socktype 0.
  120. # This generates an error exception: `_on_error: exception Socket type must be stream or datagram, not 0`
  121. # or `OSError: [Errno 22] Invalid argument` when creating socket. Force the socket type to SOCK_STREAM.
  122. if not phost:
  123. addrinfo_list = socket.getaddrinfo(
  124. hostname, port, 0, socket.SOCK_STREAM, socket.SOL_TCP)
  125. return addrinfo_list, False, None
  126. else:
  127. pport = pport and pport or 80
  128. # when running on windows 10, the getaddrinfo used above
  129. # returns a socktype 0. This generates an error exception:
  130. # _on_error: exception Socket type must be stream or datagram, not 0
  131. # Force the socket type to SOCK_STREAM
  132. addrinfo_list = socket.getaddrinfo(phost, pport, 0, socket.SOCK_STREAM, socket.SOL_TCP)
  133. return addrinfo_list, True, pauth
  134. except socket.gaierror as e:
  135. raise WebSocketAddressException(e)
  136. def _open_socket(addrinfo_list, sockopt, timeout):
  137. err = None
  138. for addrinfo in addrinfo_list:
  139. family, socktype, proto = addrinfo[:3]
  140. sock = socket.socket(family, socktype, proto)
  141. sock.settimeout(timeout)
  142. for opts in DEFAULT_SOCKET_OPTION:
  143. sock.setsockopt(*opts)
  144. for opts in sockopt:
  145. sock.setsockopt(*opts)
  146. address = addrinfo[4]
  147. err = None
  148. while not err:
  149. try:
  150. sock.connect(address)
  151. except socket.error as error:
  152. error.remote_ip = str(address[0])
  153. try:
  154. eConnRefused = (errno.ECONNREFUSED, errno.WSAECONNREFUSED)
  155. except:
  156. eConnRefused = (errno.ECONNREFUSED, )
  157. if error.errno == errno.EINTR:
  158. continue
  159. elif error.errno in eConnRefused:
  160. err = error
  161. continue
  162. else:
  163. if sock:
  164. sock.close()
  165. raise error
  166. else:
  167. break
  168. else:
  169. continue
  170. break
  171. else:
  172. if err:
  173. raise err
  174. return sock
  175. def _wrap_sni_socket(sock, sslopt, hostname, check_hostname):
  176. context = ssl.SSLContext(sslopt.get('ssl_version', ssl.PROTOCOL_TLS))
  177. if sslopt.get('cert_reqs', ssl.CERT_NONE) != ssl.CERT_NONE:
  178. cafile = sslopt.get('ca_certs', None)
  179. capath = sslopt.get('ca_cert_path', None)
  180. if cafile or capath:
  181. context.load_verify_locations(cafile=cafile, capath=capath)
  182. elif hasattr(context, 'load_default_certs'):
  183. context.load_default_certs(ssl.Purpose.SERVER_AUTH)
  184. if sslopt.get('certfile', None):
  185. context.load_cert_chain(
  186. sslopt['certfile'],
  187. sslopt.get('keyfile', None),
  188. sslopt.get('password', None),
  189. )
  190. # see
  191. # https://github.com/liris/websocket-client/commit/b96a2e8fa765753e82eea531adb19716b52ca3ca#commitcomment-10803153
  192. context.verify_mode = sslopt['cert_reqs']
  193. if HAVE_CONTEXT_CHECK_HOSTNAME:
  194. context.check_hostname = check_hostname
  195. if 'ciphers' in sslopt:
  196. context.set_ciphers(sslopt['ciphers'])
  197. if 'cert_chain' in sslopt:
  198. certfile, keyfile, password = sslopt['cert_chain']
  199. context.load_cert_chain(certfile, keyfile, password)
  200. if 'ecdh_curve' in sslopt:
  201. context.set_ecdh_curve(sslopt['ecdh_curve'])
  202. return context.wrap_socket(
  203. sock,
  204. do_handshake_on_connect=sslopt.get('do_handshake_on_connect', True),
  205. suppress_ragged_eofs=sslopt.get('suppress_ragged_eofs', True),
  206. server_hostname=hostname,
  207. )
  208. def _ssl_socket(sock, user_sslopt, hostname):
  209. sslopt = dict(cert_reqs=ssl.CERT_REQUIRED)
  210. sslopt.update(user_sslopt)
  211. certPath = os.environ.get('WEBSOCKET_CLIENT_CA_BUNDLE')
  212. if certPath and os.path.isfile(certPath) \
  213. and user_sslopt.get('ca_certs', None) is None:
  214. sslopt['ca_certs'] = certPath
  215. elif certPath and os.path.isdir(certPath) \
  216. and user_sslopt.get('ca_cert_path', None) is None:
  217. sslopt['ca_cert_path'] = certPath
  218. if sslopt.get('server_hostname', None):
  219. hostname = sslopt['server_hostname']
  220. check_hostname = sslopt["cert_reqs"] != ssl.CERT_NONE and sslopt.pop(
  221. 'check_hostname', True)
  222. sock = _wrap_sni_socket(sock, sslopt, hostname, check_hostname)
  223. if not HAVE_CONTEXT_CHECK_HOSTNAME and check_hostname:
  224. match_hostname(sock.getpeercert(), hostname)
  225. return sock
  226. def _tunnel(sock, host, port, auth):
  227. debug("Connecting proxy...")
  228. connect_header = "CONNECT %s:%d HTTP/1.1\r\n" % (host, port)
  229. connect_header += "Host: %s:%d\r\n" % (host, port)
  230. # TODO: support digest auth.
  231. if auth and auth[0]:
  232. auth_str = auth[0]
  233. if auth[1]:
  234. auth_str += ":" + auth[1]
  235. encoded_str = base64encode(auth_str.encode()).strip().decode().replace('\n', '')
  236. connect_header += "Proxy-Authorization: Basic %s\r\n" % encoded_str
  237. connect_header += "\r\n"
  238. dump("request header", connect_header)
  239. send(sock, connect_header)
  240. try:
  241. status, resp_headers, status_message = read_headers(sock)
  242. except Exception as e:
  243. raise WebSocketProxyException(str(e))
  244. if status != 200:
  245. raise WebSocketProxyException(
  246. "failed CONNECT via proxy status: %r" % status)
  247. return sock
  248. def read_headers(sock):
  249. status = None
  250. status_message = None
  251. headers = {}
  252. trace("--- response header ---")
  253. while True:
  254. line = recv_line(sock)
  255. line = line.decode('utf-8').strip()
  256. if not line:
  257. break
  258. trace(line)
  259. if not status:
  260. status_info = line.split(" ", 2)
  261. status = int(status_info[1])
  262. if len(status_info) > 2:
  263. status_message = status_info[2]
  264. else:
  265. kv = line.split(":", 1)
  266. if len(kv) == 2:
  267. key, value = kv
  268. if key.lower() == "set-cookie" and headers.get("set-cookie"):
  269. headers["set-cookie"] = headers.get("set-cookie") + "; " + value.strip()
  270. else:
  271. headers[key.lower()] = value.strip()
  272. else:
  273. raise WebSocketException("Invalid header")
  274. trace("-----------------------")
  275. return status, headers, status_message