main.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. "githubapp.com",
  48. "github.com"
  49. ]
  50. addr2ip = {}
  51. hostLocation = r"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.getIpFromipapi(site)
  67. if trueip != None:
  68. addr2ip[site] = trueip
  69. print(site + "\t" + trueip)
  70. with open(hostLocation, "r") as f1:
  71. f1_lines = f1.readlines()
  72. with open("temphost", "w") as f2:
  73. for line in f1_lines: # 为了防止 host 越写用越长,需要删除之前更新的含有github相关内容
  74. if dropDuplication(line) == False:
  75. f2.write(line)
  76. f2.write("#*********************github " +
  77. str(today) + " update********************\n")
  78. for key in addr2ip:
  79. f2.write(addr2ip[key] + "\t" + key + "\n")
  80. os.remove(hostLocation)
  81. os.rename("temphost",hostLocation)
  82. if __name__ == "__main__":
  83. updateHost()