#14188·XX-Net

`set_iptables` creates duplicate NAT rules when executed multiple times

Author: Zohaibcode740Created Sep 29, 2026Updated Sep 29, 2026

Description

The set_iptables command does not check whether the REDSOCKS chain or its rules already exist.

The script currently contains:

bash
iptables -t nat -N REDSOCKS

followed by multiple iptables -t nat -A ... commands.

Because the script does not use set -e or check the return status of each command, running set_iptables more than once can result in:

  1. iptables -N REDSOCKS failing because the chain already exists.
  2. The subsequent -A commands still executing.
  3. Duplicate rules being appended to the existing REDSOCKS chain.
  4. Duplicate PREROUTING rules being added.

The script itself currently has the comment:

bash
# TODO: check if rule exist

Steps to reproduce

On a Linux/OpenWrt system with iptables:

bash
./start set_iptables
./start set_iptables

Then inspect the NAT table:

bash
iptables -t nat -L REDSOCKS --line-numbers
iptables -t nat -L PREROUTING --line-numbers

The second execution can leave duplicate rules.

Expected behavior

Running:

bash
./start set_iptables

multiple times should be idempotent: existing chains/rules should be detected and not added again.

Suggested fix

Before creating the chain/rules, check whether they already exist, or make set_iptables idempotent by deleting/recreating the managed rules safely.

For example, the script could check:

bash
iptables -t nat -L REDSOCKS >/dev/null 2>&1 || \
    iptables -t nat -N REDSOCKS

and similarly check the individual rules before adding them.

The cleanup operation should also remain safe when the rules/chain do not exist.