Created
February 15, 2020 12:56
-
-
Save akbaryahya/2e64f22ca01eca0da8968a580b3800cf to your computer and use it in GitHub Desktop.
Port Forwarding OpenVPN Dengan CentOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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