update.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env python3
  2. '''
  3. @作者: 风沐白
  4. @文件: update.py
  5. @描述: 从网络来源更新白名单规则
  6. '''
  7. import requests
  8. import re
  9. import os
  10. import time
  11. # 默认来源 git@github.com:felixonmars/dnsmasq-china-list.git, 可能需要代理
  12. confurl = 'https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf'
  13. if __name__ == "__main__":
  14. conffile = 'accelerated-domains.china.conf'
  15. sorlfile = 'white-list.sorl'
  16. rules = set()
  17. up_time=time.ctime()
  18. headline = ['[SwitchyOmega Conditions]\n',
  19. '; Require: SwitchyOmega >= 2.3.2\n',
  20. '; Update @ {}\n'.format(up_time),
  21. '\n',
  22. '; cn域名都不走代理\n',
  23. '*.cn\n',
  24. '\n',
  25. '; 局域网IP不走代理\n',
  26. '10.*.*.*\n',
  27. '172.16.*.*\n',
  28. '172.17.*.*\n',
  29. '172.18.*.*\n',
  30. '172.19.*.*\n',
  31. '172.20.*.*\n',
  32. '172.21.*.*\n',
  33. '172.22.*.*\n',
  34. '172.23.*.*\n',
  35. '172.24.*.*\n',
  36. '172.25.*.*\n',
  37. '172.26.*.*\n',
  38. '172.27.*.*\n',
  39. '172.28.*.*\n',
  40. '172.29.*.*\n',
  41. '172.30.*.*\n',
  42. '172.31.*.*\n',
  43. '169.254.*.*\n',
  44. '192.168.*.*\n',
  45. '\n',
  46. '; 教育网\n',
  47. '*.acm.org\n',
  48. '*.dblp.org\n',
  49. '*.ebscohost.com\n',
  50. '*.edu\n',
  51. '*.edu.*\n',
  52. '*.engineeringvillage.com\n',
  53. '*.ieee.org\n',
  54. '*.jstor.org\n',
  55. '*.lexis.com\n',
  56. '*.msftconnecttest.com\n',
  57. '*.nature.com\n',
  58. '*.oclc.org\n',
  59. '*.proquest.com\n',
  60. '*.researchgate.net\n',
  61. '*.sciencedirect.com\n',
  62. '*.sciencemag.org\n',
  63. '*.springer.com\n',
  64. '*.tandfonline.com\n',
  65. '*.uni-trier.de\n',
  66. '*.webofknowledge.com\n',
  67. '*.wiley.com\n',
  68. '\n',
  69. '; 常规列表\n']
  70. r = requests.get(confurl)
  71. with open(conffile, 'wb') as f:
  72. f.write(r.content)
  73. with open(conffile, 'r') as f:
  74. for line in f.readlines():
  75. if line[0] == '#':
  76. continue
  77. rules.add(re.sub(r'server=/(\S+)/\d+\.\d+\.\d+\.\d+', r'*.\1', line))
  78. rules = list(rules)
  79. rules.sort()
  80. out = [*headline, *rules]
  81. with open(sorlfile, 'w') as f:
  82. f.writelines(out)
  83. os.remove(conffile)