Visualiser une révision

[Tuto] Se connecter à son OpenVPN depuis son LAN et WAN malgré routeur pas compatible hairpinning

voxdemonix : révision n°5 (24 janvier 2018 15:59:19)

Introduction
============

Pour une raison suspicieuse, les box des Fournisseurs d'Accès Internet (FAI) sont régulièrement incompatible avec le [hairpinning](https://fr.wikipedia.org/wiki/Hairpinning). Pour outrepasser ce problème, nous allons simplement dupliquer le fichier de conf (.ovpn) afin qu'un soit compatible avec le LAN et un avec internet (WAN). Ensuite nous lancerons un script au démarrage de la machine qui décidera quel fichier de conf (.ovpn) il faut lancer en fonction de si votre serveur est sur le même réseau ou non.
Ce script est compatible avec les tunnels SSH (au choix en local ou en wan ou même les deux ou aucun). 


Mise en place sur le client
===========================

**Note :** pour ce tuto je pars du principe que votre [OpenVPN est déjà installé](https://www.0rion.netlib.re/forum4/viewtopic.php?f=14&t=610) et le port ouvert sur le routeur du réseau du serveur (par défaut 1194).

1. **Copiez le fichier de configuration de votre client OpenVPN, nous allons ajouter _LAN dans le titre du nouveau fichier**

```
sudo cp /etc/openvpn/monUser.ovpn cp /etc/openvpn/monUser_LAN.ovpn 
```

1. **Éditez la copie qui nous servira pour le LAN**

```
sudo nano /etc/openvpn/monUser_LAN.ovpn
```

  * **A la ligne commençant par "_remote_", rajoutez _LAN dans le nom de domaine (ou remplacez le nom de domaine par l'IP LAN statique de votre serveur OpenVPN).**

```
remote www.server_LAN.com 1194
```

1. **Ensuite éditez votre fichier _/etc/hosts_ afin d'y indiquer la correspondance entre notre nouveau nom de domaine dédié au LAN et l'adresse IP du serveur OpenVPN.**

```
sudo nano /etc/hosts
```

  * **Ajoutez une ligne du style**

```
192.168.1.42       www.server_LAN.com
```

1. **Créez le script de lancement _/opt/scripts/vpnSwitcher.bash_ (_CTRL+X_ pour sauver & quitter)**

```
sudo nano /opt/scripts/vpnSwitcher.bash
```

1. **Puis collez le script suivant et adaptez le à votre config**

**Note :** [source du script](https://www.0rion.netlib.re/forum4/viewtopic.php?f=9&t=646&p=1460#p1460)

```
#!/bin/bash
# -- vpnSwitcher --
#  -> WTFPL - infos script : https://www.0rion.netlib.re/forum4/viewtopic.php?f=9&t=646&p=1460#p1460
# -> code by voxdemonix <-
# -> V1.11 (2017/09/11) <-
# uncomment next line for debug
#set -x

if [ ! "$SUDO_USER" ]; then
echo "!!! i need root !!!"
exit 0
fi

    #SCRIPTS OPTIONS
LANG="fr" # notification service ; accept : fr, usa (default)
notificationType="notify" # accept : echo; notify, nothing. Notify not work with root user.

    # NETWORK OPTIONS
INTERFACE="tun"
hostnameVpnLAN="192.168.1.69" # (VPN1 hostname) l'adresse IP locale (LAN) de votre serveur OpenVPN
MacVpnLocal="69:69:69:69:69:69" #l'adresse mac de votre serveur OpenVPN (tapez ifconfig dans un terminal sur votre server)

    # VPN OPTIONS
fichierOvpnLAN="/etc/openvpn/myUser_LAN.ovpn" # (VPN1) le PATH de votre fichier de conf .ovpn en LAN
fichierOvpnNotLAN="/etc/openvpn/myUser.ovpn" # (VPN2) le PATH de votre fichier de conf .ovpn pour les connnexions WAN/SSH
openVpn1Param=" " # add options (ex: --route-nopull) for openvpn connexion to server 1 (lan)
openVpn2Param=" " # add options (ex: --route-nopull) for openvpn connexion to server 2 (ssh/tor/wan)

    # SSH OPTIONS
hosnameVpnNotLAN="blablablablablabla.onion" # (VPN2 hostname)
UserLocalForSshTunneling="daenerys" # l'user a utiliser sur votre ordinateur pour le montage du tunnel (celui qui a exporté sa clés)
UserRemoteForSshTunneling="proxy-ssh" # l'user a utiliser côté server ( /!\ n'utilisez jamais root !)
portEntree="1194" # le port sur le pc local, par défaut 1194 (tcp)
portSortie="1194" # le port sur le serveur, par défaut 1194 (tcp)
sshLanEnable=0  # 0 for disable ; 1 for enable : enable or disable the ssh tunneling in LAN
sshNotLanEnable=0 # 0 for disable ; 1 for enable : enable or disable the ssh tunneling in Wan | Tor



            # NOTIFICATION SYSTEM
function vpnNotification {
        # $1 = message ID (watch switch/case);
        # $2 = hostnameVPN
    message=""
    #hostnameVPN=$(cat "$2" | grep "remote " | sed 's/remote //g' | sed 's/ [[:digit:]]\+//g')
    hostnameVPN=$2

    case $LANG in
        fr )
                case $1 in
                    VpnDisabled )
                            message="Connexion VPN fermée ($hostnameVPN)"
                        ;;
                    VpnConnecting )
                            message="Connexion VPN en cours ($hostnameVPN)"
                        ;;
                    VpnEnabled )
                            message="VPN Connecté ! ($hostnameVPN)"
                        ;;
                    SshStop )
                            message="Tunnel SSH STOP ($hostnameVPN)"
                        ;;
                    SshStart )
                            message="Tunnel SSH START ($hostnameVPN)"
                        ;;
                    * )
                            message="message inconnu"
                        ;;
                esac
            ;;
        usa | *)
                case $1 in
                    VpnDisabled )
                            message="VPN Connection Close ($hostnameVPN)"
                        ;;
                    VpnConnecting )
                            message="VPN Connection in progress ($hostnameVPN)"
                        ;;
                    VpnEnabled )
                            message="VPN Connected ! ($hostnameVPN)"
                        ;;
                    SshStop )
                            message="SSH Tunneling STOP ($hostnameVPN)"
                        ;;
                    SshStart )
                            message="SSH Tunneling START ($hostnameVPN)"
                        ;;
                    * )
                            message="unknow message"
                        ;;
                esac
            ;;
    esac


    case $notificationType in
        echo)
                echo "$message"
            ;;
        notify)
                notify-send "$message"
            ;;
        nothing | *)
            ;;
    esac
}

function quitOpenVPN {
        # $1 = ovpn ( $fichierOvpnLAN, $fichierOvpnNotLAN)
        # $2 = hostnameVPN ( $hostnameVpnLAN, $hosnameVpnNotLAN)
        ovpn=$1
        hostVPN=$2
            #stop VPN
        if [ $(pkill -c -f $ovpn) -gt 0 ]; then
		hostnameVPN=$(cat $ovpn | grep "remote " | sed 's/remote //g' | sed 's/ [[:digit:]]\+//g')
		vpnNotification "VpnDisabled" $hostnameVPN
        fi
            #stop ssh
		if [ $(pkill -c -f "autossh.*$UserRemoteForSshTunneling@$hostVPN") -gt 0 ]; then
            vpnNotification "SshStop" $hostVPN
        fi
        sleep 1
}

function startOpenVPN {
        # $1 = hostname VPN ($hostnameVpnLAN, $hosnameVpnNotLAN)
        # $2 = ovpn ($fichierOvpnLAN, $fichierOvpnNotLAN)
        # $3 = ssh enable (1) or not (0) ($sshLanEnable, $sshNotLanEnable)
        # $4 = OpenVpn Param ($openVpn1Param, $openVpn2Param)

        hostVPN=$1
        ovpn=$2
        sshChoice=$3
        openvpnParam=$4

                # SSH TUNNELING
            if [ $sshChoice == 1 ]; then
                    # kill oprevious ssh tunneling
                for pid in `ps -ef | grep "[a]utossh" | grep "$hostVPN" | awk '{print $2}'` ; do kill $pid ; done
                    # creat new ssh tunneling
                su $UserLocalForSshTunneling -c "autossh -M 0 -q -N -o 'ServerAliveInterval 60' -o 'ServerAliveCountMax 3' -L $portEntree:127.0.0.1:$portSortie $UserRemoteForSshTunneling@$hostVPN -f"
		        if ps -aux | grep "[a]utossh" | grep "$hostVPN"  | grep -v "grep" >> /dev/null 2>&1; then
		            vpnNotification "SshStart" $hostVPN
		        fi
            fi

                # START OpenVPN
            openvpn --daemon $openvpnParam --config $ovpn

                # NOTIFICATIONS
            hostnameVPN=$(cat $ovpn | grep "remote " | sed 's/remote //g' | sed 's/ [[:digit:]]\+//g')
            vpnNotification "VpnConnecting" $hostnameVPN

                # boucle qui permet de verifier si le VPN se connecte bien
        stop=0
        while [ $stop -lt 10 ]
        do       # temps d'attente entre chaque tentative
            if ifconfig | grep -iq $INTERFACE; then
                    vpnNotification "VpnEnabled" $hostnameVPN
                    stop=10
                    exit
            fi
                sleep 5
               stop=$((stop+1))
        done

}



        # boucle qui permet d'attendre que le réseau soit connecté
stop="0"
while [ $stop -lt 1 ]
do
	sleep 3       # temps d'attente entre chaque tentative

	_IP=$(hostname -I) || true
	if [ "$_IP" ]; then
	        stop=1
	fi
done

if ps -aux | grep "[o]penvpn" >> /dev/null 2>&1; then
    #Connexion VPN active

            # vpn server 1
        quitOpenVPN $fichierOvpnLAN $hostnameVpnLAN
            # vpn server 2
        quitOpenVPN $fichierOvpnNotLAN $hosnameVpnNotLAN


else

        ping $hostnameVpnLAN -c 2 >> /dev/null 2>&1
        macRecover=$(arp -n | grep -i -o $MacVpnLocal)

        if [ "$macRecover" == "$MacVpnLocal" ]; then
        #        echo "local/LAN/First VPN"
            startOpenVPN $hostnameVpnLAN $fichierOvpnLAN $sshLanEnable $openVpn1Param

        else
        #        echo "tor/wan/second VPN"
            startOpenVPN $hosnameVpnNotLAN $fichierOvpnNotLAN $sshNotLanEnable $openVpn2Param

        fi

fi
```

* **Éditez les valeurs suivantes**

  * _**hostnameVpnLAN="192.168.1.69"**_ => (VPN1 hostname) l'adresse IP locale (LAN) de votre serveur OpenVPN
  * _**MacVpnLocal="69:69:69:69:69:69"**_ => l'adresse mac de votre serveur OpenVPN (tapez ifconfig dans un terminal sur votre server)
  * **_fichierOvpnLAN="/etc/openvpn/myUser_LAN.ovpn"_** => (VPN1) le PATH de votre fichier de conf .ovpn en LAN
  * _**fichierOvpnNotLAN="/etc/openvpn/myUser.ovpn"**_ => (VPN2) le PATH de votre fichier de conf .ovpn pour les connnexions WAN/SSH

1. **Sauvez et quittez avec _CTRL+X_, puis rendez votre fichier exécutable**

```
sudo chmod +x /opt/scripts/vpnSwitcher.bash
```

1. **Lancez ensuite le script afin de le tester**

```
sudo /opt/scripts/vpnSwitcher.bash
```

**S'il fonctionne correctement, ajoutez le au démarrage du client soit via _/etc/rc.local_ soit via cron**

```
sudo crontab -e
```

1. **Ajoutez enfin la ligne suivante (CTRL+X pour sauver et quitter)**

```
@reboot		/opt/scripts/vpnSwitcher.bash
```



Farm Link
=========


* [Scripts pour VPN](https://www.0rion.netlib.re/forum4/viewtopic.php?f=9&t=646)
* [[Tuto/HowTo] [GNU/Linux] Connecter 2 réseaux OpenVPN](https://www.0rion.netlib.re/forum4/viewtopic.php?f=14&t=648)
* [[Tuto/HowTo] [GNU/Linux] Monter un serveur VPN sur Odroid (Ubuntu Minimal) avec PiVPN](https://www.0rion.netlib.re/forum4/viewtopic.php?f=14&t=621)
* [[Tuto/HowTo] [GNU/Linux] Monter un serveur VPN sur Raspberry Pi (Raspbian) avec PiVPN](https://linuxfr.org/forums/linux-debian-ubuntu/posts/tuto-howto-monter-un-serveur-vpn-avec-pivpn-sur-raspbian)
* [[SOLUTION] Désactiver sur Ubuntu le passage de tout le trafic réseau par le VPN](https://www.0rion.netlib.re/forum4/viewtopic.php?f=14&t=589)
* [[Tuto/HowTo] [GNU/Linux] Utilisation d'OpenVPN côté client](https://www.0rion.netlib.re/forum4/viewtopic.php?f=14&t=610)
* [[Tuto/HowTo] [GNU/Linux] Accéder à son VPN depuis le réseau Tor via SSH Tunneling](https://linuxfr.org/forums/linux-debian-ubuntu/posts/tuto-howto-acceder-a-son-vpn-depuis-le-reseau-tor-via-ssh-tunneling)
* [OpenVPN - Trucs & Astuces](https://www.0rion.netlib.re/forum4/viewtopic.php?f=14&t=622)
* [[Tuto/HowTo] [GNU/Linux] Fixer IP sur Ubuntu](https://www.0rion.netlib.re/forum4/viewtopic.php?f=14&t=649)
* [[Tuto/HowTo] [GNU/Linux] Fixer IP sur Raspbian Jessie Light](https://www.0rion.netlib.re/forum4/viewtopic.php?f=14&t=571)
* [[Tuto/HowTo] Restreindre l'accès à un serveur en fonction du pays grâce à IPTables et GeoIP](https://www.0rion.netlib.re/forum4/viewtopic.php?f=14&t=516)
* [Outrepasser les problèmes de hairpinning (boucle local) sur GNU/Linux](https://linuxfr.org/wiki/outrepasser-les-problemes-de-hairpinning-boucle-local-sur-gnu-linux)
* [[Tuto/HowTo] Se connecter à son OpenVPN depuis son LAN et depuis internet malgré routeur/box pas compatible hairpinnning](https://www.0rion.netlib.re/forum4/viewtopic.php?f=14&t=676) (tutoriel d'origine)