main.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @Author : liuyuqi
  5. @Contact : liuyuqi.gov@msn.cn
  6. @Time : 2020/04/26 23:01:40
  7. @Version : 1.0
  8. @License : Copyright © 2017-2020 liuyuqi. All Rights Reserved.
  9. @Desc : github.com
  10. '''
  11. import shutil
  12. import os,sys,ctypes
  13. import datetime
  14. import get_ip_utils
  15. import platform
  16. # 需要获取ip的网址
  17. sites = [
  18. "github.global.ssl.fastly.net",
  19. "assets-cdn.github.com",
  20. "documentcloud.github.com",
  21. "gist.github.com",
  22. "gist.githubusercontent.com",
  23. "github.githubassets.com",# 很卡
  24. "help.github.com",
  25. "nodeload.github.com",
  26. "raw.github.com",
  27. "status.github.com",
  28. "training.github.com",
  29. "avatars.githubusercontent.com",
  30. "avatars0.githubusercontent.com",
  31. "avatars1.githubusercontent.com",
  32. "avatars2.githubusercontent.com",
  33. "avatars3.githubusercontent.com",
  34. "avatars4.githubusercontent.com",
  35. "avatars5.githubusercontent.com",
  36. "avatars6.githubusercontent.com",
  37. "avatars7.githubusercontent.com",
  38. "avatars8.githubusercontent.com",
  39. "favicons.githubusercontent.com",
  40. "codeload.github.com",
  41. "github-cloud.s3.amazonaws.com",
  42. "github-com.s3.amazonaws.com",
  43. "github-production-release-asset-2e65be.s3.amazonaws.com",
  44. "github-production-user-asset-6210df.s3.amazonaws.com",
  45. "github-production-repository-file-5c1aeb.s3.amazonaws.com",
  46. "githubstatus.com",
  47. "github.community",
  48. "media.githubusercontent.com",
  49. "camo.githubusercontent.com",
  50. "raw.githubusercontent.com",
  51. "cloud.githubusercontent.com",
  52. "user-images.githubusercontent.com",
  53. "customer-stories-feed.github.com",
  54. "pages.github.com",
  55. "api.github.com",
  56. "live.github.com",
  57. "githubapp.com",
  58. "github.com"
  59. ]
  60. addr2ip = {}
  61. hostLocation = r"hosts"
  62. def dropDuplication(line):
  63. flag = False
  64. if "#*******" in line:
  65. return True
  66. for site in sites:
  67. if site in line:
  68. flag = flag or True
  69. else:
  70. flag = flag or False
  71. return flag
  72. # 更新host, 并刷新本地DNS
  73. def updateHost():
  74. today = datetime.date.today()
  75. for site in sites:
  76. trueip=get_ip_utils.getIpFromipapi(site)
  77. if trueip != None:
  78. addr2ip[site] = trueip
  79. print(site + "\t" + trueip)
  80. with open(hostLocation, "r") as f1:
  81. f1_lines = f1.readlines()
  82. with open("temphost", "w") as f2:
  83. for line in f1_lines: # 为了防止 host 越写用越长,需要删除之前更新的含有github相关内容
  84. if dropDuplication(line) == False:
  85. f2.write(line)
  86. f2.write("#*********************github " +
  87. str(today) + " update********************\n")
  88. f2.write("#******* get latest hosts: http://blog.yoqi.me/lyq/16489.html\n")
  89. for key in addr2ip:
  90. f2.write(addr2ip[key] + "\t" + key + "\n")
  91. os.remove(hostLocation)
  92. os.rename("temphost",hostLocation)
  93. if __name__ == "__main__":
  94. updateHost()