├── Makefile ├── README.md ├── LICENSE └── pia.sh /Makefile: -------------------------------------------------------------------------------- 1 | install: 2 | cp pia.sh /usr/bin/pia 3 | chmod +x /usr/bin/pia 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | pia v0.5 Features: 2 | ========== 3 | - Update openvpn configuration files using any of the 5 available configuration zips. 4 | - Auto-update whenever PIA releases new configuration zips. 5 | - Auto reconnect on dropped connections. 6 | - Instant connections with secure permissions of VPN password. 7 | - Optionally AES encrypt creds with openssl and a password. 8 | - Forward ports and change port fowarding identity. 9 | - Change DNS to PIA secure leak-proof DNS servers. 10 | - Enable firewall to block all non-tunnel traffic. 11 | - Enable PIA MACE DNS based ad blocking. 12 | - Enable internet killswitch. 13 | - Detailed verbose output. 14 | - Designed for debian and arch based linux but should work on any linux. 15 | 16 | This client has all of the functionality of the official one and works on any linux with bash, openvpn and iptables installed. 17 | 18 | 19 | It also has the added advantage of using the versions of OpenVPN and OpenSSL installed on your system, which will always be more secure than the old fork of old versions of this software that the official PIA app uses as long as you regulary update. 20 | 21 | 22 | pia can be run interactivley or with switches. It will only ask you to supply your credentials once and then after that it connects without asking. 23 | 24 | 25 | The credentials file is permissions protected by 'chmod 400', which means only the root user can view the file. You can also optionally AES encrypt the creds file on disk with a password using the the -x arg. 26 | 27 | 28 | The ovpn files are editited and 'auth-nocache' option is added, which means openvpn will not store your creds in memory. 29 | 30 | 31 | Dependencies: 32 | ========== 33 | - bash 34 | - iptables 35 | - openvpn 36 | - openssl 37 | - curl 38 | - unzip 39 | - git 40 | 41 | Installation: 42 | ========== 43 | Clone the repository to a suitable place: 44 | `cd ~/scripts && git clone https://github.com/d4rkcat/pia` 45 | 46 | Then to install the script: 47 | `cd ~/scripts/pia && sudo make install` 48 | 49 | pia will now be installed and can be run from any directory with: 50 | `sudo pia [options]` 51 | 52 | You can update to the latest version easily with: 53 | `cd ~/scripts/pia && git pull && sudo make install` 54 | 55 | Usage: 56 | ========== 57 | Usage: pia [options] 58 | 59 | -s - Server number to connect to. 60 | -l - List available servers. 61 | -u - Update PIA openvpn files before connecting. 62 | -p - Forward a port. 63 | -n - Change to another random port. 64 | -d - Change DNS servers to PIA. 65 | -f - Enable firewall to block all non tunnel traffic. 66 | -e - Allow LAN through firewall. 67 | -m - Enable PIA MACE ad blocking. 68 | -k - Enable internet killswitch. 69 | -x - Encrypt the credetials file. 70 | -v - Display verbose information. 71 | -h - Display this help. 72 | 73 | Examples: 74 | pia -dps 6 - Change DNS, forward a port and connect to CA_Montreal. 75 | pia -nfv - Forward a new port, run firewall and be verbose. 76 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /pia.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ## pia v0.5 Copyright (C) 2017 d4rkcat (thed4rkcat@yandex.com) 4 | # 5 | ## This program is free software: you can redistribute it and/or modify 6 | ## it under the terms of the GNU General Public License Version 2 as published by 7 | ## the Free Software Foundation. 8 | # 9 | ## This program is distributed in the hope that it will be useful, 10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | ## GNU General Public License at (http://www.gnu.org/licenses/) for 13 | ## more details. 14 | 15 | fupdate() # Update the PIA openvpn files. 16 | { 17 | CONFIGDEFAULT="https://www.privateinternetaccess.com/openvpn/openvpn.zip" 18 | CONFIGSTRONG="https://www.privateinternetaccess.com/openvpn/openvpn-strong.zip" 19 | CONFIGIP="https://www.privateinternetaccess.com/openvpn/openvpn-ip.zip" 20 | CONFIGTCP="https://www.privateinternetaccess.com/openvpn/openvpn-tcp.zip" 21 | CONFIGTCPSTRONG="https://www.privateinternetaccess.com/openvpn/openvpn-strong-tcp.zip" 22 | 23 | if [ $CONFIGNUM -eq 0 ];then 24 | echo "$PROMPT Please choose configuration:" 25 | echo " $BOLD$RED[$RESET""1""$BOLD$RED]$RESET Default UDP (aes-128-cbc sha1 rsa-2048)" 26 | echo " $BOLD$RED[$RESET""2""$BOLD$RED]$RESET Strong UDP (aes-256-cbc sha256 rsa-4096)" 27 | echo " $BOLD$RED[$RESET""3""$BOLD$RED]$RESET Direct IP (aes-128-cbc sha1 rsa-2048)" 28 | echo " $BOLD$RED[$RESET""4""$BOLD$RED]$RESET Default TCP (aes-128-cbc sha1 rsa-2048)" 29 | echo " $BOLD$RED[$RESET""5""$BOLD$RED]$RESET Strong TCP (aes-256-cbc sha256 rsa-4096)" 30 | read -p "$PROMPT " CONFIGNUM 31 | fi 32 | 33 | if [[ $CONFIGNUM =~ ^[0-9]+$ && $CONFIGNUM -lt 6 && $CONFIGNUM -gt 0 ]];then 34 | case $CONFIGNUM in 35 | 1) DOWNURL=$CONFIGDEFAULT;echo "$INFO Selected Default UDP configuration.";; 36 | 2) DOWNURL=$CONFIGSTRONG;echo "$INFO Selected Strong UDP configuration.";; 37 | 3) DOWNURL=$CONFIGIP;echo "$INFO Selected Direct IP configuration.";; 38 | 4) DOWNURL=$CONFIGTCP;echo "$INFO Selected Default TCP configuration.";; 39 | 5) DOWNURL=$CONFIGTCPSTRONG;echo "$INFO Selected Strong TCP configuration.";; 40 | esac 41 | else 42 | echo "$ERROR $CONFIGNUM is not a valid option! 1-5 only." 43 | exit 1 44 | fi 45 | 46 | echo -n "$PROMPT Updating PIA openvpn files..." 47 | rm -rf $VPNPATH/*.ovpn $VPNPATH/servers.txt $VPNPATH/*.crt $VPNPATH/*.pem 48 | curl -so $VPNPATH/pia.zip $DOWNURL 49 | echo "$CONFIGNUM $DOWNURL $(curl -sI $DOWNURL | grep Last-Modified | cut -d ' ' -f 2-)" > $VPNPATH/configversion.txt 50 | cd $VPNPATH && unzip -q pia.zip && rm pia.zip 51 | cd $VPNPATH && for CONFIGFILE in *.ovpn;do mv "$CONFIGFILE" $(echo $CONFIGFILE | tr ' ' '_') &>/dev/null;done 52 | OLDS=("auth-user-pass" "crl-verify crl.rsa.2048.pem" "crl-verify crl.rsa.4096.pem" "ca ca.rsa.2048.crt" "ca ca.rsa.4096.crt" "verb 1") 53 | NEWS=("auth-user-pass $VPNPATH/pass.txt" "crl-verify $VPNPATH/crl.rsa.2048.pem" "crl-verify $VPNPATH/crl.rsa.4096.pem" "ca $VPNPATH/ca.rsa.2048.crt" "ca $VPNPATH/ca.rsa.4096.crt" "verb 2") 54 | for CONFIGFILE in $VPNPATH/*.ovpn;do 55 | CNT=0 56 | for OLD in "${OLDS[@]}";do 57 | sed -i "s%$OLD%${NEWS[$CNT]}%g" $CONFIGFILE 58 | ((++CNT)) 59 | done 60 | echo -e "auth-nocache\nlog /var/log/pia.log" >> $CONFIGFILE 61 | echo -n $(basename $CONFIGFILE | cut -d '.' -f 1)" " >> $VPNPATH/servers.txt 62 | cat $CONFIGFILE | grep .com | awk '{print $2}' >> $VPNPATH/servers.txt 63 | done 64 | echo -e "\r$INFO Files Updated. " 65 | } 66 | 67 | fforward() # Forward a port. 68 | { 69 | echo -n "$PROMPT Forwarding a port..." 70 | sleep 1.5 71 | if [ ! -f $VPNPATH/client_id ];then head -n 100 /dev/urandom | sha256sum | tr -d " -" > $VPNPATH/client_id;fi 72 | while [ $(echo $FORWARDEDPORT | wc -c) -lt 3 ];do 73 | FORWARDEDPORT=$(curl -s -m 4 "http://209.222.18.222:2000/?client_id=$(cat $VPNPATH/client_id)" | cut -d ':' -f 2 | cut -d '}' -f 1) 74 | done 75 | } 76 | 77 | fnewport() # Change port forwarded. 78 | { 79 | NEWPORT=1 80 | PORTFORWARD=1 81 | mv $VPNPATH/client_id $VPNPATH/client_id.bak 82 | head -n 100 /dev/urandom | sha256sum | tr -d " -" > $VPNPATH/client_id 83 | } 84 | 85 | ffirewall() # Set up iptables firewall rules to only allow traffic on tunneled interface and (optionally) within LAN. 86 | { 87 | fresetfirewall 88 | LAN=$(ip route show | grep default | awk '{print $3 }' | cut -d '.' -f 1-3)".0/24" 89 | DEFAULTDEVICE=$(ip route show | grep default | awk '{print $5}') 90 | VPNDEVICE=$(echo "$PLOG" | grep 'TUN/TAP device' | awk '{print $8}') 91 | VPNPORT=$(cat $VPNPATH/$CONFIG | grep 'remote ' | awk '{print $3}') 92 | PROTO=$(cat $VPNPATH/$CONFIG | grep proto | awk '{print $2}') 93 | 94 | iptables -P OUTPUT DROP # default policy for outgoing packets 95 | iptables -P INPUT DROP # default policy for incoming packets 96 | iptables -P FORWARD DROP # default policy for forwarded packets 97 | 98 | # allowed outputs 99 | iptables -A OUTPUT -o lo -j ACCEPT # enable localhost out 100 | iptables -A OUTPUT -o $VPNDEVICE -j ACCEPT # enable outgoing connections on tunnel 101 | if [[ "$PROTO" == "udp" ]];then 102 | iptables -A OUTPUT -o $DEFAULTDEVICE -p udp --dport $VPNPORT -j ACCEPT # enable port for communicating with PIA on default device 103 | else 104 | iptables -A OUTPUT -o $DEFAULTDEVICE -p tcp --dport $VPNPORT -j ACCEPT 105 | fi 106 | 107 | # allowed inputs 108 | iptables -A INPUT -i lo -j ACCEPT # enable localhost in 109 | iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # enable requested packets on tunnel 110 | 111 | if [ $PORTFORWARD -eq 1 ];then 112 | iptables -A INPUT -i $VPNDEVICE -p tcp --dport $FORWARDEDPORT -j ACCEPT # enable port forwarding 113 | iptables -A INPUT -i $VPNDEVICE -p udp --dport $FORWARDEDPORT -j ACCEPT 114 | fi 115 | 116 | if [ $FLAN -eq 1 ];then 117 | iptables -A OUTPUT -o $DEFAULTDEVICE -d $LAN -j ACCEPT # enable incoming and outgoing connections within LAN (potentially dangerous!) 118 | iptables -A INPUT -i $DEFAULTDEVICE -s $LAN -j ACCEPT 119 | fi 120 | echo "$INFO Firewall enabled." 121 | } 122 | 123 | fresetfirewall() 124 | { 125 | iptables --policy INPUT ACCEPT 126 | iptables --policy OUTPUT ACCEPT 127 | iptables --policy FORWARD ACCEPT 128 | iptables -Z 129 | iptables -F 130 | iptables -X 131 | } 132 | 133 | flockdown() 134 | { 135 | fresetfirewall 136 | iptables -P OUTPUT DROP 137 | iptables -P INPUT DROP 138 | iptables -P FORWARD DROP 139 | } 140 | 141 | fhelp() # Help function. 142 | { 143 | echo """Usage: $(basename $0) [options] 144 | 145 | -s - Server number to connect to. 146 | -l - List available servers. 147 | -u - Update PIA openvpn files before connecting. 148 | -p - Forward a port. 149 | -n - Change to another random port. 150 | -d - Change DNS servers to PIA. 151 | -f - Enable firewall to block all non tunnel traffic. 152 | -e - Allow LAN through firewall. 153 | -m - Enable PIA MACE ad blocking. 154 | -k - Enable internet killswitch. 155 | -x - Encrypt the credetials file. 156 | -v - Display verbose information. 157 | -h - Display this help. 158 | 159 | Examples: 160 | pia -dps 6 - Change DNS, forward a port and connect to CA_Montreal. 161 | pia -nfv - Forward a new port, run firewall and be verbose. 162 | """ 163 | } 164 | 165 | fvpnreset() # Restore all settings and exit openvpn gracefully. 166 | { 167 | echo 168 | if [ $DNS -eq 1 ];then 169 | fdnsrestore 170 | fi 171 | 172 | kill -s SIGINT $VPNPID &>/dev/null 173 | 174 | if [ $RESTARTVPN -eq 0 ];then 175 | if [[ $FIREWALL -eq 1 && $KILLS -eq 0 ]];then 176 | fresetfirewall 177 | echo "$INFO Firewall disabled." 178 | elif [[ $KILLS -eq 1 && $RESTARTVPN -lt 1 ]];then 179 | echo -e "\r $BOLD$RED[$BOLD$GREEN*$BOLD$RED] WARNING:$RESET Killswitch engaged, no internet will be available until you run this script again." 180 | flockdown 181 | echo > $VPNPATH/.killswitch 182 | fi 183 | 184 | echo "$INFO VPN Disconnected." 185 | exit 0 186 | else 187 | RESTARTVPN=0 188 | echo "$PROMPT Restarting VPN..." 189 | fi 190 | } 191 | 192 | fdnschange() # Change DNS servers to PIA. 193 | { 194 | cp /etc/resolv.conf /etc/resolv.conf.bak 195 | echo '''#PIA DNS Servers 196 | nameserver 209.222.18.222 197 | nameserver 209.222.18.218 198 | ''' > /etc/resolv.conf.pia 199 | cp /etc/resolv.conf.pia /etc/resolv.conf 200 | echo "$INFO Changed DNS to PIA servers." 201 | } 202 | 203 | fmace() # Enable PIA MACE DNS based ad blocking. 204 | { 205 | curl -s "http://209.222.18.222:1111/" 206 | echo "$INFO PIA MACE enabled." 207 | } 208 | 209 | fdnsrestore() # Revert to original DNS servers. 210 | { 211 | cp /etc/resolv.conf.bak /etc/resolv.conf 212 | echo "$INFO Restored DNS servers." 213 | } 214 | 215 | flist() # List available servers. 216 | { 217 | if [ ! -f $VPNPATH/servers.txt ];then 218 | fupdate 219 | fi 220 | 221 | echo "$INFO$BOLD$GREEN Green$RESET servers allow port forwarding." 222 | for i in $(seq $(cat $VPNPATH/servers.txt | wc -l));do 223 | echo -n " $BOLD$RED[$RESET$i$BOLD$RED]$RESET " 224 | SERVERNAME=$(cat $VPNPATH/servers.txt | head -n $i | tail -n 1 | awk '{print $1}') 225 | case $SERVERNAME in 226 | "Netherlands") echo $BOLD$GREEN$SERVERNAME$RESET;; 227 | "Switzerland") echo $BOLD$GREEN$SERVERNAME$RESET;; 228 | "CA_Toronto") echo $BOLD$GREEN$SERVERNAME$RESET;; 229 | "CA_Montreal") echo $BOLD$GREEN$SERVERNAME$RESET;; 230 | "CA_Vancouver") echo $BOLD$GREEN$SERVERNAME$RESET;; 231 | "Romania") echo $BOLD$GREEN$SERVERNAME$RESET;; 232 | "Israel") echo $BOLD$GREEN$SERVERNAME$RESET;; 233 | "Sweden") echo $BOLD$GREEN$SERVERNAME$RESET;; 234 | "France") echo $BOLD$GREEN$SERVERNAME$RESET;; 235 | "Germany") echo $BOLD$GREEN$SERVERNAME$RESET;; 236 | *) echo $SERVERNAME;; 237 | esac 238 | done 239 | } 240 | 241 | fchecklog() # Check openvpn logs to get connection state. 242 | { 243 | LOGRETURN=0 244 | while [ $LOGRETURN -eq 0 ];do 245 | VCONNECT=$(cat /var/log/pia.log) 246 | if [ $(echo "$VCONNECT" | grep 'auth-failure' | wc -c) -gt 1 ];then 247 | LOGRETURN=2 248 | elif [ $(echo "$VCONNECT" | grep 'RESOLVE: Cannot resolve host address' | wc -c) -gt 1 ];then 249 | LOGRETURN=3 250 | elif [ $(echo "$VCONNECT" | grep 'process exiting' | wc -c) -gt 1 ];then 251 | LOGRETURN=4 252 | elif [ $(echo "$VCONNECT" | grep 'Exiting due to fatal error' | wc -c) -gt 1 ];then 253 | LOGRETURN=5 254 | elif [ $(echo "$VCONNECT" | grep 'ERROR: Linux route add command failed:' | wc -c) -gt 1 ];then 255 | LOGRETURN=6 256 | elif [ $(echo "$VCONNECT" | grep 'Initialization Sequence Completed' | wc -c) -gt 1 ];then 257 | LOGRETURN=1 258 | fi 259 | sleep 0.2 260 | done 261 | } 262 | 263 | fping() # Get latency to VPN server. 264 | { 265 | PINGINT=0 266 | while [ $PINGINT -lt 1 ];do 267 | PING=$(ping -c 3 $1 | grep rtt | cut -d '/' -f 4 | awk '{print $3}') 268 | PINGINT=$(echo $PING | cut -d '.' -f 1) 269 | done 270 | 271 | SPEEDCOLOR=$BOLD$GREEN 272 | SPEEDNAME="fast" 273 | if [ $PINGINT -gt 39 ];then 274 | SPEEDCOLOR=$BOLD$CYAN 275 | SPEEDNAME="medium" 276 | fi 277 | if [ $PINGINT -gt 79 ];then 278 | SPEEDCOLOR=$BOLD$BLUE 279 | SPEEDNAME="slow" 280 | fi 281 | if [ $PINGINT -gt 159 ];then 282 | SPEEDCOLOR=$BOLD$RED 283 | SPEEDNAME="very slow" 284 | fi 285 | } 286 | 287 | fcheckupdate() # Check if a new config zip is available and download. 288 | { 289 | CONFIGNUM=$(cat $VPNPATH/configversion.txt | cut -d ' ' -f 1) 290 | CONFIGURL=$(cat $VPNPATH/configversion.txt | cut -d ' ' -f 2) 291 | CONFIGVERSION=$(cat $VPNPATH/configversion.txt | cut -d ' ' -f 3-) 292 | CONFIGMODIFIED='' 293 | while [ $(echo $CONFIGMODIFIED | wc -c) -lt 6 ];do 294 | CONFIGMODIFIED=$(curl -sI $CONFIGURL | grep Last-Modified | cut -d ' ' -f 2-) 295 | done 296 | if [ $(echo $CONFIGMODIFIED | wc -c) -gt 6 ];then 297 | if [ "$CONFIGVERSION" != "$CONFIGMODIFIED" ];then 298 | RESTARTVPN=1 299 | echo "$ERROR WARNING: OpenVPN configuration is out of date!" 300 | echo "$PROMPT New PIA OpenVPN config file available! Updating..." 301 | else 302 | if [[ $VERBOSE -eq 1 && UPDATEOUTPUT -eq 1 ]];then 303 | echo "$INFO OpenVPN configuration up to date: $CONFIGMODIFIED" 304 | fi 305 | fi 306 | else 307 | if [ $UPDATEOUTPUT -eq 1 ];then 308 | echo "$ERROR Failed to check OpenVPN config Last-Modified date!" 309 | fi 310 | fi 311 | UPDATEOUTPUT=0 312 | } 313 | 314 | fconnect() # Main function 315 | { 316 | if [ $UNLOCK -eq 1 ];then 317 | fresetfirewall 318 | UNLOCK=0 319 | fi 320 | 321 | if [ $VERBOSE -eq 1 ];then 322 | echo -n "$PROMPT Testing latency to $DOMAIN..." 323 | fping $DOMAIN 324 | echo -e "\r$INFO $SERVERNAME latency: $SPEEDCOLOR$PING ms ($SPEEDNAME)$RESET " 325 | fi 326 | 327 | if [[ -f $VPNPATH/pass.enc && $(echo $CREDS | wc -c) -lt 3 ]];then 328 | fdecryptcreds 329 | fi 330 | 331 | if [ $(echo $CREDS | wc -c) -gt 3 ];then 332 | echo "$CREDS" > $VPNPATH/pass.txt 333 | fi 334 | 335 | echo -n "$PROMPT Connecting to $BOLD$GREEN$SERVERNAME$RESET, Please wait..." 336 | cd $VPNPATH && openvpn --config $CONFIG --daemon 337 | VPNPID=$(ps aux | grep openvpn | grep root | grep -v grep | awk '{print $2}') 338 | 339 | fchecklog 340 | 341 | case $LOGRETURN in 342 | 1) echo -e "\r$INFO$BOLD$GREEN Connected$RESET, OpenVPN is running daemonized on PID $BOLD$CYAN$VPNPID$RESET ";; 343 | 2) echo -e "\r$ERROR Authorization Failed. Please rerun script to enter correct login details. ";rm $VPNPATH/pass.txt;kill -s SIGINT $VPNPID&>/dev/null;exit 1;; 344 | 3) echo -e "\r$ERROR OpenVPN failed to resolve $DOMAIN. ";kill -s SIGINT $VPNPID&>/dev/null;exit 1;; 345 | 4) echo -e "\r$ERROR OpenVPN exited unexpectedly. Please review log: ";cat /var/log/pia.log;kill -s SIGINT $VPNPID&>/dev/null;exit 1;; 346 | 5) echo -e "\r$ERROR OpenVPN suffered a fatal error. Please review log: ";cat /var/log/pia.log;kill -s SIGINT $VPNPID&>/dev/null;exit 1;; 347 | 6) echo -e "\r$ERROR OpenVPN failed to add routes. Please review log: ";cat /var/log/pia.log;kill -s SIGINT $VPNPID&>/dev/null;exit 1;; 348 | esac 349 | 350 | if [ $ENCRYPT -eq 1 ];then 351 | CREDS="$(cat $VPNPATH/pass.txt 2>/dev/null)" 352 | fencryptcreds 353 | fi 354 | 355 | UPDATEOUTPUT=1 356 | fcheckupdate 357 | if [ $RESTARTVPN -eq 1 ];then 358 | fupdate 359 | fvpnreset 360 | return 0 361 | fi 362 | 363 | PLOG=$(cat /var/log/pia.log) 364 | 365 | if [ $VERBOSE -eq 1 ];then 366 | echo "$INFO OpenVPN Logs:" 367 | echo -n $CYAN 368 | while IFS= read -r LNE ;do echo " $LNE" | awk '{$1=$2=$3=$4=$5=""; print $0}';done <<< "$PLOG" 369 | echo "$RESET$PROMPT OpenVPN Settings:" 370 | SETTINGS=$(cat $VPNPATH/$CONFIG) 371 | if [ $(echo "$SETTINGS" | grep 'proto udp' | wc -c) -gt 3 ];then 372 | echo "$INFO$BOLD$GREEN UDP$RESET Protocol." 373 | fi 374 | if [ $(echo "$SETTINGS" | grep 'proto tcp' | wc -c) -gt 3 ];then 375 | echo "$INFO$BOLD$CYAN TCP$RESET Protocol." 376 | fi 377 | if [ $(echo "$SETTINGS" | grep 'ca.rsa.2048.crt' | wc -c) -gt 3 ];then 378 | echo "$INFO$BOLD$CYAN 2048 Bit RSA$RESET Certificate." 379 | fi 380 | if [ $(echo "$SETTINGS" | grep 'ca.rsa.4096.crt' | wc -c) -gt 3 ];then 381 | echo "$INFO$BOLD$GREEN 4096 Bit RSA$RESET Certificate." 382 | fi 383 | if [ $(echo "$SETTINGS" | grep 'cipher aes-128-cbc' | wc -c) -gt 3 ];then 384 | echo "$INFO$BOLD$CYAN 128 Bit AES-CBC$RESET Cipher." 385 | fi 386 | if [ $(echo "$SETTINGS" | grep 'cipher aes-256-cbc' | wc -c) -gt 3 ];then 387 | echo "$INFO$BOLD$GREEN 256 Bit AES-CBC$RESET Cipher." 388 | fi 389 | if [ $(echo "$SETTINGS" | grep 'auth sha1' | wc -c) -gt 3 ];then 390 | echo "$INFO$BOLD$CYAN SHA1$RESET Authentication." 391 | fi 392 | if [ $(echo "$SETTINGS" | grep 'auth sha256' | wc -c) -gt 3 ];then 393 | echo "$INFO$BOLD$GREEN SHA256$RESET Authentication." 394 | fi 395 | 396 | echo -n "$PROMPT Fetching IP..." 397 | while [ $(echo $NEWIP | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | wc -c) -lt 3 ];do 398 | NEWIP=$(curl -s -m 4 icanhazip.com) 399 | done 400 | 401 | while [ $(echo $MYIP | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | wc -c) -lt 3 ];do 402 | MYIP=$(cat /tmp/ip.txt) 403 | sleep 0.3 404 | done 405 | 406 | if [ $(echo $NEWIP | wc -c) -gt 2 ];then 407 | WHOISOLD="$(whois $MYIP)" 408 | WHOISNEW="$(whois $NEWIP)" 409 | COUNTRYOLD=$(echo "$WHOISOLD" | grep country | head -n 1) 410 | COUNTRYNEW=$(echo "$WHOISNEW" | grep country | head -n 1) 411 | DESCROLD="$(echo "$WHOISOLD" | grep descr)"$RESET 412 | DESCRNEW="$(echo "$WHOISNEW" | grep descr)"$RESET 413 | 414 | echo -e "\r$PROMPT Old IP:$RED$BOLD $MYIP" 415 | if [ $(echo $COUNTRYOLD | wc -c) -gt 8 ];then 416 | while IFS= read -r LNE ;do echo " $LNE";done <<< "$COUNTRYOLD" 417 | fi 418 | if [ $(echo $DESCROLD | wc -c) -gt 8 ];then 419 | while IFS= read -r LNE ;do echo " $LNE";done <<< "$DESCROLD" 420 | fi 421 | echo "$PROMPT Current IP:$GREEN$BOLD $NEWIP" 422 | if [ $(echo $COUNTRYNEW | wc -c) -gt 8 ];then 423 | while IFS= read -r LNE ;do echo " $LNE";done <<< "$COUNTRYNEW" 424 | fi 425 | if [ $(echo $DESCRNEW | wc -c) -gt 8 ];then 426 | while IFS= read -r LNE ;do echo " $LNE";done <<< "$DESCRNEW" 427 | fi 428 | else 429 | echo -e "\r$ERROR Failed to fetch new IP. " 430 | fi 431 | fi 432 | 433 | if [ $PORTFORWARD -eq 1 ];then 434 | if [ $NEWPORT -eq 1 ]; then 435 | echo -e "\r$INFO Identity changed to $BOLD$GREEN$(cat $VPNPATH/client_id)$RESET" 436 | else 437 | if [ $VERBOSE -eq 1 ];then 438 | echo -e "\r$PROMPT Using port forwarding identity $BOLD$CYAN$(cat $VPNPATH/client_id)$RESET" 439 | fi 440 | fi 441 | case $SERVERNAME in 442 | "Netherlands") fforward;; 443 | "Switzerland") fforward;; 444 | "CA_Toronto") fforward;; 445 | "CA_Montreal") fforward;; 446 | "Romania") fforward;; 447 | "Israel") fforward;; 448 | "Sweden") fforward;; 449 | "France") fforward;; 450 | "Germany") fforward;; 451 | "CA_Vancouver") fforward;; 452 | *) NOPORT=1;; 453 | esac 454 | if [ $NOPORT -eq 0 ];then 455 | if [ $FORWARDEDPORT -gt 0 ] &>/dev/null;then 456 | echo -e "\r$INFO Port $GREEN$BOLD$FORWARDEDPORT$RESET has been forwarded to you. " 457 | else 458 | echo -e "\r$ERROR $SERVERNAME failed to forward us a port! " 459 | fi 460 | else 461 | echo "$ERROR Port forwarding is only available at: Netherlands, Switzerland, CA_Toronto, CA_Montreal, CA_Vancouver, Romania, Israel, Sweden, France and Germany." 462 | fi 463 | fi 464 | 465 | if [ $DNS -eq 1 ];then 466 | fdnschange 467 | fi 468 | 469 | if [ $MACE -eq 1 ];then 470 | fmace 471 | fi 472 | 473 | if [ $FIREWALL -eq 1 ];then 474 | ffirewall 475 | fi 476 | if [[ $KILLS -eq 1 && $VERBOSE -eq 1 ]];then 477 | echo "$PROMPT Killswitch activated." 478 | fi 479 | 480 | 481 | echo -n "$INFO VPN setup complete, press$BOLD$RED Ctrl+C$RESET to shut down." 482 | flogwatcher 483 | if [ $RESTARTVPN -eq 0 ];then 484 | echo -e "\r$ERROR$RED$BOLD WARNING:$RESET New OpenVPN log entries detected: " 485 | NEWLOGS=$(cat /var/log/pia.log | tail -n +$(($LOGLENGTH + 1)) | sed '/^$/d') 486 | while IFS= read -r LNE ;do echo "$BOLD$RED $LNE$RESET";done <<< "$NEWLOGS" 487 | RESTARTVPN=1 488 | if [ $FIREWALL -eq 1 ];then 489 | flockdown 490 | UNLOCK=1 491 | fi 492 | else 493 | fupdate 494 | fi 495 | fvpnreset 496 | } 497 | 498 | flogwatcher() # Check the log for new entries 499 | { 500 | LOGLENGTH=$(cat /var/log/pia.log | wc -l) 501 | LOGALERT=$LOGLENGTH 502 | while [ $LOGALERT -eq $LOGLENGTH ];do 503 | sleep 20 504 | LOGALERT=$(cat /var/log/pia.log | wc -l) 505 | fcheckupdate 506 | if [ $RESTARTVPN -eq 1 ];then 507 | return 0 508 | fi 509 | done 510 | 511 | } 512 | 513 | fgetip() # Get external IP 514 | { 515 | while [ $(echo $MYIP | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | wc -c) -lt 3 ];do 516 | MYIP=$(curl -m 5 -s icanhazip.com) 517 | done 518 | echo $MYIP > /tmp/ip.txt 519 | } 520 | 521 | fdecryptcreds() 522 | { 523 | if [ $(cat $VPNPATH/pass.txt 2>/dev/null | wc -c) -lt 6 ];then 524 | if [ $(cat $VPNPATH/pass.enc | wc -c) -gt 3 ];then 525 | echo "$PROMPT Decrypting creds.." 526 | cat $VPNPATH/pass.enc | openssl base64 -d | openssl enc -d -aes-256-cbc > $VPNPATH/pass.txt 527 | chmod 400 $VPNPATH/pass.txt && CREDS="$(cat $VPNPATH/pass.txt 2>/dev/null)" 528 | if [ $ENCRYPT -eq 0 ];then 529 | rm $VPNPATH/pass.enc 530 | fi 531 | else 532 | rm $VPNPATH/pass.enc 533 | fi 534 | fi 535 | } 536 | 537 | fencryptcreds() 538 | { 539 | echo "$INFO Encypting creds.." 540 | cat $VPNPATH/pass.txt 2>/dev/null | openssl enc -e -aes-256-cbc -a > $VPNPATH/pass.enc && rm $VPNPATH/pass.txt && chmod 400 $VPNPATH/pass.enc 541 | } 542 | 543 | 544 | # Colour codes for terminal. 545 | BOLD=$(tput bold) 546 | BLUE=$(tput setf 1) 547 | GREEN=$(tput setf 2) 548 | CYAN=$(tput setf 3) 549 | RED=$(tput setf 4) 550 | RESET=$(tput sgr0) 551 | 552 | INFO=" [$BOLD$GREEN*$RESET]" 553 | ERROR=" [$BOLD$RED"'X'"$RESET]" 554 | PROMPT=" [$BOLD$BLUE>$RESET]" 555 | 556 | # This is where we will store PIA openVPN files and user config. 557 | VPNPATH='/etc/openvpn/pia' 558 | 559 | # Initialize switches. 560 | PORTFORWARD=0 561 | NEWPORT=0 562 | NEWIP=0 563 | NOPORT=0 564 | MACE=0 565 | KILLS=0 566 | DNS=0 567 | FORWARDEDPORT=0 568 | VERBOSE=0 569 | FIREWALL=0 570 | SERVERNUM=0 571 | FLAN=0 572 | UNKNOWNOS=0 573 | MISSINGDEP=0 574 | CONFIGNUM=0 575 | RESTARTVPN=0 576 | UNLOCK=0 577 | UPDATEOUTPUT=0 578 | ENCRYPT=0 579 | CREDS=0 580 | 581 | # Check if user is root. 582 | if [ $(id -u) != 0 ];then echo "$ERROR Script must be run as root." && exit 1;fi 583 | 584 | # Check for missing dependencies and install. 585 | if [ $(command -v pacman) ];then 586 | INSTALLCMD="pacman --noconfirm -S" 587 | elif [ $(command -v apt-get) ];then 588 | INSTALLCMD="apt-get install -y" 589 | elif [ $(command -v yum) ];then 590 | INSTALLCMD="yum install -y" 591 | else 592 | UNKNOWNOS=1 593 | fi 594 | 595 | if [ $UNKNOWNOS -eq 1 ];then 596 | command -v openvpn >/dev/null 2>&1 || MISSINGDEP=1 597 | command -v openssl >/dev/null 2>&1 || MISSINGDEP=1 598 | command -v iptables >/dev/null 2>&1 || MISSINGDEP=1 599 | command -v curl >/dev/null 2>&1 || MISSINGDEP=1 600 | command -v unzip >/dev/null 2>&1 || MISSINGDEP=1 601 | if [ $MISSINGDEP -eq 1 ];then 602 | echo "$ERROR OS not identified as arch or debian based, please install openvpn, openssl, iptables, curl and unzip and run script again." 603 | exit 1 604 | fi 605 | else 606 | command -v openvpn >/dev/null 2>&1 || { echo >&2 "$INFO openvpn required, installing...";$INSTALLCMD openvpn; } 607 | command -v openssl >/dev/null 2>&1 || { echo >&2 "$INFO openssl required, installing...";$INSTALLCMD openssl; } 608 | command -v iptables >/dev/null 2>&1 || { echo >&2 "$INFO iptables required, installing...";$INSTALLCMD iptables; } 609 | command -v curl >/dev/null 2>&1 || { echo >&2 "$INFO curl required, installing...";$INSTALLCMD curl; } 610 | command -v unzip >/dev/null 2>&1 || { echo >&2 "$INFO unzip required, installing...";$INSTALLCMD unzip; } 611 | fi 612 | 613 | 614 | if [ ! -d $VPNPATH ];then mkdir -p $VPNPATH;fi 615 | 616 | # Check for existence of credentials file. 617 | if [[ ! -f $VPNPATH/pass.txt && ! -f $VPNPATH/pass.enc ]];then 618 | read -p "$PROMPT Please enter your username: " USERNAME 619 | read -sp "$PROMPT Please enter your password: " PASSWORD 620 | echo -e "$USERNAME\n$PASSWORD" > $VPNPATH/pass.txt 621 | echo 622 | chmod 400 $VPNPATH/pass.txt 623 | unset USERNAME PASSWORD 624 | fi 625 | 626 | while getopts "lhupnmkdfvxes:" opt 627 | do 628 | case $opt in 629 | l) flist;exit 0;; 630 | h) fhelp;exit 0;; 631 | u) fupdate;; 632 | p) PORTFORWARD=1;; 633 | n) fnewport;; 634 | m) MACE=1;DNS=1;; 635 | k) KILLS=1;FIREWALL=1;; 636 | d) DNS=1;; 637 | f) FIREWALL=1;; 638 | e) FLAN=1;FIREWALL=1;; 639 | v) VERBOSE=1;fgetip&;; 640 | x) ENCRYPT=1;; 641 | s) SERVERNUM=$OPTARG;; 642 | *) echo "$ERROR Error: Unrecognized arguments.";fhelp;exit 1;; 643 | esac 644 | done 645 | 646 | if [ ! -f $VPNPATH/servers.txt ];then fupdate;fi 647 | MAXSERVERS=$(cat $VPNPATH/servers.txt | wc -l) 648 | 649 | if [ $SERVERNUM -lt 1 ];then 650 | echo "$PROMPT Please choose a server: " 651 | flist 652 | read -p "$PROMPT " SERVERNUM 653 | clear 654 | fi 655 | 656 | trap fvpnreset INT 657 | 658 | if [[ $SERVERNUM =~ ^[0-9]+$ && $SERVERNUM -gt 0 && $SERVERNUM -le $MAXSERVERS ]];then 659 | : 660 | else 661 | flist 662 | echo "$ERROR $SERVERNUM is not valid! 1-$MAXSERVERS only." 663 | exit 1 664 | fi 665 | 666 | SERVERNAME=$(cat $VPNPATH/servers.txt | head -n $SERVERNUM | tail -n 1 | awk '{print $1}') 667 | DOMAIN=$(cat $VPNPATH/servers.txt | head -n $SERVERNUM | tail -n 1 | awk '{print $2}') 668 | CONFIG=$SERVERNAME.ovpn 669 | 670 | if [ -f $VPNPATH/.killswitch ];then 671 | UNLOCK=1 672 | rm -rf $VPNPATH/.killswitch 673 | fi 674 | 675 | while [ true ];do 676 | fconnect 677 | done 678 | --------------------------------------------------------------------------------