mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
Bug fix for upnp support
This commit is contained in:
parent
00b712f5bf
commit
70a31ee260
1 changed files with 48 additions and 32 deletions
|
@ -14,7 +14,7 @@ from yunohost import YunoHostError, win_msg
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def firewall_allow(protocol=None,port=None,ipv6=None):
|
def firewall_allow(protocol=None,port=None,ipv6=None,upnp=None):
|
||||||
"""
|
"""
|
||||||
Allow port in iptables
|
Allow port in iptables
|
||||||
|
|
||||||
|
@ -22,6 +22,7 @@ def firewall_allow(protocol=None,port=None,ipv6=None):
|
||||||
protocol -- Protocol used
|
protocol -- Protocol used
|
||||||
port -- Port to open
|
port -- Port to open
|
||||||
ipv6 -- Boolean ipv6
|
ipv6 -- Boolean ipv6
|
||||||
|
upnp --Boolean upnp
|
||||||
|
|
||||||
Return
|
Return
|
||||||
Dict
|
Dict
|
||||||
|
@ -41,11 +42,11 @@ def firewall_allow(protocol=None,port=None,ipv6=None):
|
||||||
else:
|
else:
|
||||||
raise YunoHostError(22,_("Port not between 1 and 65535 : ")+str(port))
|
raise YunoHostError(22,_("Port not between 1 and 65535 : ")+str(port))
|
||||||
|
|
||||||
return firewall_reload()
|
return firewall_reload(upnp)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def firewall_disallow(protocol=None,port=None,ipv6=None):
|
def firewall_disallow(protocol=None,port=None,ipv6=None,upnp=None):
|
||||||
"""
|
"""
|
||||||
Disallow port in iptables
|
Disallow port in iptables
|
||||||
|
|
||||||
|
@ -53,6 +54,7 @@ def firewall_disallow(protocol=None,port=None,ipv6=None):
|
||||||
protocol -- Protocol used
|
protocol -- Protocol used
|
||||||
port -- Port to open
|
port -- Port to open
|
||||||
ipv6 -- Boolean ipv6
|
ipv6 -- Boolean ipv6
|
||||||
|
upnp --Boolan upnp
|
||||||
|
|
||||||
Return
|
Return
|
||||||
Dict
|
Dict
|
||||||
|
@ -67,7 +69,7 @@ def firewall_disallow(protocol=None,port=None,ipv6=None):
|
||||||
update_yml(port,protocol,'r',ipv6)
|
update_yml(port,protocol,'r',ipv6)
|
||||||
win_msg(_("Port successfully closed"))
|
win_msg(_("Port successfully closed"))
|
||||||
|
|
||||||
return firewall_reload()
|
return firewall_reload(upnp)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -88,12 +90,12 @@ def firewall_list():
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def firewall_reload():
|
def firewall_reload(upnp=None):
|
||||||
'''
|
'''
|
||||||
Reload iptables configuration
|
Reload iptables configuration
|
||||||
|
|
||||||
Keyword arguments:
|
Keyword arguments:
|
||||||
None
|
upnp --Boolean upnp
|
||||||
|
|
||||||
Return
|
Return
|
||||||
Dict
|
Dict
|
||||||
|
@ -111,56 +113,57 @@ def firewall_reload():
|
||||||
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")
|
||||||
|
if upnp:
|
||||||
u=miniupnpc.UPnP()
|
upnp=miniupnpc.UPnP()
|
||||||
u.discoverdelay=200
|
upnp.discoverdelay=200
|
||||||
|
nbigd= upnp.discover()
|
||||||
if u.discover()==0:
|
if nbigd:
|
||||||
igd=False
|
try:
|
||||||
win_msg(_("No igd devices found, you'll need to manually open port on your router"))
|
upnp.selectigd()
|
||||||
|
except:
|
||||||
|
raise YunoHostError(167,_("No upnp devices found"))
|
||||||
else:
|
else:
|
||||||
u.selectigd()
|
raise YunoHostError(22,_("Can't connect to the igd device"))
|
||||||
igd=True
|
igd=True
|
||||||
# list the redirections :
|
# list the redirections :
|
||||||
i = 0
|
i = 0
|
||||||
while True:
|
for i in (0,100):
|
||||||
p = u.getgenericportmapping(i)
|
p = upnp.getgenericportmapping(i)
|
||||||
if p==None:
|
if p==None:
|
||||||
break
|
break
|
||||||
(port, proto, (ihost,iport), desc, c, d, e) = p
|
port=p[0]
|
||||||
u.deleteportmapping(port,proto);
|
proto=p[1]
|
||||||
#print port, desc
|
upnp.deleteportmapping(port,proto);
|
||||||
i = i + 1
|
i += 1
|
||||||
|
|
||||||
|
|
||||||
|
print("plop")
|
||||||
if 22 not in firewall['ipv6']['TCP']:
|
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")
|
||||||
if igd:
|
if upnp:
|
||||||
u.addportmapping(port,'TCP',u.lanaddr,port,'UPnP IGD Tester port %u' % port, '')
|
add_portmapping(port,'TCP',upnp)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
for i,port in enumerate (firewall['ipv4']['UDP']):
|
for i,port in enumerate (firewall['ipv4']['UDP']):
|
||||||
os.system ("iptables -A INPUT -p udp -i eth0 --dport "+ str(port) +" -j ACCEPT")
|
os.system ("iptables -A INPUT -p udp -i eth0 --dport "+ str(port) +" -j ACCEPT")
|
||||||
if igd:
|
if upnp:
|
||||||
u.addportmapping(port,'UDP',u.lanaddr,port,'UPnP IGD Tester port %u' % port, '')
|
add_portmapping(port,'UDP',upnp)
|
||||||
|
|
||||||
|
|
||||||
for i,port in enumerate (firewall['ipv6']['TCP']):
|
for i,port in enumerate (firewall['ipv6']['TCP']):
|
||||||
os.system ("ip6tables -A INPUT -p tcp -i eth0 --dport "+ str(port) +" -j ACCEPT")
|
os.system ("ip6tables -A INPUT -p tcp -i eth0 --dport "+ str(port) +" -j ACCEPT")
|
||||||
if igd:
|
if upnp:
|
||||||
u.addportmapping(port,'TCP',u.lanaddr,port,'UPnP IGD Tester port %u' % port, '')
|
add_portmapping(port,'TCP',upnp)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
for i,port in enumerate (firewall['ipv6']['UDP']):
|
for i,port in enumerate (firewall['ipv6']['UDP']):
|
||||||
os.system ("ip6tables -A INPUT -p udp -i eth0 --dport "+ str(port) +" -j ACCEPT")
|
os.system ("ip6tables -A INPUT -p udp -i eth0 --dport "+ str(port) +" -j ACCEPT")
|
||||||
if igd:
|
if upnp:
|
||||||
u.addportmapping(port,'UDP',u.lanaddr,port,'UPnP IGD Tester port %u' % port, '')
|
add_portmapping(port,'UDP',upnp)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
os.system ("iptables -P INPUT DROP")
|
os.system ("iptables -P INPUT DROP")
|
||||||
|
@ -212,3 +215,16 @@ def update_yml(port=None,protocol=None,mode=None,ipv6=None):
|
||||||
|
|
||||||
with open('firewall.yml','w') as f:
|
with open('firewall.yml','w') as f:
|
||||||
yaml.dump(firewall,f)
|
yaml.dump(firewall,f)
|
||||||
|
|
||||||
|
def add_portmapping(port=None,protocol=None,upnp=None):
|
||||||
|
"""
|
||||||
|
Send a port mapping rules to igd device
|
||||||
|
Keyword arguments:
|
||||||
|
protocol -- Protocol used
|
||||||
|
port -- Port to open
|
||||||
|
|
||||||
|
Return
|
||||||
|
None
|
||||||
|
"""
|
||||||
|
upnp.addportmapping(port,protocol,upnp.lanaddr,port,'UPnP IGD Tester port %u' % port, '')
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue