main.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. "avatars0.githubusercontent.com",
  30. "avatars1.githubusercontent.com",
  31. "avatars2.githubusercontent.com",
  32. "avatars3.githubusercontent.com",
  33. "avatars4.githubusercontent.com",
  34. "avatars5.githubusercontent.com",
  35. "avatars6.githubusercontent.com",
  36. "avatars7.githubusercontent.com",
  37. "avatars8.githubusercontent.com",
  38. "codeload.github.com",
  39. "camo.githubusercontent.com",
  40. "raw.githubusercontent.com",
  41. "cloud.githubusercontent.com",
  42. "user-images.githubusercontent.com",
  43. "customer-stories-feed.github.com",
  44. "pages.github.com",
  45. "api.github.com",
  46. "live.github.com",
  47. "github.com"
  48. ]
  49. addr2ip = {}
  50. # hostLocation = r"C:\Windows\System32\drivers\etc\hosts"
  51. hostLocation = r"E:\downloads\chrome\speed-github-master\hosts"
  52. def dropDuplication(line):
  53. flag = False
  54. if "#*********************github" in line:
  55. return True
  56. for site in sites:
  57. if site in line:
  58. flag = flag or True
  59. else:
  60. flag = flag or False
  61. return flag
  62. # 更新host, 并刷新本地DNS
  63. def updateHost():
  64. today = datetime.date.today()
  65. for site in sites:
  66. trueip=get_ip_utils.getIpFromChinaz(site)
  67. if trueip != None:
  68. addr2ip[site] = trueip
  69. print(site + "\t" + trueip)
  70. shutil.copy(hostLocation, hostLocation + ".bak") # 做一份host备份
  71. with open(hostLocation, "r") as f1:
  72. f1_lines = f1.readlines()
  73. with open("temphost", "w") as f2:
  74. for line in f1_lines: # 为了防止 host 越写用越长,需要删除之前更新的含有github相关内容
  75. if dropDuplication(line) == False:
  76. f2.write(line)
  77. f2.write("#*********************github " +
  78. str(today) + " update********************\r\n")
  79. for key in addr2ip:
  80. f2.write(addr2ip[key] + "\t" + key + "\n")
  81. shutil.copy("./temphost", hostLocation)
  82. os.system("ipconfig /flushdns")
  83. def is_admin():
  84. try:
  85. return ctypes.windll.shell32.IsUserAnAdmin()
  86. except Exception as e:
  87. return False
  88. if __name__ == "__main__":
  89. os=platform.system()
  90. if os=="Windows":
  91. # if is_admin():
  92. # print("admin.....")
  93. # else:
  94. # if sys.version_info[0]==3:
  95. # ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
  96. # print("hahhah....")
  97. updateHost()