LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 05-25-2018, 01:41 PM   #1
netpumber
Member
 
Registered: Sep 2007
Location: In My Box
Distribution: Arch Linux
Posts: 423

Rep: Reputation: 33
Bash-script with iptables rules for VPN & IRC & TOR review


Hi all.

I have a raspberry pi machine in my home that runs linux and is used for the following two things.

1. IRC server through TOR network
2. VPN router

In order to used as a VPN router, it has two wireless adaptors. One is set as AP (hostapd) while the other is connected to an external WIFI that user will choose. To control these both functions (IRC and VPN router) I've built the following bash script where a user can run either

Code:
./script irc start/restart/stop
or
Code:
./script vpn-router start/stop
I've noticed that if the IRC server is first started and then the VPN router is started or stopped, then a problem occurs in IRC server-client connection. Either it has lags or if the user decides to disconnect (due to the lags) he/her cannot connect back unless some time is passed (e.g 10-25 mins).

I'm thinking that something is going on with the traffic between tun0 device and the other devices when tun0 is getting up or down. Here are some of my main thoughts:
1) there is a miss-config of my iptables rules.
2) it takes time to reroute after VPN get up/down.
3) something is happening with TOR.

Anyway. Here is the simple script I wrote to control the mentioned functions. If you think that an alter on iptables rules will help the situation, feel free to suggest it.

Code:
#!/bin/bash
 
# This script is starting the Inspircd and Tor AND VPNrouting.
 
checkInternetAccess(){
    if nc -zw1 google.com 443; then
        echo "[+] Internet connection found"
    else
	echo "[!] No internet connection found"
	nmcli device wifi rescan
	nmcli device wifi list
	echo ""
	echo "[!] Please connect to one of the found WIFi's or to an ethernet port."
	echo "[!] Consider using the following command for the WIFi" 
	echo "[>] nmcli device wifi connect SSID-Name password wireless-password" 
	echo ""
	exit 1
    fi
}
 
# TOR AND INSPIRCD EXECUTION CONTROL
 
if [ "$1" == "irc" ] && [ "$2" == "start" ]; then
 
    checkInternetAccess
 
    echo "[+] Starting Inspircd..."
    sudo systemctl start inspircd
    echo "[+] Starting Tor..."
    sudo systemctl start tor
    sleep 5
    echo "[+] IRC is listenning @ $(sudo cat /var/lib/tor/hidden_service/hostname):6667" 
    echo ""
    exit $?
 
elif [ "$1" == "irc" ] && [ "$2" == "restart" ]; then
 
    checkInternetAccess
 
    echo "[+] Re-starting Inspircd..."
    sudo systemctl restart inspircd
    echo "[+] Re-starting Tor..."
    sudo systemctl restart tor
    sleep 5
    echo "[+] IRC is listenning @ $(sudo cat /var/lib/tor/hidden_service/hostname):6667" 
    echo ""
    exit $?
 
elif [ "$1" == "irc" ] && [ "$2" == "stop" ]; then
 
    echo "[+] Stopping Inspircd..."
    sudo systemctl stop inspircd
    echo "[+] Stopping Tor..."
    sudo systemctl stop tor
 
# VPN ROUTING EXECUTION CONTROL
 
elif [ "$1" == "vpn-router" ] && [ "$2" == "start" ]; then
 
    checkInternetAccess
 
    # enable ip forwarding
    echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
    # start openvpn
    echo "[+] Starting openVPN..."
    cd /etc/openvpn/client
    sudo nohup openvpn --config myVPN.conf &
    # setup iptables rules
    echo "[+] Seting iptables rules..."
    sudo iptables -t nat -A POSTROUTING -o tun0 -j MASQUERADE
    sudo iptables -t filter -A FORWARD -i tun0 -o wlan-AP -m state --state RELATED,ESTABLISHED -j ACCEPT
    sudo iptables -t filter -A FORWARD -i wlan-AP -o tun0 -j ACCEPT
 
elif [ "$1" == "vpn-router" ] && [ "$2" == "stop" ]; then
 
    # disable ip forwarding
    echo 0 | sudo tee /proc/sys/net/ipv4/ip_forward
    # remove iptables rules
    echo "[+] Removing iptables rules..."
    sudo iptables -t nat -D POSTROUTING -o tun0 -j MASQUERADE
    sudo iptables -t filter -D FORWARD -i tun0 -o wlan-AP -m state --state RELATED,ESTABLISHED -j ACCEPT
    sudo iptables -t filter -D FORWARD -i wlan-AP -o tun0 -j ACCEPT
    # stop openvpn
    echo "[+] Stoping openVPN..."
    sudo killall openvpn
 
else
    echo ""
    echo "Usage: control [service] [action]"
    echo "-----------------------------------"
    echo ""
    echo "services : irc | vpn-router"
    echo "actions  : start | restart | stop"
    echo ""
fi
Thank you.
 
Old 05-29-2018, 04:02 PM   #2
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
I don't understand what the vpn does in this setup.
 
Old 05-30-2018, 03:10 AM   #3
netpumber
Member
 
Registered: Sep 2007
Location: In My Box
Distribution: Arch Linux
Posts: 423

Original Poster
Rep: Reputation: 33
As I said it has two wireless adapters. One to connect to the internet (wlan-EXT) and one to set up an AP (wlan-AP) for users to connect to it. Once the VPN is up, the traffic of the users is transferred through the VPN like (wlan-AP ---> tun0 (VPN) -----> wlan-EXT ----> Internet)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] User permissions to see IPTables rules & packets. CrAzYoNi Linux - Newbie 12 09-21-2017 03:23 AM
Linux with TOR & VPN Rocketrick Linux - Software 16 05-24-2016 04:31 PM
iptables forward rules for OpenVPN & PPTP crspy Linux - Networking 2 04-18-2013 02:12 AM
iptables rules for RHEL5 & AD wegadnie Linux - Networking 3 07-20-2009 11:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 02:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration