From 130ce3318b912d79540c98e1c86b4d7e11e83e82 Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Sun, 6 Feb 2022 08:27:23 +0000 Subject: [PATCH] fix rootless network host-container communication --- generate.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/generate.py b/generate.py index abd8ea5..ea80cf7 100644 --- a/generate.py +++ b/generate.py @@ -9,6 +9,7 @@ import base64 path_get_gateway = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), 'tools/get-gateway.py') path_get_ip = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), 'tools/get-ip.py') +path_get_lan_ip = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), 'tools/get-lan-ip.py') path_bin_dir = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), 'bin') path_app_dir = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), 'app') @@ -401,16 +402,21 @@ class Parser: self.result_postup.extend(self.result_container_prebootstrap) - if not self.flag_container_must_host: - self.result_postup.append('PostUp={}'.format( - self.get_podman_cmd_with('CT_GATEWAY=$(/usr/bin/python3 {} {}); podman exec -t -e GATEWAY_IP=$CT_GATEWAY -e WG_PORT={} {} /usr/bin/python3 /root/app/bootstrap.py'.format( - path_get_gateway, self.get_container_network_name(), self.wg_port, self.get_container_name())) - )) - else: + if self.flag_container_must_host: self.result_postup.append('PostUp={}'.format( self.get_podman_cmd_with('podman exec -t -e GATEWAY_IP=127.0.0.1 -e WG_PORT={} {} /usr/bin/python3 /root/app/bootstrap.py'.format( self.wg_port, self.get_container_name())) )) + elif self.podman_user: + self.result_postup.append('PostUp={}'.format( + self.get_podman_cmd_with('CT_GATEWAY=$(/usr/bin/python3 {}); podman exec -t -e GATEWAY_IP=$CT_GATEWAY -e WG_PORT={} {} /usr/bin/python3 /root/app/bootstrap.py'.format( + path_get_lan_ip, self.wg_port, self.get_container_name())) + )) + else: + self.result_postup.append('PostUp={}'.format( + self.get_podman_cmd_with('CT_GATEWAY=$(/usr/bin/python3 {} {}); podman exec -t -e GATEWAY_IP=$CT_GATEWAY -e WG_PORT={} {} /usr/bin/python3 /root/app/bootstrap.py'.format( + path_get_gateway, self.get_container_network_name(), self.wg_port, self.get_container_name())) + )) self.result_postup.extend(self.result_container_postbootstrap) @@ -450,13 +456,14 @@ class Parser: if addr_host == "gateway": tunnel_addr = "" - if not self.flag_container_must_host: + if self.flag_container_must_host or self.podman_user: + self.result_postup.append("PostUp=wg set {} peer {} endpoint 127.0.0.1:{}".format( + self.wg_name, current_pubkey, addr_port)) + else: self.result_postup.append("PostUp=CT_IP=$({}); wg set {} peer {} endpoint $CT_IP:{}".format( self.get_podman_cmd_with('/usr/bin/python3 {} {} {}'.format(path_get_ip, self.get_container_network_name(), self.get_container_name())), self.wg_name, current_pubkey, addr_port)) - else: - self.result_postup.append("PostUp=wg set {} peer {} endpoint 127.0.0.1:{}".format( - self.wg_name, current_pubkey, addr_port)) + elif tunnel_addr: tunnel_addr = "127.0.0.1:{}".format(tunnel_addr)