|
1 | 1 | #!/bin/bash
|
2 | 2 |
|
3 |
| -# File to store the IP addresses |
4 |
| -ip_file="/root/ip_addresses.txt" |
5 |
| - |
6 |
| -# Path to the current script |
7 |
| -script_path=$(realpath "$0") |
8 |
| - |
9 |
| -# Function to get the main IP address of the server |
10 |
| -get_main_ip() { |
11 |
| - curl -s https://api.ipify.org || ip -4 addr show scope global | grep inet | awk '{print $2}' | cut -d "/" -f 1 |
12 |
| -} |
13 |
| - |
14 |
| -if [[ $1 == "uninstall" ]]; then |
15 |
| - # Uninstall mode |
16 |
| - systemctl stop iptables-config |
17 |
| - systemctl disable iptables-config |
18 |
| - rm /etc/systemd/system/iptables-config.service |
19 |
| - rm "$ip_file" |
20 |
| - echo "Service and IP addresses file removed." |
21 |
| - exit 0 |
| 3 | +script_path="/root/LazyTunnel.sh" |
| 4 | + |
| 5 | +mainland_ip=$(curl -s https://api.ipify.org) |
| 6 | + |
| 7 | +if [[ "$1" == "uninstall" ]]; then |
| 8 | + echo "Uninstalling..." |
| 9 | + iptables -t nat -D PREROUTING -p tcp --dport 22 -j DNAT --to-destination ${mainland_ip} |
| 10 | + iptables -t nat -D PREROUTING -j DNAT --to-destination ${foreign_ip} |
| 11 | + iptables -t nat -D POSTROUTING -j MASQUERADE |
| 12 | + rm /etc/systemd/system/iptables.service |
| 13 | + rm /root/ip.txt |
| 14 | + systemctl daemon-reload |
| 15 | + rm "${script_path}" |
| 16 | + exit 0 |
22 | 17 | fi
|
23 | 18 |
|
24 |
| -if [ ! -f "$ip_file" ]; then |
25 |
| - # File does not exist, generate Mainland IP and ask for Foreign IP |
26 |
| - mainland_ip=$(get_main_ip) |
27 |
| - echo "Mainland IP Address (automatically detected): $mainland_ip" |
28 |
| - echo "" |
29 |
| - echo -n "Enter Foreign IP Address: " |
30 |
| - read foreign_ip |
| 19 | +# Check if the rules are already in place |
| 20 | +if iptables -t nat -C PREROUTING -p tcp --dport 22 -j DNAT --to-destination ${mainland_ip} 2>/dev/null; then |
| 21 | + echo "IPTables rules are already set, nothing to do." |
| 22 | + exit 0 |
| 23 | +fi |
31 | 24 |
|
32 |
| - # Store the input values for future use |
33 |
| - echo "$mainland_ip" > "$ip_file" |
34 |
| - echo "$foreign_ip" >> "$ip_file" |
| 25 | +echo "Mainland IP Address (automatically detected): ${mainland_ip}" |
| 26 | +read -p "Foreign IP Address : " foreign_ip |
| 27 | +echo ${foreign_ip} > /root/ip.txt |
35 | 28 |
|
36 |
| - # Create systemd service file |
37 |
| - cat > /etc/systemd/system/iptables-config.service << EOF |
38 |
| -[Unit] |
39 |
| -Description=Configure IPTables |
| 29 | +sysctl net.ipv4.ip_forward=1 |
40 | 30 |
|
41 |
| -[Service] |
42 |
| -ExecStart=$script_path |
| 31 | +iptables -t nat -A PREROUTING -p tcp --dport 22 -j DNAT --to-destination ${mainland_ip} |
| 32 | +iptables -t nat -A PREROUTING -j DNAT --to-destination ${foreign_ip} |
| 33 | +iptables -t nat -A POSTROUTING -j MASQUERADE |
43 | 34 |
|
| 35 | +echo "[Unit] |
| 36 | +Description=Persistent IPTables NAT rules |
| 37 | +Before=network.target |
| 38 | +[Service] |
| 39 | +ExecStart=/sbin/iptables-restore /root/ip.txt |
| 40 | +RemainAfterExit=yes |
44 | 41 | [Install]
|
45 |
| -WantedBy=multi-user.target |
46 |
| -EOF |
47 |
| - |
48 |
| - # Enable the service |
49 |
| - systemctl enable iptables-config |
50 |
| -else |
51 |
| - # File exists, read the values |
52 |
| - mainland_ip=$(sed -n '1p' "$ip_file") |
53 |
| - foreign_ip=$(sed -n '2p' "$ip_file") |
54 |
| -fi |
| 42 | +WantedBy=multi-user.target" > /etc/systemd/system/iptables.service |
55 | 43 |
|
56 |
| -# Run sysctl command |
57 |
| -sysctl net.ipv4.ip_forward=1 |
| 44 | +systemctl enable iptables |
| 45 | +systemctl start iptables |
58 | 46 |
|
59 |
| -# Run iptables commands with stored or input values |
60 |
| -iptables -t nat -A PREROUTING -p tcp --dport 22 -j DNAT --to-destination $mainland_ip |
61 |
| -iptables -t nat -A PREROUTING -j DNAT --to-destination $foreign_ip |
62 |
| -iptables -t nat -A POSTROUTING -j MASQUERADE |
| 47 | +# Save a copy of the script locally |
| 48 | +cp "$0" "${script_path}" |
0 commit comments