|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import ipaddress |
| 4 | +import os |
| 5 | +import re |
| 6 | +import socket |
| 7 | +import subprocess |
| 8 | +import syslog |
| 9 | + |
| 10 | +UNIT_TESTING = 0 |
| 11 | + |
| 12 | +# NOTE: |
| 13 | +# Unable to use python-iptables as that does not create rules per ip-tables default |
| 14 | +# which is nf_tables. So rules added via iptc package will not be listed under |
| 15 | +# "sudo iptables -t nat -L -n". But available in kernel. To list, we need to |
| 16 | +# use legacy mode as "sudo iptables-legacy -t nat -L -n". |
| 17 | +# As we can't use two modes and using non-default could make any debugging effort |
| 18 | +# very tough. |
| 19 | + |
| 20 | + |
| 21 | +from urllib.parse import urlparse |
| 22 | + |
| 23 | +DST_FILE = "/etc/systemd/system/docker.service.d/http_proxy.conf" |
| 24 | +DST_IP = None |
| 25 | +DST_PORT = None |
| 26 | +SQUID_PORT = "3128" |
| 27 | + |
| 28 | +def _get_ip(ip_str): |
| 29 | + ret = "" |
| 30 | + if ip_str: |
| 31 | + try: |
| 32 | + ipaddress.ip_address(ip_str) |
| 33 | + ret = ip_str |
| 34 | + except ValueError: |
| 35 | + pass |
| 36 | + |
| 37 | + if not ret: |
| 38 | + try: |
| 39 | + ret = socket.gethostbyname(ip_str) |
| 40 | + except (OSError, socket.error): |
| 41 | + pass |
| 42 | + if not ret: |
| 43 | + syslog.syslog(syslog.LOG_ERR, "{} is neither IP nor resolves to IP". |
| 44 | + format(ip_str)) |
| 45 | + return ret |
| 46 | + |
| 47 | + |
| 48 | +def _get_dst_info(): |
| 49 | + global DST_IP, DST_PORT |
| 50 | + DST_IP = None |
| 51 | + DST_PORT = None |
| 52 | + print("DST_FILE={}".format(DST_FILE)) |
| 53 | + if os.path.exists(DST_FILE): |
| 54 | + with open(DST_FILE, "r") as s: |
| 55 | + for line in s.readlines(): |
| 56 | + url_match = re.search('^Environment=.HTTP_PROXY=(.+?)"', line) |
| 57 | + if url_match: |
| 58 | + url = urlparse(url_match.group(1)) |
| 59 | + DST_IP = _get_ip(url.hostname) |
| 60 | + DST_PORT = url.port |
| 61 | + break |
| 62 | + else: |
| 63 | + print("{} not available".format(DST_FILE)) |
| 64 | + print("DST_IP={}".format(DST_IP)) |
| 65 | + |
| 66 | + |
| 67 | +def _is_rule_match(rule): |
| 68 | + expect = "DNAT tcp -- 0.0.0.0/0 {} tcp dpt:{} to:".format( |
| 69 | + DST_IP, DST_PORT) |
| 70 | + |
| 71 | + # Remove duplicate spaces |
| 72 | + rule = " ".join(rule.split()).strip() |
| 73 | + |
| 74 | + if rule.startswith(expect): |
| 75 | + return rule[len(expect):] |
| 76 | + else: |
| 77 | + return "" |
| 78 | + |
| 79 | + |
| 80 | +def check_proc(proc): |
| 81 | + if proc.returncode: |
| 82 | + syslog.syslog(syslog.LOG_ERR, "Failed to run: cmd: {}".format(proc.args)) |
| 83 | + syslog.syslog(syslog.LOG_ERR, "Failed to run: stdout: {}".format(proc.stdout)) |
| 84 | + syslog.syslog(syslog.LOG_ERR, "Failed to run: stderr: {}".format(proc.stderr)) |
| 85 | + if not UNIT_TESTING: |
| 86 | + assert False |
| 87 | + |
| 88 | + |
| 89 | +def iptable_proxy_rule_upd(ip_str, port = SQUID_PORT): |
| 90 | + _get_dst_info() |
| 91 | + if not DST_IP: |
| 92 | + # There is no proxy in use. Bail out. |
| 93 | + return "" |
| 94 | + |
| 95 | + destination = "" |
| 96 | + if ip_str: |
| 97 | + upd_ip = _get_ip(ip_str) |
| 98 | + if not upd_ip: |
| 99 | + return "" |
| 100 | + destination = "{}:{}".format(upd_ip, port) |
| 101 | + |
| 102 | + found = False |
| 103 | + num = 0 |
| 104 | + |
| 105 | + while True: |
| 106 | + num += 1 |
| 107 | + |
| 108 | + cmd = "sudo iptables -t nat -n -L OUTPUT {}".format(num) |
| 109 | + proc = subprocess.run(cmd, shell=True, capture_output=True) |
| 110 | + check_proc(proc) |
| 111 | + |
| 112 | + if not proc.stdout: |
| 113 | + # No more rule |
| 114 | + break |
| 115 | + |
| 116 | + rule_dest = _is_rule_match(proc.stdout.decode("utf-8").strip()) |
| 117 | + if rule_dest: |
| 118 | + if not found and destination and (rule_dest == destination): |
| 119 | + found = True |
| 120 | + else: |
| 121 | + # Duplicate or different IP - delete it |
| 122 | + cmd = "sudo iptables -t nat -D OUTPUT {}".format(num) |
| 123 | + proc = subprocess.run(cmd, shell=True, capture_output=True) |
| 124 | + check_proc(proc) |
| 125 | + # Decrement number to accommodate deleted rule |
| 126 | + num -= 1 |
| 127 | + |
| 128 | + if destination and not found: |
| 129 | + cmd = "sudo iptables -t nat -A OUTPUT -p tcp -d {} --dport {} -j DNAT --to-destination {}".format( |
| 130 | + DST_IP, DST_PORT, destination) |
| 131 | + proc = subprocess.run(cmd, shell=True, capture_output=True) |
| 132 | + |
| 133 | + check_proc(proc) |
| 134 | + |
| 135 | + return destination |
0 commit comments