| 1 | #!/bin/sh |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | set -eux |
| 7 | |
| 8 | readonly chain=LIMADNS |
| 9 | |
| 10 | chain_exists() { |
| 11 | iptables --table nat -n --list "${chain}" >/dev/null 2>&1 |
| 12 | } |
| 13 | |
| 14 | |
| 15 | if command -v iptables >/dev/null 2>&1; then |
| 16 | if ! chain_exists; then |
| 17 | iptables --table nat --new-chain ${chain} |
| 18 | iptables --table nat --insert PREROUTING 1 --jump "${chain}" |
| 19 | iptables --table nat --insert OUTPUT 1 --jump "${chain}" |
| 20 | fi |
| 21 | |
| 22 | |
| 23 | iptables --table nat --flush ${chain} |
| 24 | |
| 25 | if [ -n "${LIMA_CIDATA_UDP_DNS_LOCAL_PORT}" ] && [ "${LIMA_CIDATA_UDP_DNS_LOCAL_PORT}" -ne 0 ]; then |
| 26 | iptables --table nat --append "${chain}" --destination "${LIMA_CIDATA_SLIRP_DNS}" --protocol udp --dport 53 --jump DNAT \ |
| 27 | --to-destination "${LIMA_CIDATA_SLIRP_GATEWAY}:${LIMA_CIDATA_UDP_DNS_LOCAL_PORT}" |
| 28 | fi |
| 29 | if [ -n "${LIMA_CIDATA_TCP_DNS_LOCAL_PORT}" ] && [ "${LIMA_CIDATA_TCP_DNS_LOCAL_PORT}" -ne 0 ]; then |
| 30 | iptables --table nat --append "${chain}" --destination "${LIMA_CIDATA_SLIRP_DNS}" --protocol tcp --dport 53 --jump DNAT \ |
| 31 | --to-destination "${LIMA_CIDATA_SLIRP_GATEWAY}:${LIMA_CIDATA_TCP_DNS_LOCAL_PORT}" |
| 32 | fi |
| 33 | fi |