mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
Firewall code sexyness
This commit is contained in:
parent
ad4e8e57c2
commit
11c2a78835
1 changed files with 54 additions and 87 deletions
|
@ -35,7 +35,7 @@ def firewall_allow(protocol=None,port=None,ipv6=None,upnp=False):
|
|||
if (upnp):
|
||||
add_portmapping(protocol, upnp, ipv6)
|
||||
|
||||
if port<65536 and port>0:
|
||||
if 0 < port < 65536:
|
||||
if protocol == "Both":
|
||||
update_yml(port, 'TCP', 'a', ipv6)
|
||||
update_yml(port, 'UDP', 'a', ipv6)
|
||||
|
@ -50,6 +50,7 @@ def firewall_allow(protocol=None,port=None,ipv6=None,upnp=False):
|
|||
|
||||
return firewall_reload(upnp)
|
||||
|
||||
|
||||
def firewall_disallow(protocol=None, port=None, ipv6=None, upnp=False):
|
||||
"""
|
||||
Disallow port in iptables
|
||||
|
@ -75,6 +76,7 @@ def firewall_disallow(protocol=None,port=None,ipv6=None,upnp=False):
|
|||
|
||||
return firewall_reload(upnp)
|
||||
|
||||
|
||||
def firewall_list():
|
||||
"""
|
||||
Allow port in iptables
|
||||
|
@ -124,32 +126,6 @@ def firewall_reload(upnp=False):
|
|||
add_portmapping('TCP', upnp, True);
|
||||
add_portmapping('UDP', upnp, True);
|
||||
|
||||
"""for i,port in enumerate (firewall['ipv4']['TCP']):
|
||||
os.system ("iptables -A INPUT -p tcp -i eth0 --dport "+ str(port) +" -j ACCEPT")
|
||||
if upnp:
|
||||
add_portmapping(port,'TCP',upnp)
|
||||
|
||||
|
||||
|
||||
for i,port in enumerate (firewall['ipv4']['UDP']):
|
||||
os.system ("iptables -A INPUT -p udp -i eth0 --dport "+ str(port) +" -j ACCEPT")
|
||||
if upnp:
|
||||
add_portmapping(port,'UDP',upnp)
|
||||
|
||||
|
||||
for i,port in enumerate (firewall['ipv6']['TCP']):
|
||||
os.system ("ip6tables -A INPUT -p tcp -i eth0 --dport "+ str(port) +" -j ACCEPT")
|
||||
if upnp:
|
||||
add_portmapping(port,'TCP',upnp)
|
||||
|
||||
|
||||
|
||||
for i,port in enumerate (firewall['ipv6']['UDP']):
|
||||
os.system ("ip6tables -A INPUT -p udp -i eth0 --dport "+ str(port) +" -j ACCEPT")
|
||||
if upnp:
|
||||
add_portmapping(port,'UDP',upnp)"""
|
||||
|
||||
|
||||
os.system ("iptables -P INPUT DROP")
|
||||
os.system ("ip6tables -P INPUT DROP")
|
||||
|
||||
|
@ -157,6 +133,7 @@ def firewall_reload(upnp=False):
|
|||
|
||||
return firewall_list()
|
||||
|
||||
|
||||
def update_yml(port=None, protocol=None, mode=None, ipv6=None):
|
||||
"""
|
||||
Update firewall.yml
|
||||
|
@ -169,10 +146,8 @@ def update_yml(port=None,protocol=None,mode=None,ipv6=None):
|
|||
Return
|
||||
None
|
||||
"""
|
||||
if ipv6:
|
||||
ip = 'ipv6'
|
||||
else:
|
||||
ip = 'ipv4'
|
||||
if ipv6: ip = 'ipv6'
|
||||
else: ip = 'ipv4'
|
||||
|
||||
with open('firewall.yml', 'r') as f:
|
||||
firewall = yaml.load(f)
|
||||
|
@ -198,10 +173,9 @@ def update_yml(port=None,protocol=None,mode=None,ipv6=None):
|
|||
with open('firewall.yml', 'w') as f:
|
||||
yaml.dump(firewall, f)
|
||||
|
||||
|
||||
def add_portmapping(protocol=None, upnp=False, ipv6=None):
|
||||
"""
|
||||
|
||||
|
||||
Send a port mapping rules to igd device
|
||||
Keyword arguments:
|
||||
protocol -- Protocol used
|
||||
|
@ -226,21 +200,14 @@ def add_portmapping(protocol=None,upnp=False,ipv6=None):
|
|||
raise YunoHostError(22,_("Can't connect to the igd device"))
|
||||
|
||||
# list the redirections :
|
||||
i = 0
|
||||
for i in (0,100):
|
||||
for i in xrange(100):
|
||||
p = upnp.getgenericportmapping(i)
|
||||
if p==None:
|
||||
break
|
||||
port=p[0]
|
||||
proto=p[1]
|
||||
upnp.deleteportmapping(port,proto);
|
||||
i += 1
|
||||
if p is None: break
|
||||
upnp.deleteportmapping(p[0], p[1])
|
||||
|
||||
|
||||
if ipv6:
|
||||
ip = 'ipv6'
|
||||
else:
|
||||
ip = 'ipv4'
|
||||
if ipv6: ip = 'ipv6'
|
||||
else: ip = 'ipv4'
|
||||
with open('firewall.yml', 'r') as f:
|
||||
firewall = yaml.load(f)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue