lua_blocking_from_disconnect.py 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python
  2. '''
  3. moreRules
  4. hardeningRules
  5. license
  6. categories
  7. Disconnect
  8. Content
  9. Analytics
  10. Advertising
  11. Social
  12. filteringRules
  13. '''
  14. import requests
  15. import json
  16. import sys
  17. PDNS_LUA_FILE = '/etc/pdns/blacklist.lua'
  18. DISCONNECT_LIST_URL = 'https://services.disconnect.me/disconnect-plaintext.json'
  19. CATEGORIES_TO_REMOVE = ['Advertising', 'Analytics', 'Disconnect', 'Social']
  20. HOST_TO_ACCEPT = ['reddit.com', 'linkedin.com', 'licdn.com',
  21. 'google.ch', 'google.com', 'google.fr']
  22. def fetch_url():
  23. try:
  24. r = requests.get(DISCONNECT_LIST_URL)
  25. except:
  26. sys.exit()
  27. if r.status_code == 200:
  28. try:
  29. return r.json()
  30. except:
  31. print('Seems like we did not fetch a json dict')
  32. sys.exit()
  33. else:
  34. print('Incorrect return code from {0}: {1}'.format(DISCONNECT_LIST_URL, r.status_code))
  35. sys.exit()
  36. def build_list(j):
  37. host_list = []
  38. if 'categories' in j:
  39. for category in j['categories']:
  40. if category in CATEGORIES_TO_REMOVE:
  41. for sub_dict in j['categories'][category]:
  42. for entity in sub_dict:
  43. for main_url in sub_dict[entity]:
  44. h_list = sub_dict[entity][main_url]
  45. if isinstance(h_list, list):
  46. for host in h_list:
  47. if (host not in HOST_TO_ACCEPT) and (host not in host_list):
  48. host_list.append(host)
  49. else:
  50. print('"categories" key not found in dict, nothing to process')
  51. sys.exit()
  52. # Return the list sorted
  53. return sorted(host_list)
  54. def make_pdns_lua_file(host_list):
  55. lua_return = 'return{0}"{1}"{2}'.format('{', u'","'.join(host_list).encode('utf-8'), '}')
  56. f = open(PDNS_LUA_FILE, 'w')
  57. f.write(lua_return)
  58. def main():
  59. # Retrieve the json file online
  60. j = fetch_url()
  61. # Build a list from it
  62. host_list = build_list(j)
  63. # Create pdns file
  64. make_pdns_lua_file(host_list)
  65. if __name__ == "__main__":
  66. main()