#!/bin/bash

# Определяем правила iptables
add_rules() {
    iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 50991 -j DNAT --to-destination 10.25.10.102:9001
    iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 50022 -j DNAT --to-destination 10.25.10.102:22
    iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 50144 -j DNAT --to-destination 10.25.10.102:1433
	iptables -t nat -A PREROUTING -i vmbr0 -p udp --dport 9987 -j DNAT --to-destination 10.25.10.102:9987
	iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 30033 -j DNAT --to-destination 10.25.10.102:30033
	iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 10011 -j DNAT --to-destination 10.25.10.102:10011
	iptables -t nat -A PREROUTING -i vmbr0 -p udp --dport 13370 -j DNAT --to-destination 10.25.10.102:13370
	iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 13370 -j DNAT --to-destination 10.25.10.102:13370
}

remove_rules() {
    iptables -t nat -D PREROUTING -i vmbr0 -p tcp --dport 50991 -j DNAT --to-destination 10.25.10.102:9001
    iptables -t nat -D PREROUTING -i vmbr0 -p tcp --dport 50022 -j DNAT --to-destination 10.25.10.102:22
    iptables -t nat -D PREROUTING -i vmbr0 -p tcp --dport 50144 -j DNAT --to-destination 10.25.10.102:1433
	iptables -t nat -D PREROUTING -i vmbr0 -p udp --dport 9987 -j DNAT --to-destination 10.25.10.102:9987
	iptables -t nat -D PREROUTING -i vmbr0 -p tcp --dport 30033 -j DNAT --to-destination 10.25.10.102:30033
	iptables -t nat -D PREROUTING -i vmbr0 -p tcp --dport 10011 -j DNAT --to-destination 10.25.10.102:10011
	iptables -t nat -D PREROUTING -i vmbr0 -p udp --dport 13370 -j DNAT --to-destination 10.25.10.102:13370
	iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 13370 -j DNAT --to-destination 10.25.10.102:13370
	
}

# Проверяем количество аргументов
if [ "$#" -ne 2 ]; then
    echo "Usage: $0 {start|stop}"
    exit 1
fi

# Обработка аргументов
case "$2" in
    pre-start)
        add_rules
        ;;
    post-stop)
        remove_rules
        ;;
    *)
        echo "Usage: $0 {start|stop}"
        exit 1
        ;;
esac
