|
|
@ -9,6 +9,7 @@ import urllib2 |
|
|
|
import subprocess |
|
|
|
import subprocess |
|
|
|
import argparse |
|
|
|
import argparse |
|
|
|
import configobj |
|
|
|
import configobj |
|
|
|
|
|
|
|
import tempfile |
|
|
|
|
|
|
|
|
|
|
|
# parse command line options |
|
|
|
# parse command line options |
|
|
|
parser = argparse.ArgumentParser( |
|
|
|
parser = argparse.ArgumentParser( |
|
|
@ -57,8 +58,6 @@ opener = urllib2.build_opener(proxy_handler) |
|
|
|
all_sources = ExternalSource.objects.filter(active=True) |
|
|
|
all_sources = ExternalSource.objects.filter(active=True) |
|
|
|
|
|
|
|
|
|
|
|
for source in all_sources: |
|
|
|
for source in all_sources: |
|
|
|
# use separate file for each host |
|
|
|
|
|
|
|
filename = 'hosts.txt.' + source.name |
|
|
|
|
|
|
|
if source.last_modified: |
|
|
|
if source.last_modified: |
|
|
|
last_modified = source.last_modified.strftime('%a, %d %b %Y %H:%M:%S GMT') |
|
|
|
last_modified = source.last_modified.strftime('%a, %d %b %Y %H:%M:%S GMT') |
|
|
|
# prevent redownloading of hosts-file by passing If-Modified-Since http header |
|
|
|
# prevent redownloading of hosts-file by passing If-Modified-Since http header |
|
|
@ -86,7 +85,9 @@ for source in all_sources: |
|
|
|
except: |
|
|
|
except: |
|
|
|
log.warning('failed to read data from %s', source.name) |
|
|
|
log.warning('failed to read data from %s', source.name) |
|
|
|
continue |
|
|
|
continue |
|
|
|
f = open(filename, 'w') |
|
|
|
# save fetched content into temporary file |
|
|
|
|
|
|
|
fd, tmpfile = tempfile.mkstemp(text=True) |
|
|
|
|
|
|
|
f = os.fdopen(fd, 'w') |
|
|
|
f.write(content) |
|
|
|
f.write(content) |
|
|
|
f.close() |
|
|
|
f.close() |
|
|
|
# get last-modified info from header |
|
|
|
# get last-modified info from header |
|
|
@ -104,9 +105,10 @@ for source in all_sources: |
|
|
|
source.save() |
|
|
|
source.save() |
|
|
|
# form commnd-line for invoke injector |
|
|
|
# form commnd-line for invoke injector |
|
|
|
log.info('adding hosts from: %s', source.name) |
|
|
|
log.info('adding hosts from: %s', source.name) |
|
|
|
sp_args = ['py-i2phosts-injector', '-s', '-a', '-f', filename, '-d', |
|
|
|
sp_args = ['py-i2phosts-injector', '-s', '-a', '-f', tmpfile, '-d', |
|
|
|
'Auto-added from ' + source.name] |
|
|
|
'Auto-added from ' + source.name] |
|
|
|
p = subprocess.Popen(sp_args, shell=False, stdin=None, |
|
|
|
p = subprocess.Popen(sp_args, shell=False, stdin=None, |
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
|
|
|
out = p.communicate()[0] |
|
|
|
out = p.communicate()[0] |
|
|
|
|
|
|
|
os.remove(tmpfile) |
|
|
|
log.info('injector output: \n%s', out) |
|
|
|
log.info('injector output: \n%s', out) |
|
|
|