| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 # coding: utf-8 | 1 # coding: utf-8 |
| 2 | 2 |
| 3 # This file is part of the Adblock Plus web scripts, | 3 # This file is part of the Adblock Plus web scripts, |
| 4 # Copyright (C) 2006-2016 Eyeo GmbH | 4 # Copyright (C) 2006-2016 Eyeo GmbH |
| 5 # | 5 # |
| 6 # Adblock Plus is free software: you can redistribute it and/or modify | 6 # Adblock Plus is free software: you can redistribute it and/or modify |
| 7 # it under the terms of the GNU General Public License version 3 as | 7 # it under the terms of the GNU General Public License version 3 as |
| 8 # published by the Free Software Foundation. | 8 # published by the Free Software Foundation. |
| 9 # | 9 # |
| 10 # Adblock Plus is distributed in the hope that it will be useful, | 10 # Adblock Plus is distributed in the hope that it will be useful, |
| 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 # GNU General Public License for more details. | 13 # GNU General Public License for more details. |
| 14 # | 14 # |
| 15 # You should have received a copy of the GNU General Public License | 15 # You should have received a copy of the GNU General Public License |
| 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 17 | 17 |
| 18 import os, subprocess, codecs, urllib, zipfile, tempfile, shutil | 18 import os, subprocess, codecs, urllib2, zipfile, tempfile, shutil, sys |
| 19 import ConfigParser | |
| 19 from StringIO import StringIO | 20 from StringIO import StringIO |
| 20 from sitescripts.utils import get_config | 21 from sitescripts.utils import get_config |
| 21 | 22 |
| 23 | |
| 24 filterlist_header = '''[Adblock Plus 1.1] | |
| 25 ! This is a list of malware domains generated from malwaredomains.com data. | |
| 26 ! Homepage: http://malwaredomains.com/?page_id=2 | |
| 27 ! Last modified: %timestamp% | |
| 28 ! Expires: 1d | |
| 29 !''' | |
| 30 | |
| 31 # Default value for malwaredomains_mirrors in the config. | |
| 32 # Equivalent to the following in the config: | |
| 33 # malwaredomains_mirrors= | |
|
Sebastian Noack
2016/03/14 13:36:07
Example configuration should be documented in .sit
Vasily Kuznetsov
2016/03/14 14:18:12
Acknowledged.
Vasily Kuznetsov
2016/03/14 14:47:58
Done.
| |
| 34 # http://mirror3.malwaredomains.com | |
| 35 # http://mirror1.malwaredomains.com | |
| 36 # http://mirror2.malwaredomains.com | |
| 37 default_mirrors_list = [ | |
|
Sebastian Noack
2016/03/14 13:36:07
I'd rather wait until sitescripts.ini got updated
Vasily Kuznetsov
2016/03/14 14:18:11
I'm cool with this, but I'm not very sure how to u
Sebastian Noack
2016/03/14 15:40:12
Matze, Felix or Wladimir can change the config man
| |
| 38 'http://mirror3.malwaredomains.com', | |
| 39 'http://mirror1.malwaredomains.com', | |
| 40 'http://mirror2.malwaredomains.com' | |
| 41 ] | |
| 42 | |
| 43 malwaredomains_path = '/files/justdomains.zip' | |
|
Sebastian Noack
2016/03/14 13:36:07
Nit: Please use upper case for constant-like varia
Vasily Kuznetsov
2016/03/14 14:18:11
Acknowledged.
Vasily Kuznetsov
2016/03/14 14:47:58
Done.
| |
| 44 | |
| 45 | |
| 46 def try_mirror(mirror, path): | |
| 47 try: | |
| 48 response = urllib2.urlopen(mirror + path) | |
| 49 return response.read() | |
| 50 except urllib2.HTTPError, err: | |
| 51 print >>sys.stderr, '{}: {}'.format(mirror, err) | |
|
Sebastian Noack
2016/03/14 13:36:07
Any output we generate will result into an email s
Vasily Kuznetsov
2016/03/14 14:18:11
Acknowledged.
Vasily Kuznetsov
2016/03/14 14:47:58
Done.
| |
| 52 return None | |
| 53 | |
| 54 | |
| 22 if __name__ == '__main__': | 55 if __name__ == '__main__': |
| 23 repository = get_config().get('subscriptionDownloads', 'malwaredomains_reposit ory') | 56 config = get_config() |
| 57 repository = config.get('subscriptionDownloads', 'malwaredomains_repository') | |
| 58 try: | |
| 59 mirrors = config.get('subscriptionDownloads', 'malwaredomains_mirrors') | |
| 60 mirrors_list = filter(None, [mirror.strip() for mirror in mirrors.split()]) | |
| 61 except ConfigParser.NoOptionError: | |
| 62 mirrors_list = default_mirrors_list | |
| 63 | |
| 24 tempdir = tempfile.mkdtemp(prefix='malwaredomains') | 64 tempdir = tempfile.mkdtemp(prefix='malwaredomains') |
| 25 try: | 65 try: |
| 26 subprocess.check_call(['hg', '-q', 'clone', '-U', repository, tempdir]) | 66 subprocess.check_call(['hg', '-q', 'clone', '-U', repository, tempdir]) |
| 27 subprocess.check_call(['hg', '-q', 'up', '-R', tempdir, '-r', 'default']) | 67 subprocess.check_call(['hg', '-q', 'up', '-R', tempdir, '-r', 'default']) |
| 28 | 68 |
| 29 path = os.path.join(tempdir, 'malwaredomains_full.txt') | 69 path = os.path.join(tempdir, 'malwaredomains_full.txt') |
| 30 file = codecs.open(path, 'wb', encoding='utf-8') | 70 file = codecs.open(path, 'wb', encoding='utf-8') |
| 31 | 71 |
| 32 print >>file, '''[Adblock Plus 1.1] | 72 print >>file, filterlist_header |
| 33 ! This is a list of malware domains generated from malwaredomains.com data. | |
| 34 ! Homepage: http://malwaredomains.com/?page_id=2 | |
| 35 ! Last modified: %timestamp% | |
| 36 ! Expires: 1d | |
| 37 !''' | |
| 38 | 73 |
| 39 data = urllib.urlopen('http://mirror3.malwaredomains.com/files/justdomains.z ip').read() | 74 for mirror in mirrors_list: |
| 75 data = try_mirror(mirror, malwaredomains_path) | |
| 76 if data is not None: | |
| 77 break | |
| 78 else: | |
| 79 print >>sys.stderr, 'Unable to fetch malware domains list.' | |
| 80 sys.exit(1) | |
| 81 | |
| 40 zip = zipfile.ZipFile(StringIO(data), 'r') | 82 zip = zipfile.ZipFile(StringIO(data), 'r') |
| 41 info = zip.infolist()[0] | 83 info = zip.infolist()[0] |
| 42 for line in str(zip.read(info.filename)).splitlines(): | 84 for line in str(zip.read(info.filename)).splitlines(): |
| 43 domain = line.strip() | 85 domain = line.strip() |
| 44 if not domain: | 86 if not domain: |
| 45 continue | 87 continue |
| 46 | 88 |
| 47 print >>file, '||%s^' % domain.decode('idna') | 89 print >>file, '||%s^' % domain.decode('idna') |
| 48 file.close(); | 90 file.close(); |
| 49 | 91 |
| 50 if subprocess.check_output(['hg', 'stat', '-R', tempdir]) != '': | 92 if subprocess.check_output(['hg', 'stat', '-R', tempdir]) != '': |
| 51 subprocess.check_call(['hg', '-q', 'commit', '-R', tempdir, '-A', '-u', 'h gbot', '-m', 'Updated malwaredomains.com data']) | 93 subprocess.check_call(['hg', '-q', 'commit', '-R', tempdir, '-A', '-u', 'h gbot', '-m', 'Updated malwaredomains.com data']) |
| 52 subprocess.check_call(['hg', '-q', 'push', '-R', tempdir]) | 94 subprocess.check_call(['hg', '-q', 'push', '-R', tempdir]) |
| 53 finally: | 95 finally: |
| 54 shutil.rmtree(tempdir, ignore_errors=True) | 96 shutil.rmtree(tempdir, ignore_errors=True) |
| OLD | NEW |