Correction de la non prise en charge des blacklists appartements
This commit is contained in:
parent
8b86e04dba
commit
14167847db
5 changed files with 66 additions and 82 deletions
|
@ -30,24 +30,23 @@ if not os.path.exists(IPSET_PATH):
|
|||
|
||||
class IpsetError(Exception):
|
||||
# Gestion des erreurs d'ipset
|
||||
def __init__(self,cmd,err_code,output):
|
||||
self.cmd=cmd
|
||||
self.err_code=err_code
|
||||
self.output=output
|
||||
def __init__(self, cmd, err_code, output):
|
||||
self.cmd = cmd
|
||||
self.err_code = err_code
|
||||
self.output = output
|
||||
def __str__(self):
|
||||
return "%s\n status : %s\n %s" % (self.cmd,self.err_code,self.output)
|
||||
return "%s\n status : %s\n %s" % (self.cmd, self.err_code, self.output)
|
||||
|
||||
class Ipset(object):
|
||||
ipset=IPSET_PATH
|
||||
ipset = IPSET_PATH
|
||||
|
||||
def __str__(self):
|
||||
return self.set
|
||||
|
||||
def __init__(self,set,type,typeopt=''):
|
||||
self.set=set
|
||||
self.type=type
|
||||
self.typeopt=typeopt
|
||||
self.squeeze = os.uname()[2] < '3'
|
||||
def __init__(self, set, type, typeopt=''):
|
||||
self.set = set
|
||||
self.type = type
|
||||
self.typeopt = typeopt
|
||||
try:
|
||||
self.create()
|
||||
except IpsetError as error:
|
||||
|
@ -57,62 +56,58 @@ class Ipset(object):
|
|||
raise
|
||||
pass
|
||||
|
||||
def call(self,cmd,arg=''):
|
||||
def call(self, cmd, arg=''):
|
||||
"""Appel système à ipset"""
|
||||
cmd_line="%s %s %s %s" % (self.ipset,cmd,self.set,arg)
|
||||
status,output=commands.getstatusoutput(cmd_line)
|
||||
cmd_line = "%s %s %s %s" % (self.ipset, cmd, self.set, arg)
|
||||
status, output = commands.getstatusoutput(cmd_line)
|
||||
if status:
|
||||
raise IpsetError(cmd_line,status,output)
|
||||
raise IpsetError(cmd_line, status, output)
|
||||
return output
|
||||
|
||||
def create(self,opt=''):
|
||||
self.call("-N","%s %s" % (self.type, self.typeopt))
|
||||
def create(self, opt=''):
|
||||
self.call("create", "%s %s" % (self.type, self.typeopt))
|
||||
|
||||
def add(self,arg):
|
||||
self.call("-A",arg)
|
||||
def add(self, arg):
|
||||
self.call("add", arg)
|
||||
|
||||
def list(self):
|
||||
output=self.call("-L").splitlines()
|
||||
list=[]
|
||||
output = self.call("list").splitlines()
|
||||
list = []
|
||||
for line in output[6:]:
|
||||
if line=='Bindings:':
|
||||
if line == 'Bindings:':
|
||||
break
|
||||
list.append(line)
|
||||
return list
|
||||
|
||||
def delete(self,ip):
|
||||
def delete(self, ip):
|
||||
"""Delete an IP"""
|
||||
self.call("-D",ip)
|
||||
|
||||
def restore(self,rules):
|
||||
self.call("del", ip)
|
||||
|
||||
def restore(self, rules):
|
||||
""" restore le set courrant"""
|
||||
rules_str=self.restore_format(rules)
|
||||
if self.squeeze:
|
||||
create_str="-N %s %s %s" % (self.set,self.type,self.typeopt)
|
||||
str="%s\n%s\nCOMMIT\n" % (create_str,rules_str)
|
||||
else:
|
||||
str="%s\nCOMMIT\n" % rules_str
|
||||
path='/tmp/ipset_%s' % self.set
|
||||
f=open(path, 'w+')
|
||||
rules_str = self.restore_format(rules)
|
||||
str = "%s\nCOMMIT\n" % rules_str
|
||||
path = '/tmp/ipset_%s' % self.set
|
||||
f = open(path, 'w+')
|
||||
f.write(str)
|
||||
f.close()
|
||||
try:
|
||||
self.flush()
|
||||
if self.squeeze:
|
||||
self.destroy()
|
||||
except IpsetError as error: sys.stderr.write("%s\n" % error)
|
||||
cmd="cat %s | %s -R" % (path,self.ipset)
|
||||
status,output=commands.getstatusoutput(cmd)
|
||||
except IpsetError as error:
|
||||
sys.stderr.write("%s\n" % error)
|
||||
|
||||
cmd = "cat %s | %s -R" % (path, self.ipset)
|
||||
status, output = commands.getstatusoutput(cmd)
|
||||
if status:
|
||||
raise IpsetError(cmd,status,output)
|
||||
raise IpsetError(cmd, status, output)
|
||||
return output
|
||||
|
||||
def flush(self):
|
||||
self.call("-F")
|
||||
self.call("flush")
|
||||
|
||||
def destroy(self):
|
||||
self.call("-X")
|
||||
self.call("destroy")
|
||||
|
||||
def restore_format(self,rules):
|
||||
return '\n'.join(["-A %s %s" % (self.set,data) for data in rules])
|
||||
def restore_format(self, rules):
|
||||
return '\n'.join(["add %s %s" % (self.set, data) for data in rules])
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue