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.

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import os
  2. import socket
  3. import struct
  4. from urllib.parse import unquote, urlparse
  5. """
  6. _url.py
  7. websocket - WebSocket client library for Python
  8. Copyright 2023 engn33r
  9. Licensed under the Apache License, Version 2.0 (the "License");
  10. you may not use this file except in compliance with the License.
  11. You may obtain a copy of the License at
  12. http://www.apache.org/licenses/LICENSE-2.0
  13. Unless required by applicable law or agreed to in writing, software
  14. distributed under the License is distributed on an "AS IS" BASIS,
  15. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. See the License for the specific language governing permissions and
  17. limitations under the License.
  18. """
  19. __all__ = ["parse_url", "get_proxy_info"]
  20. def parse_url(url: str) -> tuple:
  21. """
  22. parse url and the result is tuple of
  23. (hostname, port, resource path and the flag of secure mode)
  24. Parameters
  25. ----------
  26. url: str
  27. url string.
  28. """
  29. if ":" not in url:
  30. raise ValueError("url is invalid")
  31. scheme, url = url.split(":", 1)
  32. parsed = urlparse(url, scheme="http")
  33. if parsed.hostname:
  34. hostname = parsed.hostname
  35. else:
  36. raise ValueError("hostname is invalid")
  37. port = 0
  38. if parsed.port:
  39. port = parsed.port
  40. is_secure = False
  41. if scheme == "ws":
  42. if not port:
  43. port = 80
  44. elif scheme == "wss":
  45. is_secure = True
  46. if not port:
  47. port = 443
  48. else:
  49. raise ValueError("scheme %s is invalid" % scheme)
  50. if parsed.path:
  51. resource = parsed.path
  52. else:
  53. resource = "/"
  54. if parsed.query:
  55. resource += "?" + parsed.query
  56. return hostname, port, resource, is_secure
  57. DEFAULT_NO_PROXY_HOST = ["localhost", "127.0.0.1"]
  58. def _is_ip_address(addr: str) -> bool:
  59. try:
  60. socket.inet_aton(addr)
  61. except socket.error:
  62. return False
  63. else:
  64. return True
  65. def _is_subnet_address(hostname: str) -> bool:
  66. try:
  67. addr, netmask = hostname.split("/")
  68. return _is_ip_address(addr) and 0 <= int(netmask) < 32
  69. except ValueError:
  70. return False
  71. def _is_address_in_network(ip: str, net: str) -> bool:
  72. ipaddr = struct.unpack('!I', socket.inet_aton(ip))[0]
  73. netaddr, netmask = net.split('/')
  74. netaddr = struct.unpack('!I', socket.inet_aton(netaddr))[0]
  75. netmask = (0xFFFFFFFF << (32 - int(netmask))) & 0xFFFFFFFF
  76. return ipaddr & netmask == netaddr
  77. def _is_no_proxy_host(hostname: str, no_proxy: list) -> bool:
  78. if not no_proxy:
  79. v = os.environ.get("no_proxy", os.environ.get("NO_PROXY", "")).replace(" ", "")
  80. if v:
  81. no_proxy = v.split(",")
  82. if not no_proxy:
  83. no_proxy = DEFAULT_NO_PROXY_HOST
  84. if '*' in no_proxy:
  85. return True
  86. if hostname in no_proxy:
  87. return True
  88. if _is_ip_address(hostname):
  89. return any([_is_address_in_network(hostname, subnet) for subnet in no_proxy if _is_subnet_address(subnet)])
  90. for domain in [domain for domain in no_proxy if domain.startswith('.')]:
  91. if hostname.endswith(domain):
  92. return True
  93. return False
  94. def get_proxy_info(
  95. hostname: str, is_secure: bool, proxy_host: str = None, proxy_port: int = 0, proxy_auth: tuple = None,
  96. no_proxy: list = None, proxy_type: str = 'http') -> tuple:
  97. """
  98. Try to retrieve proxy host and port from environment
  99. if not provided in options.
  100. Result is (proxy_host, proxy_port, proxy_auth).
  101. proxy_auth is tuple of username and password
  102. of proxy authentication information.
  103. Parameters
  104. ----------
  105. hostname: str
  106. Websocket server name.
  107. is_secure: bool
  108. Is the connection secure? (wss) looks for "https_proxy" in env
  109. before falling back to "http_proxy"
  110. proxy_host: str
  111. http proxy host name.
  112. proxy_port: str or int
  113. http proxy port.
  114. no_proxy: list
  115. Whitelisted host names that don't use the proxy.
  116. proxy_auth: tuple
  117. HTTP proxy auth information. Tuple of username and password. Default is None.
  118. proxy_type: str
  119. Specify the proxy protocol (http, socks4, socks4a, socks5, socks5h). Default is "http".
  120. Use socks4a or socks5h if you want to send DNS requests through the proxy.
  121. """
  122. if _is_no_proxy_host(hostname, no_proxy):
  123. return None, 0, None
  124. if proxy_host:
  125. port = proxy_port
  126. auth = proxy_auth
  127. return proxy_host, port, auth
  128. env_keys = ["http_proxy"]
  129. if is_secure:
  130. env_keys.insert(0, "https_proxy")
  131. for key in env_keys:
  132. value = os.environ.get(key, os.environ.get(key.upper(), "")).replace(" ", "")
  133. if value:
  134. proxy = urlparse(value)
  135. auth = (unquote(proxy.username), unquote(proxy.password)) if proxy.username else None
  136. return proxy.hostname, proxy.port, auth
  137. return None, 0, None