_url.py 4.7 KB

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