mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
commit
bb4a1065fe
2 changed files with 67 additions and 86 deletions
|
@ -45,9 +45,9 @@ def pretty_print_dict(d, depth=0):
|
||||||
elif isinstance(v, list):
|
elif isinstance(v, list):
|
||||||
print((" ") * depth + ("%s: " % k))
|
print((" ") * depth + ("%s: " % k))
|
||||||
for value in v:
|
for value in v:
|
||||||
print((" ") * (depth+1) + "- " + value)
|
print((" ") * (depth+1) + "- " +str(value))
|
||||||
else:
|
else:
|
||||||
print((" ") * depth + "%s: %s" % (k, v))
|
print((" ") * depth + "%s: %s" % (k, str(v)))
|
||||||
|
|
||||||
def win_msg(astr):
|
def win_msg(astr):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -8,6 +8,7 @@ except ImportError:
|
||||||
sys.stderr.write('Error: Yunohost CLI Require yaml lib\n')
|
sys.stderr.write('Error: Yunohost CLI Require yaml lib\n')
|
||||||
sys.stderr.write('apt-get install python-yaml\n')
|
sys.stderr.write('apt-get install python-yaml\n')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
from yunohost import YunoHostError, win_msg
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -24,30 +25,21 @@ def firewall_allow(protocol=None,port=None,ipv6=None):
|
||||||
Dict
|
Dict
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if ipv6 == True:
|
port=int(port)
|
||||||
ip = 'ipv6'
|
if port<65536 and port>0:
|
||||||
iptables="ip6tables"
|
|
||||||
else:
|
|
||||||
ip = 'ipv4'
|
|
||||||
iptables="iptables"
|
|
||||||
|
|
||||||
if protocol == "Both":
|
if protocol == "Both":
|
||||||
TCP_rule = iptables+" -A INPUT -p tcp -i eth0 --dport "+ port +" -j ACCEPT"
|
update_yml(port,'TCP','a',ipv6)
|
||||||
UDP_rule = iptables+" -A INPUT -p udp -i eth0 --dport "+ port +" -j ACCEPT"
|
update_yml(port,'UDP','a',ipv6)
|
||||||
|
|
||||||
update_yml(port,'tcp','a',ip)
|
|
||||||
update_yml(port,'udp','a',ip)
|
|
||||||
|
|
||||||
os.system(TCP_rule)
|
|
||||||
os.system(UDP_rule)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
rule = iptables+" -A INPUT -p "+ protocol +" -i eth0 --dport "+ port +" -j ACCEPT"
|
update_yml(port,protocol,'a',ipv6)
|
||||||
update_yml(port,protocol,'a',ip)
|
|
||||||
os.system(rule)
|
|
||||||
|
|
||||||
win_msg(_("Port successfully openned"))
|
win_msg(_("Port successfully openned"))
|
||||||
return firewall_list()
|
|
||||||
|
else:
|
||||||
|
raise YunoHostError(22,_("Port not between 1 and 65535 : ")+str(port))
|
||||||
|
|
||||||
|
return firewall_reload()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -65,29 +57,15 @@ def firewall_disallow(protocol=None,port=None,ipv6=None):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if ipv6 == True:
|
port=int(port)
|
||||||
ip = 'ipv6'
|
|
||||||
iptables="ip6tables"
|
|
||||||
else:
|
|
||||||
ip = 'ipv4'
|
|
||||||
iptables="ip6tables"
|
|
||||||
|
|
||||||
if protocol == "Both":
|
if protocol == "Both":
|
||||||
TCP_rule = iptables+" -A INPUT -p tcp -i eth0 --dport "+ port +" -j REJECT"
|
update_yml(port,'TCP','r',ipv6)
|
||||||
UDP_rule = iptables+" -A INPUT -p udp -i eth0 --dport "+ port +" -j REJECT"
|
update_yml(port,'UDP','r',ipv6)
|
||||||
|
|
||||||
update_yml(port,'tcp','r',ip)
|
|
||||||
update_yml(port,'udp','r',ip)
|
|
||||||
|
|
||||||
os.system(TCP_rule)
|
|
||||||
os.system(UDP_rule)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
rule = iptables+" -A INPUT -p "+ protocol +" -i eth0 --dport "+ port +" -j REJECT"
|
update_yml(port,protocol,'r',ipv6)
|
||||||
update_yml(port,protocol,'r',ip)
|
|
||||||
os.system(rule)
|
|
||||||
win_msg(_("Port successfully closed"))
|
win_msg(_("Port successfully closed"))
|
||||||
return firewall_list
|
|
||||||
|
return firewall_reload()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -124,15 +102,15 @@ def firewall_reload():
|
||||||
os.system ("iptables -P INPUT ACCEPT")
|
os.system ("iptables -P INPUT ACCEPT")
|
||||||
os.system ("iptables -F")
|
os.system ("iptables -F")
|
||||||
os.system ("iptables -X")
|
os.system ("iptables -X")
|
||||||
os.system ("iptables -A INPUT -p tcp -i eth0 --dport 22 -j ACCEPT")
|
if 22 not in firewall['ipv4']['TCP']:
|
||||||
update_yml('22','TCP','a',False)
|
update_yml(22,'TCP','a',False)
|
||||||
|
|
||||||
|
|
||||||
os.system ("ip6tables -P INPUT ACCEPT")
|
os.system ("ip6tables -P INPUT ACCEPT")
|
||||||
os.system ("ip6tables -F")
|
os.system ("ip6tables -F")
|
||||||
os.system ("ip6tables -X")
|
os.system ("ip6tables -X")
|
||||||
os.system ("ip6tables -A INPUT -p tcp -i eth0 --dport 22 -j ACCEPT")
|
if 22 not in firewall['ipv6']['TCP']:
|
||||||
update_yml('22','TCP','a',True)
|
update_yml(22,'TCP','a',True)
|
||||||
|
|
||||||
for i,port in enumerate (firewall['ipv4']['TCP']):
|
for i,port in enumerate (firewall['ipv4']['TCP']):
|
||||||
os.system ("iptables -A INPUT -p tcp -i eth0 --dport "+ str(port) +" -j ACCEPT")
|
os.system ("iptables -A INPUT -p tcp -i eth0 --dport "+ str(port) +" -j ACCEPT")
|
||||||
|
@ -154,14 +132,14 @@ def firewall_reload():
|
||||||
os.system ("ip6tables -P INPUT DROP")
|
os.system ("ip6tables -P INPUT DROP")
|
||||||
|
|
||||||
win_msg(_("Firewall successfully reloaded"))
|
win_msg(_("Firewall successfully reloaded"))
|
||||||
|
|
||||||
return firewall_list()
|
return firewall_list()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def update_yml(port=None,protocol=None,mode=None,ip=None):
|
def update_yml(port=None,protocol=None,mode=None,ipv6=None):
|
||||||
"""
|
"""
|
||||||
Update firewall.yml
|
Update firewall.yml
|
||||||
|
|
||||||
Keyword arguments:
|
Keyword arguments:
|
||||||
protocol -- Protocol used
|
protocol -- Protocol used
|
||||||
port -- Port to open
|
port -- Port to open
|
||||||
|
@ -170,29 +148,32 @@ def update_yml(port=None,protocol=None,mode=None,ip=None):
|
||||||
|
|
||||||
Return
|
Return
|
||||||
None
|
None
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
if ipv6:
|
||||||
|
ip = 'ipv6'
|
||||||
|
else:
|
||||||
|
ip = 'ipv4'
|
||||||
|
|
||||||
with open('firewall.yml','r') as f:
|
with open('firewall.yml','r') as f:
|
||||||
firewall = yaml.load(f)
|
firewall = yaml.load(f)
|
||||||
|
|
||||||
if mode == 'a':
|
if mode == 'a':
|
||||||
if int(port) not in firewall[ip][protocol]:
|
if port not in firewall[ip][protocol]:
|
||||||
firewall[ip][protocol].append(int(port))
|
firewall[ip][protocol].append(port)
|
||||||
print("Port "+port+" on protocol "+protocol+" with "+ip+" Open")
|
|
||||||
else:
|
else:
|
||||||
print("Port already open")
|
raise YunoHostError(22,_("Port already openned :")+str(port))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if int(port) in firewall[ip][protocol]:
|
if port in firewall[ip][protocol]:
|
||||||
firewall[ip][protocol].remove(int(port))
|
firewall[ip][protocol].remove(port)
|
||||||
print("Port "+port+" on protocol "+protocol+" with "+ip+" Close")
|
|
||||||
else:
|
else:
|
||||||
print("Port already close")
|
raise YunoHostError(22,_("Port already closed :")+str(port))
|
||||||
|
|
||||||
firewall[ip][protocol].sort()
|
firewall[ip][protocol].sort()
|
||||||
|
|
||||||
os.system("mv firewall.yml firewall.yml.old")
|
os.system("mv firewall.yml firewall.yml.old")
|
||||||
|
|
||||||
with open('firewall.yml','w') as f:
|
with open('firewall.yml','w') as f:
|
||||||
yaml.dump(firewall,f)
|
yaml.dump(firewall,f)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue