main.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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"hosts"
  51. def dropDuplication(line):
  52. flag = False
  53. if "#*********************github" in line:
  54. return True
  55. for site in sites:
  56. if site in line:
  57. flag = flag or True
  58. else:
  59. flag = flag or False
  60. return flag
  61. # 更新host, 并刷新本地DNS
  62. def updateHost():
  63. today = datetime.date.today()
  64. for site in sites:
  65. trueip=get_ip_utils.getIpFromChinaz(site)
  66. if trueip != None:
  67. addr2ip[site] = trueip
  68. print(site + "\t" + trueip)
  69. with open(hostLocation, "r") as f1:
  70. f1_lines = f1.readlines()
  71. with open("temphost", "w") as f2:
  72. for line in f1_lines: # 为了防止 host 越写用越长,需要删除之前更新的含有github相关内容
  73. if dropDuplication(line) == False:
  74. f2.write(line)
  75. f2.write("#*********************github " +
  76. str(today) + " update********************\r\n")
  77. for key in addr2ip:
  78. f2.write(addr2ip[key] + "\t" + key + "\n")
  79. os.remove("./temphost")
  80. if __name__ == "__main__":
  81. updateHost()