From dd504249abe202f79a7236935447a369fa42eae8 Mon Sep 17 00:00:00 2001 From: CUI Wei Date: Sun, 27 Jan 2019 22:35:15 +0800 Subject: [PATCH 1/2] add UDP4 mapping for iptables --- src/utils/nettools.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/utils/nettools.py b/src/utils/nettools.py index 4f9e7f8..3c8525c 100755 --- a/src/utils/nettools.py +++ b/src/utils/nettools.py @@ -433,6 +433,11 @@ class portcontrol(object): return [True, str(free_port)] except subprocess.CalledProcessError as suberror: return [False, "set port mapping failed : %s" % suberror.stdout.decode('utf-8')] + try: + subprocess.run(['iptables','-t','nat','-A','PREROUTING','-p','udp','--dport',str(free_port),"-j","DNAT",'--to-destination','%s:%s'%(container_ip,container_port)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True) + return [True, str(free_port)] + except subprocess.CalledProcessError as suberror: + return [False, "set port mapping failed : %s" % suberror.stdout.decode('utf-8')] @staticmethod def release_port_mapping(container_name, container_ip, container_port): @@ -447,6 +452,10 @@ class portcontrol(object): subprocess.run(['iptables','-t','nat','-D','PREROUTING','-p','tcp','--dport',str(free_port),"-j","DNAT",'--to-destination','%s:%s'%(container_ip,container_port)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True) except subprocess.CalledProcessError as suberror: return [False, "release port mapping failed : %s" % suberror.stdout.decode('utf-8')] + try: + subprocess.run(['iptables','-t','nat','-D','PREROUTING','-p','udp','--dport',str(free_port),"-j","DNAT",'--to-destination','%s:%s'%(container_ip,container_port)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True) + except subprocess.CalledProcessError as suberror: + return [False, "release port mapping failed : %s" % suberror.stdout.decode('utf-8')] ports_lock.acquire() free_ports[free_port] = True allocated_ports[container_name].pop(container_port) From 3ffa010725b58bfb79c4a79ee1a9ab6b0992dbd5 Mon Sep 17 00:00:00 2001 From: Yujian Zhu Date: Mon, 28 Jan 2019 16:31:11 +0800 Subject: [PATCH 2/2] Update nettools.py --- src/utils/nettools.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/utils/nettools.py b/src/utils/nettools.py index 3c8525c..9293278 100755 --- a/src/utils/nettools.py +++ b/src/utils/nettools.py @@ -430,7 +430,6 @@ class portcontrol(object): ports_lock.release() try: subprocess.run(['iptables','-t','nat','-A','PREROUTING','-p','tcp','--dport',str(free_port),"-j","DNAT",'--to-destination','%s:%s'%(container_ip,container_port)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True) - return [True, str(free_port)] except subprocess.CalledProcessError as suberror: return [False, "set port mapping failed : %s" % suberror.stdout.decode('utf-8')] try: