diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..1a5eb7f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+*.diff
diff --git a/Default.txt b/Default.txt
index d3a01c1..cb733db 100644
--- a/Default.txt
+++ b/Default.txt
@@ -4,8 +4,6 @@ proto udp
remote IPv4pub 1194
resolv-retry infinite
nobind
-persist-key
-persist-tun
key-direction 1
remote-cert-tls server
tls-version-min 1.2
diff --git a/LatestUpdate.md b/LatestUpdate.md
index 91fc828..0d33980 100644
--- a/LatestUpdate.md
+++ b/LatestUpdate.md
@@ -1,11 +1,151 @@
# Information of Latest updates
This file has the objective of describing the major changes for each merge from test to master in a similar way as a
-patch release notes.
+patch release notes.
-Everytime Test branch is merged into master, a new entry should be created with the date and changes being merged.
+Everytime Test branch is merged into master, a new entry should be created with the date and changes being merged.
+
+## Jan 20th 2020
+
+- Allow setting DHCP reservation preference with --unattended
+- Flip condition check on $dhcpReserv: first check if empty, and if not, check if it's not 1.
+ Doing it the other way (first check if not 1) would give a shell error if $dhcpReserv was empty.
+- Prepend 'pivpn-' to unstable repo files to limit naming conflicts
+- Update variables inside unattended examples
+- Remove openvpn logging setting when uninstalling the package
+- Run 'apt-get update' after removing the WireGuard PPA
+
+## Jan 18th 2020
+
+Distro Support, Bug Fixes, Unattended install
+
+Tested and added Support on Debian 9
+tested and added support on Ubuntu 16.04 & 18.08
+ * Fixed wireguard not installing, added pkg cache update after adding ppa
+ * added kernel headers to dependencies as its requred for wireguard-dkms
+unattended install
+ * When user is provided and doest exist, it will create one without password set
+ * Use metapackage to install kernel headers on Ubuntu
+
+
+## Jan 8th 2020
+
+Updates and improvements
+Issue #871: fix backup script
+
+install.sh
+ installScripts function:
+ update script not being copied over to /opt therefore update funcion was probably broken.
+ changed script to copy all .sh scripts from .pivpn/scripts directory.
+
+Issue #871: fix backup script
+ I was probably very drunk when i first wrote this backup script.
+ fixed it, now works with new code refactoring,
+ loads vars from setupVars
+ Added backup for wireguard
+ Moved script to global pivpnscripts.
+ Added backup script to bash-completion
+ Added backup script to pivpn script
+
+update.sh
+ Commented the update from master branch to avoid users trying to update test from master.
+
+
+Updated LatestChages.md
+
+## Jan 7th 2020
+
+Changes for FR #897
+Support For DHCP IP Reservation
+
+Main:
+ - added If statement to skipp SetStaticIPv4 if dhcpRserv=1
+
+getStaticIPv4Settings:
+ - Added Whiptail asking if user wants to use DHCP reservation Settings, this will add dhcpReserv and
+ skip setStaticIPv4 while still logging everything.
+ConfigExample files:
+ - Added staticReserv=0 to config examples. so it can be used with Unattended install
+ * 0 means static ip will be setup.
+ * 1 means DHCP Reservation will be used and no chage will be made to the interfaces
+
+
+Updated LatestChanges
+
+## Jan 6th 2020
+
+* Removed Unecessary pipe on availableInterfaces
+* Changed OS Support messages accross the script
+ - Removed OS Version names from the script, this avoids having to change the code everytime a new OS Version is Released, instead we update the wiki with propper information.
+* Changed MaybeOSSupport whiptail tiltes and messages to make it more clear.
+ - Messages and titles could cause confusion to users and specially developers
+* Moved Funcions Comment to correct place.
+* DistroCheck Function:
+ - Moved up before other functions so it better refflects the order they are called.
+ - changed Case identation to make it easier to read.
+ - Added info to # compatibility Comment, Removed unecessary comments
+ - added break to exit out of case, easier to understand that the script should move on.
+* Added Shellcheck ignores,
+* chooseinterface Function:
+ - Changed function Logic and cleaned it up
+ - Fixed Issue #906
+ - Added exit code if no interfaces are found
+* Updated LatestUpdate.md
+* Use radiolist to select a VPN
+
+## Jan 3rd 2020
+
+- Revise route query for IP & GW selection from Quad9 to TEST-NET-1
+- Replace mention of 'Google' with 'Quad9'
+
+## Jan 2nd 2020
+
+- Fix mv command when copying the DH parameters to final destination
+
+## Dec 30th 2019
+
+- Fix paths inside the update script
+- Use the wireguard script for WireGuard as well
+- Updated the README in accordance to changes in the test branch
+
+## Dec 29th 2019
+
+* Handle running the install script over an existing installation (as the script already did before branching to test-wireguard), providing:
+ - Update, downloads latest scripts from git repo
+ - Repair, reinstall PiVPN while keeping existing settings
+ - Reconfigure, start over overwriting the existing settings
+* Tag iptables rules as an attempt to make sure that the uninstall script only removes PiVPN rules
+* Change the armv6l installation to reflect the split of WireGuard snapshots into wireguard-linux-compat and wireguard-tools
+
+## Dec 27th 2019
+
+ - When suggesting to use Pi-hole, use the VPN server IP instead of the LAN IP to allow DNS resolution even if the user does not route the local network through the tunnel.
+- Format listCONF in a similar way as listOVPN
+- Specifically look for a free octet in the last word of clients.txt and not just any word.
+ Necessary otherwhise public keys starting with a number will match against an octet.
+ Example: if line is 'name 5abcdefgh 4', then looking for ' 5' will match but '5$' will
+ not (correctly).
+- 'pivpn -c' will show the Connected Clients List for WireGuard too
+
+## Dec 10th 2019
+
+- Use dedicated openvpn user and group for increased security
+- Added basic safeguards to avoid wrecking /etc/ufw/before.rules
+- Applied some Shellcheck suggested changes.
+- Added safeguards to rm -rf when downloading the git repo.
+- Use more variables instead of hardcoding data
+- Add local resolver as DNS option
+
+## Dec 3rd 2019
+
+- Better client stats formatting
+
+## Dec 2nd 2019
+
+* Properly avoid pulling unwanted packages from unstable repo:
+ - Currently apt pulls all packages from the unstable repo because the script intendation created the file 'limit-unstable' with tabs in it. Fixed using printf to create a multiline file.
+- Accept debug fixes using just the enter key
-## Oct 12th 2019 - On test
## Nov 25th 2019 - On Master
* Changed pivpn command exit codes from 1 to 0
@@ -13,11 +153,80 @@ Everytime Test branch is merged into master, a new entry should be created with
* added backup script to backup openvpn and pivpn generated certificates
* added update script to update /opt/pivpn scripts, -t | --test | test update from test branch
* Fixed hostname length issue #831
- - the script now checks for hostname length right at the beginning and prompts for a new one.
- - HOST_NAME to host_name, as best practice variables with capitals, should be used by system variables only.
+ - the script now checks for hostname length right at the beginning and prompts for a new one.
+ - HOST_NAME to host_name, as best practice variables with capitals, should be used by system variables only.
* fixed ubuntu 18.04 being detected as not supported OS, now fully supported and tested.
-* changed how scripts are copied to /opt/pivpn, it hat a lot of long repetitive lines, now it copies all *.sh files making it easier to manage when adding new scripts/features
-* Changed how supported OS are presented when maybeOS_Support() is called.
+* changed how scripts are copied to /opt/pivpn, it hat a lot of long repetitive lines, now it copies all `*.sh` files making it easier to manage when adding new scripts/features
+* Changed how supported OS are presented when maybeOS_Support() is called.
+
+## Nov 19th 2019
+
+- Added Ubuntu Bionic support
+
+## Nov 16th 2019
+
+- Added back unattended installation: as expected, the user can call the install script with --unattended followed by a config file and PiVPN will be installed non-interactively.
+- Removed persist-key and persist-tun from the client config.
+- Reverted keepalive setting on the server to smaller values.
+- See @TinCanTech's posts for the reasons of the above two: #864 (comment)
+- Copied validDomain() function from the test branch.
+- Removed 1024 bit certificate options, since on Buster OpenVPN does not start with such small certificates (It's related to OpenSSL 1.1.1).
+- Backup /etc/openvpn and /etc/wireguard before installing.
+- Always remove VPN configuration when uninstalling, but do not wipe the folder, just remove what PiVPN added.
+- Fetch latest WireGuard snapshot instead of hardcoding it into the script.
+
+## Nov 7th 2019
+
+- Add back the uninstall script
+- Only uninstall packages that were not already installed when running the PiVPN install script.
+- Detect and offer to use Pi-hole
+- Use checkinstall to install wireguard-tools for easy uninstallation
+- Added missing dkms dependency
+
+## Oct 19th 2019
+
+- MakeOVPN has been updated to include the -i iOS function to allow users to create an OVPN12 format file that can be used with the iOS keychain.
+- Check if -i iOS can be used (can't be used with ECDSA certificates).
+- Fixed the issues with special characters in OVPN12 files.
+
+## Oct 17th 2019
+
+- Allow subdomain in custom DNS search list.
+- Unified PiVPN configuration into the single /etc/pivpn/setupVars.conf file.
+- Functions that ask the user for the port, protocol, dns, domain don't apply the setting anymore, they only save the variable on disk. Settings are applied in confOpenVPN, confOVPN, confWireGuard.
+- Support and use WireGuard by default with an initial set of scripts matching current PiVPN scripts (list, create, remove clients).
+- Removed OpenVPN ECDSA option.
+- Renamed some variables (see pull request 849).
+- Refactored several functions.
+
+## Oct 12th 2019 - On test
+
+* Changed pivpn command exit codes from 1 to 0
+ - exit code 1 means general error hence should not be used for exiting successfully
+* added backup script to backup openvpn and pivpn generated certificates
+* added update script to update /opt/pivpn scripts, -t | --test | test update from test branch
+* Fixed hostname length issue #831
+ - the script now checks for hostname length right at the beginning and prompts for a new one.
+ - HOST_NAME to host_name, as best practice variables with capitals, should be used by system variables only.
+* fixed ubuntu 18.04 being detected as not supported OS, now fully supported and tested.
+* changed how scripts are copied to /opt/pivpn, it hat a lot of long repetitive lines, now it copies all `*.sh` files making it easier to manage when adding new scripts/features
+* Changed how supported OS are presented when maybeOS_Support() is called.
+
+### Merge Patch, Sept 2nd 2019
+
+* Bitwarden integration:
+ - Bitwarden Installation removed from script, users that whish to use it should install it manually.
+ - bugfixes with pivpn add
+ - pivpn add -b will fail if bitwarden is not found
+
+* File and dirs permissions:
+ - fixed bug where ovpns being owned by root
+
+* IOS integration
+ - fixed bug where ovpn12 files not being properly generated
+
+* General improvments:
+ - when runing updates, sudo password prompt now shows up in a new line
## Sept 1st 2019
@@ -32,24 +241,6 @@ Everytime Test branch is merged into master, a new entry should be created with
* bugfixes and typos
* permissions hardening and writing uniformization
* improved pivpn user and ovpns dirs handling
-* Changes variable and file naming in `install.sh`
+* Changes variable and file naming in `install.sh`
- $pivPNUser renamed to $INSTALL_USER
- /tmp/pivpnUSR renamed to INSTALL_USER
-
-### Merge Patch, Sept 2nd 2019
-
-* Bitwarden integration:
- - Bitwarden Installation removed from script, users that whish to use it should install it manually.
- - bugfixes with pivpn add
- - pivpn add -b will fail if bitwarden is not found
-
-* File and dirs permissions:
- - fixed bug where ovpns being owned by root
-
-* IOS integration
- - fixed bug where ovpn12 files not being properly generated
-
-* General improvments:
- - when runing updates, sudo password prompt now shows up in a new line
-
-----
diff --git a/README.md b/README.md
index 46a65d3..2261afd 100644
--- a/README.md
+++ b/README.md
@@ -1,84 +1,123 @@
+![WireGuard + OpenVPN logo](logos.jpg)
+
About
-----
Visit the [PiVPN](https://pivpn.dev) site for more information.
-This is a set of shell scripts developed by **@0-kaladin** that serve to easily turn your Raspberry Pi (TM)
-into a VPN server using the free, open-source [OpenVPN](https://openvpn.net) software.
+This is a set of shell scripts initially developed by **@0-kaladin** that serve to easily turn your Raspberry Pi (TM)
+into a VPN server using two free, open-source protocols:
+ * [WireGuard](https://www.wireguard.com/)
+ * [OpenVPN](https://openvpn.net)
-Have you been looking for a good guide or tutorial for installing openvpn on a raspberry pi or ubuntu based server? Run this script and you don't need a guide or tutorial, this will do it all for you, in a fraction of the time and with hardened security settings in place by default.
+Have you been looking for a good guide or tutorial for setting up a VPN server on a Raspberry Pi or Ubuntu based server?
+Run this script and you don't need a guide or tutorial, this will do it all for you, in a fraction of the time and with hardened security settings in place by default.
-The master branch of this script installs and configures OpenVPN on Raspbian
-Jessie, Stretch, Devuan and has been tested on Ubuntu 14.04 and 16.04 running from an Amazon AWS image.
-We recommend using the Stretch or Jessie Lite image on a raspberry pi in your home so you can VPN into your home from unsecure remote locations and safely use the internet. However, the scripts do try to detect different distributions and make adjustments accordingly. They should work on the majority of Ubuntu and Debian based distributions including those using UFW by default instead of raw iptables.
+The master branch of this script installs and configures either WireGuard or OpenVPN on Raspbian, Debian or Ubuntu and it as been tested to run not only on Raspberry Pi but also in any Cloud Provider VPS.
+We recommend using the latest Raspbian Lite image on a Raspberry Pi in your home so you can VPN into your home from a unsecure remote locations and safely use the internet.
+However, the scripts do try to detect different distributions and make adjustments accordingly.
+They should work on the majority of Ubuntu and Debian based distributions including those using UFW by default instead of raw iptables.
-This scripts primary mission in life is to allow a user to have a home VPN for as cost effective as possible and without being a technical wizard. Hence the design of pivpn to work on a Raspberry Pi ($35) and then one command installer. Followed by easy management of the VPN thereafter with the 'pivpn' command. That being said...
+This scripts primary mission in life is to allow a user to have a home VPN for as cost effective as possible and without being a technical wizard.
+Hence the design of pivpn to work on a Raspberry Pi ($35) and then one command installer.
+Followed by easy management of the VPN thereafter with the 'pivpn' command.
+That being said...
-> This will also work on a free-tier Amazon AWS server using Ubuntu 14.04 - 16.04. I don't want to support every scenario there but getting it to run and install successfully on a free server in the cloud was also important. Many people have untrustworthy ISP's so running on a server elsewhere means you can connect to the VPN from home and your ISP will just see encrypted traffic as your traffic will now be leaving out the amazon infrastructure.
+> This will also work on a free-tier Amazon AWS server using Ubuntu or Debian. I don't want to support every scenario there but getting it to run and install successfully on a free server in the cloud was also important.
+Many people have untrustworthy ISP's so running on a server elsewhere means you can connect to the VPN from home and your ISP will just see encrypted traffic as your traffic will now be leaving out the Amazon infrastructure.
Prerequisites
-------------
-To follow this guide and use the script to setup OpenVPN, you will need to have
-a Raspberry Pi Model B or later with an ethernet port, an SD or microSD card
-(depending on the model) with Raspbian installed, a power adapter appropriate to
- the power needs of your model, and an ethernet cable or wifi adapter to connect your Pi to your
-router or gateway. It is recommended that you use a fresh image of Raspbian
-Stretch Lite from https://raspberrypi.org/downloads, but if you don't,
-be sure to make a backup image of your existing installation before proceeding.
-You should also setup your Pi with a static IP address (see either source
- 1 or 2 at the bottom of this Readme) but it is not required as the script can do this for you.
- You will need to have your router forward UDP port 1194 (or whatever custom port you may have chose in the installer)
- (varies by model & manufacturer; consult your router manufacturer's
- documentation to do this).
- Enabling SSH on your Pi is also highly recommended, so that
- you can run a very compact headless server without a monitor or keyboard and
- be able to access it even more conveniently (This is also covered by source 2).
+To follow this guide and use the script to setup a VPN, you will need to have
+a Raspberry Pi Model B or later with, an SD or microSD card with Raspbian installed,
+a power adapter appropriate to the power needs of your model, and an ethernet cable or wifi
+adapter to connect your Pi to your router or gateway.
+It is recommended that you use a fresh image of the latest Raspbian Lite from
+https://raspberrypi.org/downloads, but if you don't, be sure to make a backup
+image of your existing installation before proceeding.
+You should also setup your Pi with a static IP address
+(see either source 1 or 2 at the bottom of this Readme)
+but it is not required as the script can do this for you.
+You will need to have your router forwarding UDP port 1194 or whatever custom
+port you may have chose in the installer
+(varies by model & manufacturer; consult your router manufacturer's documentation to do this).
+Enabling SSH on your Pi is also highly recommended, so that you can run a very
+compact headless server without a monitor or keyboard and be able to access it
+even more conveniently (This is also covered by source 2).
Installation
-----------------
-
-```shell
+**Method 1**
+```Shell
curl -L https://install.pivpn.dev | bash
```
-The script will first update your APT repositories, upgrade packages, and install OpenVPN,
-which will take some time.
-It will ask which authentication method you wish the guts of your server to use, 1024-bit, 2048-bit, or 4096-bit.
-If you're unsure or don't have a convincing reason one way or the other I'd use 2048 today. From the OpenVPN site:
+**Method 2**
+```Shell
+curl -L https://install.pivpn.dev > pivpn.sh
+sudo sh pivpn.sh
+```
+
+**Method 3**
+```Shell
+git clone https://github.com/pivpn/pivpn.git
+sudo sh pivpn/auto_install/install.sh
+```
+
+**OBS:**
+In alternative to install.pivpn.dev you can use the raw github link:
+https://raw.githubusercontent.com/pivpn/pivpn/master/auto_install/install.sh
+
+**To install from Test/Development branch**
+
+Check our [Wiki Page](https://github.com/pivpn/pivpn/wiki#testing)
+
+**How it works**
+
+The script will first update your APT repositories, upgrade packages, and install WireGuard (default) or OpenVPN, which will take some time.
+
+It will ask which authentication method you wish the guts of your server to use. If you go for WireGuard, you don't get to choose: you will use a Curve25519 public key, which provides 128-bit security. On the other end, if you prefer OpenVPN, you can choose between a 2048-bit, 3072-bit, or 4096-bit RSA certificate. If you're unsure or don't have a convincing reason one way or the other I'd use 2048 today (provides 112-bit security).
+
+From the OpenVPN site:
+
> For asymmetric keys, general wisdom is that 1024-bit keys are no longer sufficient to protect against well-equipped adversaries. Use of 2048-bit is a good minimum. It is wise to ensure all keys across your active PKI (including the CA root keypair) are using at least 2048-bit keys.
> Up to 4096-bit is accepted by nearly all RSA systems (including OpenVPN), but use of keys this large will dramatically increase generation time, TLS handshake delays, and CPU usage for TLS operations; the benefit beyond 2048-bit keys is small enough not to be of great use at the current time. It is often a larger benefit to consider lower validity times than more bits past 2048, but that is for you to decide.
-Luckily, OpenVPN 2.4 supports ECDSA certificates, which are based on Elliptic Curves, allowing much smaller keys while providing an equivalent security level (256 bit long, equivalent to 3072 bit RSA). For this reason, PiVPN now uses ECDSA certs if you choose to enable OpenVPN 2.4 features. If not, the usual RSA certificates are generated in case the user has clients running an older version of OpenVPN.
+After this, the script will go back to the command line as it builds the server's own certificate authority (OpenVPN only). The script will ask you if you'd like to change the default port, protocol, client's DNS server, etc. If you know you want to change these things, feel free, and the script will put all the information where it needs to go in the various config files.
-After this, the script will go back to the command line as it builds the server's own
-certificate authority. The script will ask you if you'd like to change the certificate fields,
-the default port, client's DNS server, etc. If you know you want to change these things, feel free,
-and the script will put all the information where it needs to go in the various config files.
-If you aren't sure, it has been designed that you can simply hit 'Enter' through all the questions
-and have a working configuration at the end.
+If you aren't sure, it has been designed that you can simply hit 'Enter' through all the questions and have a working configuration at the end.
-Finally, the script will take some time to build the server's Diffie-Hellman key
-exchange. If you chose 1024-bit encryption, this will just take a few minutes, but if you
-chose 2048-bit, it will take much longer (anywhere from 40 minutes to several hours on a
-Model B+).
+Finally, the script will take some time to build the server's Diffie-Hellman key exchange (OpenVPN only). If you chose 2048-bit encryption, it will take about 40 minutes on a Model B+, and several hours if you choose a larger size.
-NOTE: Diffie-Hellman parameters are NOT generated if you choose not to use OpenVPN 2.4.
+The script will also make some changes to your system to allow it to forward internet traffic and allow VPN connections through the Pi's firewall. When the script informs you that it has finished configuring PiVPN, it will ask if you want to reboot. I have it where you do not need to reboot when done but it also can't hurt.
-The script will also make some changes to your system to allow it to forward
-internet traffic and allow VPN connections through the Pi's firewall. When the script
-informs you that it has finished configuring OpenVPN, it will ask if you want to reboot.
-I have it where you do not need to reboot when done but it also can't hurt.
+After the installation is complete you can use the command `pivpn` to manage the server. The commands below are just to get started, run `pivpn -h` to see the full list of options.
-
-Managing the PiVPN
+Managing the PiVPN (WireGuard)
----------------------
-After the installation is complete you can use the command 'pivpn' to manage the server.
+`pivpn add`
+You will be prompted to enter a name for your client. Pick anything you like and hit 'enter'.
+The script will assemble the client .conf file and place it in the directory 'configs' within your
+home directory.
-"pivpn add"
+`pivpn remove`
+Asks you for the name of the client to remove. Once you remove a client, it will no longer allow you to use
+the given client config (specifically its public key) to connect. This is useful for many reasons but some ex:
+You have a profile on a mobile phone and it was lost or stolen. Remove its key and generate a new
+one for your new phone. Or even if you suspect that a key may have been compromised in any way,
+just remove it and generate a new one.
+
+`pivpn list`
+If you add more than a few clients, this gives you a nice list of their names and associated keys.
+
+Managing the PiVPN (OpenVPN)
+----------------------
+
+`pivpn add`
You will be prompted to enter a name for your client. Pick anything you like and hit 'enter'.
You will be asked to enter a pass phrase for the client key; make sure it's one you'll remember.
The script will assemble the client .ovpn file and place it in the directory 'ovpns' within your
@@ -87,48 +126,79 @@ home directory.
If you need to create a client certificate that is not password protected (IE for use on a router),
then you can use the 'pivpn add nopass' option to generate that.
-"pivpn revoke"
+`pivpn revoke`
Asks you for the name of the client to revoke. Once you revoke a client, it will no longer allow you to use
the given client certificate (ovpn config) to connect. This is useful for many reasons but some ex:
You have a profile on a mobile phone and it was lost or stolen. Revoke its cert and generate a new
one for your new phone. Or even if you suspect that a cert may have been compromised in any way,
just revoke it and generate a new one.
-"pivpn list"
+`pivpn list`
If you add more than a few clients, this gives you a nice list of their names and whether their certificate
is still valid or has been revoked. Great way to keep track of what you did with 'pivpn add' and 'pivpn revoke'.
-You can run just 'pivpn' to see all the options.
-
-Importing .ovpn Profiles on Client Machines
+Importing Profiles on Client Machines
--------------------------------------------
-To move a client .ovpn profile to Windows, use a program like WinSCP or Cyberduck. Note that
-you may need administrator permission to move files to some folders on your Windows machine,
-so if you have trouble transferring the profile to a particular folder with your chosen file
-transfer program, try moving it to your desktop. To move a profile to Android, you can either
-retrieve it on PC and then move it to your device via USB, or you can use an app like Turbo
-FTP & SFTP client to retrieve it directly from your Android device.
+**Windows**: Use a program like WinSCP or Cyberduck. Note that you may need administrator permission to move files to some folders on your Windows machine, so if you have trouble transferring the profile to a particular folder with your chosen file transfer program, try moving it to your desktop.
-To import the profile to OpenVPN on Windows, download the OpenVPN GUI from the community downloads
-section of openvpn.net, install it, and place the profile in the 'config' folder of your OpenVPN
-directory, i.e., in 'C:\Program Files\OpenVPN\config'. To import the profile on Android, install
-the OpenVPN Connect app, select 'Import' from the drop-down menu in the upper right corner of the
-main screen, choose the directory on your device where you stored the .ovpn file, and select the
-file.
+**Mac/Linux**: Open the Terminal app and copy the config from the Raspberry Pi using `scp pi-user@ip-of-your-raspberry:configs/whatever.conf .` (if using WireGuard) or `scp pi-user@ip-of-your-raspberry:ovpns/whatever.ovpn .` (if using OpenVPN). The file will be downloaded in the current working directory, which usually is the home folder of your PC.
-After importing, connect to the VPN server on Windows by running the OpenVPN GUI with
-administrator permissions, right-clicking on the icon in the system tray, and clicking 'Connect',
-or on Android by selecting the profile under 'OpenVPN Profile' and pressing 'Connect'. You'll be
-asked to enter the pass phrase you chose. Do so, and you're in! Enjoy your ~$50 USD private VPN.
+**Android/iOS** (WireGuard only): Just skip to _Connecting to the PiVPN server (WireGuard)_
+
+**Android**: You can either retrieve it on PC and then move it to your device via USB, or you can use an app like Turbo FTP & SFTP client to retrieve it directly from your Android device.
+
+**iOS**: You can use an app that supports SFTP like Documents by Readdle to retrieve it directly from your iOS device.
+
+Connecting to the PiVPN server (WireGuard)
+--------------------------------------------
+
+**Windows/Mac**: Download the [WireGuard GUI app](https://www.wireguard.com/install/), import the configuration and activate the tunnel.
+
+**Linux**: Install [WireGuard](https://www.wireguard.com/install/) following the instructions for your distribution. Now, as root user, create the /etc/wireguard folder and prevent anyone but root to enter it (you only need to do this the first time):
+```
+mkdir -p /etc/wireguard
+chown root:root /etc/wireguard
+chmod 700 /etc/wireguard
+```
+Move the config and activate the tunnel:
+```
+mv whatever.conf /etc/wireguard/
+wg-quick up whatever
+```
+Run `wg-quick down whatever` to deactivate the tunnel.
+
+**Android/iOS:** Run `pivpn -qr` on the PiVPN server to generate a QR code of your config, download the Wireguard app [Android link](https://play.google.com/store/apps/details?id=com.wireguard.android) / [iOS link](https://apps.apple.com/it/app/wireguard/id1441195209), click the '+' sign and scan the QR code with your phone's camera. Flip the switch to activate the tunnel.
+
+Connecting to the PiVPN server (OpenVPN)
+--------------------------------------------
+
+**Windows**: Download the [OpenVPN GUI](https://openvpn.net/community-downloads/), install it, and place the profile in the 'config' folder of your OpenVPN directory, i.e., in 'C:\Program Files\OpenVPN\config'. After importing, connect to the VPN server on Windows by running the OpenVPN GUI with administrator permissions, right-clicking on the icon in the system tray, and clicking 'Connect'.
+
+**Linux**: Install OpenVPN using your package manager (APT in this example). Now, as root user, create the /etc/openvpn/client folder and prevent anyone but root to enter it (you only need to do this the first time):
+```
+apt install openvpn
+mkdir -p /etc/openvpn/client
+chown root:root /etc/openvpn/client
+chmod 700 /etc/openvpn/client
+```
+Move the config and connect (input the pass phrase if you set one):
+```
+mv whatever.ovpn /etc/openvpn/client/
+openvpn /etc/openvpn/client/whatever.ovpn
+```
+Press CTRL-C to disconnect.
+
+**Mac**: You can use an OpenVPN client like [Tunnelblick](https://tunnelblick.net/downloads.html). Here's a [guide](https://tunnelblick.net/czUsing.html) to import the configuration.
+
+**Android**: Install the [OpenVPN Connect app](https://play.google.com/store/apps/details?id=net.openvpn.openvpn), select 'Import' from the drop-down menu in the upper right corner of the main screen, choose the directory on your device where you stored the .ovpn file, and select the file. Connect by selecting the profile under 'OpenVPN Profile' and pressing 'Connect'.
+
+**iOS**: Install the [OpenVPN Connect app](https://apps.apple.com/it/app/openvpn-connect/id590379981). Then go to the app where you copied the .ovpn file to, select the file, find an icon or button to 'Share' or 'Open with', and choose to open with the OpenVPN app.
Removing PiVPN
----------------
-If at any point you wish to remove OpenVPN from your Pi and revert it to a
-pre-installation state, such as if you want to undo a failed installation to try again or
-you want to remove OpenVPN without installing a fresh Raspbian image, just run
-'pivpn uninstall'
+If at any point you wish to remove PiVPN from your Pi and revert it to a pre-installation state, such as if you want to undo a failed installation to try again or you want to remove PiVPN without installing a fresh Raspbian image, just run `pivpn uninstall`.
Feedback & Support
--------
@@ -140,7 +210,7 @@ Please be respectful and be aware that this is maintained with our free time!
for community support or general questions.
Feel free to post on our subreddit
-You can also join #pivpn on freenode in IRC
+You can also join #pivpn on freenode in IRC
For code related issues, code contributions, feature requests, feel free to open an issue here at github.
We will classify the issues the best we can to keep things sorted.
@@ -157,6 +227,9 @@ A secure docker container that sets up PiVPN and SSH.
[OpenVPN](https://openvpn.net)
The foundation for all open-source VPN projects.
+[WireGuard](https://www.wireguard.com/)
+*An extremely simple yet fast and modern VPN that utilizes state-of-the-art cryptography.*
+
Contributions
-------------
@@ -175,6 +248,8 @@ sources.
3. Of course there is [OpenVPN](https://openvpn.net)
-4. And as always the ever vigilant [EFF](https://www.eff.org/)
+4. Also [WireGuard](https://www.wireguard.com/)
+
+5. And as always the ever vigilant [EFF](https://www.eff.org/)
PiVPN is not taking donations at this time but if you want to show your appreciation, then contribute or leave feedback on suggestions or improvements.
diff --git a/auto_install/install.sh b/auto_install/install.sh
index 05241e1..1e5f7d6 100755
--- a/auto_install/install.sh
+++ b/auto_install/install.sh
@@ -1,43 +1,56 @@
#!/usr/bin/env bash
-# PiVPN: Trivial OpenVPN setup and configuration
-# Easiest setup and mangement of OpenVPN on Raspberry Pi
-# http://pivpn.io
+# PiVPN: Trivial OpenVPN or WireGuard setup and configuration
+# Easiest setup and mangement of OpenVPN or WireGuard on Raspberry Pi
+# https://pivpn.dev
# Heavily adapted from the pi-hole.net project and...
# https://github.com/StarshipEngineer/OpenVPN-Setup/
#
# Install with this command (from your Pi):
#
-# curl -L https://install.pivpn.io | bash
+# curl -L https://install.pivpn.dev | bash
# Make sure you have `curl` installed
-set -e
######## VARIABLES #########
-setupVars=/etc/pivpn/setupVars.conf
-useUpdateVars=false
+pivpnGitUrl="https://github.com/pivpn/pivpn.git"
+setupVars="/etc/pivpn/setupVars.conf"
+pivpnFilesDir="/etc/.pivpn"
### PKG Vars ###
PKG_MANAGER="apt-get"
PKG_CACHE="/var/lib/apt/lists/"
+### FIXME: quoting UPDATE_PKG_CACHE and PKG_INSTALL hangs the script, shellcheck SC2086
UPDATE_PKG_CACHE="${PKG_MANAGER} update"
PKG_INSTALL="${PKG_MANAGER} --yes --no-install-recommends install"
PKG_COUNT="${PKG_MANAGER} -s -o Debug::NoLocking=true upgrade | grep -c ^Inst || true"
-PIVPN_DEPS=(openvpn git tar wget grep iptables-persistent dnsutils expect whiptail net-tools grepcidr jq)
-### ###
+# Dependencies that are required by the script, regardless of the VPN protocol chosen
+BASE_DEPS=(git tar wget grep dnsutils whiptail net-tools bsdmainutils)
+
+# Dependencies that where actually installed by the script. For example if the script requires
+# grep and dnsutils but dnsutils is already installed, we save grep here. This way when uninstalling
+# PiVPN we won't prompt to remove packages that may have been installed by the user for other reasons
+TO_INSTALL=()
-pivpnGitUrl="https://github.com/pivpn/pivpn.git"
-pivpnFilesDir="/etc/.pivpn"
easyrsaVer="3.0.6"
easyrsaRel="https://github.com/OpenVPN/easy-rsa/releases/download/v${easyrsaVer}/EasyRSA-unix-v${easyrsaVer}.tgz"
+subnetClass="24"
+dhcpcdFile="/etc/dhcpcd.conf"
+debianOvpnUserGroup="openvpn:openvpn"
+
# Raspbian's unattended-upgrades package downloads Debian's config, so this is the link for the proper config
-UNATTUPG_RELEASE="1.9"
+UNATTUPG_RELEASE="1.16"
UNATTUPG_CONFIG="https://github.com/mvo5/unattended-upgrades/archive/${UNATTUPG_RELEASE}.tar.gz"
# Find the rows and columns. Will default to 80x24 if it can not be detected.
screen_size=$(stty size 2>/dev/null || echo 24 80)
-rows=$(echo $screen_size | awk '{print $1}')
-columns=$(echo $screen_size | awk '{print $2}')
+rows=$(echo "$screen_size" | awk '{print $1}')
+columns=$(echo "$screen_size" | awk '{print $2}')
+
+######## Undocumented Flags. Shhh ########
+runUnattended=false
+skipSpaceCheck=false
+reconfigure=false
# Divide by two so the dialogs take up half of the screen, which looks nice.
r=$(( rows / 2 ))
@@ -46,213 +59,458 @@ c=$(( columns / 2 ))
r=$(( r < 20 ? 20 : r ))
c=$(( c < 70 ? 70 : c ))
-######## Undocumented Flags. Shhh ########
-skipSpaceCheck=false
-reconfigure=false
-runUnattended=false
-
# Find IP used to route to outside world
+IPv4addr=$(ip route get 192.0.2.1 | awk '{print $7}')
+IPv4gw=$(ip route get 192.0.2.1 | awk '{print $3}')
+availableInterfaces=$(ip -o link | awk '/state UP/ {print $2}' | cut -d':' -f1 | cut -d'@' -f1)
-IPv4dev=$(ip route get 8.8.8.8 | awk '{for(i=1;i<=NF;i++)if($i~/dev/)print $(i+1)}')
-IPv4addr=$(ip route get 8.8.8.8| awk '{print $7}')
-IPv4gw=$(ip route get 8.8.8.8 | awk '{print $3}')
+######## SCRIPT ############
-availableInterfaces=$(ip -o link | grep "state UP" | awk '{print $2}' | cut -d':' -f1 | cut -d'@' -f1)
-dhcpcdFile=/etc/dhcpcd.conf
+main(){
-# Next see if we are on a tested and supported OS
-function noOS_Support() {
- whiptail --msgbox --backtitle "INVALID OS DETECTED" --title "Invalid OS" "We have not been able to detect a supported OS.
-Currently this installer supports Raspbian and Debian (Jessie and Stretch), Devuan (Jessie) and Ubuntu from 14.04 (trusty) to 17.04 (zesty).
-If you think you received this message in error, you can post an issue on the GitHub at https://github.com/pivpn/pivpn/issues." ${r} ${c}
- exit 1
-}
+ ######## FIRST CHECK ########
+ # Must be root to install
+ echo ":::"
+ if [[ $EUID -eq 0 ]];then
+ echo "::: You are root."
+ else
+ echo "::: sudo will be used for the install."
+ # Check if it is actually installed
+ # If it isn't, exit because the install cannot complete
+ if [[ $(dpkg-query -s sudo) ]];then
+ export SUDO="sudo"
+ export SUDOE="sudo -E"
+ else
+ echo "::: Please install sudo or run this as root."
+ exit 1
+ fi
+ fi
-function maybeOS_Support() {
- if (whiptail --backtitle "Not Supported OS" --title "Not Supported OS" --yesno "You are on an OS that we have not tested but MAY work.
-Currently suppoerted:
- Raspbian, Debian, Devuan (Jessie, strech, buster)
- Ubuntu from 14.04 (trusty) to 18.04 (zesty).
+ # Check arguments for the undocumented flags
+ for var in "$@"; do
+ case "$var" in
+ "--i_do_not_follow_recommendations" ) skipSpaceCheck=false;;
+ "--unattended" ) runUnattended=true;;
+ "--reconfigure" ) reconfigure=true;;
+ esac
+ done
-Would you like to continue anyway?" ${r} ${c}) then
- echo "::: Did not detect perfectly supported OS but,"
- echo "::: Continuing installation at user's own risk..."
- else
- echo "::: Exiting due to unsupported OS"
- exit 1
- fi
-}
+ if [[ "${runUnattended}" == true ]]; then
+ echo "::: --unattended passed to install script, no whiptail dialogs will be displayed"
+ if [ -z "$2" ]; then
+ echo "::: No configuration file passed, using default settings..."
+ else
+ if [ -r "$2" ]; then
+ # shellcheck disable=SC1090
+ source "$2"
+ else
+ echo "::: Can't open $2"
+ exit 1
+ fi
+ fi
+ fi
-# Compatibility
-distro_check() {
- # if lsb_release command is on their system
- if hash lsb_release 2>/dev/null; then
+ if [ -r "$setupVars" ]; then
+ if [[ "${reconfigure}" == true ]]; then
+ echo "::: --reconfigure passed to install script, will reinstall PiVPN overwriting existing settings"
+ UpdateCmd="Reconfigure"
+ elif [[ "${runUnattended}" == true ]]; then
+ ### What should the script do when passing --unattended to an existing installation?
+ UpdateCmd="Reconfigure"
+ else
+ askAboutExistingInstall
+ fi
+ fi
- PLAT=$(lsb_release -si)
- OSCN=$(lsb_release -sc) # We want this to be trusty xenial, jessie, stretch, buster
+ if [ -z "$UpdateCmd" ] || [ "$UpdateCmd" = "Reconfigure" ]; then
+ :
+ elif [ "$UpdateCmd" = "Update" ]; then
+ $SUDO /opt/pivpn/update.sh "$@"
+ exit 0
+ elif [ "$UpdateCmd" = "Repair" ]; then
+ # shellcheck disable=SC1090
+ source "$setupVars"
+ runUnattended=true
+ fi
- else # else get info from os-release
+ # Check for supported distribution
+ distroCheck
- source /etc/os-release
- PLAT=$(awk '{print $1}' <<< "$NAME")
- VER="$VERSION_ID"
- declare -A VER_MAP=(["10"]="buster" ["9"]="stretch" ["8"]="jessie" ["18.04"]="bionic" ["16.04"]="xenial" ["14.04"]="trusty")
- OSCN=${VER_MAP["${VER}"]}
- fi
+ # Checks for hostname Length
+ checkHostname
- if [[ ${OSCN} != "bionic" ]]; then
- PIVPN_DEPS+=(dhcpcd5)
+ # Start the installer
+ # Verify there is enough disk space for the install
+ if [[ "${skipSpaceCheck}" == true ]]; then
+ echo "::: --i_do_not_follow_recommendations passed to script, skipping free disk space verification!"
+ else
+ verifyFreeDiskSpace
+ fi
- fi
+ updatePackageCache
- case ${PLAT} in
- Ubuntu|Raspbian|Debian|Devuan)
- case ${OSCN} in
- trusty|xenial|jessie|stretch|buster|bionic)
- ;;
- *)
- maybeOS_Support
- ;;
- esac
- ;;
- *)
- noOS_Support
- ;;
- esac
+ # Notify user of package availability
+ notifyPackageUpdatesAvailable
- echo "${PLAT}" > /tmp/DET_PLATFORM
+ # Install packages used by this installation script
+ preconfigurePackages
+ installDependentPackages BASE_DEPS[@]
+
+ # Display welcome dialogs
+ welcomeDialogs
+
+ # Find interfaces and let the user choose one
+ chooseInterface
+
+ if [ "$PLAT" != "Raspbian" ]; then
+ avoidStaticIPv4Ubuntu
+ else
+ getStaticIPv4Settings
+ if [ -z "$dhcpReserv" ] || [ "$dhcpReserv" -ne 1 ]; then
+ setStaticIPv4
+ fi
+ fi
+
+ # Choose the user for the ovpns
+ chooseUser
+
+ # Clone/Update the repos
+ cloneOrUpdateRepos
+
+ # Install
+ if installPiVPN; then
+ echo "::: Install Complete..."
+ else
+ exit 1
+ fi
+
+ # Start services
+ restartServices
+
+ # Ask if unattended-upgrades will be enabled
+ askUnattendedUpgrades
+
+ if [ "$UNATTUPG" -eq 1 ]; then
+ confUnattendedUpgrades
+ fi
+
+ # Save installation setting to the final location
+ echo "TO_INSTALL=(${TO_INSTALL[*]})" >> /tmp/setupVars.conf
+ $SUDO cp /tmp/setupVars.conf "$setupVars"
+
+ installScripts
+
+ # Ensure that cached writes reach persistent storage
+ echo "::: Flushing writes to disk..."
+ sync
+ echo "::: done."
+
+ displayFinalMessage
+ echo ":::"
}
####### FUNCTIONS ##########
-spinner()
-{
- local pid=$1
- local delay=0.50
- local spinstr='/-\|'
- while [ "$(ps a | awk '{print $1}' | grep "${pid}")" ]; do
- local temp=${spinstr#?}
- printf " [%c] " "${spinstr}"
- local spinstr=${temp}${spinstr%"$temp"}
- sleep ${delay}
- printf "\b\b\b\b\b\b"
- done
- printf " \b\b\b\b"
+
+askAboutExistingInstall(){
+ opt1a="Update"
+ opt1b="Get the latest PiVPN scripts"
+
+ opt2a="Repair"
+ opt2b="Reinstall PiVPN using existing settings"
+
+ opt3a="Reconfigure"
+ opt3b="Reinstall PiVPN with new settings"
+
+ UpdateCmd=$(whiptail --title "Existing Install Detected!" --menu "\nWe have detected an existing install.\n\nPlease choose from the following options:" ${r} ${c} 3 \
+ "${opt1a}" "${opt1b}" \
+ "${opt2a}" "${opt2b}" \
+ "${opt3a}" "${opt3b}" 3>&2 2>&1 1>&3) || \
+ { echo "::: Cancel selected. Exiting"; exit 1; }
+
+ echo "::: ${UpdateCmd} option selected."
}
-welcomeDialogs() {
- # Display the welcome dialog
- whiptail --msgbox --backtitle "Welcome" --title "PiVPN Automated Installer" "This installer will transform your Raspberry Pi into an OpenVPN server!" ${r} ${c}
- # Explain the need for a static address
- whiptail --msgbox --backtitle "Initiating network interface" --title "Static IP Needed" "The PiVPN is a SERVER so it needs a STATIC IP ADDRESS to function properly.
+# Compatibility, functions to check for supported OS
+# distroCheck, maybeOSSupport, noOSSupport
+distroCheck(){
+ # if lsb_release command is on their system
+ if hash lsb_release 2>/dev/null; then
+
+ PLAT=$(lsb_release -si)
+ OSCN=$(lsb_release -sc)
+
+ else # else get info from os-release
+
+ # shellcheck disable=SC1091
+ source /etc/os-release
+ PLAT=$(awk '{print $1}' <<< "$NAME")
+ VER="$VERSION_ID"
+ declare -A VER_MAP=(["9"]="stretch" ["10"]="buster" ["16.04"]="xenial" ["18.04"]="bionic")
+ OSCN=${VER_MAP["${VER}"]}
+ fi
+
+ case ${PLAT} in
+ Debian|Raspbian|Ubuntu)
+ case ${OSCN} in
+ buster|xenial|bionic|stretch)
+ :
+ ;;
+ *)
+ maybeOSSupport
+ ;;
+ esac
+ ;;
+ *)
+ noOSSupport
+ ;;
+ esac
+
+ if [ "$PLAT" = "Raspbian" ]; then
+ BASE_DEPS+=(dhcpcd5)
+ fi
+
+ echo "PLAT=${PLAT}" > /tmp/setupVars.conf
+ echo "OSCN=${OSCN}" >> /tmp/setupVars.conf
+}
+
+noOSSupport(){
+ if [ "${runUnattended}" = 'true' ]; then
+ echo "::: Invalid OS detected"
+ echo "::: We have not been able to detect a supported OS."
+ echo "::: Currently this installer supports Raspbian, Debian and Ubuntu."
+ exit 1
+ fi
+
+ whiptail --msgbox --backtitle "INVALID OS DETECTED" --title "Invalid OS" "We have not been able to detect a supported OS.
+Currently this installer supports Raspbian, Debian and Ubuntu.
+For more details, check our documentation at https://github.com/pivpn/pivpn/wiki " ${r} ${c}
+ exit 1
+}
+
+maybeOSSupport(){
+ if [ "${runUnattended}" = 'true' ]; then
+ echo "::: OS Not Supported"
+ echo "::: You are on an OS that we have not tested but MAY work, continuing anyway..."
+ return
+ fi
+
+ if (whiptail --backtitle "Untested OS" --title "Untested OS" --yesno "You are on an OS that we have not tested but MAY work.
+Currently this installer supports Raspbian, Debian and Ubuntu.
+For more details about supported OS please check our documentation at https://github.com/pivpn/pivpn/wiki
+Would you like to continue anyway?" ${r} ${c}) then
+ echo "::: Did not detect perfectly supported OS but,"
+ echo "::: Continuing installation at user's own risk..."
+ else
+ echo "::: Exiting due to untested OS"
+ exit 1
+ fi
+}
+
+
+checkHostname(){
+###Checks for hostname size
+ host_name=$(hostname -s)
+ if [[ ! ${#host_name} -le 28 ]]; then
+ if [ "${runUnattended}" = 'true' ]; then
+ echo "::: Your hostname is too long."
+ echo "::: Use 'hostnamectl set-hostname YOURHOSTNAME' to set a new hostname"
+ echo "::: It must be less then 28 characters long and it must not use special characters"
+ exit 1
+ fi
+ until [[ ${#host_name} -le 28 && $host_name =~ ^[a-zA-Z0-9][a-zA-Z0-9-]{1,28}$ ]]; do
+ host_name=$(whiptail --inputbox "Your hostname is too long.\\nEnter new hostname with less then 28 characters\\nNo special characters allowed." \
+ --title "Hostname too long" ${r} ${c} 3>&1 1>&2 2>&3)
+ $SUDO hostnamectl set-hostname "${host_name}"
+ if [[ ${#host_name} -le 28 && $host_name =~ ^[a-zA-Z0-9][a-zA-Z0-9-]{1,28}$ ]]; then
+ echo "::: Hostname valid and length OK, proceeding..."
+ fi
+ done
+ else
+ echo "::: Hostname length OK"
+ fi
+}
+
+spinner(){
+ local pid=$1
+ local delay=0.50
+ local spinstr='/-\|'
+ while ps a | awk '{print $1}' | grep "${pid}"; do
+ local temp=${spinstr#?}
+ printf " [%c] " "${spinstr}"
+ local spinstr=${temp}${spinstr%"$temp"}
+ sleep ${delay}
+ printf "\\b\\b\\b\\b\\b\\b"
+ done
+ printf " \\b\\b\\b\\b"
+}
+
+verifyFreeDiskSpace(){
+ # If user installs unattended-upgrades we'd need about 60MB so will check for 75MB free
+ echo "::: Verifying free disk space..."
+ local required_free_kilobytes=76800
+ local existing_free_kilobytes
+ existing_free_kilobytes=$(df -Pk | grep -m1 '\/$' | awk '{print $4}')
+
+ # - Unknown free disk space , not a integer
+ if ! [[ "${existing_free_kilobytes}" =~ ^([0-9])+$ ]]; then
+ echo "::: Unknown free disk space!"
+ echo "::: We were unable to determine available free disk space on this system."
+ if [ "${runUnattended}" = 'true' ]; then
+ exit 1
+ fi
+ echo "::: You may continue with the installation, however, it is not recommended."
+ read -r -p "::: If you are sure you want to continue, type YES and press enter :: " response
+ case $response in
+ [Y][E][S])
+ ;;
+ *)
+ echo "::: Confirmation not received, exiting..."
+ exit 1
+ ;;
+ esac
+ # - Insufficient free disk space
+ elif [[ ${existing_free_kilobytes} -lt ${required_free_kilobytes} ]]; then
+ echo "::: Insufficient Disk Space!"
+ echo "::: Your system appears to be low on disk space. PiVPN recommends a minimum of $required_free_kilobytes KiloBytes."
+ echo "::: You only have ${existing_free_kilobytes} KiloBytes free."
+ echo "::: If this is a new install on a Raspberry Pi you may need to expand your disk."
+ echo "::: Try running 'sudo raspi-config', and choose the 'expand file system option'"
+ echo "::: After rebooting, run this installation again. (curl -L https://install.pivpn.dev | bash)"
+
+ echo "Insufficient free space, exiting..."
+ exit 1
+ fi
+}
+
+updatePackageCache(){
+ #Running apt-get update/upgrade with minimal output can cause some issues with
+ #requiring user input
+
+ #Check to see if apt-get update has already been run today
+ #it needs to have been run at least once on new installs!
+ timestamp=$(stat -c %Y ${PKG_CACHE})
+ timestampAsDate=$(date -d @"${timestamp}" "+%b %e")
+ today=$(date "+%b %e")
+
+
+ if [ ! "${today}" == "${timestampAsDate}" ]; then
+ #update package lists
+ echo ":::"
+ echo -ne "::: ${PKG_MANAGER} update has not been run today. Running now...\\n"
+ # shellcheck disable=SC2086
+ $SUDO ${UPDATE_PKG_CACHE} &> /dev/null
+ echo " done!"
+ fi
+}
+
+notifyPackageUpdatesAvailable(){
+ # Let user know if they have outdated packages on their system and
+ # advise them to run a package update at soonest possible.
+ echo ":::"
+ echo -n "::: Checking ${PKG_MANAGER} for upgraded packages...."
+ updatesToInstall=$(eval "${PKG_COUNT}")
+ echo " done!"
+ echo ":::"
+ if [[ ${updatesToInstall} -eq "0" ]]; then
+ echo "::: Your system is up to date! Continuing with PiVPN installation..."
+ else
+ echo "::: There are ${updatesToInstall} updates available for your system!"
+ echo "::: We recommend you update your OS after installing PiVPN! "
+ echo ":::"
+ fi
+}
+
+preconfigurePackages(){
+ # Add support for https repositories if there are any that use it otherwise the installation will silently fail
+ if [[ -f /etc/apt/sources.list ]]; then
+ if grep -q https /etc/apt/sources.list; then
+ BASE_DEPS+=("apt-transport-https")
+ fi
+ fi
+
+ if [[ ${OSCN} == "buster" ]]; then
+ $SUDO update-alternatives --set iptables /usr/sbin/iptables-legacy
+ $SUDO update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
+ fi
+
+ # if ufw is enabled, configure that.
+ # running as root because sometimes the executable is not in the user's $PATH
+ if $SUDO bash -c 'hash ufw' 2>/dev/null; then
+ if LANG=en_US.UTF-8 $SUDO ufw status | grep -q inactive; then
+ USING_UFW=0
+ else
+ USING_UFW=1
+ fi
+ else
+ USING_UFW=0
+ fi
+
+ if [ "$USING_UFW" -eq 0 ]; then
+ BASE_DEPS+=(iptables-persistent)
+ echo iptables-persistent iptables-persistent/autosave_v4 boolean true | $SUDO debconf-set-selections
+ echo iptables-persistent iptables-persistent/autosave_v6 boolean false | $SUDO debconf-set-selections
+ fi
+
+ echo "USING_UFW=${USING_UFW}" >> /tmp/setupVars.conf
+}
+
+installDependentPackages(){
+ # Install packages passed in via argument array
+ # No spinner - conflicts with set -e
+ declare -a argArray1=("${!1}")
+
+ for i in "${argArray1[@]}"; do
+ echo -n "::: Checking for $i..."
+ if dpkg-query -W -f='${Status}' "${i}" 2>/dev/null | grep -q "ok installed"; then
+ echo " installed!"
+ else
+ TO_INSTALL+=("${i}")
+ echo " not installed!"
+ fi
+ done
+
+ if command -v debconf-apt-progress &> /dev/null; then
+ # shellcheck disable=SC2086
+ $SUDO debconf-apt-progress -- ${PKG_INSTALL} "${argArray1[@]}"
+ else
+ ${PKG_INSTALL} "${argArray1[@]}"
+ fi
+}
+
+welcomeDialogs(){
+ if [ "${runUnattended}" = 'true' ]; then
+ echo "::: PiVPN Automated Installer"
+ echo "::: This installer will transform your ${PLAT} host into an OpenVPN or WireGuard server!"
+ echo "::: Initiating network interface"
+ return
+ fi
+
+ # Display the welcome dialog
+ whiptail --msgbox --backtitle "Welcome" --title "PiVPN Automated Installer" "This installer will transform your Raspberry Pi into an OpenVPN or WireGuard server!" ${r} ${c}
+
+ # Explain the need for a static address
+ whiptail --msgbox --backtitle "Initiating network interface" --title "Static IP Needed" "The PiVPN is a SERVER so it needs a STATIC IP ADDRESS to function properly.
In the next section, you can choose to use your current network settings (DHCP) or to manually edit them." ${r} ${c}
}
-chooseUser() {
- # Explain the local user
- whiptail --msgbox --backtitle "Parsing User List" --title "Local Users" "Choose a local user that will hold your ovpn configurations." ${r} ${c}
- # First, let's check if there is a user available.
- numUsers=$(awk -F':' 'BEGIN {count=0} $3>=1000 && $3<=60000 { count++ } END{ print count }' /etc/passwd)
- if [ "$numUsers" -eq 0 ]
- then
- # We don't have a user, let's ask to add one.
- if userToAdd=$(whiptail --title "Choose A User" --inputbox "No non-root user account was found. Please type a new username." ${r} ${c} 3>&1 1>&2 2>&3)
- then
- # See http://askubuntu.com/a/667842/459815
- PASSWORD=$(whiptail --title "password dialog" --passwordbox "Please enter the new user password" ${r} ${c} 3>&1 1>&2 2>&3)
- CRYPT=$(perl -e 'printf("%s\n", crypt($ARGV[0], "password"))' "${PASSWORD}")
- $SUDO useradd -m -p "${CRYPT}" -s /bin/bash "${userToAdd}"
- if [[ $? = 0 ]]; then
- echo "Succeeded"
- ((numUsers+=1))
- else
- exit 1
- fi
- else
- exit 1
- fi
- fi
- availableUsers=$(awk -F':' '$3>=1000 && $3<=60000 {print $1}' /etc/passwd)
- local userArray=()
- local firstloop=1
-
- while read -r line
- do
- mode="OFF"
- if [[ $firstloop -eq 1 ]]; then
- firstloop=0
- mode="ON"
- fi
- userArray+=("${line}" "" "${mode}")
- done <<< "${availableUsers}"
- chooseUserCmd=(whiptail --title "Choose A User" --separate-output --radiolist "Choose (press space to select):" ${r} ${c} ${numUsers})
- chooseUserOptions=$("${chooseUserCmd[@]}" "${userArray[@]}" 2>&1 >/dev/tty)
- if [[ $? = 0 ]]; then
- for desiredUser in ${chooseUserOptions}; do
- INSTALL_USER=${desiredUser}
- echo "::: Using User: $INSTALL_USER"
- echo "${INSTALL_USER}" > /tmp/INSTALL_USER
- done
- else
- echo "::: Cancel selected, exiting...."
- exit 1
- fi
-}
-
-verifyFreeDiskSpace() {
- # If user installs unattended-upgrades we'd need about 60MB so will check for 75MB free
- echo "::: Verifying free disk space..."
- local required_free_kilobytes=76800
- local existing_free_kilobytes=$(df -Pk | grep -m1 '\/$' | awk '{print $4}')
-
- # - Unknown free disk space , not a integer
- if ! [[ "${existing_free_kilobytes}" =~ ^([0-9])+$ ]]; then
- echo "::: Unknown free disk space!"
- echo "::: We were unable to determine available free disk space on this system."
- echo "::: You may continue with the installation, however, it is not recommended."
- read -r -p "::: If you are sure you want to continue, type YES and press enter :: " response
- case $response in
- [Y][E][S])
- ;;
- *)
- echo "::: Confirmation not received, exiting..."
- exit 1
- ;;
- esac
- # - Insufficient free disk space
- elif [[ ${existing_free_kilobytes} -lt ${required_free_kilobytes} ]]; then
- echo "::: Insufficient Disk Space!"
- echo "::: Your system appears to be low on disk space. PiVPN recommends a minimum of $required_free_kilobytes KiloBytes."
- echo "::: You only have ${existing_free_kilobytes} KiloBytes free."
- echo "::: If this is a new install on a Raspberry Pi you may need to expand your disk."
- echo "::: Try running 'sudo raspi-config', and choose the 'expand file system option'"
- echo "::: After rebooting, run this installation again. (curl -L https://install.pivpn.io | bash)"
-
- echo "Insufficient free space, exiting..."
- exit 1
- fi
-}
-
-
-chooseInterface() {
- # Turn the available interfaces into an array so it can be used with a whiptail dialog
- local interfacesArray=()
- # Number of available interfaces
- local interfaceCount
- # Whiptail variable storage
- local chooseInterfaceCmd
- # Temporary Whiptail options storage
- local chooseInterfaceOptions
- # Loop sentinel variable
- local firstloop=1
-
- if [[ $(echo "${availableInterfaces}" | wc -l) -eq 1 ]]; then
- pivpnInterface="${availableInterfaces}"
- echo "${pivpnInterface}" > /tmp/pivpnINT
- return
- fi
+chooseInterface(){
+# Turn the available interfaces into an array so it can be used with a whiptail dialog
+local interfacesArray=()
+# Number of available interfaces
+local interfaceCount
+# Whiptail variable storage
+local chooseInterfaceCmd
+# Temporary Whiptail options storage
+local chooseInterfaceOptions
+# Loop sentinel variable
+local firstloop=1
+if [ -z "$availableInterfaces" ]; then
+ echo "::: Could not find any active network interface, exiting"
+ exit 1
+else
while read -r line; do
mode="OFF"
if [[ ${firstloop} -eq 1 ]]; then
@@ -260,1242 +518,1426 @@ chooseInterface() {
mode="ON"
fi
interfacesArray+=("${line}" "available" "${mode}")
+ ((interfaceCount++))
done <<< "${availableInterfaces}"
+fi
- # Find out how many interfaces are available to choose from
- interfaceCount=$(echo "${availableInterfaces}" | wc -l)
- chooseInterfaceCmd=(whiptail --separate-output --radiolist "Choose An Interface (press space to select):" ${r} ${c} ${interfaceCount})
- chooseInterfaceOptions=$("${chooseInterfaceCmd[@]}" "${interfacesArray[@]}" 2>&1 >/dev/tty)
- if [[ $? = 0 ]]; then
- for desiredInterface in ${chooseInterfaceOptions}; do
- pivpnInterface=${desiredInterface}
- echo "::: Using interface: $pivpnInterface"
- echo "${pivpnInterface}" > /tmp/pivpnINT
- done
+if [ "${runUnattended}" = 'true' ]; then
+ if [ -z "$IPv4dev" ]; then
+ if [ $interfaceCount -eq 1 ]; then
+ IPv4dev="${availableInterfaces}"
+ echo "::: No interface specified, but only ${IPv4dev} is available, using it"
+ else
+ echo "::: No interface specified and failed to determine one"
+ exit 1
+ fi
else
- echo "::: Cancel selected, exiting...."
- exit 1
+ if ip -o link | grep -qw "${IPv4dev}"; then
+ echo "::: Using interface: ${IPv4dev}"
+ else
+ echo "::: Interface ${IPv4dev} does not exist"
+ exit 1
+ fi
fi
+ echo "IPv4dev=${IPv4dev}" >> /tmp/setupVars.conf
+ return
+else
+ if [ "$interfaceCount" -eq 1 ]; then
+ IPv4dev="${availableInterfaces}"
+ echo "IPv4dev=${IPv4dev}" >> /tmp/setupVars.conf
+ return
+ fi
+fi
+
+chooseInterfaceCmd=(whiptail --separate-output --radiolist "Choose An interface (press space to select):" "${r}" "${c}" "${interfaceCount}")
+if chooseInterfaceOptions=$("${chooseInterfaceCmd[@]}" "${interfacesArray[@]}" 2>&1 >/dev/tty) ; then
+ for desiredInterface in ${chooseInterfaceOptions}; do
+ IPv4dev=${desiredInterface}
+ echo "::: Using interface: $IPv4dev"
+ echo "IPv4dev=${IPv4dev}" >> /tmp/setupVars.conf
+ done
+else
+ echo "::: Cancel selected, exiting...."
+ exit 1
+fi
}
avoidStaticIPv4Ubuntu() {
- # If we are in Ubuntu then they need to have previously set their network, so just use what you have.
- whiptail --msgbox --backtitle "IP Information" --title "IP Information" "Since we think you are not using Raspbian, we will not configure a static IP for you.
+ if [ "${runUnattended}" = 'true' ]; then
+ echo "::: Since we think you are not using Raspbian, we will not configure a static IP for you."
+ return
+ fi
+
+ # If we are in Ubuntu then they need to have previously set their network, so just use what you have.
+ whiptail --msgbox --backtitle "IP Information" --title "IP Information" "Since we think you are not using Raspbian, we will not configure a static IP for you.
If you are in Amazon then you can not configure a static IP anyway. Just ensure before this installer started you had set an elastic IP on your instance." ${r} ${c}
}
+validIP(){
+ local ip=$1
+ local stat=1
+
+ if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
+ OIFS=$IFS
+ IFS='.'
+ read -r -a ip <<< "$ip"
+ IFS=$OIFS
+ [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
+ && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
+ stat=$?
+ fi
+ return $stat
+}
+
getStaticIPv4Settings() {
- local ipSettingsCorrect
- # Grab their current DNS Server
- IPv4dns=$(nslookup 127.0.0.1 | grep Server: | awk '{print $2}')
- # Ask if the user wants to use DHCP settings as their static IP
- if (whiptail --backtitle "Calibrating network interface" --title "Static IP Address" --yesno "Do you want to use your current network settings as a static address?
- IP address: ${IPv4addr}
- Gateway: ${IPv4gw}" ${r} ${c}); then
- # If they choose yes, let the user know that the IP address will not be available via DHCP and may cause a conflict.
- whiptail --msgbox --backtitle "IP information" --title "FYI: IP Conflict" "It is possible your router could still try to assign this IP to a device, which would cause a conflict. But in most cases the router is smart enough to not do that.
+ # Grab their current DNS Server
+ IPv4dns=$(grep nameserver /etc/resolv.conf | awk '{print $2}' | xargs)
+
+ if [ "${runUnattended}" = 'true' ]; then
+
+ if [ -z "$dhcpReserv" ] || [ "$dhcpReserv" -ne 1 ]; then
+ local INVALID_STATIC_IPV4_SETTINGS=0
+
+ if [ -z "$IPv4addr" ]; then
+ echo "::: Missing static IP address"
+ INVALID_STATIC_IPV4_SETTINGS=1
+ fi
+
+ if [ -z "$IPv4gw" ]; then
+ echo "::: Missing static IP gateway"
+ INVALID_STATIC_IPV4_SETTINGS=1
+ fi
+
+ if [ "$INVALID_STATIC_IPV4_SETTINGS" -eq 1 ]; then
+ echo "::: Incomplete static IP settings"
+ exit 1
+ fi
+
+ if [ -z "$IPv4addr" ] && [ -z "$IPv4gw" ]; then
+ echo "::: No static IP settings, using current settings"
+ echo "::: Your static IPv4 address: ${IPv4addr}"
+ echo "::: Your static IPv4 gateway: ${IPv4gw}"
+ else
+ if validIP "${IPv4addr%/*}"; then
+ echo "::: Your static IPv4 address: ${IPv4addr}"
+ else
+ echo "::: ${IPv4addr%/*} is not a valid IP address"
+ exit 1
+ fi
+
+ if validIP "${IPv4gw}"; then
+ echo "::: Your static IPv4 gateway: ${IPv4gw}"
+ else
+ echo "::: ${IPv4gw} is not a valid IP address"
+ exit 1
+ fi
+ fi
+ else
+ echo "::: Skipping setting static IP address"
+ fi
+
+ echo "dhcpReserv=${dhcpReserv}" >> /tmp/setupVars.conf
+ echo "IPv4addr=${IPv4addr%/*}" >> /tmp/setupVars.conf
+ echo "IPv4gw=${IPv4gw}" >> /tmp/setupVars.conf
+ return
+ fi
+
+ local ipSettingsCorrect
+ # Some users reserve IP addresses on another DHCP Server or on their routers,
+ # Lets ask them if they want to make any changes to their interfaces.
+ if (whiptail --backtitle "Calibrating network interface" --title "DHCP Reservation" --yesno \
+ "Are you Using DHCP Reservation on your Router/DHCP Server?
+These are your current Network Settings:
+
+ IP address: ${IPv4addr}
+ Gateway: ${IPv4gw}
+
+Yes: Keep using DHCP reservation
+No: Setup static IP address
+Don't know what DHCP Reservation is? Answer No." ${r} ${c}); then
+ dhcpReserv=1
+ # shellcheck disable=SC2129
+ echo "dhcpReserv=${dhcpReserv}" >> /tmp/setupVars.conf
+ echo "IPv4addr=${IPv4addr%/*}" >> /tmp/setupVars.conf
+ echo "IPv4gw=${IPv4gw}" >> /tmp/setupVars.conf
+ else
+ # Ask if the user wants to use DHCP settings as their static IP
+ if (whiptail --backtitle "Calibrating network interface" --title "Static IP Address" --yesno "Do you want to use your current network settings as a static address?
+ IP address: ${IPv4addr}
+ Gateway: ${IPv4gw}" ${r} ${c}); then
+
+ echo "IPv4addr=${IPv4addr%/*}" >> /tmp/setupVars.conf
+ echo "IPv4gw=${IPv4gw}" >> /tmp/setupVars.conf
+ # If they choose yes, let the user know that the IP address will not be available via DHCP and may cause a conflict.
+ whiptail --msgbox --backtitle "IP information" --title "FYI: IP Conflict" "It is possible your router could still try to assign this IP to a device, which would cause a conflict. But in most cases the router is smart enough to not do that.
If you are worried, either manually set the address, or modify the DHCP reservation pool so it does not include the IP you want.
It is also possible to use a DHCP reservation, but if you are going to do that, you might as well set a static address." ${r} ${c}
- # Nothing else to do since the variables are already set above
- else
- # Otherwise, we need to ask the user to input their desired settings.
- # Start by getting the IPv4 address (pre-filling it with info gathered from DHCP)
- # Start a loop to let the user enter their information with the chance to go back and edit it if necessary
- until [[ ${ipSettingsCorrect} = True ]]; do
- # Ask for the IPv4 address
- IPv4addr=$(whiptail --backtitle "Calibrating network interface" --title "IPv4 address" --inputbox "Enter your desired IPv4 address" ${r} ${c} "${IPv4addr}" 3>&1 1>&2 2>&3)
- if [[ $? = 0 ]]; then
- echo "::: Your static IPv4 address: ${IPv4addr}"
- # Ask for the gateway
- IPv4gw=$(whiptail --backtitle "Calibrating network interface" --title "IPv4 gateway (router)" --inputbox "Enter your desired IPv4 default gateway" ${r} ${c} "${IPv4gw}" 3>&1 1>&2 2>&3)
- if [[ $? = 0 ]]; then
- echo "::: Your static IPv4 gateway: ${IPv4gw}"
- # Give the user a chance to review their settings before moving on
- if (whiptail --backtitle "Calibrating network interface" --title "Static IP Address" --yesno "Are these settings correct?
- IP address: ${IPv4addr}
- Gateway: ${IPv4gw}" ${r} ${c}); then
- # If the settings are correct, then we need to set the pivpnIP
- echo "${IPv4addr%/*}" > /tmp/pivpnIP
- echo "$pivpnInterface" > /tmp/pivpnINT
- # After that's done, the loop ends and we move on
- ipSettingsCorrect=True
- else
- # If the settings are wrong, the loop continues
- ipSettingsCorrect=False
- fi
- else
- # Cancelling gateway settings window
- ipSettingsCorrect=False
- echo "::: Cancel selected. Exiting..."
- exit 1
- fi
- else
- # Cancelling IPv4 settings window
- ipSettingsCorrect=False
- echo "::: Cancel selected. Exiting..."
- exit 1
- fi
- done
- # End the if statement for DHCP vs. static
- fi
+ # Nothing else to do since the variables are already set above
+ else
+ # Otherwise, we need to ask the user to input their desired settings.
+ # Start by getting the IPv4 address (pre-filling it with info gathered from DHCP)
+ # Start a loop to let the user enter their information with the chance to go back and edit it if necessary
+ until [[ ${ipSettingsCorrect} = True ]]; do
+ # Ask for the IPv4 address
+ if IPv4addr=$(whiptail --backtitle "Calibrating network interface" --title "IPv4 address" --inputbox "Enter your desired IPv4 address" ${r} ${c} "${IPv4addr}" 3>&1 1>&2 2>&3) ; then
+ echo "::: Your static IPv4 address: ${IPv4addr}"
+ # Ask for the gateway
+ if IPv4gw=$(whiptail --backtitle "Calibrating network interface" --title "IPv4 gateway (router)" --inputbox "Enter your desired IPv4 default gateway" ${r} ${c} "${IPv4gw}" 3>&1 1>&2 2>&3) ; then
+ echo "::: Your static IPv4 gateway: ${IPv4gw}"
+ # Give the user a chance to review their settings before moving on
+ if (whiptail --backtitle "Calibrating network interface" --title "Static IP Address" --yesno "Are these settings correct?
+ IP address: ${IPv4addr}
+ Gateway: ${IPv4gw}" ${r} ${c}); then
+ # If the settings are correct, then we need to set the pivpnIP
+ echo "IPv4addr=${IPv4addr%/*}" >> /tmp/setupVars.conf
+ echo "IPv4gw=${IPv4gw}" >> /tmp/setupVars.conf
+ # After that's done, the loop ends and we move on
+ ipSettingsCorrect=True
+ else
+ # If the settings are wrong, the loop continues
+ ipSettingsCorrect=False
+ fi
+ else
+ # Cancelling gateway settings window
+ ipSettingsCorrect=False
+ echo "::: Cancel selected. Exiting..."
+ exit 1
+ fi
+ else
+ # Cancelling IPv4 settings window
+ ipSettingsCorrect=False
+ echo "::: Cancel selected. Exiting..."
+ exit 1
+ fi
+ done
+ # End the if statement for DHCP vs. static
+ fi
+ # End of If Statement for DCHCP Reservation
+ fi
}
-setDHCPCD() {
- # Append these lines to dhcpcd.conf to enable a static IP
- echo "interface ${pivpnInterface}
- static ip_address=${IPv4addr}
- static routers=${IPv4gw}
- static domain_name_servers=${IPv4dns}" | $SUDO tee -a ${dhcpcdFile} >/dev/null
+setDHCPCD(){
+ # Append these lines to dhcpcd.conf to enable a static IP
+ echo "interface ${IPv4dev}
+ static ip_address=${IPv4addr}
+ static routers=${IPv4gw}
+ static domain_name_servers=${IPv4dns}" | $SUDO tee -a ${dhcpcdFile} >/dev/null
}
-setStaticIPv4() {
- # Tries to set the IPv4 address
- if [[ -f /etc/dhcpcd.conf ]]; then
- if grep -q "${IPv4addr}" ${dhcpcdFile}; then
- echo "::: Static IP already configured."
- else
- setDHCPCD
- $SUDO ip addr replace dev "${pivpnInterface}" "${IPv4addr}"
- echo ":::"
- echo "::: Setting IP to ${IPv4addr}. You may need to restart after the install is complete."
- echo ":::"
- fi
- else
- echo "::: Critical: Unable to locate configuration file to set static IPv4 address!"
- exit 1
- fi
+setStaticIPv4(){
+ # Tries to set the IPv4 address
+ if [[ -f /etc/dhcpcd.conf ]]; then
+ if grep -q "${IPv4addr}" ${dhcpcdFile}; then
+ echo "::: Static IP already configured."
+ else
+ setDHCPCD
+ $SUDO ip addr replace dev "${IPv4dev}" "${IPv4addr}"
+ echo ":::"
+ echo "::: Setting IP to ${IPv4addr}. You may need to restart after the install is complete."
+ echo ":::"
+ fi
+ else
+ echo "::: Critical: Unable to locate configuration file to set static IPv4 address!"
+ exit 1
+ fi
}
-function valid_ip()
-{
- local ip=$1
- local stat=1
+chooseUser(){
+ if [ "${runUnattended}" = 'true' ]; then
+ if [ -z "$install_user" ]; then
+ if [ "$(awk -F':' 'BEGIN {count=0} $3>=1000 && $3<=60000 { count++ } END{ print count }' /etc/passwd)" -eq 1 ]; then
+ install_user="$(awk -F':' '$3>=1000 && $3<=60000 {print $1}' /etc/passwd)"
+ echo "::: No user specified, but only ${install_user} is available, using it"
+ else
+ echo "::: No user specified"
+ exit 1
+ fi
+ else
+ if awk -F':' '$3>=1000 && $3<=60000 {print $1}' /etc/passwd | grep -qw "${install_user}"; then
+ echo "::: ${install_user} will hold your ovpn configurations."
+ else
+ echo "::: User ${install_user} does not exist, creating..."
+ $SUDO useradd -m -s /bin/bash "${install_user}"
+ echo "::: User created without a password, please do sudo passwd $install_user to create one"
+ fi
+ fi
+ install_home=$(grep -m1 "^${install_user}:" /etc/passwd | cut -d: -f6)
+ install_home=${install_home%/}
+ echo "install_user=${install_user}" >> /tmp/setupVars.conf
+ echo "install_home=${install_home}" >> /tmp/setupVars.conf
+ return
+ fi
- if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
- OIFS=$IFS
- IFS='.'
- ip=($ip)
- IFS=$OIFS
- [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
- && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
- stat=$?
- fi
- return $stat
+ # Explain the local user
+ whiptail --msgbox --backtitle "Parsing User List" --title "Local Users" "Choose a local user that will hold your ovpn configurations." ${r} ${c}
+ # First, let's check if there is a user available.
+ numUsers=$(awk -F':' 'BEGIN {count=0} $3>=1000 && $3<=60000 { count++ } END{ print count }' /etc/passwd)
+ if [ "$numUsers" -eq 0 ]
+ then
+ # We don't have a user, let's ask to add one.
+ if userToAdd=$(whiptail --title "Choose A User" --inputbox "No non-root user account was found. Please type a new username." ${r} ${c} 3>&1 1>&2 2>&3)
+ then
+ # See https://askubuntu.com/a/667842/459815
+ PASSWORD=$(whiptail --title "password dialog" --passwordbox "Please enter the new user password" ${r} ${c} 3>&1 1>&2 2>&3)
+ CRYPT=$(perl -e 'printf("%s\n", crypt($ARGV[0], "password"))' "${PASSWORD}")
+ if $SUDO useradd -m -p "${CRYPT}" -s /bin/bash "${userToAdd}" ; then
+ echo "Succeeded"
+ ((numUsers+=1))
+ else
+ exit 1
+ fi
+ else
+ exit 1
+ fi
+ fi
+ availableUsers=$(awk -F':' '$3>=1000 && $3<=60000 {print $1}' /etc/passwd)
+ local userArray=()
+ local firstloop=1
+
+ while read -r line
+ do
+ mode="OFF"
+ if [[ $firstloop -eq 1 ]]; then
+ firstloop=0
+ mode="ON"
+ fi
+ userArray+=("${line}" "" "${mode}")
+ done <<< "${availableUsers}"
+ chooseUserCmd=(whiptail --title "Choose A User" --separate-output --radiolist
+ "Choose (press space to select):" "${r}" "${c}" "${numUsers}")
+ if chooseUserOptions=$("${chooseUserCmd[@]}" "${userArray[@]}" 2>&1 >/dev/tty) ; then
+ for desiredUser in ${chooseUserOptions}; do
+ install_user=${desiredUser}
+ echo "::: Using User: $install_user"
+ install_home=$(grep -m1 "^${install_user}:" /etc/passwd | cut -d: -f6)
+ install_home=${install_home%/} # remove possible trailing slash
+ echo "install_user=${install_user}" >> /tmp/setupVars.conf
+ echo "install_home=${install_home}" >> /tmp/setupVars.conf
+ done
+ else
+ echo "::: Cancel selected, exiting...."
+ exit 1
+ fi
+}
+
+isRepo(){
+ # If the directory does not have a .git folder it is not a repo
+ echo -n "::: Checking $1 is a repo..."
+ cd "${1}" &> /dev/null || return 1
+ $SUDO git status &> /dev/null && echo " OK!"; return 0 || echo " not found!"; return 1
+}
+
+updateRepo(){
+ if [ "${UpdateCmd}" = "Repair" ]; then
+ echo "::: Repairing an existing installation, not downloading/updating local repos"
+ else
+ # Pull the latest commits
+ echo -n "::: Updating repo in $1..."
+ ### FIXME: Never call rm -rf with a plain variable. Never again as SU!
+ #$SUDO rm -rf "${1}"
+ if test -n "$1"; then
+ $SUDO rm -rf "$(dirname "$1")/.pivpn"
+ fi
+ # Go back to /etc otherwise git will complain when the current working
+ # directory has just been deleted (/etc/.pivpn).
+ cd /etc && \
+ $SUDO git clone -q --depth 1 --no-single-branch "${2}" "${1}" > /dev/null & spinner $!
+ cd "${1}" || exit 1
+ if [ -z "${TESTING+x}" ]; then
+ :
+ else
+ ${SUDOE} git checkout test
+ fi
+ echo " done!"
+ fi
+}
+
+makeRepo(){
+ # Remove the non-repos interface and clone the interface
+ echo -n "::: Cloning $2 into $1..."
+ ### FIXME: Never call rm -rf with a plain variable. Never again as SU!
+ #$SUDO rm -rf "${1}"
+ if test -n "$1"; then
+ $SUDO rm -rf "$(dirname "$1")/.pivpn"
+ fi
+ # Go back to /etc otherwhise git will complain when the current working
+ # directory has just been deleted (/etc/.pivpn).
+ cd /etc && \
+ $SUDO git clone -q --depth 1 --no-single-branch "${2}" "${1}" > /dev/null & spinner $!
+ cd "${1}" || exit 1
+ if [ -z "${TESTING+x}" ]; then
+ :
+ else
+ ${SUDOE} git checkout test
+ fi
+ echo " done!"
+}
+
+getGitFiles(){
+ # Setup git repos for base files
+ echo ":::"
+ echo "::: Checking for existing base files..."
+ if isRepo "${1}"; then
+ updateRepo "${1}" "${2}"
+ else
+ makeRepo "${1}" "${2}"
+ fi
+}
+
+cloneOrUpdateRepos(){
+ # Get Git files
+ getGitFiles ${pivpnFilesDir} ${pivpnGitUrl} || \
+ { echo "!!! Unable to clone ${pivpnGitUrl} into ${pivpnFilesDir}, unable to continue."; \
+ exit 1; \
+}
+}
+
+installPiVPN(){
+ $SUDO mkdir -p /etc/pivpn/
+ askWhichVPN
+
+ if [ "$VPN" = "openvpn" ]; then
+ installOpenVPN
+ askCustomProto
+ askCustomPort
+ askClientDNS
+ askCustomDomain
+ askPublicIPOrDNS
+ askEncryption
+ confOpenVPN
+ confOVPN
+ confNetwork
+ confLogging
+ elif [ "$VPN" = "wireguard" ]; then
+ installWireGuard
+ askCustomPort
+ askClientDNS
+ askPublicIPOrDNS
+ confWireGuard
+ confNetwork
+ fi
+}
+
+askWhichVPN(){
+ if [ "${runUnattended}" = 'true' ]; then
+ if [ -z "$VPN" ]; then
+ echo ":: No VPN protocol specified, using WireGuard"
+ VPN="wireguard"
+ else
+ VPN="${VPN,,}"
+ if [ "$VPN" = "wireguard" ]; then
+ echo "::: WireGuard will be installed"
+ elif [ "$VPN" = "openvpn" ]; then
+ echo "::: OpenVPN will be installed"
+ else
+ echo ":: $VPN is not a supported VPN protocol, please specify 'wireguard' or 'openvpn'"
+ exit 1
+ fi
+ fi
+ else
+ chooseVPNCmd=(whiptail --backtitle "Setup PiVPN" --title "Installation mode" --separate-output --radiolist "WireGuard is a new kind of VPN that provides near-istantaneous connection speed, high performance, modern cryptography.\\n\\nIt's the recommended choice expecially if you use mobile devices where WireGuard is easier on battery than OpenVPN.\\n\\nOpenVPN is still available if you need the traditional, flexible, trusted VPN protocol. Or if you need features like TCP and custom search domain.\\n\\nChoose a VPN (press space to select):" "${r}" "${c}" 2)
+ VPNChooseOptions=(WireGuard "" on
+ OpenVPN "" off)
+
+ if VPN=$("${chooseVPNCmd[@]}" "${VPNChooseOptions[@]}" 2>&1 >/dev/tty) ; then
+ echo "::: Using VPN: $VPN"
+ VPN="${VPN,,}"
+ else
+ echo "::: Cancel selected, exiting...."
+ exit 1
+ fi
+ fi
+
+ if [ "$VPN" = "wireguard" ]; then
+ pivpnPROTO="udp"
+ pivpnDEV="wg0"
+ pivpnNET="10.6.0.0"
+ elif [ "$VPN" = "openvpn" ]; then
+ pivpnDEV="tun0"
+ pivpnNET="10.8.0.0"
+ fi
+ vpnGw="${pivpnNET/.0.0/.0.1}"
+
+ echo "VPN=${VPN}" >> /tmp/setupVars.conf
+}
+
+installOpenVPN(){
+ echo "::: Installing OpenVPN from Debian package... "
+ # grepcidr is used to redact IPs in the debug log whereas expect is used
+ # to feed easy-rsa with passwords
+ local PIVPN_DEPS
+ PIVPN_DEPS=(openvpn grepcidr expect)
+ installDependentPackages PIVPN_DEPS[@]
+}
+
+installWireGuard(){
+ if [ "$PLAT" = "Raspbian" ]; then
+
+ # If this Raspberry Pi uses armv7l we can use the package from the repo
+ # https://lists.zx2c4.com/pipermail/wireguard/2017-November/001885.html
+ # Otherwhise compile and build the kernel module via DKMS (so it will
+ # be recompiled on kernel upgrades)
+
+ if [ "$(uname -m)" = "armv7l" ]; then
+
+ echo "::: Installing WireGuard from Debian package... "
+ # dirmngr is used to download repository keys, whereas qrencode is used to generate qrcodes
+ # from config file, for use with mobile clients
+ PIVPN_DEPS=(dirmngr qrencode)
+ installDependentPackages PIVPN_DEPS[@]
+ # Do not upgrade packages from the unstable repository except for wireguard
+ echo "::: Adding Debian repository... "
+ echo "deb https://deb.debian.org/debian/ unstable main" | $SUDO tee /etc/apt/sources.list.d/pivpn-unstable.list > /dev/null
+ printf 'Package: *\nPin: release a=unstable\nPin-Priority: 1\n\nPackage: wireguard wireguard-dkms wireguard-tools\nPin: release a=unstable\nPin-Priority: 500\n' | $SUDO tee /etc/apt/preferences.d/pivpn-limit-unstable > /dev/null
+
+ $SUDO apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 04EE7237B7D453EC 648ACFD622F3D138
+ # shellcheck disable=SC2086
+ $SUDO ${UPDATE_PKG_CACHE} &> /dev/null
+ PIVPN_DEPS=(raspberrypi-kernel-headers wireguard wireguard-tools wireguard-dkms)
+ installDependentPackages PIVPN_DEPS[@]
+
+ elif [ "$(uname -m)" = "armv6l" ]; then
+
+ echo "::: Installing WireGuard from source... "
+ PIVPN_DEPS=(checkinstall dkms libmnl-dev libelf-dev raspberrypi-kernel-headers build-essential pkg-config qrencode jq)
+ installDependentPackages PIVPN_DEPS[@]
+
+ # Delete any leftover code
+ $SUDO rm -rf /usr/src/wireguard-*
+
+ WG_TOOLS_SNAPSHOT="$(curl -s https://build.wireguard.com/distros.json | jq -r '."upstream-tools"."version"')"
+ WG_TOOLS_SOURCE="https://git.zx2c4.com/wireguard-tools/snapshot/wireguard-tools-${WG_TOOLS_SNAPSHOT}.tar.xz"
+
+ echo "::: Downloading wireguard-tools source code... "
+ wget -qO- "${WG_TOOLS_SOURCE}" | $SUDO tar Jxf - --directory /usr/src
+ echo "done!"
+
+ ## || exits if cd fails.
+ cd /usr/src/wireguard-tools-"${WG_TOOLS_SNAPSHOT}/src" || exit 1
+
+ # We install the userspace tools manually since DKMS only compiles and
+ # installs the kernel module
+ echo "::: Compiling WireGuard tools... "
+ if $SUDO make; then
+ echo "done!"
+ else
+ echo "failed!"
+ exit 1
+ fi
+
+ # Use checkinstall to install userspace tools so if the user wants to uninstall
+ # PiVPN we can just do apt remove wireguard-tools, instead of manually removing
+ # files from the file system
+ echo "::: Installing WireGuard tools... "
+ if $SUDO checkinstall --pkgname wireguard-tools --pkgversion "${WG_TOOLS_SNAPSHOT}" -y; then
+ TO_INSTALL+=("wireguard-tools")
+ echo "done!"
+ else
+ echo "failed!"
+ exit 1
+ fi
+
+ echo "WG_TOOLS_SNAPSHOT=${WG_TOOLS_SNAPSHOT}" >> /tmp/setupVars.conf
+
+ WG_MODULE_SNAPSHOT="$(curl -s https://build.wireguard.com/distros.json | jq -r '."upstream-linuxcompat"."version"')"
+ WG_MODULE_SOURCE="https://git.zx2c4.com/wireguard-linux-compat/snapshot/wireguard-linux-compat-${WG_MODULE_SNAPSHOT}.tar.xz"
+
+ echo "::: Downloading wireguard-linux-compat source code... "
+ wget -qO- "${WG_MODULE_SOURCE}" | $SUDO tar Jxf - --directory /usr/src
+ echo "done!"
+
+ # Rename wireguard-linux-compat folder and move the source code to the parent folder
+ # such that dkms picks up the module when referencing wireguard/"${WG_MODULE_SNAPSHOT}"
+ cd /usr/src && \
+ $SUDO mv wireguard-linux-compat-"${WG_MODULE_SNAPSHOT}" wireguard-"${WG_MODULE_SNAPSHOT}"
+ cd wireguard-"${WG_MODULE_SNAPSHOT}" && \
+ $SUDO mv src/* . && \
+ $SUDO rmdir src
+
+ echo "::: Adding WireGuard modules via DKMS... "
+ if $SUDO dkms add wireguard/"${WG_MODULE_SNAPSHOT}"; then
+ echo "done!"
+ else
+ echo "failed!"
+ $SUDO dkms remove wireguard/"${WG_MODULE_SNAPSHOT}" --all
+ exit 1
+ fi
+
+ echo "::: Compiling WireGuard modules via DKMS... "
+ if $SUDO dkms build wireguard/"${WG_MODULE_SNAPSHOT}"; then
+ echo "done!"
+ else
+ echo "failed!"
+ $SUDO dkms remove wireguard/"${WG_MODULE_SNAPSHOT}" --all
+ exit 1
+ fi
+
+ echo "::: Installing WireGuard modules via DKMS... "
+ if $SUDO dkms install wireguard/"${WG_MODULE_SNAPSHOT}"; then
+ TO_INSTALL+=("wireguard-dkms")
+ echo "done!"
+ else
+ echo "failed!"
+ $SUDO dkms remove wireguard/"${WG_MODULE_SNAPSHOT}" --all
+ exit 1
+ fi
+
+ echo "WG_MODULE_SNAPSHOT=${WG_MODULE_SNAPSHOT}" >> /tmp/setupVars.conf
+
+ fi
+
+ elif [ "$PLAT" = "Debian" ]; then
+
+ echo "::: Installing WireGuard from Debian package... "
+ echo "::: Adding Debian repository... "
+ echo "deb https://deb.debian.org/debian/ unstable main" | $SUDO tee /etc/apt/sources.list.d/pivpn-unstable.list > /dev/null
+ printf 'Package: *\nPin: release a=unstable\nPin-Priority: 90\n' | $SUDO tee /etc/apt/preferences.d/pivpn-limit-unstable > /dev/null
+ # shellcheck disable=SC2086
+ $SUDO ${UPDATE_PKG_CACHE} &> /dev/null
+ PIVPN_DEPS=(linux-headers-amd64 qrencode wireguard wireguard-tools wireguard-dkms)
+ installDependentPackages PIVPN_DEPS[@]
+
+ elif [ "$PLAT" = "Ubuntu" ]; then
+
+ echo "::: Installing WireGuard from PPA... "
+ $SUDO add-apt-repository ppa:wireguard/wireguard -y
+ $SUDO ${UPDATE_PKG_CACHE}
+ PIVPN_DEPS=(qrencode wireguard wireguard-tools wireguard-dkms linux-headers-generic)
+ installDependentPackages PIVPN_DEPS[@]
+
+ fi
+}
+
+askCustomProto(){
+ if [ "${runUnattended}" = 'true' ]; then
+ if [ -z "$pivpnPROTO" ]; then
+ echo "::: No TCP/IP protocol specified, using the default protocol udp"
+ pivpnPROTO="udp"
+ else
+ pivpnPROTO="${pivpnPROTO,,}"
+ if [ "$pivpnPROTO" = "udp" ] || [ "$pivpnPROTO" = "tcp" ]; then
+ echo "::: Using the $pivpnPROTO protocol"
+ else
+ echo ":: $pivpnPROTO is not a supported TCP/IP protocol, please specify 'udp' or 'tcp'"
+ exit 1
+ fi
+ fi
+ echo "pivpnPROTO=${pivpnPROTO}" >> /tmp/setupVars.conf
+ return
+ fi
+
+ # Set the available protocols into an array so it can be used with a whiptail dialog
+ if pivpnPROTO=$(whiptail --title "Protocol" --radiolist \
+ "Choose a protocol (press space to select). Please only choose TCP if you know why you need TCP." ${r} ${c} 2 \
+ "UDP" "" ON \
+ "TCP" "" OFF 3>&1 1>&2 2>&3)
+ then
+ # Convert option into lowercase (UDP->udp)
+ pivpnPROTO="${pivpnPROTO,,}"
+ echo "::: Using protocol: $pivpnPROTO"
+ echo "pivpnPROTO=${pivpnPROTO}" >> /tmp/setupVars.conf
+ else
+ echo "::: Cancel selected, exiting...."
+ exit 1
+ fi
+}
+
+askCustomPort(){
+ if [ "${runUnattended}" = 'true' ]; then
+ if [ -z "$pivpnPORT" ]; then
+ if [ "$VPN" = "wireguard" ]; then
+ echo "::: No port specified, using the default port 51820"
+ pivpnPORT=51820
+ elif [ "$VPN" = "openvpn" ]; then
+ if [ "$pivpnPROTO" = "udp" ]; then
+ echo "::: No port specified, using the default port 1194"
+ pivpnPORT=1194
+ elif [ "$pivpnPROTO" = "tcp" ]; then
+ echo "::: No port specified, using the default port 443"
+ pivpnPORT=443
+ fi
+ fi
+ else
+ if [[ "$pivpnPORT" =~ ^[0-9]+$ ]] && [ "$pivpnPORT" -ge 1 ] && [ "$pivpnPORT" -le 65535 ]; then
+ echo "::: Using port $pivpnPORT"
+ else
+ echo "::: $pivpnPORT is not a valid port, use a port within the range [1,65535] (inclusive)"
+ exit 1
+ fi
+ fi
+ echo "pivpnPORT=${pivpnPORT}" >> /tmp/setupVars.conf
+ return
+ fi
+
+ until [[ $PORTNumCorrect = True ]]
+ do
+ portInvalid="Invalid"
+
+ if [ "$VPN" = "wireguard" ]; then
+ DEFAULT_PORT=51820
+ elif [ "$VPN" = "openvpn" ]; then
+ if [ "$pivpnPROTO" = "udp" ]; then
+ DEFAULT_PORT=1194
+ else
+ DEFAULT_PORT=443
+ fi
+ fi
+
+ if pivpnPORT=$(whiptail --title "Default $VPN Port" --inputbox "You can modify the default $VPN port. \\nEnter a new value or hit 'Enter' to retain the default" ${r} ${c} $DEFAULT_PORT 3>&1 1>&2 2>&3)
+ then
+ if [[ "$pivpnPORT" =~ ^[0-9]+$ ]] && [ "$pivpnPORT" -ge 1 ] && [ "$pivpnPORT" -le 65535 ]; then
+ :
+ else
+ pivpnPORT=$portInvalid
+ fi
+ else
+ echo "::: Cancel selected, exiting...."
+ exit 1
+ fi
+
+ if [[ $pivpnPORT == "$portInvalid" ]]; then
+ whiptail --msgbox --backtitle "Invalid Port" --title "Invalid Port" "You entered an invalid Port number.\\n Please enter a number from 1 - 65535.\\n If you are not sure, please just keep the default." ${r} ${c}
+ PORTNumCorrect=False
+ else
+ if (whiptail --backtitle "Specify Custom Port" --title "Confirm Custom Port Number" --yesno "Are these settings correct?\\n PORT: $pivpnPORT" ${r} ${c}) then
+ PORTNumCorrect=True
+ else
+ # If the settings are wrong, the loop continues
+ PORTNumCorrect=False
+ fi
+ fi
+ done
+ # write out the port
+ echo "pivpnPORT=${pivpnPORT}" >> /tmp/setupVars.conf
+}
+
+askClientDNS(){
+ if [ "${runUnattended}" = 'true' ]; then
+
+ if [ -z "$pivpnDNS1" ] && [ -n "$pivpnDNS2" ]; then
+ pivpnDNS1="$pivpnDNS2"
+ unset pivpnDNS2
+ elif [ -z "$pivpnDNS1" ] && [ -z "$pivpnDNS2" ]; then
+ pivpnDNS1="9.9.9.9"
+ pivpnDNS2="149.112.112.112"
+ echo "::: No DNS provider specified, using Quad9 DNS ($pivpnDNS1 $pivpnDNS2)"
+ fi
+
+ local INVALID_DNS_SETTINGS=0
+
+ if ! validIP "$pivpnDNS1"; then
+ INVALID_DNS_SETTINGS=1
+ echo "::: Invalid DNS $pivpnDNS1"
+ fi
+
+ if [ -n "$pivpnDNS2" ] && ! validIP "$pivpnDNS2"; then
+ INVALID_DNS_SETTINGS=1
+ echo "::: Invalid DNS $pivpnDNS2"
+ fi
+
+ if [ "$INVALID_DNS_SETTINGS" -eq 0 ]; then
+ echo "::: Using DNS $pivpnDNS1 $pivpnDNS2"
+ else
+ exit 1
+ fi
+
+ echo "pivpnDNS1=${pivpnDNS1}" >> /tmp/setupVars.conf
+ echo "pivpnDNS2=${pivpnDNS2}" >> /tmp/setupVars.conf
+ return
+ fi
+
+ # Detect and offer to use Pi-hole
+ if command -v pihole &>/dev/null; then
+ if (whiptail --backtitle "Setup PiVPN" --title "Pi-hole" --yesno "We have detected a Pi-hole installation, do you want to use it as the DNS server for the VPN, so you get ad blocking on the go?" ${r} ${c}); then
+ pivpnDNS1="$vpnGw"
+ echo "interface=$pivpnDEV" | $SUDO tee /etc/dnsmasq.d/02-pivpn.conf > /dev/null
+ $SUDO pihole restartdns
+ echo "pivpnDNS1=${pivpnDNS1}" >> /tmp/setupVars.conf
+ echo "pivpnDNS2=${pivpnDNS2}" >> /tmp/setupVars.conf
+ return
+ fi
+ fi
+
+ DNSChoseCmd=(whiptail --separate-output --radiolist "Select the DNS Provider
+ for your VPN Clients (press space to select). To use your own, select
+ Custom.\\n\\nIn case you have a local resolver running, i.e. unbound, select
+ \"PiVPN-is-local-DNS\" and make sure your resolver is listening on
+ \"$vpnGw\", allowing requests from \"${pivpnNET}/${subnetClass}\"." "${r}" "${c}" 6)
+ DNSChooseOptions=(Quad9 "" on
+ OpenDNS "" off
+ Level3 "" off
+ DNS.WATCH "" off
+ Norton "" off
+ FamilyShield "" off
+ CloudFlare "" off
+ Google "" off
+ PiVPN-is-local-DNS "" off
+ Custom "" off)
+
+ if DNSchoices=$("${DNSChoseCmd[@]}" "${DNSChooseOptions[@]}" 2>&1 >/dev/tty)
+ then
+
+ if [[ ${DNSchoices} != "Custom" ]]; then
+
+ echo "::: Using ${DNSchoices} servers."
+ declare -A DNS_MAP=(["Quad9"]="9.9.9.9 149.112.112.112"
+ ["OpenDNS"]="208.67.222.222 208.67.220.220"
+ ["Level3"]="209.244.0.3 209.244.0.4"
+ ["DNS.WATCH"]="84.200.69.80 84.200.70.40"
+ ["Norton"]="199.85.126.10 199.85.127.10"
+ ["FamilyShield"]="208.67.222.123 208.67.220.123"
+ ["CloudFlare"]="1.1.1.1 1.0.0.1"
+ ["Google"]="8.8.8.8 8.8.4.4"
+ ["PiVPN-is-local-DNS"]="$vpnGw")
+
+ pivpnDNS1=$(awk '{print $1}' <<< "${DNS_MAP["${DNSchoices}"]}")
+ pivpnDNS2=$(awk '{print $2}' <<< "${DNS_MAP["${DNSchoices}"]}")
+
+ else
+
+ until [[ $DNSSettingsCorrect = True ]]; do
+ strInvalid="Invalid"
+
+ if pivpnDNS=$(whiptail --backtitle "Specify Upstream DNS Provider(s)" --inputbox "Enter your desired upstream DNS provider(s), separated by a comma.\\n\\nFor example '1.1.1.1, 9.9.9.9'" ${r} ${c} "" 3>&1 1>&2 2>&3)
+ then
+ pivpnDNS1=$(echo "$pivpnDNS" | sed 's/[, \t]\+/,/g' | awk -F, '{print$1}')
+ pivpnDNS2=$(echo "$pivpnDNS" | sed 's/[, \t]\+/,/g' | awk -F, '{print$2}')
+ if ! validIP "$pivpnDNS1" || [ ! "$pivpnDNS1" ]; then
+ pivpnDNS1=$strInvalid
+ fi
+ if ! validIP "$pivpnDNS2" && [ "$pivpnDNS2" ]; then
+ pivpnDNS2=$strInvalid
+ fi
+ else
+ echo "::: Cancel selected, exiting...."
+ exit 1
+ fi
+
+ if [[ $pivpnDNS1 == "$strInvalid" ]] || [[ $pivpnDNS2 == "$strInvalid" ]]; then
+ whiptail --msgbox --backtitle "Invalid IP" --title "Invalid IP" "One or both entered IP addresses were invalid. Please try again.\\n\\n DNS Server 1: $pivpnDNS1\\n DNS Server 2: $pivpnDNS2" ${r} ${c}
+ if [[ $pivpnDNS1 == "$strInvalid" ]]; then
+ pivpnDNS1=""
+ fi
+ if [[ $pivpnDNS2 == "$strInvalid" ]]; then
+ pivpnDNS2=""
+ fi
+ DNSSettingsCorrect=False
+ else
+ if (whiptail --backtitle "Specify Upstream DNS Provider(s)" --title "Upstream DNS Provider(s)" --yesno "Are these settings correct?\\n DNS Server 1: $pivpnDNS1\\n DNS Server 2: $pivpnDNS2" ${r} ${c}) then
+ DNSSettingsCorrect=True
+ else
+ # If the settings are wrong, the loop continues
+ DNSSettingsCorrect=False
+ fi
+ fi
+ done
+ fi
+
+ else
+ echo "::: Cancel selected. Exiting..."
+ exit 1
+ fi
+
+ echo "pivpnDNS1=${pivpnDNS1}" >> /tmp/setupVars.conf
+ echo "pivpnDNS2=${pivpnDNS2}" >> /tmp/setupVars.conf
}
#Call this function to use a regex to check user input for a valid custom domain
-function valid_domain()
-{
+validDomain(){
local domain=$1
local stat=1
if [[ $domain =~ ^(([a-zA-Z0-9]{1,63}|([a-zA-Z0-9]{1,60}[-a-zA-Z0-9()]{0,2}[a-zA-Z0-9]{1,60}))\.){1,6}([a-zA-Z]{2,})$ ]]; then
- stat=$?
+ stat=$?
fi
return $stat
}
-installScripts() {
- # Install the scripts from /etc/.pivpn to their various locations
- $SUDO echo ":::"
- $SUDO echo -n "::: Installing scripts to /opt/pivpn..."
- if [ ! -d /opt/pivpn ]; then
- $SUDO mkdir -p /opt/pivpn
- $SUDO chown "$INSTALL_USER":root /opt/pivpn
- $SUDO chmod 0755 /opt/pivpn
- fi
- $SUDO cp /etc/.pivpn/scripts/*.sh /opt/pivpn/
- $SUDO chmod 0755 /opt/pivpn/*.sh
- $SUDO cp /etc/.pivpn/pivpn /usr/local/bin/pivpn
- $SUDO chmod 0755 /usr/local/bin/pivpn
- $SUDO cp /etc/.pivpn/scripts/bash-completion /etc/bash_completion.d/pivpn
- . /etc/bash_completion.d/pivpn
- # Copy interface setting for debug
- $SUDO cp /tmp/pivpnINT /etc/pivpn/pivpnINTERFACE
-
- $SUDO echo " done."
-}
-
-package_check_install() {
- dpkg-query -W -f='${Status}' "${1}" 2>/dev/null | grep -c "ok installed" || ${PKG_INSTALL} "${1}"
-}
-
-addSoftwareRepo() {
- # Add the official OpenVPN repo for distros that don't have the latest version in their default repos
- case ${PLAT} in
- Ubuntu|Debian|Devuan)
- case ${OSCN} in
- trusty|xenial|wheezy|jessie)
- wget -qO- https://swupdate.openvpn.net/repos/repo-public.gpg | $SUDO apt-key add -
- echo "deb http://build.openvpn.net/debian/openvpn/stable $OSCN main" | $SUDO tee /etc/apt/sources.list.d/swupdate.openvpn.net.list > /dev/null
- echo -n "::: Adding OpenVPN repo for $PLAT $OSCN ..."
- $SUDO $PKG_MANAGER -qq update & spinner $!
- echo " done!"
- ;;
- esac
- ;;
- esac
-}
-
-update_package_cache() {
- #Running apt-get update/upgrade with minimal output can cause some issues with
- #requiring user input
-
- #Check to see if apt-get update has already been run today
- #it needs to have been run at least once on new installs!
- timestamp=$(stat -c %Y ${PKG_CACHE})
- timestampAsDate=$(date -d @"${timestamp}" "+%b %e")
- today=$(date "+%b %e")
-
-
- if [ ! "${today}" == "${timestampAsDate}" ]; then
- #update package lists
- echo ":::"
- echo -ne "::: ${PKG_MANAGER} update has not been run today. Running now...\n"
- $SUDO ${UPDATE_PKG_CACHE} &> /dev/null
- echo " done!"
- fi
-}
-
-notify_package_updates_available() {
- # Let user know if they have outdated packages on their system and
- # advise them to run a package update at soonest possible.
- echo ":::"
- echo -n "::: Checking ${PKG_MANAGER} for upgraded packages...."
- updatesToInstall=$(eval "${PKG_COUNT}")
- echo " done!"
- echo ":::"
- if [[ ${updatesToInstall} -eq "0" ]]; then
- echo "::: Your system is up to date! Continuing with PiVPN installation..."
- else
- echo "::: There are ${updatesToInstall} updates available for your system!"
- echo "::: We recommend you update your OS after installing PiVPN! "
- echo ":::"
- fi
-}
-
-install_dependent_packages() {
- # Install packages passed in via argument array
- # No spinner - conflicts with set -e
- declare -a argArray1=("${!1}")
-
- if [[ ${OSCN} == "buster" ]]; then
- $SUDO update-alternatives --set iptables /usr/sbin/iptables-legacy
- $SUDO update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
- fi
- echo iptables-persistent iptables-persistent/autosave_v4 boolean true | $SUDO debconf-set-selections
- echo iptables-persistent iptables-persistent/autosave_v6 boolean false | $SUDO debconf-set-selections
-
- # Add support for https repositories if there are any that use it otherwise the installation will silently fail
- if grep -q https /etc/apt/sources.list; then
- PIVPN_DEPS+=("apt-transport-https")
- fi
-
- if command -v debconf-apt-progress &> /dev/null; then
- $SUDO debconf-apt-progress -- ${PKG_INSTALL} "${argArray1[@]}"
- else
- for i in "${argArray1[@]}"; do
- echo -n "::: Checking for $i..."
- $SUDO package_check_install "${i}" &> /dev/null
- echo " installed!"
- done
- fi
-}
-
-unattendedUpgrades() {
- whiptail --msgbox --backtitle "Security Updates" --title "Unattended Upgrades" "Since this server will have at least one port open to the internet, it is recommended you enable unattended-upgrades.\nThis feature will check daily for security package updates only and apply them when necessary.\nIt will NOT automatically reboot the server so to fully apply some updates you should periodically reboot." ${r} ${c}
-
- if (whiptail --backtitle "Security Updates" --title "Unattended Upgrades" --yesno "Do you want to enable unattended upgrades of security patches to this server?" ${r} ${c}) then
- UNATTUPG="unattended-upgrades"
- else
- UNATTUPG=""
- fi
-}
-
-stopServices() {
- # Stop openvpn
- $SUDO echo ":::"
- $SUDO echo -n "::: Stopping OpenVPN service..."
- case ${PLAT} in
- Ubuntu|Debian|*vuan)
- $SUDO service openvpn stop || true
- ;;
- *)
- $SUDO systemctl stop openvpn.service || true
- ;;
- esac
- $SUDO echo " done."
-}
-
-getGitFiles() {
- # Setup git repos for base files
- echo ":::"
- echo "::: Checking for existing base files..."
- if is_repo "${1}"; then
- update_repo "${1}" "${2}"
- else
- make_repo "${1}" "${2}"
- fi
-}
-
-is_repo() {
- # If the directory does not have a .git folder it is not a repo
- echo -n "::: Checking $1 is a repo..."
- cd "${1}" &> /dev/null || return 1
- $SUDO git status &> /dev/null && echo " OK!"; return 0 || echo " not found!"; return 1
-}
-
-make_repo() {
- # Remove the non-repos interface and clone the interface
- echo -n "::: Cloning $2 into $1..."
- $SUDO rm -rf "${1}"
- $SUDO git clone -q --depth 1 --no-single-branch "${2}" "${1}" > /dev/null & spinner $!
- if [ -z "${TESTING+x}" ]; then
- :
- else
- $SUDO git -C "${1}" checkout test
- fi
- echo " done!"
-}
-
-update_repo() {
- if [[ "${reconfigure}" == true ]]; then
- echo "::: --reconfigure passed to install script. Not downloading/updating local repos"
- else
- # Pull the latest commits
- echo -n "::: Updating repo in $1..."
- $SUDO rm -rf "${1}"
- # Go back to /etc otherwhise git will complain when the current working directory has
- # just been deleted (/etc/.pivpn).
- cd /etc
- $SUDO git clone -q --depth 1 --no-single-branch "${2}" "${1}" > /dev/null & spinner $!
- cd "${1}" || exit 1
- if [ -z "${TESTING+x}" ]; then
- :
- else
- ${SUDOE} git checkout test
- fi
- echo " done!"
- fi
-}
-
-setCustomProto() {
- # Set the available protocols into an array so it can be used with a whiptail dialog
- if protocol=$(whiptail --title "Protocol" --radiolist \
- "Choose a protocol (press space to select). Please only choose TCP if you know why you need TCP." ${r} ${c} 2 \
- "UDP" "" ON \
- "TCP" "" OFF 3>&1 1>&2 2>&3)
- then
- # Convert option into lowercase (UDP->udp)
- pivpnProto="${protocol,,}"
- echo "::: Using protocol: $pivpnProto"
- echo "${pivpnProto}" > /tmp/pivpnPROTO
- else
- echo "::: Cancel selected, exiting...."
- exit 1
- fi
- # write out the PROTO
- PROTO=$pivpnProto
- $SUDO cp /tmp/pivpnPROTO /etc/pivpn/INSTALL_PROTO
-}
-
-
-setCustomPort() {
- until [[ $PORTNumCorrect = True ]]
- do
- portInvalid="Invalid"
-
- PROTO=$(cat /etc/pivpn/INSTALL_PROTO)
- if [ "$PROTO" = "udp" ]; then
- DEFAULT_PORT=1194
- else
- DEFAULT_PORT=443
- fi
- if PORT=$(whiptail --title "Default OpenVPN Port" --inputbox "You can modify the default OpenVPN port. \nEnter a new value or hit 'Enter' to retain the default" ${r} ${c} $DEFAULT_PORT 3>&1 1>&2 2>&3)
- then
- if [[ "$PORT" =~ ^[0-9]+$ ]] && [ "$PORT" -ge 1 ] && [ "$PORT" -le 65535 ]; then
- :
- else
- PORT=$portInvalid
- fi
- else
- echo "::: Cancel selected, exiting...."
- exit 1
- fi
-
- if [[ $PORT == "$portInvalid" ]]; then
- whiptail --msgbox --backtitle "Invalid Port" --title "Invalid Port" "You entered an invalid Port number.\n Please enter a number from 1 - 65535.\n If you are not sure, please just keep the default." ${r} ${c}
- PORTNumCorrect=False
- else
- if (whiptail --backtitle "Specify Custom Port" --title "Confirm Custom Port Number" --yesno "Are these settings correct?\n PORT: $PORT" ${r} ${c}) then
- PORTNumCorrect=True
- else
- # If the settings are wrong, the loop continues
- PORTNumCorrect=False
- fi
- fi
- done
- # write out the port
- echo ${PORT} > /tmp/INSTALL_PORT
- $SUDO cp /tmp/INSTALL_PORT /etc/pivpn/INSTALL_PORT
-}
-
-setClientDNS() {
- DNSChoseCmd=(whiptail --separate-output --radiolist "Select the DNS Provider for your VPN Clients (press space to select). To use your own, select Custom." ${r} ${c} 6)
- DNSChooseOptions=(Google "" on
- OpenDNS "" off
- Level3 "" off
- DNS.WATCH "" off
- Norton "" off
- FamilyShield "" off
- CloudFlare "" off
- Custom "" off)
-
- if DNSchoices=$("${DNSChoseCmd[@]}" "${DNSChooseOptions[@]}" 2>&1 >/dev/tty)
- then
-
- if [[ ${DNSchoices} != "Custom" ]]; then
-
- echo "::: Using ${DNSchoices} servers."
- declare -A DNS_MAP=(["Google"]="8.8.8.8 8.8.4.4" ["OpenDNS"]="208.67.222.222 208.67.220.220" ["Level3"]="209.244.0.3 209.244.0.4" ["DNS.WATCH"]="84.200.69.80 84.200.70.40" ["Norton"]="199.85.126.10 199.85.127.10" ["FamilyShield"]="208.67.222.123 208.67.220.123" ["CloudFlare"]="1.1.1.1 1.0.0.1")
-
- OVPNDNS1=$(awk '{print $1}' <<< "${DNS_MAP["${DNSchoices}"]}")
- OVPNDNS2=$(awk '{print $2}' <<< "${DNS_MAP["${DNSchoices}"]}")
-
- $SUDO sed -i '0,/\(dhcp-option DNS \)/ s/\(dhcp-option DNS \).*/\1'${OVPNDNS1}'\"/' /etc/openvpn/server.conf
- $SUDO sed -i '0,/\(dhcp-option DNS \)/! s/\(dhcp-option DNS \).*/\1'${OVPNDNS2}'\"/' /etc/openvpn/server.conf
-
- else
-
- until [[ $DNSSettingsCorrect = True ]]
- do
- strInvalid="Invalid"
-
- if OVPNDNS=$(whiptail --backtitle "Specify Upstream DNS Provider(s)" --inputbox "Enter your desired upstream DNS provider(s), separated by a comma.\n\nFor example '8.8.8.8, 8.8.4.4'" ${r} ${c} "" 3>&1 1>&2 2>&3)
- then
- OVPNDNS1=$(echo "$OVPNDNS" | sed 's/[, \t]\+/,/g' | awk -F, '{print$1}')
- OVPNDNS2=$(echo "$OVPNDNS" | sed 's/[, \t]\+/,/g' | awk -F, '{print$2}')
- if ! valid_ip "$OVPNDNS1" || [ ! "$OVPNDNS1" ]; then
- OVPNDNS1=$strInvalid
- fi
- if ! valid_ip "$OVPNDNS2" && [ "$OVPNDNS2" ]; then
- OVPNDNS2=$strInvalid
- fi
- else
- echo "::: Cancel selected, exiting...."
- exit 1
- fi
- if [[ $OVPNDNS1 == "$strInvalid" ]] || [[ $OVPNDNS2 == "$strInvalid" ]]; then
- whiptail --msgbox --backtitle "Invalid IP" --title "Invalid IP" "One or both entered IP addresses were invalid. Please try again.\n\n DNS Server 1: $OVPNDNS1\n DNS Server 2: $OVPNDNS2" ${r} ${c}
- if [[ $OVPNDNS1 == "$strInvalid" ]]; then
- OVPNDNS1=""
- fi
- if [[ $OVPNDNS2 == "$strInvalid" ]]; then
- OVPNDNS2=""
- fi
- DNSSettingsCorrect=False
- else
- if (whiptail --backtitle "Specify Upstream DNS Provider(s)" --title "Upstream DNS Provider(s)" --yesno "Are these settings correct?\n DNS Server 1: $OVPNDNS1\n DNS Server 2: $OVPNDNS2" ${r} ${c}) then
- DNSSettingsCorrect=True
- $SUDO sed -i '0,/\(dhcp-option DNS \)/ s/\(dhcp-option DNS \).*/\1'${OVPNDNS1}'\"/' /etc/openvpn/server.conf
- if [ -z ${OVPNDNS2} ]; then
- $SUDO sed -i '/\(dhcp-option DNS \)/{n;N;d}' /etc/openvpn/server.conf
- else
- $SUDO sed -i '0,/\(dhcp-option DNS \)/! s/\(dhcp-option DNS \).*/\1'${OVPNDNS2}'\"/' /etc/openvpn/server.conf
- fi
- else
- # If the settings are wrong, the loop continues
- DNSSettingsCorrect=False
- fi
- fi
- done
- fi
-
- else
- echo "::: Cancel selected. Exiting..."
- exit 1
- fi
-}
-
#This procedure allows a user to specify a custom search domain if they have one.
-setCustomDomain() {
- DomainSettingsCorrect=False
+askCustomDomain(){
+ if [ "${runUnattended}" = 'true' ]; then
+ if [ -n "$pivpnSEARCHDOMAIN" ]; then
+ if validDomain "$pivpnSEARCHDOMAIN"; then
+ echo "::: Using custom domain $pivpnSEARCHDOMAIN"
+ else
+ echo "::: Custom domain $pivpnSEARCHDOMAIN is not valid"
+ exit 1
+ fi
+ else
+ echo "::: Skipping custom domain"
+ fi
+ echo "pivpnSEARCHDOMAIN=${pivpnSEARCHDOMAIN}" >> /tmp/setupVars.conf
+ return
+ fi
- if (whiptail --backtitle "Custom Search Domain" --title "Custom Search Domain" --yesno "Would you like to add a custom search domain? \n (This is only for advanced users who have their own domain)\n" ${r} ${c}); then
+ DomainSettingsCorrect=False
- until [[ $DomainSettingsCorrect = True ]]
- do
- if CUSTOMDomain=$(whiptail --inputbox "Enter Custom Domain\nFormat: mydomain.com" ${r} ${c} --title "Custom Domain" 3>&1 1>&2 2>&3); then
- if valid_domain "$CUSTOMDomain"; then
- if (whiptail --backtitle "Custom Search Domain" --title "Custom Search Domain" --yesno "Are these settings correct?\n Custom Search Domain: $CUSTOMDomain" ${r} ${c}); then
- DomainSettingsCorrect=True
+ if (whiptail --backtitle "Custom Search Domain" --title "Custom Search Domain" --yesno --defaultno "Would you like to add a custom search domain? \\n (This is only for advanced users who have their own domain)\\n" ${r} ${c}); then
- $SUDO sed -i '0,/\(.*dhcp-option.*\)/s//\push "dhcp-option DOMAIN '${CUSTOMDomain}'" \n&/' /etc/openvpn/server.conf
+ until [[ $DomainSettingsCorrect = True ]]
+ do
+ if pivpnSEARCHDOMAIN=$(whiptail --inputbox "Enter Custom Domain\\nFormat: mydomain.com" ${r} ${c} --title "Custom Domain" 3>&1 1>&2 2>&3); then
+ if validDomain "$pivpnSEARCHDOMAIN"; then
+ if (whiptail --backtitle "Custom Search Domain" --title "Custom Search Domain" --yesno "Are these settings correct?\\n Custom Search Domain: $pivpnSEARCHDOMAIN" ${r} ${c}); then
+ DomainSettingsCorrect=True
+ else
+ # If the settings are wrong, the loop continues
+ DomainSettingsCorrect=False
+ fi
+ else
+ whiptail --msgbox --backtitle "Invalid Domain" --title "Invalid Domain" "Domain is invalid. Please try again.\\n\\n DOMAIN: $pivpnSEARCHDOMAIN\\n" ${r} ${c}
+ DomainSettingsCorrect=False
+ fi
+ else
+ echo "::: Cancel selected. Exiting..."
+ exit 1
+ fi
+ done
+ fi
- else
- # If the settings are wrong, the loop continues
- DomainSettingsCorrect=False
- fi
- else
- whiptail --msgbox --backtitle "Invalid Domain" --title "Invalid Domain" "Domain is invalid. Please try again.\n\n DOMAIN: $CUSTOMDomain\n" ${r} ${c}
- DomainSettingsCorrect=False
- fi
- else
- echo "::: Cancel selected. Exiting..."
- exit 1
- fi
- done
-
- else
- echo sleep 0.1
- fi
+ echo "pivpnSEARCHDOMAIN=${pivpnSEARCHDOMAIN}" >> /tmp/setupVars.conf
}
-confOpenVPN() {
- # Grab the existing Hostname
- host_name=$(hostname -s)
- # Generate a random UUID for this server so that we can use verify-x509-name later that is unique for this server installation.
- NEW_UUID=$(> /tmp/setupVars.conf
+ return
+ fi
- if [[ ${useUpdateVars} == false ]]; then
- # Ask user for desired level of encryption
+ METH=$(whiptail --title "Public IP or DNS" --radiolist "Will clients use a Public IP or DNS Name to connect to your server (press space to select)?" ${r} ${c} 2 \
+ "$IPv4pub" "Use this public IP" "ON" \
+ "DNS Entry" "Use a public DNS" "OFF" 3>&1 1>&2 2>&3)
- if [[ ${useUpdateVars} == false ]]; then
+ exitstatus=$?
+ if [ $exitstatus != 0 ]; then
+ echo "::: Cancel selected. Exiting..."
+ exit 1
+ fi
- if [[ ${PLAT} == "Raspbian" ]] && [[ ${OSCN} != "stretch" ]] && [[ ${OSCN} != "buster" ]] ; then
+ if [ "$METH" == "$IPv4pub" ]; then
+ pivpnHOST="${IPv4pub}"
+ else
+ until [[ $publicDNSCorrect = True ]]
+ do
+ PUBLICDNS=$(whiptail --title "PiVPN Setup" --inputbox "What is the public DNS name of this Server?" ${r} ${c} 3>&1 1>&2 2>&3)
+ exitstatus=$?
+ if [ $exitstatus != 0 ]; then
+ echo "::: Cancel selected. Exiting..."
+ exit 1
+ fi
+ if (whiptail --backtitle "Confirm DNS Name" --title "Confirm DNS Name" --yesno "Is this correct?\\n\\n Public DNS Name: $PUBLICDNS" ${r} ${c}) then
+ publicDNSCorrect=True
+ pivpnHOST="${PUBLICDNS}"
+ else
+ publicDNSCorrect=False
+ fi
+ done
+ fi
- APPLY_TWO_POINT_FOUR=false
- else
- if (whiptail --backtitle "Setup OpenVPN" --title "Installation mode" --yesno "OpenVPN 2.4 brings support for stronger authentication and key exchange using Elliptic Curves, along with encrypted control channel.\n\nIf your clients do run OpenVPN 2.4 or later you can enable these features, otherwise choose 'No' for best compatibility.\n\nNOTE: Current mobile app, that is OpenVPN connect, is supported." ${r} ${c}); then
- APPLY_TWO_POINT_FOUR=true
- $SUDO touch /etc/pivpn/TWO_POINT_FOUR
- else
- APPLY_TWO_POINT_FOUR=false
- fi
- fi
- fi
+ echo "pivpnHOST=${pivpnHOST}" >> /tmp/setupVars.conf
+}
- if [[ ${runUnattended} == true ]] && [[ ${APPLY_TWO_POINT_FOUR} == true ]]; then
- $SUDO touch /etc/pivpn/TWO_POINT_FOUR
- fi
+askEncryption(){
+ if [ "${runUnattended}" = 'true' ]; then
+ if [ -z "$pivpnENCRYPT" ]; then
+ pivpnENCRYPT=2048
+ echo "::: Using a 2048 bit certificate"
+ else
+ if [ "$pivpnENCRYPT" -eq 2048 ] || [ "$pivpnENCRYPT" -eq 3072 ] || [ "$pivpnENCRYPT" -eq 4096 ]; then
+ echo "::: Using a ${pivpnENCRYPT}-bit certificate"
+ else
+ echo "::: ${pivpnENCRYPT} is not a valid certificate size, use 2048, 3072, or 4096"
+ exit 1
+ fi
+ fi
- if [[ ${APPLY_TWO_POINT_FOUR} == false ]]; then
+ if [ -z "$DOWNLOAD_DH_PARAM" ] || [ "$DOWNLOAD_DH_PARAM" -ne 1 ]; then
+ DOWNLOAD_DH_PARAM=0
+ echo "::: DH parameters will be generated locally"
+ else
+ echo "::: DH parameters will be downloaded from \"2 Ton Digital\""
+ fi
- ## creates a file to determine if the OpenVPN setup is compatible with the OVPN12 format for iOS keychain integration.
- echo "true" > /etc/pivpn/OVPN12_Compatible
-
- ENCRYPT=$(whiptail --backtitle "Setup OpenVPN" --title "RSA certificate size" --radiolist \
- "Choose the desired size of your certificate (press space to select):\n This is a certificate that will be generated on your system. The larger the certificate, the more time this will take. For most applications, it is recommended to use 2048 bits. If you are testing, you can use 1024 bits to speed things up, but do not use this for normal use! If you are paranoid about ... things... then grab a cup of joe and pick 4096 bits." ${r} ${c} 3 \
- "1024" "Use a 1024-bit certificate (testing only)" OFF \
- "2048" "Use a 2048-bit certificate (recommended level)" ON \
- "4096" "Use a 4096-bit certificate (paranoid level)" OFF 3>&1 1>&2 2>&3)
+ echo "pivpnENCRYPT=${pivpnENCRYPT}" >> /tmp/setupVars.conf
+ echo "DOWNLOAD_DH_PARAM=${DOWNLOAD_DH_PARAM}" >> /tmp/setupVars.conf
+ return
+ fi
- else
+ pivpnENCRYPT=$(whiptail --backtitle "Setup OpenVPN" --title "RSA certificate size" --radiolist \
+ "Choose the desired size of your certificate (press space to select):\\nThis is a certificate that will be generated on your system. The larger the certificate, the more time this will take. For most applications, it is recommended to use 2048 bits. If you are paranoid about ... things... then grab a cup of joe and pick 4096 bits." ${r} ${c} 3 \
+ "2048" "Use a 2048-bit certificate (recommended level)" ON \
+ "3072" "Use a 3072-bit certificate " OFF \
+ "4096" "Use a 4096-bit certificate (paranoid level)" OFF 3>&1 1>&2 2>&3)
- ENCRYPT=$(whiptail --backtitle "Setup OpenVPN" --title "ECDSA certificate size" --radiolist \
- "Choose the desired size of your certificate (press space to select):\n This is a certificate that will be generated on your system. The larger the certificate, the more time this will take. For most applications, it is recommended to use 256 bits. You can increase the number of bits if you care about, however, consider that 256 bits are already as secure as 3072 bit RSA." ${r} ${c} 3 \
- "256" "Use a 256-bit certificate (recommended level)" ON \
- "384" "Use a 384-bit certificate" OFF \
- "521" "Use a 521-bit certificate (paranoid level)" OFF 3>&1 1>&2 2>&3)
+ exitstatus=$?
+ if [ $exitstatus != 0 ]; then
+ echo "::: Cancel selected. Exiting..."
+ exit 1
+ fi
- fi
+ if ([ "$pivpnENCRYPT" -ge "3072" ] && whiptail --backtitle "Setup OpenVPN" --title "Download Diffie-Hellman Parameters" --yesno --defaultno "Download Diffie-Hellman parameters from a public DH parameter generation service?\\n\\nGenerating DH parameters for a $pivpnENCRYPT-bit key can take many hours on a Raspberry Pi. You can instead download DH parameters from \"2 Ton Digital\" that are generated at regular intervals as part of a public service. Downloaded DH parameters will be randomly selected from their database.\\nMore information about this service can be found here: https://2ton.com.au/safeprimes/\\n\\nIf you're paranoid, choose 'No' and Diffie-Hellman parameters will be generated on your device." ${r} ${c}); then
+ DOWNLOAD_DH_PARAM=1
+ else
+ DOWNLOAD_DH_PARAM=0
+ fi
- exitstatus=$?
- if [ $exitstatus != 0 ]; then
- echo "::: Cancel selected. Exiting..."
- exit 1
- fi
- fi
+ echo "pivpnENCRYPT=${pivpnENCRYPT}" >> /tmp/setupVars.conf
+ echo "DOWNLOAD_DH_PARAM=${DOWNLOAD_DH_PARAM}" >> /tmp/setupVars.conf
+}
- # If easy-rsa exists, remove it
- if [[ -d /etc/openvpn/easy-rsa/ ]]; then
- $SUDO rm -rf /etc/openvpn/easy-rsa/
- fi
+confOpenVPN(){
+ # Grab the existing Hostname
+ host_name=$(hostname -s)
+ # Generate a random UUID for this server so that we can use verify-x509-name later that is unique for this server installation.
+ NEW_UUID=$( /dev/null
- # Get easy-rsa
- wget -q -O - "${easyrsaRel}" | $SUDO tar xz -C /etc/openvpn && $SUDO mv /etc/openvpn/EasyRSA-v${easyrsaVer} /etc/openvpn/easy-rsa
- # fix ownership
- $SUDO chown -R root:root /etc/openvpn/easy-rsa
- $SUDO mkdir /etc/openvpn/easy-rsa/pki
+ if [ -f /etc/openvpn/server.conf ]; then
+ $SUDO rm /etc/openvpn/server.conf
+ fi
- cd /etc/openvpn/easy-rsa || exit
+ # If easy-rsa exists, remove it
+ if [[ -d /etc/openvpn/easy-rsa/ ]]; then
+ $SUDO rm -rf /etc/openvpn/easy-rsa/
+ fi
- # Write out new vars file
- set +e
- IFS= read -d '' String <<"EOF"
-if [ -z "$EASYRSA_CALLER" ]; then
- echo "Nope." >&2
- return 1
+ # Get easy-rsa
+ wget -qO- "${easyrsaRel}" | $SUDO tar xz -C /etc/openvpn
+ $SUDO mv /etc/openvpn/EasyRSA-v${easyrsaVer} /etc/openvpn/easy-rsa
+ # fix ownership
+ $SUDO chown -R root:root /etc/openvpn/easy-rsa
+ $SUDO mkdir /etc/openvpn/easy-rsa/pki
+ $SUDO chmod 700 /etc/openvpn/easy-rsa/pki
+
+ cd /etc/openvpn/easy-rsa || exit 1
+
+ # Write out new vars file
+ echo "if [ -z \"\$EASYRSA_CALLER\" ]; then
+ echo \"Nope.\" >&2
+ return 1
fi
-set_var EASYRSA "/etc/openvpn/easy-rsa"
-set_var EASYRSA_PKI "$EASYRSA/pki"
+set_var EASYRSA \"/etc/openvpn/easy-rsa\"
+set_var EASYRSA_PKI \"\$EASYRSA/pki\"
set_var EASYRSA_CRL_DAYS 3650
-EOF
- echo "${String}" | $SUDO tee vars >/dev/null
- set -e
+set_var EASYRSA_ALGO rsa
+set_var EASYRSA_KEY_SIZE ${pivpnENCRYPT}" | $SUDO tee vars >/dev/null
- # Set certificate type
- if [[ ${APPLY_TWO_POINT_FOUR} == false ]]; then
- echo "set_var EASYRSA_ALGO rsa" | $SUDO tee -a vars
- echo "set_var EASYRSA_KEY_SIZE ${ENCRYPT}" | $SUDO tee -a vars
- else
- echo "set_var EASYRSA_ALGO ec" | $SUDO tee -a vars
- echo "set_var EASYRSA_CURVE ${ECDSA_MAP["${ENCRYPT}"]}" | $SUDO tee -a vars
- fi
+ # Remove any previous keys
+ ${SUDOE} ./easyrsa --batch init-pki
- # Remove any previous keys
- ${SUDOE} ./easyrsa --batch init-pki
+ # Build the certificate authority
+ printf "::: Building CA...\\n"
+ ${SUDOE} ./easyrsa --batch build-ca nopass
+ printf "\\n::: CA Complete.\\n"
- # Build the certificate authority
- printf "::: Building CA...\n"
- ${SUDOE} ./easyrsa --batch build-ca nopass
- printf "\n::: CA Complete.\n"
+ if [ "${runUnattended}" = 'true' ]; then
+ echo "::: The server key, Diffie-Hellman parameters, and HMAC key will now be generated."
+ else
+ whiptail --msgbox --backtitle "Setup OpenVPN" --title "Server Information" "The server key, Diffie-Hellman parameters, and HMAC key will now be generated." ${r} ${c}
+ fi
- if [[ ${useUpdateVars} == false ]]; then
- if [[ ${APPLY_TWO_POINT_FOUR} == false ]]; then
- whiptail --msgbox --backtitle "Setup OpenVPN" --title "Server Information" "The server key, Diffie-Hellman parameters, and HMAC key will now be generated." ${r} ${c}
- fi
- fi
+ # Build the server
+ EASYRSA_CERT_EXPIRE=3650 ${SUDOE} ./easyrsa build-server-full "${SERVER_NAME}" nopass
- # Build the server
- EASYRSA_CERT_EXPIRE=3650 ${SUDOE} ./easyrsa build-server-full ${SERVER_NAME} nopass
+ if [ ${DOWNLOAD_DH_PARAM} -eq 1 ]; then
+ # Downloading parameters
+ ${SUDOE} curl -s "https://2ton.com.au/getprimes/random/dhparam/${pivpnENCRYPT}" -o "/etc/openvpn/easy-rsa/pki/dh${pivpnENCRYPT}.pem"
+ else
+ # Generate Diffie-Hellman key exchange
+ ${SUDOE} ./easyrsa gen-dh
+ ${SUDOE} mv "pki/dh.pem" "pki/dh${pivpnENCRYPT}.pem"
+ fi
- if [[ ${useUpdateVars} == false ]]; then
- if [[ ${APPLY_TWO_POINT_FOUR} == false ]]; then
- if ([ "$ENCRYPT" -ge "4096" ] && whiptail --backtitle "Setup OpenVPN" --title "Download Diffie-Hellman Parameters" --yesno --defaultno "Download Diffie-Hellman parameters from a public DH parameter generation service?\n\nGenerating DH parameters for a $ENCRYPT-bit key can take many hours on a Raspberry Pi. You can instead download DH parameters from \"2 Ton Digital\" that are generated at regular intervals as part of a public service. Downloaded DH parameters will be randomly selected from their database.\nMore information about this service can be found here: https://2ton.com.au/safeprimes/\n\nIf you're paranoid, choose 'No' and Diffie-Hellman parameters will be generated on your device." ${r} ${c}); then
- DOWNLOAD_DH_PARAM=true
- else
- DOWNLOAD_DH_PARAM=false
- fi
- else
- DOWNLOAD_DH_PARAM=false
- fi
- fi
+ # Generate static HMAC key to defend against DDoS
+ ${SUDOE} openvpn --genkey --secret pki/ta.key
- if [[ ${APPLY_TWO_POINT_FOUR} == false ]]; then
- if [ "$ENCRYPT" -ge "4096" ] && [[ ${DOWNLOAD_DH_PARAM} == true ]]; then
- # Downloading parameters
- ${SUDOE} curl "https://2ton.com.au/getprimes/random/dhparam/${ENCRYPT}" -o "/etc/openvpn/easy-rsa/pki/dh${ENCRYPT}.pem"
- else
- # Generate Diffie-Hellman key exchange
- ${SUDOE} ./easyrsa gen-dh
- ${SUDOE} mv pki/dh.pem pki/dh${ENCRYPT}.pem
- fi
- fi
+ # Generate an empty Certificate Revocation List
+ ${SUDOE} ./easyrsa gen-crl
+ ${SUDOE} cp pki/crl.pem /etc/openvpn/crl.pem
+ if ! getent passwd openvpn; then
+ ${SUDOE} adduser --system --home /var/lib/openvpn/ --group --disabled-login ${debianOvpnUserGroup%:*}
+ fi
+ ${SUDOE} chown "$debianOvpnUserGroup" /etc/openvpn/crl.pem
- # Generate static HMAC key to defend against DDoS
- ${SUDOE} openvpn --genkey --secret pki/ta.key
+ # Write config file for server using the template.txt file
+ $SUDO cp $pivpnFilesDir/server_config.txt /etc/openvpn/server.conf
- # Generate an empty Certificate Revocation List
- ${SUDOE} ./easyrsa gen-crl
- ${SUDOE} cp pki/crl.pem /etc/openvpn/crl.pem
- ${SUDOE} chown nobody:nogroup /etc/openvpn/crl.pem
+ # Apply client DNS settings
+ ${SUDOE} sed -i '0,/\(dhcp-option DNS \)/ s/\(dhcp-option DNS \).*/\1'${pivpnDNS1}'\"/' /etc/openvpn/server.conf
- # Write config file for server using the template.txt file
- $SUDO cp /etc/.pivpn/server_config.txt /etc/openvpn/server.conf
+ if [ -z ${pivpnDNS2} ]; then
+ ${SUDOE} sed -i '/\(dhcp-option DNS \)/{n;N;d}' /etc/openvpn/server.conf
+ else
+ ${SUDOE} sed -i '0,/\(dhcp-option DNS \)/! s/\(dhcp-option DNS \).*/\1'${pivpnDNS2}'\"/' /etc/openvpn/server.conf
+ fi
- if [[ ${APPLY_TWO_POINT_FOUR} == true ]]; then
- #If they enabled 2.4 use tls-crypt instead of tls-auth to encrypt control channel
- $SUDO sed -i "s/tls-auth \/etc\/openvpn\/easy-rsa\/pki\/ta.key 0/tls-crypt \/etc\/openvpn\/easy-rsa\/pki\/ta.key/" /etc/openvpn/server.conf
- fi
+ # Set the user encryption key size
+ $SUDO sed -i "s#\\(dh /etc/openvpn/easy-rsa/pki/dh\\).*#\\1${pivpnENCRYPT}.pem#" /etc/openvpn/server.conf
- if [[ ${APPLY_TWO_POINT_FOUR} == true ]]; then
- #If they enabled 2.4 disable dh parameters since the key exchange will use the matching curve from the ECDSA certificate
- $SUDO sed -i "s/\(dh \/etc\/openvpn\/easy-rsa\/pki\/dh\).*/dh none/" /etc/openvpn/server.conf
- else
- # Otherwise set the user encryption key size
- $SUDO sed -i "s/\(dh \/etc\/openvpn\/easy-rsa\/pki\/dh\).*/\1${ENCRYPT}.pem/" /etc/openvpn/server.conf
- fi
+ # if they modified port put value in server.conf
+ if [ "$pivpnPORT" != 1194 ]; then
+ $SUDO sed -i "s/1194/${pivpnPORT}/g" /etc/openvpn/server.conf
+ fi
- # if they modified port put value in server.conf
- if [ $PORT != 1194 ]; then
- $SUDO sed -i "s/1194/${PORT}/g" /etc/openvpn/server.conf
- fi
+ # if they modified protocol put value in server.conf
+ if [ "$pivpnPROTO" != "udp" ]; then
+ $SUDO sed -i "s/proto udp/proto tcp/g" /etc/openvpn/server.conf
+ fi
- # if they modified protocol put value in server.conf
- if [ "$PROTO" != "udp" ]; then
- $SUDO sed -i "s/proto udp/proto tcp/g" /etc/openvpn/server.conf
- fi
+ if [ -n "$pivpnSEARCHDOMAIN" ]; then
+ $SUDO sed -i "0,/\\(.*dhcp-option.*\\)/s//push \"dhcp-option DOMAIN ${pivpnSEARCHDOMAIN}\" \\n&/" /etc/openvpn/server.conf
+ fi
- # write out server certs to conf file
- $SUDO sed -i "s/\(key \/etc\/openvpn\/easy-rsa\/pki\/private\/\).*/\1${SERVER_NAME}.key/" /etc/openvpn/server.conf
- $SUDO sed -i "s/\(cert \/etc\/openvpn\/easy-rsa\/pki\/issued\/\).*/\1${SERVER_NAME}.crt/" /etc/openvpn/server.conf
+ # write out server certs to conf file
+ $SUDO sed -i "s#\\(key /etc/openvpn/easy-rsa/pki/private/\\).*#\\1${SERVER_NAME}.key#" /etc/openvpn/server.conf
+ $SUDO sed -i "s#\\(cert /etc/openvpn/easy-rsa/pki/issued/\\).*#\\1${SERVER_NAME}.crt#" /etc/openvpn/server.conf
}
-confUnattendedUpgrades() {
- cd /etc/apt/apt.conf.d
+confOVPN(){
+ $SUDO cp $pivpnFilesDir/Default.txt /etc/openvpn/easy-rsa/pki/Default.txt
- if [[ $UNATTUPG == "unattended-upgrades" ]]; then
- $SUDO $PKG_MANAGER --yes --quiet --no-install-recommends install "$UNATTUPG" > /dev/null & spinner $!
- if [[ $PLAT == "Ubuntu" ]]; then
- # Ubuntu 50unattended-upgrades should already just have security enabled
- # so we just need to configure the 10periodic file
- cat << EOT | $SUDO tee 10periodic >/dev/null
- APT::Periodic::Update-Package-Lists "1";
- APT::Periodic::Download-Upgradeable-Packages "1";
- APT::Periodic::AutocleanInterval "5";
- APT::Periodic::Unattended-Upgrade "1";
-EOT
- else
- # Fix Raspbian config
- if [[ $PLAT == "Raspbian" ]]; then
- wget -q -O - "$UNATTUPG_CONFIG" | $SUDO tar xz
- $SUDO cp "unattended-upgrades-$UNATTUPG_RELEASE/data/50unattended-upgrades.Raspbian" 50unattended-upgrades
- $SUDO rm -rf "unattended-upgrades-$UNATTUPG_RELEASE"
- fi
+ $SUDO sed -i 's/IPv4pub/'"$pivpnHOST"'/' /etc/openvpn/easy-rsa/pki/Default.txt
- # Add the remaining settings for all other distributions
- cat << EOT | $SUDO tee 02periodic >/dev/null
- APT::Periodic::Enable "1";
- APT::Periodic::Update-Package-Lists "1";
- APT::Periodic::Download-Upgradeable-Packages "1";
- APT::Periodic::Unattended-Upgrade "1";
- APT::Periodic::AutocleanInterval "7";
- APT::Periodic::Verbose "0";
-EOT
- fi
- fi
+ # if they modified port put value in Default.txt for clients to use
+ if [ "$pivpnPORT" != 1194 ]; then
+ $SUDO sed -i -e "s/1194/${pivpnPORT}/g" /etc/openvpn/easy-rsa/pki/Default.txt
+ fi
+ # if they modified protocol put value in Default.txt for clients to use
+ if [ "$pivpnPROTO" != "udp" ]; then
+ $SUDO sed -i -e "s/proto udp/proto tcp/g" /etc/openvpn/easy-rsa/pki/Default.txt
+ fi
+
+ # verify server name to strengthen security
+ $SUDO sed -i "s/SRVRNAME/${SERVER_NAME}/" /etc/openvpn/easy-rsa/pki/Default.txt
}
-confNetwork() {
- # Enable forwarding of internet traffic
- $SUDO sed -i '/net.ipv4.ip_forward=1/s/^#//g' /etc/sysctl.conf
- $SUDO sysctl -p
+confWireGuard(){
+ if [ -d /etc/wireguard ]; then
+ # Backup the wireguard folder
+ WIREGUARD_BACKUP="wireguard_$(date +%Y-%m-%d-%H%M%S).tar.gz"
+ echo "::: Backing up the wireguard folder to /etc/${WIREGUARD_BACKUP}"
+ $SUDO tar czf "/etc/${WIREGUARD_BACKUP}" /etc/wireguard &> /dev/null
- # if ufw enabled, configure that (running as root because sometimes the executable is not in the user's $PATH, on Debian for example)
- if $SUDO bash -c 'hash ufw' 2>/dev/null; then
- if LANG=en_US.UTF-8 $SUDO ufw status | grep -q inactive
- then
- noUFW=1
- else
- echo "::: Detected UFW is enabled."
- echo "::: Adding UFW rules..."
- $SUDO sed "/delete these required/i *nat\n:POSTROUTING ACCEPT [0:0]\n-I POSTROUTING -s 10.8.0.0/24 -o $IPv4dev -j MASQUERADE\nCOMMIT\n" -i /etc/ufw/before.rules
- # Insert rules at the beginning of the chain (in case there are other rules that may drop the traffic)
- $SUDO ufw insert 1 allow "$PORT"/"$PROTO" >/dev/null
+ if [ -f /etc/wireguard/wg0.conf ]; then
+ $SUDO rm /etc/wireguard/wg0.conf
+ fi
+ else
+ # If compiled from source, the wireguard folder is not being created
+ $SUDO mkdir /etc/wireguard
+ fi
- # https://askubuntu.com/a/712202
- INSTALLED_UFW=$(dpkg-query --showformat='${Version}' --show ufw)
- MINIMUM_UFW=0.34
+ # Ensure that only root is able to enter the wireguard folder
+ $SUDO chown root:root /etc/wireguard
+ $SUDO chmod 700 /etc/wireguard
- if $SUDO dpkg --compare-versions "$INSTALLED_UFW" ge "$MINIMUM_UFW"; then
- # Don't forward everything, just the traffic originated from the VPN subnet
- $SUDO ufw route insert 1 allow in on tun0 from 10.8.0.0/24 out on "$IPv4dev" to any >/dev/null
- echo 0 > /tmp/OLD_UFW
- else
- # This ufw version does not support route command, fallback to policy change
- $SUDO sed -i "s/\(DEFAULT_FORWARD_POLICY=\).*/\1\"ACCEPT\"/" /etc/default/ufw
- echo 1 > /tmp/OLD_UFW
- fi
+ if [ "${runUnattended}" = 'true' ]; then
+ echo "::: The Server Keys and Pre-Shared key will now be generated."
+ else
+ whiptail --title "Server Information" --msgbox "The Server Keys and Pre-Shared key will now be generated." "${r}" "${c}"
+ fi
- $SUDO ufw reload >/dev/null
- echo "::: UFW configuration completed."
- fi
- else
- noUFW=1
- fi
- # else configure iptables
- if [[ $noUFW -eq 1 ]]; then
- echo 1 > /tmp/noUFW
+ # Remove configs and keys folders to make space for a new server when using 'Repair' or 'Reconfigure'
+ # over an existing installation
+ $SUDO rm -rf /etc/wireguard/configs
+ $SUDO rm -rf /etc/wireguard/keys
- # Now some checks to detect which rules we need to add. On a newly installed system all policies
- # should be ACCEPT, so the only required rule would be the MASQUERADE one.
+ $SUDO mkdir -p /etc/wireguard/configs
+ $SUDO touch /etc/wireguard/configs/clients.txt
+ $SUDO mkdir -p /etc/wireguard/keys
- $SUDO iptables -t nat -I POSTROUTING -s 10.8.0.0/24 -o "$IPv4dev" -j MASQUERADE
+ # Generate private key and derive public key from it
+ wg genkey | $SUDO tee /etc/wireguard/keys/server_priv &> /dev/null
+ wg genpsk | $SUDO tee /etc/wireguard/keys/psk &> /dev/null
+ $SUDO cat /etc/wireguard/keys/server_priv | wg pubkey | $SUDO tee /etc/wireguard/keys/server_pub &> /dev/null
- # Count how many rules are in the INPUT and FORWARD chain. When parsing input from
- # iptables -S, '^-P' skips the policies and 'ufw-' skips ufw chains (in case ufw was found
- # installed but not enabled).
+ echo "::: Server Keys and Pre-Shared Key have been generated."
- # Grep returns non 0 exit code where there are no matches, however that would make the script exit,
- # for this reasons we use '|| true' to force exit code 0
- INPUT_RULES_COUNT="$($SUDO iptables -S INPUT | grep -vcE '(^-P|ufw-)' || true)"
- FORWARD_RULES_COUNT="$($SUDO iptables -S FORWARD | grep -vcE '(^-P|ufw-)' || true)"
-
- INPUT_POLICY="$($SUDO iptables -S INPUT | grep '^-P' | awk '{print $3}')"
- FORWARD_POLICY="$($SUDO iptables -S FORWARD | grep '^-P' | awk '{print $3}')"
-
- # If rules count is not zero, we assume we need to explicitly allow traffic. Same conclusion if
- # there are no rules and the policy is not ACCEPT. Note that rules are being added to the top of the
- # chain (using -I).
-
- if [ "$INPUT_RULES_COUNT" -ne 0 ] || [ "$INPUT_POLICY" != "ACCEPT" ]; then
- $SUDO iptables -I INPUT 1 -i "$IPv4dev" -p "$PROTO" --dport "$PORT" -j ACCEPT
- INPUT_CHAIN_EDITED=1
- else
- INPUT_CHAIN_EDITED=0
- fi
-
- if [ "$FORWARD_RULES_COUNT" -ne 0 ] || [ "$FORWARD_POLICY" != "ACCEPT" ]; then
- $SUDO iptables -I FORWARD 1 -d 10.8.0.0/24 -i "$IPv4dev" -o tun0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
- $SUDO iptables -I FORWARD 2 -s 10.8.0.0/24 -i tun0 -o "$IPv4dev" -j ACCEPT
- FORWARD_CHAIN_EDITED=1
- else
- FORWARD_CHAIN_EDITED=0
- fi
-
- case ${PLAT} in
- Ubuntu|Debian|Devuan)
- $SUDO iptables-save | $SUDO tee /etc/iptables/rules.v4 > /dev/null
- ;;
- *)
- $SUDO netfilter-persistent save
- ;;
- esac
- else
- echo 0 > /tmp/noUFW
- fi
-
- echo "$INPUT_CHAIN_EDITED" > /tmp/INPUT_CHAIN_EDITED
- echo "$FORWARD_CHAIN_EDITED" > /tmp/FORWARD_CHAIN_EDITED
-
- $SUDO cp /tmp/noUFW /etc/pivpn/NO_UFW
- $SUDO cp /tmp/OLD_UFW /etc/pivpn/OLD_UFW
- $SUDO cp /tmp/INPUT_CHAIN_EDITED /etc/pivpn/INPUT_CHAIN_EDITED
- $SUDO cp /tmp/FORWARD_CHAIN_EDITED /etc/pivpn/FORWARD_CHAIN_EDITED
+ echo "[Interface]
+PrivateKey = $($SUDO cat /etc/wireguard/keys/server_priv)
+Address = ${vpnGw}/${subnetClass}
+ListenPort = ${pivpnPORT}" | $SUDO tee /etc/wireguard/wg0.conf &> /dev/null
+ echo "::: Server config generated."
}
-confOVPN() {
- if ! IPv4pub=$(dig +short myip.opendns.com @208.67.222.222) || ! valid_ip "$IPv4pub"; then
- echo "dig failed, now trying to curl checkip.amazonaws.com"
- if ! IPv4pub=$(curl -s https://checkip.amazonaws.com) || ! valid_ip "$IPv4pub"; then
- echo "checkip.amazonaws.com failed, please check your internet connection/DNS"
- exit 1
- fi
- fi
- $SUDO cp /tmp/INSTALL_USER /etc/pivpn/INSTALL_USER
- $SUDO cp /tmp/DET_PLATFORM /etc/pivpn/DET_PLATFORM
+confNetwork(){
+ # Enable forwarding of internet traffic
+ $SUDO sed -i '/net.ipv4.ip_forward=1/s/^#//g' /etc/sysctl.conf
+ $SUDO sysctl -p > /dev/null
- $SUDO cp /etc/.pivpn/Default.txt /etc/openvpn/easy-rsa/pki/Default.txt
+ if [ "$USING_UFW" -eq 1 ]; then
- if [[ ${APPLY_TWO_POINT_FOUR} == true ]]; then
- #If they enabled 2.4 remove key-direction options since it's not required
- $SUDO sed -i "/key-direction 1/d" /etc/openvpn/easy-rsa/pki/Default.txt
- fi
+ echo "::: Detected UFW is enabled."
+ echo "::: Adding UFW rules..."
+ ### Basic safeguard: if file is empty, there's been something weird going on.
+ ### Note: no safeguard against imcomplete content as a result of previous failures.
+ if test -s /etc/ufw/before.rules; then
+ $SUDO cp -f /etc/ufw/before.rules /etc/ufw/before.rules.pre-pivpn
+ else
+ echo "$0: ERR: Sorry, won't touch empty file \"/etc/ufw/before.rules\".";
+ exit 1;
+ fi
+ ### If there is already a "*nat" section just add our POSTROUTING MASQUERADE
+ if $SUDO grep -q "*nat" /etc/ufw/before.rules; then
+ ### Onyl add the NAT rule if it isn't already there
+ if ! $SUDO grep -q "${VPN}-nat-rule" /etc/ufw/before.rules; then
+ $SUDO sed "/^*nat/{n;s/\(:POSTROUTING ACCEPT .*\)/\1\n-I POSTROUTING -s ${pivpnNET}\/${subnetClass} -o ${IPv4dev} -j MASQUERADE -m comment --comment ${VPN}-nat-rule/}" -i /etc/ufw/before.rules
+ fi
+ else
+ $SUDO sed "/delete these required/i *nat\n:POSTROUTING ACCEPT [0:0]\n-I POSTROUTING -s ${pivpnNET}\/${subnetClass} -o ${IPv4dev} -j MASQUERADE -m comment --comment ${VPN}-nat-rule\nCOMMIT\n" -i /etc/ufw/before.rules
+ fi
+ # Insert rules at the beginning of the chain (in case there are other rules that may drop the traffic)
+ $SUDO ufw insert 1 allow "${pivpnPORT}"/"${pivpnPROTO}" >/dev/null
+ $SUDO ufw route insert 1 allow in on "${pivpnDEV}" from "${pivpnNET}/${subnetClass}" out on "${IPv4dev}" to any >/dev/null
- if [[ ${useUpdateVars} == false ]]; then
- METH=$(whiptail --title "Public IP or DNS" --radiolist "Will clients use a Public IP or DNS Name to connect to your server (press space to select)?" ${r} ${c} 2 \
- "$IPv4pub" "Use this public IP" "ON" \
- "DNS Entry" "Use a public DNS" "OFF" 3>&1 1>&2 2>&3)
+ $SUDO ufw reload >/dev/null
+ echo "::: UFW configuration completed."
- exitstatus=$?
- if [ $exitstatus != 0 ]; then
- echo "::: Cancel selected. Exiting..."
- exit 1
- fi
+ elif [ "$USING_UFW" -eq 0 ]; then
- if [ "$METH" == "$IPv4pub" ]; then
- $SUDO sed -i 's/IPv4pub/'"$IPv4pub"'/' /etc/openvpn/easy-rsa/pki/Default.txt
- else
- until [[ $publicDNSCorrect = True ]]
- do
- PUBLICDNS=$(whiptail --title "PiVPN Setup" --inputbox "What is the public DNS name of this Server?" ${r} ${c} 3>&1 1>&2 2>&3)
- exitstatus=$?
- if [ $exitstatus != 0 ]; then
- echo "::: Cancel selected. Exiting..."
- exit 1
- fi
- if (whiptail --backtitle "Confirm DNS Name" --title "Confirm DNS Name" --yesno "Is this correct?\n\n Public DNS Name: $PUBLICDNS" ${r} ${c}) then
- publicDNSCorrect=True
- $SUDO sed -i 's/IPv4pub/'"$PUBLICDNS"'/' /etc/openvpn/easy-rsa/pki/Default.txt
- else
- publicDNSCorrect=False
- fi
- done
- fi
- else
- $SUDO sed -i 's/IPv4pub/'"$PUBLICDNS"'/' /etc/openvpn/easy-rsa/pki/Default.txt
- fi
+ # Now some checks to detect which rules we need to add. On a newly installed system all policies
+ # should be ACCEPT, so the only required rule would be the MASQUERADE one.
- # if they modified port put value in Default.txt for clients to use
- if [ $PORT != 1194 ]; then
- $SUDO sed -i -e "s/1194/${PORT}/g" /etc/openvpn/easy-rsa/pki/Default.txt
- fi
+ if ! $SUDO iptables -t nat -S | grep -q "${VPN}-nat-rule"; then
+ $SUDO iptables -t nat -I POSTROUTING -s "${pivpnNET}/${subnetClass}" -o "${IPv4dev}" -j MASQUERADE -m comment --comment "${VPN}-nat-rule"
+ fi
- # if they modified protocol put value in Default.txt for clients to use
- if [ "$PROTO" != "udp" ]; then
- $SUDO sed -i -e "s/proto udp/proto tcp/g" /etc/openvpn/easy-rsa/pki/Default.txt
- fi
+ # Count how many rules are in the INPUT and FORWARD chain. When parsing input from
+ # iptables -S, '^-P' skips the policies and 'ufw-' skips ufw chains (in case ufw was found
+ # installed but not enabled).
- # verify server name to strengthen security
- $SUDO sed -i "s/SRVRNAME/${SERVER_NAME}/" /etc/openvpn/easy-rsa/pki/Default.txt
+ # Grep returns non 0 exit code where there are no matches, however that would make the script exit,
+ # for this reasons we use '|| true' to force exit code 0
+ INPUT_RULES_COUNT="$($SUDO iptables -S INPUT | grep -vcE '(^-P|ufw-)')"
+ FORWARD_RULES_COUNT="$($SUDO iptables -S FORWARD | grep -vcE '(^-P|ufw-)')"
- INSTALL_HOME=$(grep -m1 "^${INSTALL_USER}:" /etc/passwd | cut -d: -f6)
- INSTALL_HOME=${INSTALL_HOME%/} # remove possible trailing slash
- if [ ! -d "$INSTALL_HOME/ovpns" ]; then
- $SUDO mkdir "$INSTALL_HOME/ovpns"
- fi
- $SUDO chmod 0750 "$INSTALL_HOME/ovpns"
- $SUDO chown $INSTALL_USER:$INSTALL_USER "$INSTALL_HOME/ovpns"
+ INPUT_POLICY="$($SUDO iptables -S INPUT | grep '^-P' | awk '{print $3}')"
+ FORWARD_POLICY="$($SUDO iptables -S FORWARD | grep '^-P' | awk '{print $3}')"
+
+ # If rules count is not zero, we assume we need to explicitly allow traffic. Same conclusion if
+ # there are no rules and the policy is not ACCEPT. Note that rules are being added to the top of the
+ # chain (using -I).
+
+ if [ "$INPUT_RULES_COUNT" -ne 0 ] || [ "$INPUT_POLICY" != "ACCEPT" ]; then
+ if $SUDO iptables -t nat -S | grep -q "${VPN}-input-rule"; then
+ INPUT_CHAIN_EDITED=0
+ else
+ $SUDO iptables -I INPUT 1 -i "${IPv4dev}" -p "${pivpnPROTO}" --dport "${pivpnPORT}" -j ACCEPT -m comment --comment "${VPN}-input-rule"
+ fi
+ INPUT_CHAIN_EDITED=1
+ else
+ INPUT_CHAIN_EDITED=0
+ fi
+
+ if [ "$FORWARD_RULES_COUNT" -ne 0 ] || [ "$FORWARD_POLICY" != "ACCEPT" ]; then
+ if $SUDO iptables -t nat -S | grep -q "${VPN}-forward-rule"; then
+ FORWARD_CHAIN_EDITED=0
+ else
+ $SUDO iptables -I FORWARD 1 -d "${pivpnNET}/${subnetClass}" -i "${IPv4dev}" -o "${pivpnDEV}" -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -m comment --comment "${VPN}-forward-rule"
+ $SUDO iptables -I FORWARD 2 -s "${pivpnNET}/${subnetClass}" -i "${pivpnDEV}" -o "${IPv4dev}" -j ACCEPT -m comment --comment "${VPN}-forward-rule"
+ fi
+ FORWARD_CHAIN_EDITED=1
+ else
+ FORWARD_CHAIN_EDITED=0
+ fi
+
+ case ${PLAT} in
+ Debian|Raspbian|Ubuntu)
+ $SUDO iptables-save | $SUDO tee /etc/iptables/rules.v4 > /dev/null
+ ;;
+ esac
+
+ echo "INPUT_CHAIN_EDITED=${INPUT_CHAIN_EDITED}" >> /tmp/setupVars.conf
+ echo "FORWARD_CHAIN_EDITED=${FORWARD_CHAIN_EDITED}" >> /tmp/setupVars.conf
+
+ fi
}
confLogging() {
- echo "if \$programname == 'ovpn-server' then /var/log/openvpn.log
+ echo "if \$programname == 'ovpn-server' then /var/log/openvpn.log
if \$programname == 'ovpn-server' then stop" | $SUDO tee /etc/rsyslog.d/30-openvpn.conf > /dev/null
echo "/var/log/openvpn.log
{
- rotate 4
- weekly
- missingok
- notifempty
- compress
- delaycompress
- sharedscripts
- postrotate
- invoke-rc.d rsyslog rotate >/dev/null 2>&1 || true
- endscript
+ rotate 4
+ weekly
+ missingok
+ notifempty
+ compress
+ delaycompress
+ sharedscripts
+ postrotate
+ invoke-rc.d rsyslog rotate >/dev/null 2>&1 || true
+ endscript
}" | $SUDO tee /etc/logrotate.d/openvpn > /dev/null
- # Restart the logging service
- case ${PLAT} in
- Ubuntu|Debian|*vuan)
- $SUDO service rsyslog restart || true
- ;;
- *)
- $SUDO systemctl restart rsyslog.service || true
- ;;
- esac
-}
-
-finalExports() {
- # Update variables in setupVars.conf file
- if [ -e "${setupVars}" ]; then
- $SUDO sed -i.update.bak '/INSTALL_USER/d;/UNATTUPG/d;/pivpnInterface/d;/IPv4dns/d;/IPv4addr/d;/IPv4gw/d;/pivpnProto/d;/PORT/d;/ENCRYPT/d;/DOWNLOAD_DH_PARAM/d;/PUBLICDNS/d;/OVPNDNS1/d;/OVPNDNS2/d;' "${setupVars}"
- fi
- {
- echo "INSTALL_USER=${INSTALL_USER}"
- echo "UNATTUPG=${UNATTUPG}"
- echo "pivpnInterface=${pivpnInterface}"
- echo "IPv4dns=${IPv4dns}"
- echo "IPv4addr=${IPv4addr}"
- echo "IPv4gw=${IPv4gw}"
- echo "pivpnProto=${pivpnProto}"
- echo "PORT=${PORT}"
- echo "ENCRYPT=${ENCRYPT}"
- echo "APPLY_TWO_POINT_FOUR=${APPLY_TWO_POINT_FOUR}"
- echo "DOWNLOAD_DH_PARAM=${DOWNLOAD_DH_PARAM}"
- echo "PUBLICDNS=${PUBLICDNS}"
- echo "OVPNDNS1=${OVPNDNS1}"
- echo "OVPNDNS2=${OVPNDNS2}"
- } | $SUDO tee "${setupVars}" > /dev/null
+ # Restart the logging service
+ case ${PLAT} in
+ Debian|Raspbian|Ubuntu)
+ $SUDO systemctl restart rsyslog.service || true
+ ;;
+ esac
}
-# I suggest replacing some of these names.
-
-#accountForRefactor() {
-# # At some point in the future this list can be pruned, for now we'll need it to ensure updates don't break.
-#
-# # Refactoring of install script has changed the name of a couple of variables. Sort them out here.
-# sed -i 's/INSTALL_USER/PIVPN_USER/g' ${setupVars}
-# #sed -i 's/UNATTUPG/UNATTUPG/g' ${setupVars}
-# sed -i 's/pivpnInterface/PIVPN_INTERFACE/g' ${setupVars}
-# sed -i 's/IPv4dns/IPV4_DNS/g' ${setupVars}
-# sed -i 's/IPv4addr/IPV4_ADDRESS/g' ${setupVars}
-# sed -i 's/IPv4gw/IPV4_GATEWAY/g' ${setupVars}
-# sed -i 's/pivpnProto/TRANSPORT_LAYER/g' ${setupVars}
-# #sed -i 's/PORT/PORT/g' ${setupVars}
-# #sed -i 's/ENCRYPT/ENCRYPT/g' ${setupVars}
-# #sed -i 's/DOWNLOAD_DH_PARAM/DOWNLOAD_DH_PARAM/g' ${setupVars}
-# sed -i 's/PUBLICDNS/PUBLIC_DNS/g' ${setupVars}
-# sed -i 's/OVPNDNS1/OVPN_DNS_1/g' ${setupVars}
-# sed -i 's/OVPNDNS2/OVPN_DNS_2/g' ${setupVars}
-#}
-
-installPiVPN() {
- stopServices
- $SUDO mkdir -p /etc/pivpn/
- confUnattendedUpgrades
- installScripts
- setCustomProto
- setCustomPort
- confOpenVPN
- confNetwork
- confOVPN
- setClientDNS
- setCustomDomain
- confLogging
- finalExports
+restartServices(){
+ echo "::: Restarting services..."
+ case ${PLAT} in
+ Debian|Raspbian|Ubuntu)
+ if [ "$VPN" = "openvpn" ]; then
+ $SUDO systemctl enable openvpn.service &> /dev/null
+ $SUDO systemctl restart openvpn.service
+ elif [ "$VPN" = "wireguard" ]; then
+ $SUDO systemctl enable wg-quick@wg0.service &> /dev/null
+ $SUDO systemctl restart wg-quick@wg0.service
+ fi
+ ;;
+ esac
}
-updatePiVPN() {
- #accountForRefactor
- stopServices
- confUnattendedUpgrades
- installScripts
+askUnattendedUpgrades(){
+ if [ "${runUnattended}" = 'true' ]; then
+ if [ -z "$UNATTUPG" ]; then
+ UNATTUPG=1
+ echo "::: No preference regarding unattended upgrades, assuming yes"
+ else
+ if [ "$UNATTUPG" -eq 1 ]; then
+ echo "::: Enabling unattended upgrades"
+ else
+ echo "::: Skipping unattended upgrades"
+ fi
+ fi
+ echo "UNATTUPG=${UNATTUPG}" >> /tmp/setupVars.conf
+ return
+ fi
- # setCustomProto
- # write out the PROTO
- PROTO=$pivpnProto
- $SUDO cp /tmp/pivpnPROTO /etc/pivpn/INSTALL_PROTO
+ whiptail --msgbox --backtitle "Security Updates" --title "Unattended Upgrades" "Since this server will have at least one port open to the internet, it is recommended you enable unattended-upgrades.\\nThis feature will check daily for security package updates only and apply them when necessary.\\nIt will NOT automatically reboot the server so to fully apply some updates you should periodically reboot." ${r} ${c}
- #setCustomPort
- # write out the port
- $SUDO cp /tmp/INSTALL_PORT /etc/pivpn/INSTALL_PORT
+ if (whiptail --backtitle "Security Updates" --title "Unattended Upgrades" --yesno "Do you want to enable unattended upgrades of security patches to this server?" ${r} ${c}); then
+ UNATTUPG=1
+ else
+ UNATTUPG=0
+ fi
- confOpenVPN
- confNetwork
- confOVPN
-
- # ?? Is this always OK? Also if you only select one DNS server ??
- $SUDO sed -i '0,/\(dhcp-option DNS \)/ s/\(dhcp-option DNS \).*/\1'${OVPNDNS1}'\"/' /etc/openvpn/server.conf
- $SUDO sed -i '0,/\(dhcp-option DNS \)/! s/\(dhcp-option DNS \).*/\1'${OVPNDNS2}'\"/' /etc/openvpn/server.conf
-
- finalExports #re-export setupVars.conf to account for any new vars added in new versions
+ echo "UNATTUPG=${UNATTUPG}" >> /tmp/setupVars.conf
}
+confUnattendedUpgrades(){
+ local PIVPN_DEPS
+ PIVPN_DEPS+=(unattended-upgrades)
+ installDependentPackages PIVPN_DEPS[@]
+ aptConfDir="/etc/apt/apt.conf.d"
-displayFinalMessage() {
- # Final completion message to user
- whiptail --msgbox --backtitle "Make it so." --title "Installation Complete!" "Now run 'pivpn add' to create the ovpn profiles.
-Run 'pivpn help' to see what else you can do!\n\nIf you run into any issue, please read all our documentation carefully.
-All incomplete posts or bug reports will be ignored or deleted.\n\nThank you for using PiVPN." ${r} ${c}
- if (whiptail --title "Reboot" --yesno --defaultno "It is strongly recommended you reboot after installation. Would you like to reboot now?" ${r} ${c}); then
- whiptail --title "Rebooting" --msgbox "The system will now reboot." ${r} ${c}
- printf "\nRebooting system...\n"
- $SUDO sleep 3
- $SUDO shutdown -r now
- fi
+ if [ "$PLAT" = "Ubuntu" ]; then
+
+ # Ubuntu 50unattended-upgrades should already just have security enabled
+ # so we just need to configure the 10periodic file
+ echo "APT::Periodic::Update-Package-Lists \"1\";
+ APT::Periodic::Download-Upgradeable-Packages \"1\";
+ APT::Periodic::AutocleanInterval \"5\";
+ APT::Periodic::Unattended-Upgrade \"1\";" | $SUDO tee "${aptConfDir}/10periodic" > /dev/null
+
+ else
+
+ # Fix Raspbian config
+ if [ "$PLAT" = "Raspbian" ]; then
+ wget -q -O "/tmp/${UNATTUPG_RELEASE}.tar.gz" "$UNATTUPG_CONFIG"
+ cd /tmp/ && $SUDO tar xzf "/tmp/${UNATTUPG_RELEASE}.tar.gz"
+ $SUDO cp /tmp/"unattended-upgrades-$UNATTUPG_RELEASE/data/50unattended-upgrades.Raspbian" "${aptConfDir}/50unattended-upgrades"
+ $SUDO rm -rf "/tmp/unattended-upgrades-$UNATTUPG_RELEASE"
+ fi
+
+ # Add the remaining settings for all other distributions
+ echo "APT::Periodic::Enable \"1\";
+ APT::Periodic::Update-Package-Lists \"1\";
+ APT::Periodic::Download-Upgradeable-Packages \"1\";
+ APT::Periodic::Unattended-Upgrade \"1\";
+ APT::Periodic::AutocleanInterval \"7\";
+ APT::Periodic::Verbose \"0\";" | $SUDO tee "${aptConfDir}/02periodic" > /dev/null
+
+ fi
+
+ # Enable automatic updates via the unstable repository when installing from debian package
+ if [ "$VPN" = "wireguard" ] && [ "$PLAT" != "Ubuntu" ] && [ "$(uname -m)" != "armv6l" ]; then
+ if ! grep -q '"o=Debian,a=unstable";' "${aptConfDir}/50unattended-upgrades"; then
+ $SUDO sed -i '/Unattended-Upgrade::Origins-Pattern {/a"o=Debian,a=unstable";' "${aptConfDir}/50unattended-upgrades"
+ fi
+ fi
}
-update_dialogs() {
- # reconfigure
- if [ "${reconfigure}" = true ]; then
- opt1a="Repair"
- opt1b="This will retain existing settings"
- strAdd="You will remain on the same version"
- else
- opt1a="Update"
- opt1b="This will retain existing settings."
- strAdd="You will be updated to the latest version."
- fi
- opt2a="Reconfigure"
- opt2b="This will allow you to enter new settings"
+installScripts(){
+ # Install the scripts from /etc/.pivpn to their various locations
+ echo ":::"
+ echo -n -e "::: Installing scripts to /opt/pivpn...\n"
+ if [ ! -d /opt/pivpn ]; then
+ $SUDO mkdir -p /opt/pivpn
+ $SUDO chown root:root /opt/pivpn
+ $SUDO chmod 0755 /opt/pivpn
+ fi
- UpdateCmd=$(whiptail --title "Existing Install Detected!" --menu "\n\nWe have detected an existing install.\n\nPlease choose from the following options: \n($strAdd)" ${r} ${c} 2 \
- "${opt1a}" "${opt1b}" \
- "${opt2a}" "${opt2b}" 3>&2 2>&1 1>&3) || \
- { echo "::: Cancel selected. Exiting"; exit 1; }
-
- case ${UpdateCmd} in
- ${opt1a})
- echo "::: ${opt1a} option selected."
- useUpdateVars=true
- ;;
- ${opt2a})
- echo "::: ${opt2a} option selected"
- useUpdateVars=false
- ;;
- esac
+ $SUDO cp "$pivpnFilesDir"/scripts/*.sh /opt/pivpn/
+ $SUDO cp "$pivpnFilesDir"/scripts/"$VPN"/*.sh /opt/pivpn/
+ $SUDO chmod 0755 /opt/pivpn/*.sh
+ $SUDO cp "$pivpnFilesDir"/scripts/"$VPN"/pivpn /usr/local/bin/pivpn
+ $SUDO chmod 0755 /usr/local/bin/pivpn
+ $SUDO cp "$pivpnFilesDir"/scripts/"$VPN"/bash-completion /etc/bash_completion.d/pivpn
+ $SUDO chmod 0644 /etc/bash_completion.d/pivpn
+ # shellcheck disable=SC1091
+ . /etc/bash_completion.d/pivpn
+ echo " done."
}
-clone_or_update_repos() {
- if [[ "${reconfigure}" == true ]]; then
- echo "::: --reconfigure passed to install script. Not downloading/updating local repos"
- else
- # Get Git files
- getGitFiles ${pivpnFilesDir} ${pivpnGitUrl} || \
- { echo "!!! Unable to clone ${pivpnGitUrl} into ${pivpnFilesDir}, unable to continue."; \
- exit 1; \
- }
- fi
+displayFinalMessage(){
+ if [ "${runUnattended}" = 'true' ]; then
+ echo "::: Installation Complete!"
+ echo "::: Now run 'pivpn add' to create the ovpn profiles."
+ echo "::: Run 'pivpn help' to see what else you can do!"
+ echo
+ echo "::: If you run into any issue, please read all our documentation carefully."
+ echo "::: All incomplete posts or bug reports will be ignored or deleted."
+ echo
+ echo "::: Thank you for using PiVPN."
+ echo "::: It is strongly recommended you reboot after installation."
+ return
+ fi
+
+ # Final completion message to user
+ whiptail --msgbox --backtitle "Make it so." --title "Installation Complete!" "Now run 'pivpn add' to create the ovpn profiles.
+Run 'pivpn help' to see what else you can do!\\n\\nIf you run into any issue, please read all our documentation carefully.
+All incomplete posts or bug reports will be ignored or deleted.\\n\\nThank you for using PiVPN." ${r} ${c}
+ if (whiptail --title "Reboot" --yesno --defaultno "It is strongly recommended you reboot after installation. Would you like to reboot now?" ${r} ${c}); then
+ whiptail --title "Rebooting" --msgbox "The system will now reboot." ${r} ${c}
+ printf "\\nRebooting system...\\n"
+ $SUDO sleep 3
+ $SUDO shutdown -r now
+ fi
}
-checkhostname(){
-###Checks for hostnamesize
- host_name=$(hostname -s)
- if [[ ! ${#host_name} -le 28 ]]; then
- until [[ ${#host_name} -le 28 && $host_name =~ ^[a-zA-Z0-9][a-zA-Z0-9-]{1,28}$ ]]; do
- host_name=$(whiptail --inputbox "Your hostname is too long.\nEnter new hostname with less then 28 characters\nNo special characters allowed." \
- --title "Hostname too long" ${r} ${c} 3>&1 1>&2 2>&3)
- $SUDO hostnamectl set-hostname "${host_name}"
- if [[ ${#host_name} -le 28 && $host_name =~ ^[a-zA-Z0-9][a-zA-Z0-9-]{1,28}$ ]]; then
- echo "::: Hostname valid and length OK, proceeding..."
- fi
-
- done
- else
- echo "::: Hostname length OK"
- fi
-
-}
-
-
-######## SCRIPT ############
-
-main() {
-
- ######## FIRST CHECK ########
- # Must be root to install
- echo ":::"
- if [[ $EUID -eq 0 ]];then
- echo "::: You are root."
- else
- echo "::: sudo will be used for the install."
- # Check if it is actually installed
- # If it isn't, exit because the install cannot complete
- if [[ $(dpkg-query -s sudo) ]];then
- export SUDO="sudo"
- export SUDOE="sudo -E"
- else
- echo "::: Please install sudo or run this as root."
- exit 1
- fi
- fi
-
-
- # Check for supported distribution
- distro_check
-
- # Checks for hostname Length
- checkhostname
-
- # Check arguments for the undocumented flags
- for var in "$@"; do
- case "$var" in
- "--reconfigure" ) reconfigure=true;;
- "--i_do_not_follow_recommendations" ) skipSpaceCheck=false;;
- "--unattended" ) runUnattended=true;;
- esac
- done
-
- if [[ -f ${setupVars} ]]; then
- if [[ "${runUnattended}" == true ]]; then
- echo "::: --unattended passed to install script, no whiptail dialogs will be displayed"
- useUpdateVars=true
- else
- update_dialogs
- fi
- fi
-
- # Start the installer
- # Verify there is enough disk space for the install
- if [[ "${skipSpaceCheck}" == true ]]; then
- echo "::: --i_do_not_follow_recommendations passed to script, skipping free disk space verification!"
- else
- verifyFreeDiskSpace
- fi
-
- # Install the packages (we do this first because we need whiptail)
- addSoftwareRepo
-
- update_package_cache
-
- # Notify user of package availability
- notify_package_updates_available
-
-
- # Install packages used by this installation script
- install_dependent_packages PIVPN_DEPS[@]
-
- if [[ ${useUpdateVars} == false ]]; then
- # Display welcome dialogs
- welcomeDialogs
-
- # Find interfaces and let the user choose one
- chooseInterface
-
- # Only try to set static on Raspbian, otherwise let user do it
- if [[ $PLAT != "Raspbian" ]]; then
- avoidStaticIPv4Ubuntu
- else
- getStaticIPv4Settings
- setStaticIPv4
- fi
-
- # Choose the user for the ovpns
- chooseUser
-
- # Ask if unattended-upgrades will be enabled
- unattendedUpgrades
-
- # Clone/Update the repos
- clone_or_update_repos
-
- # Install
- if installPiVPN; then
- echo "::: Install Complete..."
- else
- exit 1
- fi
- else
- # Source ${setupVars} for use in the rest of the functions.
- source ${setupVars}
-
- echo "::: Using IP address: $IPv4addr"
- echo "${IPv4addr%/*}" > /tmp/pivpnIP
- echo "::: Using interface: $pivpnInterface"
- echo "${pivpnInterface}" > /tmp/pivpnINT
- echo "::: Using User: $INSTALL_USER"
- echo "${INSTALL_USER}" > /tmp/INSTALL_USER
- echo "::: Using protocol: $pivpnProto"
- echo "${pivpnProto}" > /tmp/pivpnPROTO
- echo "::: Using port: $PORT"
- echo ${PORT} > /tmp/INSTALL_PORT
- echo ":::"
-
- # Only try to set static on Raspbian
- case ${PLAT} in
- Rasp*)
- setStaticIPv4 # This might be a problem if a user tries to modify the ip in the config file and then runs an update because of the way we check for previous configuration in /etc/dhcpcd.conf
- ;;
- *)
- echo "::: IP Information"
- echo "::: Since we think you are not using Raspbian, we will not configure a static IP for you."
- echo "::: If you are in Amazon then you can not configure a static IP anyway."
- echo "::: Just ensure before this installer started you had set an elastic IP on your instance."
- ;;
- esac
-
- # Clone/Update the repos
- clone_or_update_repos
-
-
- updatePiVPN
- fi
-
- echo "::: Restarting services..."
- # Start services
- case ${PLAT} in
- Ubuntu|Debian|*vuan)
- $SUDO service openvpn start
- ;;
- *)
- $SUDO systemctl enable openvpn.service
- $SUDO systemctl start openvpn.service
- ;;
- esac
-
- # Ensure that cached writes reach persistent storage
- echo "::: Flushing writes to disk..."
- sync
-
- echo "::: done."
-
- if [[ "${useUpdateVars}" == false ]]; then
- displayFinalMessage
- fi
-
- echo ":::"
- if [[ "${useUpdateVars}" == false ]]; then
- echo "::: Installation Complete!"
- echo "::: Now run 'pivpn add' to create an ovpn profile for each of your devices."
- echo "::: Run 'pivpn help' to see what else you can do!"
- echo "::: It is strongly recommended you reboot after installation."
- else
- echo "::: Update complete!"
- fi
-
- echo ":::"
-}
-
-if [[ "${PIVPN_TEST}" != true ]] ; then
- main "$@"
-fi
+main "$@"
diff --git a/files/etc/unbound/unbound.conf.d/recursive_verifying_resolver.conf b/files/etc/unbound/unbound.conf.d/recursive_verifying_resolver.conf
new file mode 100644
index 0000000..d40a2bd
--- /dev/null
+++ b/files/etc/unbound/unbound.conf.d/recursive_verifying_resolver.conf
@@ -0,0 +1,59 @@
+server:
+ # If no logfile is specified, syslog is used
+ logfile: "/var/log/unbound/unbound.log"
+ verbosity: 0
+ #interface: 127.0.0.1
+ interface: 10.8.0.1
+ access-control: 127.0.0.0/8 allow
+### FIXME: once config storage is up, use pivpnNet
+ access-control: 10.6.0.0/8 allow
+ access-control: 10.8.0.0/8 allow
+
+ #port: 53
+ do-ip4: yes
+ do-udp: yes
+ do-tcp: yes
+
+ # May be set to yes if you have IPv6 connectivity
+ do-ip6: no
+
+ # Use this only when you downloaded the list of primary root servers!
+ # Download:
+ # wget https://www.internic.net/domain/named.root -O /var/lib/unbound/root.hints
+ # curl https://www.internic.net/domain/named.root -o /var/lib/unbound/root.hints
+ root-hints: "/var/lib/unbound/root.hints"
+
+ # Trust glue only if it is within the servers authority
+ harden-glue: yes
+
+ # Require DNSSEC data for trust-anchored zones, if such data is absent, the zone becomes BOGUS
+ harden-dnssec-stripped: yes
+
+ # Don't use Capitalization randomization as it is known to cause DNSSEC issues sometimes
+ # see https://discourse.pi-hole.net/t/unbound-stubby-or-dnscrypt-proxy/9378 for further details
+ use-caps-for-id: no
+
+ # Reduce EDNS reassembly buffer size.
+ # Suggested by the unbound man page to reduce fragmentation reassembly problems
+ edns-buffer-size: 1472
+
+ # Perform prefetching of close to expired message cache entries
+ # This only applies to domains that have been frequently queried
+ prefetch: yes
+
+ # One thread should be sufficient, can be increased on beefy machines. In reality for most users running on small networks or on a single machine it should be unnecessary to seek performance enhancement by increasing num-threads above 1.
+ num-threads: 1
+
+ # Ensure kernel buffer is large enough to not lose messages in traffic spikes
+ so-rcvbuf: 1m
+
+ # Ensure privacy of local IP ranges
+ private-address: 192.168.0.0/16
+ private-address: 169.254.0.0/16
+ private-address: 172.16.0.0/12
+ private-address: 10.0.0.0/8
+ private-address: fd00::/8
+ private-address: fe80::/10
+
+### EOF
+### vim:tw=256:et:sts=2:st=2:sw=2:com+=b\:###:fo+=cqtr:tags=ctags:
diff --git a/logos.jpg b/logos.jpg
new file mode 100644
index 0000000..890fec9
Binary files /dev/null and b/logos.jpg differ
diff --git a/scripts/backup.sh b/scripts/backup.sh
index 61cc6cf..a8e6a1d 100755
--- a/scripts/backup.sh
+++ b/scripts/backup.sh
@@ -1,31 +1,52 @@
#!/bin/bash
-install_user=$(1
- backupzip=$date-pivpnbackup.tgz
- tar -czf $backupzip -C ${install_home} $backupdir 2&>1
- echo -e "Backup crated to $install_home/$backupdir/$backupzip \nTo restore the backup, follow instructions at:\nhttps://github.com/pivpn/pivpn/wiki/FAQ#how-can-i-migrate-my-configs-to-another-pivpn-instance"
}
+backup_openvpn(){
+
+ openvpndir=/etc/openvpn
+ ovpnsdir=${install_home}/ovpns
+ checkbackupdir
+ backupzip=$date-pivpnovpnbackup.tgz
+ # shellcheck disable=SC2210
+ tar czpf "$install_home"/"$backupdir"/"$backupzip" "$openvpndir" "$ovpnsdir" > /dev/null 2>&1
+ echo -e "Backup crated to $install_home/$backupdir/$backupzip \nTo restore the backup, follow instructions at:\nhttps://github.com/pivpn/pivpn/wiki/FAQ#how-can-i-migrate-my-configs-to-another-pivpn-instance"
+
+}
+
+backup_wireguard(){
+
+ wireguarddir=/etc/wireguard
+ configsdir=${install_home}/configs
+ checkbackupdir
+ backupzip=$date-pivpnwgbackup.tgz
+ tar czpf "$install_home"/"$backupdir"/"$backupzip" "$wireguarddir" "$configsdir" > /dev/null 2>&1
+ echo -e "Backup crated to $install_home/$backupdir/$backupzip \nTo restore the backup, follow instructions at:\nhttps://github.com/pivpn/pivpn/wiki/FAQ#how-can-i-migrate-my-configs-to-another-pivpn-instance"
+
+}
if [[ ! $EUID -eq 0 ]];then
- if [[ $(dpkg-query -s sudo) ]];then
+ if [[ $(dpkg-query -s sudo) ]];then
export SUDO="sudo"
- else
+ else
echo "::: Please install sudo or run this as root."
exit 0
fi
fi
-backup_openvpn
-
+if [[ "${VPN}" == "wireguard" ]]; then
+ backup_wireguard
+else
+ backup_openvpn
+fi
diff --git a/scripts/bash-completion b/scripts/openvpn/bash-completion
similarity index 83%
rename from scripts/bash-completion
rename to scripts/openvpn/bash-completion
index f317bc4..1b9ef7e 100644
--- a/scripts/bash-completion
+++ b/scripts/openvpn/bash-completion
@@ -4,8 +4,8 @@ _pivpn()
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
- dashopts="-a -c -d -l -r -h -u -up"
- opts="debug add clients list revoke uninstall help update"
+ dashopts="-a -c -d -l -r -h -u -up -bk"
+ opts="debug add clients list revoke uninstall help update backup"
if [ "${#COMP_WORDS[@]}" -eq 2 ]
then
if [[ ${cur} == -* ]] ; then
diff --git a/scripts/clientStat.sh b/scripts/openvpn/clientStat.sh
similarity index 68%
rename from scripts/clientStat.sh
rename to scripts/openvpn/clientStat.sh
index 2af420f..ef04114 100755
--- a/scripts/clientStat.sh
+++ b/scripts/openvpn/clientStat.sh
@@ -17,15 +17,17 @@ printf ": NOTE : The output below is NOT real-time!\n"
printf ": : It may be off by a few minutes.\n"
printf "\n"
printf "\e[1m::: Client Status List :::\e[0m\n"
-printf "\t\t\t\t\t\t\t\tBytes\t\tBytes\t\n"
-printf "\e[4mName\e[0m\t\t\t\e[4mRemote IP\e[0m\t\t\e[4mVirtual IP\e[0m\t\e[4mReceived\e[0m\t\e[4mSent\e[0m\t\t\e[4mConnected Since\e[0m \n"
+
+{
+printf "\e[4mName\e[0m \t \e[4mRemote IP\e[0m \t \e[4mVirtual IP\e[0m \t \e[4mBytes Received\e[0m \t \e[4mBytes Sent\e[0m \t \e[4mConnected Since\e[0m\n"
+
if grep -q "^CLIENT_LIST" "${STATUS_LOG}"; then
if [ -n "$(type -t numfmt)" ]; then
while read -r line; do
read -r -a array <<< $line
[[ ${array[0]} = CLIENT_LIST ]] || continue
- printf "%s\t\t%s\t%s\t%s\t\t%s\t\t%s %s %s - %s\n" ${array[1]} ${array[2]} ${array[3]} $(hr ${array[4]}) $(hr ${array[5]}) ${array[7]} ${array[8]} ${array[10]} ${array[9]}
- done <$STATUS_LOG
+ printf "%s \t %s \t %s \t %s \t %s \t %s %s %s - %s\n" ${array[1]} ${array[2]} ${array[3]} $(hr ${array[4]}) $(hr ${array[5]}) ${array[7]} ${array[8]} ${array[10]} ${array[9]}
+ done <$STATUS_LOG
else
awk -F' ' -v s='CLIENT_LIST' '$1 == s {print $2"\t\t"$3"\t"$4"\t"$5"\t\t"$6"\t\t"$8" "$9" "$11" - "$10"\n"}' ${STATUS_LOG}
fi
@@ -33,3 +35,4 @@ else
printf "\nNo Clients Connected!\n"
fi
printf "\n"
+} | column -t -s $'\t'
\ No newline at end of file
diff --git a/scripts/listOVPN.sh b/scripts/openvpn/listOVPN.sh
similarity index 100%
rename from scripts/listOVPN.sh
rename to scripts/openvpn/listOVPN.sh
diff --git a/scripts/makeOVPN.sh b/scripts/openvpn/makeOVPN.sh
similarity index 89%
rename from scripts/makeOVPN.sh
rename to scripts/openvpn/makeOVPN.sh
index 31497b3..7459164 100755
--- a/scripts/makeOVPN.sh
+++ b/scripts/openvpn/makeOVPN.sh
@@ -1,6 +1,7 @@
#!/bin/bash
# Create OVPN Client
# Default Variable Declarations
+setupVars="/etc/pivpn/setupVars.conf"
DEFAULT="Default.txt"
FILEEXT=".ovpn"
CRT=".crt"
@@ -8,9 +9,13 @@ KEY=".key"
CA="ca.crt"
TA="ta.key"
INDEX="/etc/openvpn/easy-rsa/pki/index.txt"
-INSTALL_USER=$(> "$setupVars"
fi
# Parse input arguments
@@ -70,13 +75,7 @@ do
DAYS="$_val"
;;
-i|--iOS)
- OVPN12_Compatible=$(cat /etc/pivpn/OVPN12_Compatible)
- if [[ ${OVPN12_Compatible} == "true" ]]; then
- iOS=1
- else
- echo "This function is not compatable with OVPN 2.4"
- exit 1
- fi
+ iOS=1
;;
-h|--help)
helpFunc
@@ -213,9 +212,10 @@ EOF
}
#make sure ovpns dir exists
-if [ ! -d "$INSTALL_HOME/ovpns" ]; then
- mkdir "$INSTALL_HOME/ovpns"
- chmod 0750 "$INSTALL_HOME/ovpns"
+if [ ! -d "$install_home/ovpns" ]; then
+ mkdir "$install_home/ovpns"
+ chown "$install_user":"$install_user" "$install_home/ovpns"
+ chmod 0750 "$install_home/ovpns"
fi
#bitWarden
@@ -341,29 +341,23 @@ if [ "$iOS" = "1" ]; then
sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' < "issued/${NAME}${CRT}"
echo ""
- #Finally, append the TA Private Key
- if [ -f /etc/pivpn/TWO_POINT_FOUR ]; then
- echo ""
- cat "${TA}"
- echo ""
- else
- echo ""
- cat "${TA}"
- echo ""
- fi
+ #Finally, append the tls Private Key
+ echo ""
+ cat "${TA}"
+ echo ""
} > "${NAME}${FILEEXT}"
-
+
# Copy the .ovpn profile to the home directory for convenient remote access
-
+
printf "========================================================\n"
printf "Generating an .ovpn12 file for use with iOS devices\n"
printf "Please remember the export password\n"
printf "as you will need this import the certificate on your iOS device\n"
printf "========================================================\n"
- openssl pkcs12 -passin pass:"$PASSWD_UNESCAPED" -export -in "issued/${NAME}${CRT}" -inkey "private/${NAME}${KEY}" -certfile ${CA} -name "${NAME}" -out "/home/$INSTALL_USER/ovpns/$NAME.ovpn12"
- chown "$INSTALL_USER" "/home/$INSTALL_USER/ovpns/$NAME.ovpn12"
- chmod 600 "/home/$INSTALL_USER/ovpns/$NAME.ovpn12"
+ openssl pkcs12 -passin pass:"$PASSWD_UNESCAPED" -export -in "issued/${NAME}${CRT}" -inkey "private/${NAME}${KEY}" -certfile ${CA} -name "${NAME}" -out "$install_home/ovpns/$NAME.ovpn12"
+ chown "$install_user":"$install_user" "$install_home/ovpns/$NAME.ovpn12"
+ chmod 640 "$install_home/ovpns/$NAME.ovpn12"
printf "========================================================\n"
printf "\e[1mDone! %s successfully created!\e[0m \n" "$NAME.ovpn12"
printf "You will need to transfer both the .ovpn and .ovpn12 files\n"
@@ -392,15 +386,9 @@ else
echo ""
#Finally, append the tls Private Key
- if [ -f /etc/pivpn/TWO_POINT_FOUR ]; then
- echo ""
- cat "${TA}"
- echo ""
- else
- echo ""
- cat "${TA}"
- echo ""
- fi
+ echo ""
+ cat "${TA}"
+ echo ""
} > "${NAME}${FILEEXT}"
@@ -408,15 +396,15 @@ fi
# Copy the .ovpn profile to the home directory for convenient remote access
-cp "/etc/openvpn/easy-rsa/pki/$NAME$FILEEXT" "$INSTALL_HOME/ovpns/$NAME$FILEEXT"
-chown "$INSTALL_USER" "$INSTALL_HOME/ovpns/$NAME$FILEEXT"
+cp "/etc/openvpn/easy-rsa/pki/$NAME$FILEEXT" "$install_home/ovpns/$NAME$FILEEXT"
+chown "$install_user":"$install_user" "$install_home/ovpns/$NAME$FILEEXT"
chmod 640 "/etc/openvpn/easy-rsa/pki/$NAME$FILEEXT"
-chmod 640 "$INSTALL_HOME/ovpns/$NAME$FILEEXT"
+chmod 640 "$install_home/ovpns/$NAME$FILEEXT"
printf "\n\n"
printf "========================================================\n"
printf "\e[1mDone! %s successfully created!\e[0m \n" "$NAME$FILEEXT"
printf "%s was copied to:\n" "$NAME$FILEEXT"
-printf " %s/ovpns\n" "$INSTALL_HOME"
+printf " %s/ovpns\n" "$install_home"
printf "for easy transfer. Please use this profile only on one\n"
printf "device and create additional profiles for other devices.\n"
printf "========================================================\n\n"
diff --git a/pivpn b/scripts/openvpn/pivpn
old mode 100644
new mode 100755
similarity index 98%
rename from pivpn
rename to scripts/openvpn/pivpn
index de55586..c18049f
--- a/pivpn
+++ b/scripts/openvpn/pivpn
@@ -6,7 +6,7 @@ if [[ ! $EUID -eq 0 ]];then
export SUDO="sudo"
else
echo "::: Please install sudo or run this as root."
- exit 0
+ exit 1
fi
fi
@@ -62,7 +62,7 @@ function update {
function backup {
- $SUDO /opt/pivpn/backup.sh
+ $SUDO /opt/pivpn/backup.sh
exit 0
}
diff --git a/scripts/pivpnDebug.sh b/scripts/openvpn/pivpnDebug.sh
similarity index 67%
rename from scripts/pivpnDebug.sh
rename to scripts/openvpn/pivpnDebug.sh
index 25a4bf1..df4e938 100755
--- a/scripts/pivpnDebug.sh
+++ b/scripts/openvpn/pivpnDebug.sh
@@ -1,16 +1,16 @@
#!/usr/bin/env bash
# This scripts runs as root
-PORT=$(cat /etc/pivpn/INSTALL_PORT)
-PROTO=$(cat /etc/pivpn/INSTALL_PROTO)
-IPv4dev="$(cat /etc/pivpn/pivpnINTERFACE)"
-REMOTE="$(grep 'remote ' /etc/openvpn/easy-rsa/pki/Default.txt | awk '{print $2}')"
-NO_UFW=$(cat /etc/pivpn/NO_UFW)
-OLD_UFW=$(cat /etc/pivpn/NO_UFW)
-INPUT_CHAIN_EDITED="$(cat /etc/pivpn/INPUT_CHAIN_EDITED)"
-FORWARD_CHAIN_EDITED="$(cat /etc/pivpn/FORWARD_CHAIN_EDITED)"
+setupVars="/etc/pivpn/setupVars.conf"
ERR=0
+if [ ! -f "${setupVars}" ]; then
+ echo "::: Missing setup vars file!"
+ exit 1
+fi
+
+source "${setupVars}"
+
echo -e "::::\t\t\e[4mPiVPN debug\e[0m\t\t ::::"
printf "=============================================\n"
echo -e "::::\t\t\e[4mLatest commit\e[0m\t\t ::::"
@@ -25,13 +25,13 @@ for filename in /etc/pivpn/*; do
done
printf "=============================================\n"
echo -e "::::\t\e[4msetupVars file shown below\e[0m\t ::::"
-sed "s/$REMOTE/REMOTE/" < /etc/pivpn/setupVars.conf
+sed "s/$pivpnHOST/REDACTED/" < /etc/pivpn/setupVars.conf
printf "=============================================\n"
echo -e ":::: \e[4mServer configuration shown below\e[0m ::::"
cat /etc/openvpn/server.conf
printf "=============================================\n"
echo -e ":::: \e[4mClient template file shown below\e[0m ::::"
-sed "s/$REMOTE/REMOTE/" < /etc/openvpn/easy-rsa/pki/Default.txt
+sed "s/$pivpnHOST/REDACTED/" < /etc/openvpn/easy-rsa/pki/Default.txt
printf "=============================================\n"
echo -e ":::: \t\e[4mRecursive list of files in\e[0m\t ::::\n::: \e[4m/etc/openvpn/easy-rsa/pki shows below\e[0m :::"
ls -LR /etc/openvpn/easy-rsa/pki/ -Ireqs -Icerts_by_serial
@@ -43,38 +43,36 @@ if [ "$(cat /proc/sys/net/ipv4/ip_forward)" -eq 1 ]; then
else
ERR=1
read -r -p ":: [ERR] IP forwarding is not enabled, attempt fix now? [Y/n] " REPLY
- if [[ ${REPLY} =~ ^[Yy]$ ]]; then
+ if [[ ${REPLY} =~ ^[Yy]$ ]] || [[ -z ${REPLY} ]]; then
sed -i '/net.ipv4.ip_forward=1/s/^#//g' /etc/sysctl.conf
sysctl -p
echo "Done"
fi
fi
-if [ "$NO_UFW" -eq 1 ]; then
+if [ "$USING_UFW" -eq 0 ]; then
- if iptables -t nat -C POSTROUTING -s 10.8.0.0/24 -o "${IPv4dev}" -j MASQUERADE &> /dev/null; then
+ if iptables -t nat -C POSTROUTING -s 10.8.0.0/24 -o "${IPv4dev}" -j MASQUERADE -m comment --comment "${VPN}-nat-rule" &> /dev/null; then
echo ":: [OK] Iptables MASQUERADE rule set"
else
ERR=1
read -r -p ":: [ERR] Iptables MASQUERADE rule is not set, attempt fix now? [Y/n] " REPLY
- if [[ ${REPLY} =~ ^[Yy]$ ]]; then
- iptables -t nat -F
- iptables -t nat -I POSTROUTING -s 10.8.0.0/24 -o "${IPv4dev}" -j MASQUERADE
+ if [[ ${REPLY} =~ ^[Yy]$ ]] || [[ -z ${REPLY} ]]; then
+ iptables -t nat -I POSTROUTING -s 10.8.0.0/24 -o "${IPv4dev}" -j MASQUERADE -m comment --comment "${VPN}-nat-rule"
iptables-save > /etc/iptables/rules.v4
echo "Done"
fi
fi
-
if [ "$INPUT_CHAIN_EDITED" -eq 1 ]; then
- if iptables -C INPUT -i "$IPv4dev" -p "$PROTO" --dport "$PORT" -j ACCEPT &> /dev/null; then
+ if iptables -C INPUT -i "$IPv4dev" -p "$pivpnPROTO" --dport "$pivpnPORT" -j ACCEPT -m comment --comment "${VPN}-input-rule" &> /dev/null; then
echo ":: [OK] Iptables INPUT rule set"
else
ERR=1
read -r -p ":: [ERR] Iptables INPUT rule is not set, attempt fix now? [Y/n] " REPLY
- if [[ ${REPLY} =~ ^[Yy]$ ]]; then
- iptables -I INPUT 1 -i "$IPv4dev" -p "$PROTO" --dport "$PORT" -j ACCEPT
+ if [[ ${REPLY} =~ ^[Yy]$ ]] || [[ -z ${REPLY} ]]; then
+ iptables -I INPUT 1 -i "$IPv4dev" -p "$pivpnPROTO" --dport "$pivpnPORT" -j ACCEPT -m comment --comment "${VPN}-input-rule"
iptables-save > /etc/iptables/rules.v4
echo "Done"
fi
@@ -83,14 +81,14 @@ if [ "$NO_UFW" -eq 1 ]; then
if [ "$FORWARD_CHAIN_EDITED" -eq 1 ]; then
- if iptables -C FORWARD -s 10.8.0.0/24 -i tun0 -o "$IPv4dev" -j ACCEPT &> /dev/null; then
+ if iptables -C FORWARD -s 10.8.0.0/24 -i tun0 -o "$IPv4dev" -j ACCEPT -m comment --comment "${VPN}-forward-rule" &> /dev/null; then
echo ":: [OK] Iptables FORWARD rule set"
else
ERR=1
read -r -p ":: [ERR] Iptables FORWARD rule is not set, attempt fix now? [Y/n] " REPLY
- if [[ ${REPLY} =~ ^[Yy]$ ]]; then
- iptables -I FORWARD 1 -d 10.8.0.0/24 -i "$IPv4dev" -o tun0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
- iptables -I FORWARD 2 -s 10.8.0.0/24 -i tun0 -o "$IPv4dev" -j ACCEPT
+ if [[ ${REPLY} =~ ^[Yy]$ ]] || [[ -z ${REPLY} ]]; then
+ iptables -I FORWARD 1 -d 10.8.0.0/24 -i "$IPv4dev" -o tun0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -m comment --comment "${VPN}-forward-rule"
+ iptables -I FORWARD 2 -s 10.8.0.0/24 -i tun0 -o "$IPv4dev" -j ACCEPT -m comment --comment "${VPN}-forward-rule"
iptables-save > /etc/iptables/rules.v4
echo "Done"
fi
@@ -104,59 +102,44 @@ else
else
ERR=1
read -r -p ":: [ERR] Ufw is not enabled, try to enable now? [Y/n] " REPLY
- if [[ ${REPLY} =~ ^[Yy]$ ]]; then
+ if [[ ${REPLY} =~ ^[Yy]$ ]] || [[ -z ${REPLY} ]]; then
ufw enable
fi
fi
- if iptables -t nat -C POSTROUTING -s 10.8.0.0/24 -o "${IPv4dev}" -j MASQUERADE &> /dev/null; then
+ if iptables -t nat -C POSTROUTING -s 10.8.0.0/24 -o "${IPv4dev}" -j MASQUERADE -m comment --comment "${VPN}-nat-rule" &> /dev/null; then
echo ":: [OK] Iptables MASQUERADE rule set"
else
ERR=1
read -r -p ":: [ERR] Iptables MASQUERADE rule is not set, attempt fix now? [Y/n] " REPLY
- if [[ ${REPLY} =~ ^[Yy]$ ]]; then
- sed "/delete these required/i *nat\n:POSTROUTING ACCEPT [0:0]\n-I POSTROUTING -s 10.8.0.0/24 -o $IPv4dev -j MASQUERADE\nCOMMIT\n" -i /etc/ufw/before.rules
+ if [[ ${REPLY} =~ ^[Yy]$ ]] || [[ -z ${REPLY} ]]; then
+ sed "/delete these required/i *nat\n:POSTROUTING ACCEPT [0:0]\n-I POSTROUTING -s 10.8.0.0/24 -o $IPv4dev -j MASQUERADE -m comment --comment ${VPN}-nat-rule\nCOMMIT\n" -i /etc/ufw/before.rules
ufw reload
echo "Done"
fi
fi
- if iptables -C ufw-user-input -p "${PROTO}" --dport "${PORT}" -j ACCEPT &> /dev/null; then
+ if iptables -C ufw-user-input -p "${pivpnPROTO}" --dport "${pivpnPORT}" -j ACCEPT &> /dev/null; then
echo ":: [OK] Ufw input rule set"
else
ERR=1
read -r -p ":: [ERR] Ufw input rule is not set, attempt fix now? [Y/n] " REPLY
- if [[ ${REPLY} =~ ^[Yy]$ ]]; then
- ufw insert 1 allow "$PORT"/"$PROTO"
+ if [[ ${REPLY} =~ ^[Yy]$ ]] || [[ -z ${REPLY} ]]; then
+ ufw insert 1 allow "$pivpnPORT"/"$pivpnPROTO"
ufw reload
echo "Done"
fi
fi
- if [ "$OLD_UFW" -eq 1 ]; then
- FORWARD_POLICY="$(iptables -S FORWARD | grep '^-P' | awk '{print $3}')"
- if [ "$FORWARD_POLICY" = "ACCEPT" ]; then
- echo ":: [OK] Ufw forwarding policy is accept"
- else
- ERR=1
- read -r -p ":: [ERR] Ufw forwarding policy is not 'ACCEPT', attempt fix now? [Y/n] " REPLY
- if [[ ${REPLY} =~ ^[Yy]$ ]]; then
- sed -i "s/\(DEFAULT_FORWARD_POLICY=\).*/\1\"ACCEPT\"/" /etc/default/ufw
- ufw reload > /dev/null
- echo "Done"
- fi
- fi
+ if iptables -C ufw-user-forward -i tun0 -o "${IPv4dev}" -s 10.8.0.0/24 -j ACCEPT &> /dev/null; then
+ echo ":: [OK] Ufw forwarding rule set"
else
- if iptables -C ufw-user-forward -i tun0 -o "${IPv4dev}" -s 10.8.0.0/24 -j ACCEPT &> /dev/null; then
- echo ":: [OK] Ufw forwarding rule set"
- else
- ERR=1
- read -r -p ":: [ERR] Ufw forwarding rule is not set, attempt fix now? [Y/n] " REPLY
- if [[ ${REPLY} =~ ^[Yy]$ ]]; then
- ufw route insert 1 allow in on tun0 from 10.8.0.0/24 out on "$IPv4dev" to any
- ufw reload
- echo "Done"
- fi
+ ERR=1
+ read -r -p ":: [ERR] Ufw forwarding rule is not set, attempt fix now? [Y/n] " REPLY
+ if [[ ${REPLY} =~ ^[Yy]$ ]] || [[ -z ${REPLY} ]]; then
+ ufw route insert 1 allow in on tun0 from 10.8.0.0/24 out on "$IPv4dev" to any
+ ufw reload
+ echo "Done"
fi
fi
@@ -167,7 +150,7 @@ if systemctl is-active -q openvpn; then
else
ERR=1
read -r -p ":: [ERR] OpenVPN is not running, try to start now? [Y/n] " REPLY
- if [[ ${REPLY} =~ ^[Yy]$ ]]; then
+ if [[ ${REPLY} =~ ^[Yy]$ ]] || [[ -z ${REPLY} ]]; then
systemctl start openvpn
echo "Done"
fi
@@ -178,19 +161,19 @@ if systemctl is-enabled -q openvpn; then
else
ERR=1
read -r -p ":: [ERR] OpenVPN is not enabled, try to enable now? [Y/n] " REPLY
- if [[ ${REPLY} =~ ^[Yy]$ ]]; then
+ if [[ ${REPLY} =~ ^[Yy]$ ]] || [[ -z ${REPLY} ]]; then
systemctl enable openvpn
echo "Done"
fi
fi
-# grep -w (whole word) is used so port 111940 with now match when looking for 1194
-if netstat -uanpt | grep openvpn | grep -w "${PORT}" | grep -q "${PROTO}"; then
- echo ":: [OK] OpenVPN is listening on port ${PORT}/${PROTO}"
+# grep -w (whole word) is used so port 11940 won't match when looking for 1194
+if netstat -uanpt | grep openvpn | grep -w "${pivpnPORT}" | grep -q "${pivpnPROTO}"; then
+ echo ":: [OK] OpenVPN is listening on port ${pivpnPORT}/${pivpnPROTO}"
else
ERR=1
read -r -p ":: [ERR] OpenVPN is not listening, try to restart now? [Y/n] " REPLY
- if [[ ${REPLY} =~ ^[Yy]$ ]]; then
+ if [[ ${REPLY} =~ ^[Yy]$ ]] || [[ -z ${REPLY} ]]; then
systemctl restart openvpn
echo "Done"
fi
@@ -205,7 +188,7 @@ echo -e ":::: \e[4mSnippet of the server log\e[0m ::::"
tail -20 /var/log/openvpn.log > /tmp/snippet
# Regular expession taken from https://superuser.com/a/202835, it will match invalid IPs
-# like 123.456.789.012 but it's fine because the log only contains valid ones.
+# like 123.456.789.012 but it's fine since the log only contains valid ones.
declare -a IPS_TO_HIDE=($(grepcidr -v 10.0.0.0/8,172.16.0.0/12,192.168.0.0/16 /tmp/snippet | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | uniq))
for IP in "${IPS_TO_HIDE[@]}"; do
sed -i "s/$IP/REDACTED/g" /tmp/snippet
diff --git a/scripts/removeOVPN.sh b/scripts/openvpn/removeOVPN.sh
similarity index 88%
rename from scripts/removeOVPN.sh
rename to scripts/openvpn/removeOVPN.sh
index b2d726f..f191eab 100755
--- a/scripts/removeOVPN.sh
+++ b/scripts/openvpn/removeOVPN.sh
@@ -1,10 +1,16 @@
#!/usr/bin/env bash
# PiVPN: revoke client script
-INSTALL_USER=$(/dev/null || echo 24 80)
-rows=$(echo $screen_size | awk '{print $1}')
-columns=$(echo $screen_size | awk '{print $2}')
+rows=$(echo "$screen_size" | awk '{print $1}')
+columns=$(echo "$screen_size" | awk '{print $2}')
# Divide by two so the dialogs take up half of the screen, which looks nice.
r=$(( rows / 2 ))
@@ -26,128 +28,207 @@ c=$(( columns / 2 ))
r=$(( r < 20 ? 20 : r ))
c=$(( c < 70 ? 70 : c ))
-spinner()
-{
- local pid=$1
- local delay=0.50
- local spinstr='/-\|'
- while [ "$(ps a | awk '{print $1}' | grep "$pid")" ]; do
- local temp=${spinstr#?}
- printf " [%c] " "$spinstr"
- local spinstr=$temp${spinstr%"$temp"}
- sleep $delay
- printf "\b\b\b\b\b\b"
- done
- printf " \b\b\b\b"
+### FIXME: introduce global lib
+spinner(){
+ local pid=$1
+ local delay=0.50
+ local spinstr='/-\|'
+ while ps a | awk '{print $1}' | grep "$pid"; do
+ local temp=${spinstr#?}
+ printf " [%c] " "$spinstr"
+ local spinstr=$temp${spinstr%"$temp"}
+ sleep $delay
+ printf "\\b\\b\\b\\b\\b\\b"
+ done
+ printf " \\b\\b\\b\\b"
}
-function removeAll {
- # Purge dependencies
-echo ":::"
- dependencies=( openvpn easy-rsa git iptables-persistent dnsutils expect unattended-upgrades )
- for i in "${dependencies[@]}"; do
- if [ "$(dpkg-query -W --showformat='${Status}\n' "$i" 2> /dev/null | grep -c "ok installed")" -eq 1 ]; then
- while true; do
- read -rp "::: Do you wish to remove $i from your system? [y/n]: " yn
- case $yn in
- [Yy]* ) printf ":::\tRemoving %s..." "$i"; $PKG_MANAGER -y remove --purge "$i" &> /dev/null & spinner $!; printf "done!\n";
- if [ "$i" == "openvpn" ]; then UINST_OVPN=1 ; fi
- if [ "$i" == "unattended-upgrades" ]; then UINST_UNATTUPG=1 ; fi
- break;;
- [Nn]* ) printf ":::\tSkipping %s\n" "$i"; break;;
- * ) printf "::: You must answer yes or no!\n";;
- esac
- done
- else
- printf ":::\tPackage %s not installed... Not removing.\n" "$i"
- fi
- done
+removeAll(){
+ # Stopping and disabling services
+ echo "::: Stopping and disabling services..."
- # Take care of any additional package cleaning
- printf "::: Auto removing remaining dependencies..."
- $PKG_MANAGER -y autoremove &> /dev/null & spinner $!; printf "done!\n";
- printf "::: Auto cleaning remaining dependencies..."
- $PKG_MANAGER -y autoclean &> /dev/null & spinner $!; printf "done!\n";
+ if [ "$VPN" = "wireguard" ]; then
+ systemctl stop wg-quick@wg0
+ systemctl disable wg-quick@wg0 &> /dev/null
+ elif [ "$VPN" = "openvpn" ]; then
+ systemctl stop openvpn
+ systemctl disable openvpn &> /dev/null
+ fi
- echo ":::"
- # Removing pivpn files
- echo "::: Removing pivpn system files..."
+ # Removing firewall rules.
+ echo "::: Removing firewall rules..."
- $SUDO rm -rf /opt/pivpn &> /dev/null
- $SUDO rm -rf /etc/.pivpn &> /dev/null
- $SUDO rm -rf $INSTALL_HOME/ovpns &> /dev/null
+ ### FIXME: introduce global config space!
+ if [ "$VPN" = "wireguard" ]; then
+ pivpnPROTO="udp"
+ pivpnDEV="wg0"
+ pivpnNET="10.6.0.0"
+ elif [ "$VPN" = "openvpn" ]; then
+ pivpnDEV="tun0"
+ pivpnNET="10.8.0.0"
+ fi
- rm -rf /var/log/*pivpn* &> /dev/null
- rm -rf /var/log/*openvpn* &> /dev/null
- if [[ $UINST_OVPN = 1 ]]; then
- rm -rf /etc/openvpn &> /dev/null
- if [[ $PLAT == "Ubuntu" || $PLAT == "Debian" ]]; then
- printf "::: Removing openvpn apt source..."
- rm -rf /etc/apt/sources.list.d/swupdate.openvpn.net.list &> /dev/null
- $PKG_MANAGER -qq update & spinner $!; printf "done!\n";
- fi
- fi
- if [[ $UINST_UNATTUPG = 1 ]]; then
- rm -rf /var/log/unattended-upgrades
- rm -rf /etc/apt/apt.conf.d/*periodic
- fi
- rm -rf /etc/pivpn &> /dev/null
- rm /usr/local/bin/pivpn &> /dev/null
- rm /etc/bash_completion.d/pivpn
+ if [ "$USING_UFW" -eq 1 ]; then
- # Disable IPv4 forwarding
- sed -i '/net.ipv4.ip_forward=1/c\#net.ipv4.ip_forward=1' /etc/sysctl.conf
- sysctl -p
+ ### FIXME: SC2154
+ ufw delete allow "${pivpnPORT}"/"${pivpnPROTO}" > /dev/null
+ ### FIXME: SC2154
+ ufw route delete allow in on "${pivpnDEV}" from "${pivpnNET}/${subnetClass}" out on "${IPv4dev}" to any > /dev/null
+ sed -z "s/*nat\\n:POSTROUTING ACCEPT \\[0:0\\]\\n-I POSTROUTING -s ${pivpnNET}\\/${subnetClass} -o ${IPv4dev} -j MASQUERADE -m comment --comment ${VPN}-nat-rule\\nCOMMIT\\n\\n//" -i /etc/ufw/before.rules
+ iptables -t nat -D POSTROUTING -s "${pivpnNET}/${subnetClass}" -o "${IPv4dev}" -j MASQUERADE -m comment --comment "${VPN}-nat-rule"
+ ufw reload &> /dev/null
- if [[ $NO_UFW -eq 0 ]]; then
+ elif [ "$USING_UFW" -eq 0 ]; then
- sed -z "s/*nat\n:POSTROUTING ACCEPT \[0:0\]\n-I POSTROUTING -s 10.8.0.0\/24 -o $IPv4dev -j MASQUERADE\nCOMMIT\n\n//" -i /etc/ufw/before.rules
- ufw delete allow "$PORT"/"$PROTO" >/dev/null
- if [ "$OLD_UFW" -eq 1 ]; then
- sed -i "s/\(DEFAULT_FORWARD_POLICY=\).*/\1\"DROP\"/" /etc/default/ufw
- else
- ufw route delete allow in on tun0 from 10.8.0.0/24 out on "$IPv4dev" to any >/dev/null
- fi
- ufw reload >/dev/null
- else
- iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o "${IPv4dev}" -j MASQUERADE
+ if [ "$INPUT_CHAIN_EDITED" -eq 1 ]; then
+ iptables -D INPUT -i "${IPv4dev}" -p "${pivpnPROTO}" --dport "${pivpnPORT}" -j ACCEPT -m comment --comment "${VPN}-input-rule"
+ fi
- if [ "$INPUT_CHAIN_EDITED" -eq 1 ]; then
- iptables -D INPUT -i "$IPv4dev" -p "$PROTO" --dport "$PORT" -j ACCEPT
- fi
+ if [ "$FORWARD_CHAIN_EDITED" -eq 1 ]; then
+ iptables -D FORWARD -d "${pivpnNET}/${subnetClass}" -i "${IPv4dev}" -o "${pivpnDEV}" -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -m comment --comment "${VPN}-forward-rule"
+ iptables -D FORWARD -s "${pivpnNET}/${subnetClass}" -i "${pivpnDEV}" -o "${IPv4dev}" -j ACCEPT -m comment --comment "${VPN}-forward-rule"
+ fi
- if [ "$FORWARD_CHAIN_EDITED" -eq 1 ]; then
- iptables -D FORWARD -d 10.8.0.0/24 -i "$IPv4dev" -o tun0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
- iptables -D FORWARD -s 10.8.0.0/24 -i tun0 -o "$IPv4dev" -j ACCEPT
- fi
+ iptables -t nat -D POSTROUTING -s "${pivpnNET}/${subnetClass}" -o "${IPv4dev}" -j MASQUERADE -m comment --comment "${VPN}-nat-rule"
+ iptables-save > /etc/iptables/rules.v4
- iptables-save > /etc/iptables/rules.v4
- fi
+ fi
- echo ":::"
- printf "::: Finished removing PiVPN from your system.\n"
- printf "::: Reinstall by simpling running\n:::\n:::\tcurl -L https://install.pivpn.io | bash\n:::\n::: at any time!\n:::\n"
+ # Disable IPv4 forwarding
+ sed -i '/net.ipv4.ip_forward=1/c\#net.ipv4.ip_forward=1' /etc/sysctl.conf
+ sysctl -p
+
+ # Purge dependencies
+ echo "::: Purge dependencies..."
+
+ for i in "${TO_INSTALL[@]}"; do
+ while true; do
+ read -rp "::: Do you wish to remove $i from your system? [Y/n]: " yn
+ case $yn in
+ [Yy]* ) if [ "${i}" = "wireguard" ]; then
+
+ # On Debian and armv7l Raspbian, remove the unstable repo (on armv6l Raspbian
+ # there is no wireguard package). On Ubuntu, remove the PPA.
+ ### FIXME: unconditionally rm'ing unstable.list isn't a good idea, it appears. What if someone else put it there manually?
+ if [ "$PLAT" = "Debian" ] || { [ "$PLAT" = "Raspbian" ] && [ "$(uname -m)" = "armv7l" ]; }; then
+ rm -f /etc/apt/sources.list.d/pivpn-unstable.list
+ rm -f /etc/apt/preferences.d/pivpn-limit-unstable
+ $PKG_MANAGER update &> /dev/null
+ elif [ "$PLAT" = "Ubuntu" ]; then
+ add-apt-repository ppa:wireguard/wireguard -r -y
+ $PKG_MANAGER update &> /dev/null
+ fi
+
+ elif [ "${i}" = "wireguard-dkms" ]; then
+
+ # On armv6l Raspbian we manually remove the kernel module and skip the apt
+ # uninstallation (since it's not an actual package).
+ if [ "$PLAT" = "Raspbian" ] && [ "$(uname -m)" = "armv6l" ]; then
+ dkms remove wireguard/"${WG_MODULE_SNAPSHOT}" --all
+ rm -rf /usr/src/wireguard-"${WG_MODULE_SNAPSHOT}"
+ break
+ fi
+
+ elif [ "${i}" = "wireguard-tools" ]; then
+
+ if [ "$PLAT" = "Raspbian" ] && [ "$(uname -m)" = "armv6l" ]; then
+ rm -rf /usr/src/wireguard-tools-"${WG_TOOLS_SNAPSHOT}"
+ fi
+
+ elif [ "${i}" = "dirmngr" ]; then
+
+ # If dirmngr was installed, then we had previously installed wireguard on armv7l Raspbian
+ # so we remove the repository keys
+ apt-key remove E1CF20DDFFE4B89E802658F1E0B11894F66AEC98 80D15823B7FD1561F9F7BCDDDC30D7C23CBBABEE &> /dev/null
+
+ elif [ "${i}" = "unattended-upgrades" ]; then
+
+ ### REALLY???
+ rm -rf /var/log/unattended-upgrades
+ rm -rf /etc/apt/apt.conf.d/*periodic
+ rm -rf /etc/apt/apt.conf.d/*unattended-upgrades
+
+ elif [ "${i}" = "openvpn" ]; then
+
+ deluser openvpn
+ rm -f /etc/rsyslog.d/30-openvpn.conf
+ rm -f /etc/logrotate.d/openvpn
+
+ fi
+ printf ":::\\tRemoving %s..." "$i"; $PKG_MANAGER -y remove --purge "$i" &> /dev/null & spinner $!; printf "done!\\n";
+ break
+ ;;
+ [Nn]* ) printf ":::\\tSkipping %s\\n" "$i";
+ break
+ ;;
+ * ) printf "::: You must answer yes or no!\\n";;
+ esac
+ done
+ done
+
+ # Take care of any additional package cleaning
+ printf "::: Auto removing remaining dependencies..."
+ $PKG_MANAGER -y autoremove &> /dev/null & spinner $!; printf "done!\\n";
+ printf "::: Auto cleaning remaining dependencies..."
+ $PKG_MANAGER -y autoclean &> /dev/null & spinner $!; printf "done!\\n";
+
+ echo ":::"
+ # Removing pivpn files
+ echo "::: Removing pivpn system files..."
+
+ if [ -f /etc/dnsmasq.d/02-pivpn.conf ]; then
+ rm -f /etc/dnsmasq.d/02-pivpn.conf
+ pihole restartdns
+ fi
+
+ rm -rf /opt/pivpn
+ rm -rf /etc/.pivpn
+ rm -rf /etc/pivpn
+ rm -f /var/log/*pivpn*
+ rm -f /usr/local/bin/pivpn
+ rm -f /etc/bash_completion.d/pivpn
+
+ echo ":::"
+ echo "::: Removing VPN configuration files..."
+
+ if [ "$VPN" = "wireguard" ]; then
+ rm -f /etc/wireguard/wg0.conf
+ rm -rf /etc/wireguard/configs
+ rm -rf /etc/wireguard/keys
+ ### FIXME SC2154
+ rm -rf "$install_home/configs"
+ elif [ "$VPN" = "openvpn" ]; then
+ rm -rf /var/log/*openvpn*
+ rm -f /etc/openvpn/server.conf
+ rm -f /etc/openvpn/crl.pem
+ rm -rf /etc/openvpn/easy-rsa
+ rm -rf "$install_home/ovpns"
+ fi
+
+ echo ":::"
+ printf "::: Finished removing PiVPN from your system.\\n"
+ printf "::: Reinstall by simpling running\\n:::\\n:::\\tcurl -L https://install.pivpn.dev | bash\\n:::\\n::: at any time!\\n:::\\n"
}
-function askreboot() {
- printf "It is \e[1mstrongly\e[0m recommended to reboot after un-installation.\n"
- read -p "Would you like to reboot now? [y/n]: " -n 1 -r
- echo
- if [[ ${REPLY} =~ ^[Yy]$ ]]; then
- printf "\nRebooting system...\n"
- sleep 3
- shutdown -r now
- fi
+askreboot(){
+ printf "It is \\e[1mstrongly\\e[0m recommended to reboot after un-installation.\\n"
+ read -p "Would you like to reboot now? [y/n]: " -n 1 -r
+ echo
+ if [[ ${REPLY} =~ ^[Yy]$ ]]; then
+ printf "\\nRebooting system...\\n"
+ sleep 3
+ shutdown -r now
+ fi
}
######### SCRIPT ###########
echo "::: Preparing to remove packages, be sure that each may be safely removed depending on your operating system."
echo "::: (SAFE TO REMOVE ALL ON RASPBIAN)"
while true; do
- read -rp "::: Do you wish to completely remove PiVPN configuration and installed packages from your system? (You will be prompted for each package) [y/n]: " yn
- case $yn in
- [Yy]* ) removeAll; askreboot; break;;
+ read -rp "::: Do you wish to completely remove PiVPN configuration and installed packages from your system? (You will be prompted for each package) [y/n]: " yn
+ case $yn in
+ [Yy]* ) removeAll; askreboot; break;;
- [Nn]* ) printf "::: Not removing anything, exiting...\n"; break;;
- esac
+ [Nn]* ) printf "::: Not removing anything, exiting...\\n"; break;;
+ esac
done
diff --git a/scripts/update.sh b/scripts/update.sh
index fb74450..9481c4a 100755
--- a/scripts/update.sh
+++ b/scripts/update.sh
@@ -1,99 +1,106 @@
-#/bin/bash
+#!/bin/bash
###Updates pivpn scripts (Not PiVPN)
###Main Vars
pivpnrepo="https://github.com/pivpn/pivpn.git"
pivpnlocalpath="/etc/.pivpn"
-pivpnscripts="/opt/pivpn/scripts"
-bashcompletiondir="/etc/bash_completion.d/pivpn"
+pivpnscripts="/opt/pivpn/"
+bashcompletiondir="/etc/bash_completion.d/"
+setupVars="/etc/pivpn/setupVars.conf"
+if [ ! -f "${setupVars}" ]; then
+ echo "::: Missing setup vars file!"
+ exit 1
+fi
+
+source "${setupVars}"
###Functions
##Updates scripts
updatepivpnscripts(){
- ##We don't know what sort of changes users have made.
+ ##We don't know what sort of changes users have made.
##Lets remove first /etc/.pivpn dir then clone it back again
echo "going do update PiVPN Scripts"
- if [[ -d $pivpnlocalpath ]]; then
-
- sudo rm -rf $pivpnlocalpath
+ if [[ -d "$pivpnlocalpath" ]]; then
+ if [[ -n "$pivpnlocalpath" ]]; then
+ rm -rf "${pivpnlocalpath}/../.pivpn"
cloneandupdate
-
+ fi
else
- cloneandupdate
+ cloneandupdate
fi
echo "PiVPN Scripts have been updated"
}
-##Updates scripts using test branch
+##Updates scripts using test branch
updatefromtest(){
- ##We don't know what sort of changes users have made.
+ ##We don't know what sort of changes users have made.
##Lets remove first /etc/.pivpn dir then clone it back again
echo "PiVPN Scripts updating from test branch"
- if [[ -d /etc/.pivpn ]]; then
-
- rm -rf /etc/.pivpn
+ if [[ -d "$pivpnlocalpath" ]]; then
+ if [[ -n "$pivpnlocalpath" ]]; then
+ rm -rf "${pivpnlocalpath}/../.pivpn"
cloneupdttest
-
+ fi
else
-
- cloneupdttest
-
+ cloneupdttest
fi
echo "PiVPN Scripts updated have been updated from test branch"
}
-##Clone and copy pivpn scripts to /op/
+##Clone and copy pivpn scripts to /opt/pivpn
cloneandupdate(){
-
- sudo git clone $pivpnrepo $pivpnlocalpath
- sudo cp -r $pivpnlocalpath/scripts $pivpnscripts
- sudo cp $pivpnlocalpath/scripts/bash-completion $bashcompletiondir
-
+ ##This is to be removed after merge.
+ ##Alert for users trying to update from master.
+ echo "ERROR: You have installed pivpn from test branch."
+ echo "Wireguard not yet available on master, please use -t flag"
+ exit 1
+ ## Remove Above and uncomment below when test is moved to master
+# git clone "$pivpnrepo" "$pivpnlocalpath"
+# cp "${pivpnlocalpath}"/scripts/*.sh "$pivpnscripts"
+# cp "${pivpnlocalpath}"/scripts/$VPN/*.sh "$pivpnscripts"
+# cp "${pivpnlocalpath}"/scripts/$VPN/bash-completion "$bashcompletiondir"
}
##same as cloneandupdate() but from test branch
##and falls back to master branch again after updating
cloneupdttest(){
-
- sudo git clone $pivpnrepo $pivpnlocalpath
- sudo git -C $pivpnlocalpath checkout test
- sudo git -C $pivpnlocalpath pull origin test
- sudo cp -r $pivpnlocalpath/scripts $pivpnscripts
- sudo cp $pivpnlocalpath/scripts/bash-completion $bashcompletiondir
- sudo git -C $pivpnlocalpath checkout master
-
+ git clone "$pivpnrepo" "$pivpnlocalpath"
+ git -C "$pivpnlocalpath" checkout test
+ git -C "$pivpnlocalpath" pull origin test
+ cp "${pivpnlocalpath}"/scripts/*.sh "$pivpnscripts"
+ cp "${pivpnlocalpath}"/scripts/$VPN/*.sh "$pivpnscripts"
+ cp "${pivpnlocalpath}"/scripts/$VPN/bash-completion "$bashcompletiondir"
+ git -C "$pivpnlocalpath" checkout master
}
scriptusage(){
- echo -e "Updates pivpn scripts,\n
+ echo -e "Updates pivpn scripts,
+
Usage:
pivpn update | updates from master branch
pivpn update -t or --test | updates from test branch"
-
}
## SCRIPT
if [[ $# -eq 0 ]]; then
updatepivpnscripts
-
else
while true; do
case "$1" in
- -t | --test | test )
+ -t|--test|test)
updatefromtest
exit 0
;;
- -h | --help | help )
+ -h|--help|help)
scriptusage
exit 0
;;
- * )
- updatepivpnscripts
+ * )
+ updatepivpnscripts
exit 0
;;
esac
done
fi
-
diff --git a/scripts/wireguard/bash-completion b/scripts/wireguard/bash-completion
new file mode 100644
index 0000000..a971696
--- /dev/null
+++ b/scripts/wireguard/bash-completion
@@ -0,0 +1,19 @@
+_pivpn()
+{
+ local cur prev opts
+ COMPREPLY=()
+ cur="${COMP_WORDS[COMP_CWORD]}"
+ prev="${COMP_WORDS[COMP_CWORD-1]}"
+ dashopts="-a -c -d -l -qr -r -h -u -up -bk"
+ opts="add clients debug list qrcode remove help uninstall update backup"
+ if [ "${#COMP_WORDS[@]}" -eq 2 ]
+ then
+ if [[ ${cur} == -* ]] ; then
+ COMPREPLY=( $(compgen -W "${dashopts}" -- "${cur}") )
+ else
+ COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
+ fi
+ fi
+ return 0
+}
+complete -F _pivpn pivpn
diff --git a/scripts/wireguard/clientSTAT.sh b/scripts/wireguard/clientSTAT.sh
new file mode 100755
index 0000000..d8c94cd
--- /dev/null
+++ b/scripts/wireguard/clientSTAT.sh
@@ -0,0 +1,39 @@
+#!/bin/bash
+
+cd /etc/wireguard/configs
+if [ ! -s clients.txt ]; then
+ echo "::: There are no clients to list"
+ exit 1
+fi
+
+hr(){
+ numfmt --to=iec-i --suffix=B "$1"
+}
+
+DUMP="$(wg show wg0 dump | tail -n +2)"
+
+printf "\e[1m::: Connected Clients List :::\e[0m\n"
+
+{
+printf "\e[4mName\e[0m \t \e[4mRemote IP\e[0m \t \e[4mVirtual IP\e[0m \t \e[4mBytes Received\e[0m \t \e[4mBytes Sent\e[0m \t \e[4mLast Seen\e[0m\n"
+
+while IFS= read -r LINE; do
+
+ PUBLIC_KEY="$(awk '{ print $1 }' <<< "$LINE")"
+ REMOTE_IP="$(awk '{ print $3 }' <<< "$LINE")"
+ VIRTUAL_IP="$(awk '{ print $4 }' <<< "$LINE")"
+ BYTES_RECEIVED="$(awk '{ print $6 }' <<< "$LINE")"
+ BYTES_SENT="$(awk '{ print $7 }' <<< "$LINE")"
+ LAST_SEEN="$(awk '{ print $5 }' <<< "$LINE")"
+ CLIENT_NAME="$(grep "$PUBLIC_KEY" clients.txt | awk '{ print $1 }')"
+
+ if [ "$LAST_SEEN" -ne 0 ]; then
+ printf "%s \t %s \t %s \t %s \t %s \t %s\n" "$CLIENT_NAME" "$REMOTE_IP" "${VIRTUAL_IP/\/32/}" "$(hr "$BYTES_RECEIVED")" "$(hr "$BYTES_SENT")" "$(date -d @"$LAST_SEEN" '+%b %m %Y - %T')"
+ else
+ printf "%s \t %s \t %s \t %s \t %s \t %s\n" "$CLIENT_NAME" "$REMOTE_IP" "${VIRTUAL_IP/\/32/}" "$(hr "$BYTES_RECEIVED")" "$(hr "$BYTES_SENT")" "(not yet)"
+ fi
+
+done <<< "$DUMP"
+
+printf "\n"
+} | column -t -s $'\t'
\ No newline at end of file
diff --git a/scripts/wireguard/listCONF.sh b/scripts/wireguard/listCONF.sh
new file mode 100755
index 0000000..6f8e198
--- /dev/null
+++ b/scripts/wireguard/listCONF.sh
@@ -0,0 +1,28 @@
+#!/bin/bash
+
+cd /etc/wireguard/configs
+if [ ! -s clients.txt ]; then
+ echo "::: There are no clients to list"
+ exit 1
+fi
+
+printf "\e[1m::: Clients Summary :::\e[0m\n"
+
+# Present the user with a summary of the clients, fetching info from dates.
+{
+echo -e "\e[4mClient\e[0m \t \e[4mPublic key\e[0m \t \e[4mCreation date\e[0m"
+
+while read -r LINE; do
+ CLIENT_NAME="$(awk '{print $1}' <<< "$LINE")"
+
+ PUBLIC_KEY="$(awk '{print $2}' <<< "$LINE")"
+
+ CREATION_DATE="$(awk '{print $3}' <<< "$LINE")"
+
+ # Dates are converted from UNIX time to human readable.
+ CD_FORMAT="$(date -d @"$CREATION_DATE" +'%d %b %Y, %H:%M, %Z')"
+
+ echo -e "$CLIENT_NAME \t $PUBLIC_KEY \t $CD_FORMAT"
+done < clients.txt
+
+} | column -t -s $'\t'
\ No newline at end of file
diff --git a/scripts/wireguard/makeCONF.sh b/scripts/wireguard/makeCONF.sh
new file mode 100755
index 0000000..561aa69
--- /dev/null
+++ b/scripts/wireguard/makeCONF.sh
@@ -0,0 +1,132 @@
+#!/bin/bash
+
+setupVars="/etc/pivpn/setupVars.conf"
+
+helpFunc(){
+ echo "::: Create a client conf profile"
+ echo ":::"
+ echo "::: Usage: pivpn <-a|add> [-n|--name ] [-h|--help]"
+ echo ":::"
+ echo "::: Commands:"
+ echo "::: [none] Interactive mode"
+ echo "::: -n,--name Name for the Client (default: '$HOSTNAME')"
+ echo "::: -h,--help Show this help dialog"
+}
+
+# Parse input arguments
+while test $# -gt 0; do
+ _key="$1"
+ case "$_key" in
+ -n|--name|--name=*)
+ _val="${_key##--name=}"
+ if test "$_val" = "$_key"; then
+ test $# -lt 2 && echo "::: Missing value for the optional argument '$_key'." && exit 1
+ _val="$2"
+ shift
+ fi
+ CLIENT_NAME="$_val"
+ ;;
+ -h|--help)
+ helpFunc
+ exit 0
+ ;;
+ *)
+ echo "::: Error: Got an unexpected argument '$1'"
+ helpFunc
+ exit 1
+ ;;
+ esac
+ shift
+done
+
+if [ ! -f "${setupVars}" ]; then
+ echo "::: Missing setup vars file!"
+ exit 1
+fi
+
+source "${setupVars}"
+
+# The home folder variable was sourced from the settings file.
+if [ ! -d "${install_home}/configs" ]; then
+ mkdir "${install_home}/configs"
+ chown "${install_user}":"${install_user}" "${install_home}/configs"
+ chmod 0750 "${install_home}/configs"
+fi
+
+cd /etc/wireguard
+
+if [ -z "${CLIENT_NAME}" ]; then
+ read -r -p "Enter a Name for the Client: " CLIENT_NAME
+fi
+
+if [[ "${CLIENT_NAME}" =~ [^a-zA-Z0-9.@_-] ]]; then
+ echo "Name can only contain alphanumeric characters and these characters (.-@_)."
+ exit 1
+fi
+
+if [ -z "${CLIENT_NAME}" ]; then
+ echo "::: You cannot leave the name blank."
+ exit 1
+fi
+
+if [ -f "configs/${CLIENT_NAME}.conf" ]; then
+ echo "::: A client with this name already exists"
+ exit 1
+fi
+
+wg genkey | tee "keys/${CLIENT_NAME}_priv" | wg pubkey > "keys/${CLIENT_NAME}_pub"
+echo "::: Client Keys generated"
+
+# Find an unused number for the last octet of the client IP
+for i in {2..254}; do
+ if ! grep -q " $i$" configs/clients.txt; then
+ COUNT="$i"
+ echo "${CLIENT_NAME} $(> configs/clients.txt
+ break
+ fi
+done
+
+echo -n "[Interface]
+PrivateKey = $(cat "keys/${CLIENT_NAME}_priv")
+Address = 10.6.0.${COUNT}/24
+DNS = ${pivpnDNS1}" > "configs/${CLIENT_NAME}.conf"
+
+if [ -n "${pivpnDNS2}" ]; then
+ echo ", ${pivpnDNS2}" >> "configs/${CLIENT_NAME}.conf"
+else
+ echo >> "configs/${CLIENT_NAME}.conf"
+fi
+echo >> "configs/${CLIENT_NAME}.conf"
+
+echo "[Peer]
+PublicKey = $(cat keys/server_pub)
+PresharedKey = $(cat keys/psk)
+Endpoint = ${pivpnHOST}:${pivpnPORT}
+AllowedIPs = 0.0.0.0/0" >> "configs/${CLIENT_NAME}.conf"
+echo "::: Client config generated"
+
+echo "# begin ${CLIENT_NAME}
+[Peer]
+PublicKey = $(cat "keys/${CLIENT_NAME}_pub")
+PresharedKey = $(cat keys/psk)
+AllowedIPs = 10.6.0.${COUNT}/32
+# end ${CLIENT_NAME}" >> wg0.conf
+echo "::: Updated server config"
+
+if systemctl restart wg-quick@wg0; then
+ echo "::: WireGuard restarted"
+else
+ echo "::: Failed to restart WireGuard"
+fi
+
+cp "configs/${CLIENT_NAME}.conf" "${install_home}/configs/${CLIENT_NAME}.conf"
+chown "${install_user}":"${install_user}" "${install_home}/configs/${CLIENT_NAME}.conf"
+chmod 640 "${install_home}/configs/${CLIENT_NAME}.conf"
+
+echo "======================================================================"
+echo -e "::: Done! \e[1m${CLIENT_NAME}.conf successfully created!\e[0m"
+echo "::: ${CLIENT_NAME}.conf was copied to ${install_home}/configs for easy transfer."
+echo "::: Please use this profile only on one device and create additional"
+echo -e "::: profiles for other devices. You can also use \e[1mpivpn -qr\e[0m"
+echo "::: to generate a QR Code you can scan with the mobile app."
+echo "======================================================================"
diff --git a/scripts/wireguard/pivpn b/scripts/wireguard/pivpn
new file mode 100755
index 0000000..4400a3f
--- /dev/null
+++ b/scripts/wireguard/pivpn
@@ -0,0 +1,97 @@
+#!/bin/bash
+
+# Must be root to use this tool
+if [ $EUID -ne 0 ];then
+ if dpkg-query -s sudo &> /dev/null; then
+ export SUDO="sudo"
+ else
+ echo "::: Please install sudo or run this as root."
+ exit 1
+ fi
+fi
+
+makeConf(){
+ shift
+ $SUDO /opt/pivpn/makeCONF.sh "$@"
+ exit 0
+}
+
+listConnected(){
+ $SUDO /opt/pivpn/clientSTAT.sh
+ exit 0
+}
+
+debug(){
+ $SUDO /opt/pivpn/pivpnDEBUG.sh
+ exit 0
+}
+
+listClients(){
+ $SUDO /opt/pivpn/listCONF.sh
+ exit 0
+}
+
+showQrcode(){
+ shift
+ $SUDO /opt/pivpn/qrcodeCONF.sh "$@"
+ exit 0
+}
+
+removeClient(){
+ shift
+ $SUDO /opt/pivpn/removeCONF.sh "$@"
+ exit 0
+}
+
+uninstallServer(){
+ $SUDO /opt/pivpn/uninstall.sh
+ exit 0
+}
+
+updateScripts(){
+ shift
+ $SUDO /opt/pivpn/update.sh "$@"
+ exit 0
+}
+
+backup(){
+ $SUDO /opt/pivpn/backup.sh
+}
+
+showHelp(){
+ echo "::: Control all PiVPN specific functions!"
+ echo ":::"
+ echo "::: Usage: pivpn [option]"
+ echo ":::"
+ echo "::: Commands:"
+ echo "::: -a, add Create a client conf profile"
+ echo "::: -c, clients List any connected clients to the server"
+ echo "::: -d, debug Start a debugging session if having trouble"
+ echo "::: -l, list List all clients"
+ echo "::: -qr, qrcode Show the qrcode of a client for use with the mobile app"
+ echo "::: -r, remove Remove a client"
+ echo "::: -h, help Show this help dialog"
+ echo "::: -u, uninstall Uninstall pivpn from your system!"
+ echo "::: -up, update Updates PiVPN Scripts"
+ echo "::: -bk, Backup Backup vpn configs and user profiles"
+ exit 0
+}
+
+if [ $# = 0 ]; then
+ showHelp
+fi
+
+# Handle redirecting to specific functions based on arguments
+case "$1" in
+"-a" | "add" ) makeConf "$@";;
+"-c" | "clients" ) listConnected;;
+"-d" | "debug" ) debug;;
+"-l" | "list" ) listClients;;
+"-qr" | "qrcode" ) showQrcode "$@";;
+"-r" | "remove" ) removeClient "$@";;
+"-h" | "help" ) showHelp;;
+"-u" | "uninstall" ) uninstallServer;;
+"-up" | "update" ) updateScripts "$@" ;;
+"-bk" | "backup" ) backup ;;
+* ) showHelp;;
+esac
diff --git a/scripts/wireguard/pivpnDEBUG.sh b/scripts/wireguard/pivpnDEBUG.sh
new file mode 100755
index 0000000..56d9bd5
--- /dev/null
+++ b/scripts/wireguard/pivpnDEBUG.sh
@@ -0,0 +1,203 @@
+#!/usr/bin/env bash
+# This scripts runs as root
+
+setupVars="/etc/pivpn/setupVars.conf"
+
+if [ ! -f "${setupVars}" ]; then
+ echo "::: Missing setup vars file!"
+ exit 1
+fi
+
+source "${setupVars}"
+
+EXAMPLE="$(head -1 /etc/wireguard/configs/clients.txt | awk '{print $1}')"
+ERR=0
+
+echo -e "::::\t\t\e[4mPiVPN debug\e[0m\t\t ::::"
+printf "=============================================\n"
+echo -e "::::\t\t\e[4mLatest commit\e[0m\t\t ::::"
+git --git-dir /etc/.pivpn/.git log -n 1
+printf "=============================================\n"
+echo -e "::::\t \e[4mInstallation settings\e[0m \t ::::"
+sed "s/$pivpnHOST/REDACTED/" < /etc/pivpn/setupVars.conf
+printf "=============================================\n"
+echo -e ":::: \e[4mServer configuration shown below\e[0m ::::"
+cd /etc/wireguard/keys
+cp ../wg0.conf ../wg0.tmp
+# Replace every key in the server configuration with just its file name
+for k in *; do
+ sed "s#$(cat "$k")#$k#" -i ../wg0.tmp
+done
+cat ../wg0.tmp
+rm ../wg0.tmp
+printf "=============================================\n"
+echo -e ":::: \e[4mClient configuration shown below\e[0m ::::"
+if [ -n "$EXAMPLE" ]; then
+ cp ../configs/"$EXAMPLE".conf ../configs/"$EXAMPLE".tmp
+ for k in *; do
+ sed "s#$(cat "$k")#$k#" -i ../configs/"$EXAMPLE".tmp
+ done
+ sed "s/$pivpnHOST/REDACTED/" < ../configs/"$EXAMPLE".tmp
+ rm ../configs/"$EXAMPLE".tmp
+else
+ echo "::: There are no clients yet"
+fi
+
+printf "=============================================\n"
+echo -e ":::: \t\e[4mRecursive list of files in\e[0m\t ::::\n::::\e\t[4m/etc/wireguard shown below\e[0m\t ::::"
+ls -LR /etc/wireguard
+printf "=============================================\n"
+echo -e "::::\t\t\e[4mSelf check\e[0m\t\t ::::"
+
+if [ "$(cat /proc/sys/net/ipv4/ip_forward)" -eq 1 ]; then
+ echo ":: [OK] IP forwarding is enabled"
+else
+ ERR=1
+ read -r -p ":: [ERR] IP forwarding is not enabled, attempt fix now? [Y/n] " REPLY
+ if [[ ${REPLY} =~ ^[Yy]$ ]] || [[ -z ${REPLY} ]]; then
+ sed -i '/net.ipv4.ip_forward=1/s/^#//g' /etc/sysctl.conf
+ sysctl -p
+ echo "Done"
+ fi
+fi
+
+if [ "$USING_UFW" -eq 0 ]; then
+
+ if iptables -t nat -C POSTROUTING -s 10.6.0.0/24 -o "${IPv4dev}" -j MASQUERADE -m comment --comment "${VPN}-nat-rule" &> /dev/null; then
+ echo ":: [OK] Iptables MASQUERADE rule set"
+ else
+ ERR=1
+ read -r -p ":: [ERR] Iptables MASQUERADE rule is not set, attempt fix now? [Y/n] " REPLY
+ if [[ ${REPLY} =~ ^[Yy]$ ]] || [[ -z ${REPLY} ]]; then
+ iptables -t nat -I POSTROUTING -s 10.6.0.0/24 -o "${IPv4dev}" -j MASQUERADE -m comment --comment "${VPN}-nat-rule"
+ iptables-save > /etc/iptables/rules.v4
+ echo "Done"
+ fi
+ fi
+
+ if [ "$INPUT_CHAIN_EDITED" -eq 1 ]; then
+
+ if iptables -C INPUT -i "$IPv4dev" -p udp --dport "$pivpnPORT" -j ACCEPT -m comment --comment "${VPN}-input-rule" &> /dev/null; then
+ echo ":: [OK] Iptables INPUT rule set"
+ else
+ ERR=1
+ read -r -p ":: [ERR] Iptables INPUT rule is not set, attempt fix now? [Y/n] " REPLY
+ if [[ ${REPLY} =~ ^[Yy]$ ]] || [[ -z ${REPLY} ]]; then
+ iptables -I INPUT 1 -i "$IPv4dev" -p udp --dport "$pivpnPORT" -j ACCEPT -m comment --comment "${VPN}-input-rule"
+ iptables-save > /etc/iptables/rules.v4
+ echo "Done"
+ fi
+ fi
+ fi
+
+ if [ "$FORWARD_CHAIN_EDITED" -eq 1 ]; then
+
+ if iptables -C FORWARD -s 10.6.0.0/24 -i wg0 -o "$IPv4dev" -j ACCEPT -m comment --comment "${VPN}-forward-rule" &> /dev/null; then
+ echo ":: [OK] Iptables FORWARD rule set"
+ else
+ ERR=1
+ read -r -p ":: [ERR] Iptables FORWARD rule is not set, attempt fix now? [Y/n] " REPLY
+ if [[ ${REPLY} =~ ^[Yy]$ ]] || [[ -z ${REPLY} ]]; then
+ iptables -I FORWARD 1 -d 10.6.0.0/24 -i "$IPv4dev" -o wg0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -m comment --comment "${VPN}-forward-rule"
+ iptables -I FORWARD 2 -s 10.6.0.0/24 -i wg0 -o "$IPv4dev" -j ACCEPT -m comment --comment "${VPN}-forward-rule"
+ iptables-save > /etc/iptables/rules.v4
+ echo "Done"
+ fi
+ fi
+ fi
+
+else
+
+ if LANG="en_US.UTF-8" ufw status | grep -qw 'active'; then
+ echo ":: [OK] Ufw is enabled"
+ else
+ ERR=1
+ read -r -p ":: [ERR] Ufw is not enabled, try to enable now? [Y/n] " REPLY
+ if [[ ${REPLY} =~ ^[Yy]$ ]] || [[ -z ${REPLY} ]]; then
+ ufw enable
+ fi
+ fi
+
+ if iptables -t nat -C POSTROUTING -s 10.6.0.0/24 -o "${IPv4dev}" -j MASQUERADE -m comment --comment "${VPN}-nat-rule" &> /dev/null; then
+ echo ":: [OK] Iptables MASQUERADE rule set"
+ else
+ ERR=1
+ read -r -p ":: [ERR] Iptables MASQUERADE rule is not set, attempt fix now? [Y/n] " REPLY
+ if [[ ${REPLY} =~ ^[Yy]$ ]] || [[ -z ${REPLY} ]]; then
+ sed "/delete these required/i *nat\n:POSTROUTING ACCEPT [0:0]\n-I POSTROUTING -s 10.6.0.0/24 -o $IPv4dev -j MASQUERADE -m comment --comment ${VPN}-nat-rule\nCOMMIT\n" -i /etc/ufw/before.rules
+ ufw reload
+ echo "Done"
+ fi
+ fi
+
+ if iptables -C ufw-user-input -p udp --dport "${pivpnPORT}" -j ACCEPT &> /dev/null; then
+ echo ":: [OK] Ufw input rule set"
+ else
+ ERR=1
+ read -r -p ":: [ERR] Ufw input rule is not set, attempt fix now? [Y/n] " REPLY
+ if [[ ${REPLY} =~ ^[Yy]$ ]] || [[ -z ${REPLY} ]]; then
+ ufw insert 1 allow "$pivpnPORT"/udp
+ ufw reload
+ echo "Done"
+ fi
+ fi
+
+ if iptables -C ufw-user-forward -i wg0 -o "${IPv4dev}" -s 10.6.0.0/24 -j ACCEPT &> /dev/null; then
+ echo ":: [OK] Ufw forwarding rule set"
+ else
+ ERR=1
+ read -r -p ":: [ERR] Ufw forwarding rule is not set, attempt fix now? [Y/n] " REPLY
+ if [[ ${REPLY} =~ ^[Yy]$ ]] || [[ -z ${REPLY} ]]; then
+ ufw route insert 1 allow in on wg0 from 10.6.0.0/24 out on "$IPv4dev" to any
+ ufw reload
+ echo "Done"
+ fi
+ fi
+
+fi
+
+if systemctl is-active -q wg-quick@wg0; then
+ echo ":: [OK] WireGuard is running"
+else
+ ERR=1
+ read -r -p ":: [ERR] WireGuard is not running, try to start now? [Y/n] " REPLY
+ if [[ ${REPLY} =~ ^[Yy]$ ]] || [[ -z ${REPLY} ]]; then
+ systemctl start wg-quick@wg0
+ echo "Done"
+ fi
+fi
+
+if systemctl is-enabled -q wg-quick@wg0; then
+ echo ":: [OK] WireGuard is enabled (it will automatically start on reboot)"
+else
+ ERR=1
+ read -r -p ":: [ERR] WireGuard is not enabled, try to enable now? [Y/n] " REPLY
+ if [[ ${REPLY} =~ ^[Yy]$ ]] || [[ -z ${REPLY} ]]; then
+ systemctl enable wg-quick@wg0
+ echo "Done"
+ fi
+fi
+
+# grep -w (whole word) is used so port 11940 won't match when looking for 1194
+if netstat -uanp | grep -w "${pivpnPORT}" | grep -q 'udp'; then
+ echo ":: [OK] WireGuard is listening on port ${pivpnPORT}/udp"
+else
+ ERR=1
+ read -r -p ":: [ERR] WireGuard is not listening, try to restart now? [Y/n] " REPLY
+ if [[ ${REPLY} =~ ^[Yy]$ ]] || [[ -z ${REPLY} ]]; then
+ systemctl restart wg-quick@wg0
+ echo "Done"
+ fi
+fi
+
+if [ "$ERR" -eq 1 ]; then
+ echo -e "[INFO] Run \e[1mpivpn -d\e[0m again to see if we detect issues"
+fi
+printf "=============================================\n"
+echo -e ":::: \e[1mWARNING\e[0m: This script should have automatically masked sensitive ::::"
+echo -e ":::: information, however, still make sure that \e[4mPrivateKey\e[0m, \e[4mPublicKey\e[0m ::::"
+echo -e ":::: and \e[4mPresharedKey\e[0m are masked before reporting an issue. An example key ::::"
+echo ":::: that you should NOT see in this log looks like this: ::::"
+echo ":::: WJhKKx+Uk1l1TxaH2KcEGeBdPBTp/k/Qy4EpBig5UnI= ::::"
+printf "=============================================\n"
+echo -e "::::\t\t\e[4mDebug complete\e[0m\t\t ::::"
diff --git a/scripts/wireguard/qrcodeCONF.sh b/scripts/wireguard/qrcodeCONF.sh
new file mode 100755
index 0000000..0420eb5
--- /dev/null
+++ b/scripts/wireguard/qrcodeCONF.sh
@@ -0,0 +1,63 @@
+#!/bin/bash
+
+helpFunc(){
+ echo "::: Show the qrcode of a client for use with the mobile app"
+ echo ":::"
+ echo "::: Usage: pivpn <-qr|qrcode> [-h|--help] [] ... [] ..."
+ echo ":::"
+ echo "::: Commands:"
+ echo "::: [none] Interactive mode"
+ echo "::: Client(s) to show"
+ echo "::: -h,--help Show this help dialog"
+}
+
+# Parse input arguments
+while test $# -gt 0
+do
+ _key="$1"
+ case "$_key" in
+ -h|--help)
+ helpFunc
+ exit 0
+ ;;
+ *)
+ CLIENTS_TO_SHOW+=("$1")
+ ;;
+ esac
+ shift
+done
+
+cd /etc/wireguard/configs
+if [ ! -s clients.txt ]; then
+ echo "::: There are no clients to show"
+ exit 1
+fi
+
+if [ "${#CLIENTS_TO_SHOW[@]}" -eq 0 ]; then
+
+ echo -e "::\e[4m Client list \e[0m::"
+ LIST=($(awk '{print $1}' clients.txt))
+ COUNTER=1
+ while [ $COUNTER -le ${#LIST[@]} ]; do
+ echo "• ${LIST[(($COUNTER-1))]}"
+ ((COUNTER++))
+ done
+
+ read -r -p "Please enter the Name of the Client to show: " CLIENTS_TO_SHOW
+
+ if [ -z "${CLIENTS_TO_SHOW}" ]; then
+ echo "::: You can not leave this blank!"
+ exit 1
+ fi
+fi
+
+for CLIENT_NAME in "${CLIENTS_TO_SHOW[@]}"; do
+ if grep -qw "${CLIENT_NAME}" clients.txt; then
+ echo -e "::: Showing client \e[1m${CLIENT_NAME}\e[0m below"
+ echo "====================================================================="
+ qrencode -t ansiutf8 < "${CLIENT_NAME}.conf"
+ echo "====================================================================="
+ else
+ echo -e "::: \e[1m${CLIENT_NAME}\e[0m does not exist"
+ fi
+done
\ No newline at end of file
diff --git a/scripts/wireguard/removeCONF.sh b/scripts/wireguard/removeCONF.sh
new file mode 100755
index 0000000..b00deb6
--- /dev/null
+++ b/scripts/wireguard/removeCONF.sh
@@ -0,0 +1,119 @@
+#!/bin/bash
+
+setupVars="/etc/pivpn/setupVars.conf"
+
+helpFunc(){
+ echo "::: Remove a client conf profile"
+ echo ":::"
+ echo "::: Usage: pivpn <-r|remove> [-h|--help] [] ... [] ..."
+ echo ":::"
+ echo "::: Commands:"
+ echo "::: [none] Interactive mode"
+ echo "::: Client(s) to remove"
+ echo "::: -h,--help Show this help dialog"
+}
+
+# Parse input arguments
+while test $# -gt 0
+do
+ _key="$1"
+ case "$_key" in
+ -h|--help)
+ helpFunc
+ exit 0
+ ;;
+ *)
+ CLIENTS_TO_REMOVE+=("$1")
+ ;;
+ esac
+ shift
+done
+
+if [ ! -f "${setupVars}" ]; then
+ echo "::: Missing setup vars file!"
+ exit 1
+fi
+
+source "${setupVars}"
+
+cd /etc/wireguard
+if [ ! -s configs/clients.txt ]; then
+ echo "::: There are no clients to remove"
+ exit 1
+fi
+
+if [ "${#CLIENTS_TO_REMOVE[@]}" -eq 0 ]; then
+
+ echo -e "::\e[4m Client list \e[0m::"
+ LIST=($(awk '{print $1}' configs/clients.txt))
+ COUNTER=1
+ while [ $COUNTER -le ${#LIST[@]} ]; do
+ echo "• ${LIST[(($COUNTER-1))]}"
+ ((COUNTER++))
+ done
+
+ read -r -p "Please enter the Name of the Client to be removed from the list above: " CLIENTS_TO_REMOVE
+
+ if [ -z "${CLIENTS_TO_REMOVE}" ]; then
+ echo "::: You can not leave this blank!"
+ exit 1
+ fi
+fi
+
+DELETED_COUNT=0
+
+for CLIENT_NAME in "${CLIENTS_TO_REMOVE[@]}"; do
+
+ if ! grep -qw "${CLIENT_NAME}" configs/clients.txt; then
+ echo -e "::: \e[1m${CLIENT_NAME}\e[0m does not exist"
+ else
+ REQUESTED="$(sha256sum "configs/${CLIENT_NAME}.conf" | cut -c 1-64)"
+ read -r -p "Do you really want to delete $CLIENT_NAME? [Y/n] "
+
+ if [[ $REPLY =~ ^[Yy]$ ]]; then
+
+ # Grab the least significant octed of the client IP address
+ COUNT=$(grep "${CLIENT_NAME}" configs/clients.txt | awk '{print $4}')
+ # The creation date of the client
+ CREATION_DATE="$(grep "${CLIENT_NAME}" configs/clients.txt | awk '{print $3}')"
+ # And its public key
+ PUBLIC_KEY="$(grep "${CLIENT_NAME}" configs/clients.txt | awk '{print $2}')"
+
+ # Then remove the client matching the variables above
+ sed "\#${CLIENT_NAME} ${PUBLIC_KEY} ${CREATION_DATE} ${COUNT}#d" -i configs/clients.txt
+
+ # Remove the peer section from the server config
+ sed "/# begin ${CLIENT_NAME}/,/# end ${CLIENT_NAME}/d" -i wg0.conf
+ echo "::: Updated server config"
+
+ rm "configs/${CLIENT_NAME}.conf"
+ echo "::: Client config for ${CLIENT_NAME} removed"
+
+ rm "keys/${CLIENT_NAME}_priv"
+ rm "keys/${CLIENT_NAME}_pub"
+ echo "::: Client Keys for ${CLIENT_NAME} removed"
+
+ # Find all .conf files in the home folder of the user matching the checksum of the
+ # config and delete them. '-maxdepth 3' is used to avoid traversing too many folders.
+ find "${install_home}" -maxdepth 3 -type f -name '*.conf' -print0 | while IFS= read -r -d '' CONFIG; do
+ if sha256sum -c <<< "${REQUESTED} ${CONFIG}" &> /dev/null; then
+ rm "${CONFIG}"
+ fi
+ done
+
+ echo "::: Successfully deleted ${CLIENT_NAME}"
+
+ ((DELETED_COUNT++))
+ fi
+ fi
+
+done
+
+# Restart WireGuard only if some clients were actually deleted
+if [ "${DELETED_COUNT}" -gt 0 ]; then
+ if systemctl restart wg-quick@wg0; then
+ echo "::: WireGuard restarted"
+ else
+ echo "::: Failed to restart WireGuard"
+ fi
+fi
diff --git a/server_config.txt b/server_config.txt
index 93143b1..5032a12 100644
--- a/server_config.txt
+++ b/server_config.txt
@@ -8,8 +8,8 @@ dh /etc/openvpn/easy-rsa/pki/dh2048.pem
topology subnet
server 10.8.0.0 255.255.255.0
# Set your primary domain name server address for clients
-push "dhcp-option DNS 8.8.8.8"
-push "dhcp-option DNS 8.8.4.4"
+push "dhcp-option DNS 9.9.9.9"
+push "dhcp-option DNS 149.112.112.112"
# Prevent DNS leaks on Windows
push "block-outside-dns"
# Override the Client default gateway by using 0.0.0.0/1 and
@@ -17,14 +17,14 @@ push "block-outside-dns"
# overriding but not wiping out the original default gateway.
push "redirect-gateway def1"
client-to-client
-keepalive 1800 3600
+keepalive 15 120
remote-cert-tls client
tls-version-min 1.2
tls-auth /etc/openvpn/easy-rsa/pki/ta.key 0
cipher AES-256-CBC
auth SHA256
-user nobody
-group nogroup
+user openvpn
+group openvpn
persist-key
persist-tun
crl-verify /etc/openvpn/crl.pem
diff --git a/unattended_openvpn_example.conf b/unattended_openvpn_example.conf
new file mode 100644
index 0000000..ae9d9b7
--- /dev/null
+++ b/unattended_openvpn_example.conf
@@ -0,0 +1,13 @@
+IPv4dev=eth0
+dhcpReserv=0
+install_user=pi
+VPN=openvpn
+pivpnPROTO=udp
+pivpnPORT=1194
+pivpnDNS1=9.9.9.9
+pivpnDNS2=149.112.112.112
+pivpnHOST=pivpn.example.com
+pivpnENCRYPT=2048
+pivpnSEARCHDOMAIN=searchdomain.example.com
+DOWNLOAD_DH_PARAM=0
+UNATTUPG=1
diff --git a/unattended_wireguard_example.conf b/unattended_wireguard_example.conf
new file mode 100644
index 0000000..196d4d5
--- /dev/null
+++ b/unattended_wireguard_example.conf
@@ -0,0 +1,9 @@
+IPv4dev=eth0
+dhcpReserv=0
+install_user=pi
+VPN=wireguard
+pivpnPORT=51820
+pivpnDNS1=9.9.9.9
+pivpnDNS2=149.112.112.112
+pivpnHOST=pivpn.example.com
+UNATTUPG=1