Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

181 řádky
5.8 KiB

před 3 roky
před 3 roky
před 3 roky
před 3 roky
před 3 roky
před 3 roky
před 3 roky
před 3 roky
před 3 roky
před 3 roky
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. #
  4. # Author : XueWeiHan
  5. # E-mail : 595666367@qq.com
  6. # Date : 2020-05-19 15:27
  7. # Desc : 获取最新的 GitHub 相关域名对应 IP
  8. import os
  9. import re
  10. import json
  11. import traceback
  12. from datetime import datetime, timezone, timedelta
  13. from collections import Counter
  14. import requests
  15. from retry import retry
  16. RAW_URL = [
  17. "alive.github.com",
  18. "live.github.com",
  19. "github.githubassets.com",
  20. "central.github.com",
  21. "desktop.githubusercontent.com",
  22. "assets-cdn.github.com",
  23. "camo.githubusercontent.com",
  24. "github.map.fastly.net",
  25. "github.global.ssl.fastly.net",
  26. "gist.github.com",
  27. "github.io",
  28. "github.com",
  29. "github.blog",
  30. "api.github.com",
  31. "raw.githubusercontent.com",
  32. "user-images.githubusercontent.com",
  33. "favicons.githubusercontent.com",
  34. "avatars5.githubusercontent.com",
  35. "avatars4.githubusercontent.com",
  36. "avatars3.githubusercontent.com",
  37. "avatars2.githubusercontent.com",
  38. "avatars1.githubusercontent.com",
  39. "avatars0.githubusercontent.com",
  40. "avatars.githubusercontent.com",
  41. "codeload.github.com",
  42. "github-cloud.s3.amazonaws.com",
  43. "github-com.s3.amazonaws.com",
  44. "github-production-release-asset-2e65be.s3.amazonaws.com",
  45. "github-production-user-asset-6210df.s3.amazonaws.com",
  46. "github-production-repository-file-5c1aeb.s3.amazonaws.com",
  47. "githubstatus.com",
  48. "github.community",
  49. "media.githubusercontent.com"]
  50. IPADDRESS_PREFIX = ".ipaddress.com"
  51. HOSTS_TEMPLATE = """# GitHub520 Host Start
  52. {content}
  53. # Update time: {update_time}
  54. # Update url: https://raw.hellogithub.com/hosts
  55. # Star me: https://github.com/521xueweihan/GitHub520
  56. # GitHub520 Host End\n"""
  57. def write_file(hosts_content: str, update_time: str):
  58. output_doc_file_path = os.path.join(os.path.dirname(__file__), "README.md")
  59. template_path = os.path.join(os.path.dirname(__file__),
  60. "README_template.md")
  61. write_host_file(hosts_content)
  62. with open(output_doc_file_path, "r") as old_readme_fb:
  63. old_content = old_readme_fb.read()
  64. old_hosts = old_content.split("```bash")[1].split("```")[0].strip()
  65. old_hosts = old_hosts.split("# Update time:")[0]
  66. if old_hosts == hosts_content:
  67. print("host not change")
  68. return False
  69. with open(template_path, "r") as temp_fb:
  70. template_str = temp_fb.read()
  71. hosts_content = template_str.format(hosts_str=hosts_content,
  72. update_time=update_time)
  73. with open(output_doc_file_path, "w") as output_fb:
  74. output_fb.write(hosts_content)
  75. return True
  76. def write_host_file(hosts_content: str):
  77. output_file_path = os.path.join(os.path.dirname(__file__), 'hosts')
  78. with open(output_file_path, "w") as output_fb:
  79. output_fb.write(hosts_content)
  80. def write_json_file(hosts_list: list):
  81. output_file_path = os.path.join(os.path.dirname(__file__), 'hosts.json')
  82. with open(output_file_path, "w") as output_fb:
  83. json.dump(hosts_list, output_fb)
  84. def make_ipaddress_url(raw_url: str):
  85. """
  86. ipaddress url
  87. :param raw_url: url
  88. :return: ipaddress url
  89. """
  90. dot_count = raw_url.count(".")
  91. if dot_count > 1:
  92. raw_url_list = raw_url.split(".")
  93. tmp_url = raw_url_list[-2] + "." + raw_url_list[-1]
  94. ipaddress_url = "https://" + tmp_url + IPADDRESS_PREFIX + "/" + raw_url
  95. else:
  96. ipaddress_url = "https://" + raw_url + IPADDRESS_PREFIX
  97. return ipaddress_url
  98. @retry(tries=3)
  99. def get_ip(session: requests.session, raw_url: str):
  100. url = make_ipaddress_url(raw_url)
  101. try:
  102. rs = session.get(url, timeout=5)
  103. pattern = r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b"
  104. ip_list = re.findall(pattern, rs.text)
  105. ip_counter_obj = Counter(ip_list).most_common(1)
  106. if ip_counter_obj:
  107. return raw_url, ip_counter_obj[0][0]
  108. raise Exception("ip address empty")
  109. except Exception as ex:
  110. print("get: {}, error: {}".format(url, ex))
  111. raise Exception
  112. @retry(tries=3)
  113. def update_gitee_gist(session: requests.session, host_content):
  114. gitee_token = os.getenv("gitee_token")
  115. gitee_gist_id = os.getenv("gitee_gist_id")
  116. gist_file_name = os.getenv("gitee_gist_file_name")
  117. url = "https://gitee.com/api/v5/gists/{}".format(gitee_gist_id)
  118. headers = {
  119. "Content-Type": "application/json"}
  120. data = {
  121. "access_token": gitee_token,
  122. "files": {gist_file_name: {"content": host_content}},
  123. "public": "true"}
  124. json_data = json.dumps(data)
  125. try:
  126. response = session.patch(url, data=json_data, headers=headers,
  127. timeout=20)
  128. if response.status_code == 200:
  129. print("update gitee gist success")
  130. else:
  131. print("update gitee gist fail: {} {}".format(response.status_code,
  132. response.content))
  133. except Exception as e:
  134. traceback.print_exc(e)
  135. raise Exception(e)
  136. def main():
  137. session = requests.session()
  138. content = ""
  139. content_list = []
  140. for raw_url in RAW_URL:
  141. try:
  142. host_name, ip = get_ip(session, raw_url)
  143. content += ip.ljust(30) + host_name + "\n"
  144. content_list.append((ip, host_name,))
  145. except Exception:
  146. continue
  147. if not content:
  148. return
  149. update_time = datetime.utcnow().astimezone(
  150. timezone(timedelta(hours=8))).replace(microsecond=0).isoformat()
  151. hosts_content = HOSTS_TEMPLATE.format(content=content, update_time=update_time)
  152. has_change = write_file(hosts_content, update_time)
  153. if has_change:
  154. write_json_file(content_list)
  155. print(hosts_content)
  156. if __name__ == '__main__':
  157. main()