Skip to content

Instantly share code, notes, and snippets.

@akbaryahya
Created Feb 15, 2020
Embed
What would you like to do?
Port Forwarding OpenVPN Dengan CentOS
#!/bin/bash
#Restart VPN?
systemctl restart openvpn@server.service
# Allow forwarding
sysctl net.ipv4.ip_forward=1
# Restart iptables
service iptables restart
iptables -F
# Config (todo bikin otomatis setiap pengguna konek)
tamu="10.8.0.6"
server=`hostname -i`
echo "Hello saya $tamu mau terhubung ke $server"
# 88 ip public dan 81 ip vpn dari rumahku dan terkonek mikrotik
declare -A rport
rport[80]=85
rport[81]=88
rport[4444]=4444
# Rumus Port Forwarding
for item in "${!rport[@]}";
do
resp=`netstat -tunl | grep ":${rport[$item]} "`
if [ -z "$resp" ]; then
printf "Port $tamu:$item terkonek ke $server:${rport[$item]} \n"
iptables -A PREROUTING -t nat -p tcp --dport ${rport[$item]} -j DNAT --to-destination $tamu:$item
iptables -A PREROUTING -t nat -p udp --dport ${rport[$item]} -j DNAT --to-destination $tamu:$item
iptables -A FORWARD -p tcp -d $tamu --dport $item -j ACCEPT
iptables -A FORWARD -p udp -d $tamu --dport $item -j ACCEPT
else
printf "$server:${rport[$item]} sedang terpakai \n"
fi
done
# Allow tap dan etch0 saling terhubung
iptables -A POSTROUTING -t nat -j MASQUERADE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment