Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_http.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. """
  2. _http.py
  3. websocket - WebSocket client library for Python
  4. Copyright 2023 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("http_proxy_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: str, 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: str, 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_from_url, resource, is_secure = parse_url(url)
  93. if socket:
  94. return socket, (hostname, port_from_url, resource)
  95. addrinfo_list, need_tunnel, auth = _get_addrinfo_list(
  96. hostname, port_from_url, is_secure, proxy)
  97. if not addrinfo_list:
  98. raise WebSocketException(
  99. "Host not found.: " + hostname + ":" + str(port_from_url))
  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_from_url, 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_from_url, 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. sock.close()
  153. error.remote_ip = str(address[0])
  154. try:
  155. eConnRefused = (errno.ECONNREFUSED, errno.WSAECONNREFUSED, errno.ENETUNREACH)
  156. except AttributeError:
  157. eConnRefused = (errno.ECONNREFUSED, errno.ENETUNREACH)
  158. if error.errno in eConnRefused:
  159. err = error
  160. continue
  161. else:
  162. raise error
  163. else:
  164. break
  165. else:
  166. continue
  167. break
  168. else:
  169. if err:
  170. raise err
  171. return sock
  172. def _wrap_sni_socket(sock, sslopt, hostname, check_hostname):
  173. context = sslopt.get('context', None)
  174. if not context:
  175. context = ssl.SSLContext(sslopt.get('ssl_version', ssl.PROTOCOL_TLS_CLIENT))
  176. if sslopt.get('cert_reqs', ssl.CERT_NONE) != ssl.CERT_NONE:
  177. cafile = sslopt.get('ca_certs', None)
  178. capath = sslopt.get('ca_cert_path', None)
  179. if cafile or capath:
  180. context.load_verify_locations(cafile=cafile, capath=capath)
  181. elif hasattr(context, 'load_default_certs'):
  182. context.load_default_certs(ssl.Purpose.SERVER_AUTH)
  183. if sslopt.get('certfile', None):
  184. context.load_cert_chain(
  185. sslopt['certfile'],
  186. sslopt.get('keyfile', None),
  187. sslopt.get('password', None),
  188. )
  189. # Python 3.10 switch to PROTOCOL_TLS_CLIENT defaults to "cert_reqs = ssl.CERT_REQUIRED" and "check_hostname = True"
  190. # If both disabled, set check_hostname before verify_mode
  191. # see https://github.com/liris/websocket-client/commit/b96a2e8fa765753e82eea531adb19716b52ca3ca#commitcomment-10803153
  192. if sslopt.get('cert_reqs', ssl.CERT_NONE) == ssl.CERT_NONE and not sslopt.get('check_hostname', False):
  193. context.check_hostname = False
  194. context.verify_mode = ssl.CERT_NONE
  195. else:
  196. context.check_hostname = sslopt.get('check_hostname', True)
  197. context.verify_mode = sslopt.get('cert_reqs', ssl.CERT_REQUIRED)
  198. if 'ciphers' in sslopt:
  199. context.set_ciphers(sslopt['ciphers'])
  200. if 'cert_chain' in sslopt:
  201. certfile, keyfile, password = sslopt['cert_chain']
  202. context.load_cert_chain(certfile, keyfile, password)
  203. if 'ecdh_curve' in sslopt:
  204. context.set_ecdh_curve(sslopt['ecdh_curve'])
  205. return context.wrap_socket(
  206. sock,
  207. do_handshake_on_connect=sslopt.get('do_handshake_on_connect', True),
  208. suppress_ragged_eofs=sslopt.get('suppress_ragged_eofs', True),
  209. server_hostname=hostname,
  210. )
  211. def _ssl_socket(sock, user_sslopt, hostname):
  212. sslopt = dict(cert_reqs=ssl.CERT_REQUIRED)
  213. sslopt.update(user_sslopt)
  214. certPath = os.environ.get('WEBSOCKET_CLIENT_CA_BUNDLE')
  215. if certPath and os.path.isfile(certPath) \
  216. and user_sslopt.get('ca_certs', None) is None:
  217. sslopt['ca_certs'] = certPath
  218. elif certPath and os.path.isdir(certPath) \
  219. and user_sslopt.get('ca_cert_path', None) is None:
  220. sslopt['ca_cert_path'] = certPath
  221. if sslopt.get('server_hostname', None):
  222. hostname = sslopt['server_hostname']
  223. check_hostname = sslopt.get('check_hostname', True)
  224. sock = _wrap_sni_socket(sock, sslopt, hostname, check_hostname)
  225. return sock
  226. def _tunnel(sock, host, port, auth):
  227. debug("Connecting proxy...")
  228. connect_header = "CONNECT {h}:{p} HTTP/1.1\r\n".format(h=host, p=port)
  229. connect_header += "Host: {h}:{p}\r\n".format(h=host, p=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 {str}\r\n".format(str=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: {status}".format(status=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