open($LocalZipFile) === TRUE) { logThis("[Unzipping file $LocalZipFile into $OpenVpnLocation]",true); $output->extractTo($OpenVpnLocation); $output->close(); } else { exit ("Exiting script. Unzip failed\n"); } // Delete the original zip file logThis("[Deleting Zip File $LocalZipFile]",true); unlink($LocalZipFile); // Change directory logThis("[Changing directory to $OpenVpnLocation]",true); chdir($OpenVpnLocation); // Create credential file $output = fopen($CredentialFilename, "w"); if ($output) { logThis("[Creating Credential File $CredentialFilename]",true); fwrite($output, $PiaUsername . PHP_EOL); fwrite($output, $PiaPassword . PHP_EOL); fclose($output); } else { exit ("Exiting script. Unable to create $CredentialFilename\n"); } // Update permissions on the credential file logThis("[Changing permissions on $CredentialFilename]",true); chmod($CredentialFilename, 0500); // Copy the openvpn file into the appropriate filename logThis("[Copying $LocationName to $VpnConfigFilename]",true); copy($LocationName, $VpnConfigFilename); // Update to have auth with login in the VpnConfigFilename logThis("Updating $VpnConfigFilename with login file]",true); $fileContent = file_get_contents($VpnConfigFilename); $newFileContent = str_replace($OldPattern, $NewPattern, $fileContent); file_put_contents($VpnConfigFilename, $newFileContent); // Helps to ensure no DNS leaks logThis("[Updating $VpnConfigFilename to help ensure no DNS leaks]",true); $myfile = file_put_contents($VpnConfigFilename, $DataAppend1.PHP_EOL,FILE_APPEND | LOCK_EX); // Enable openvpn with the config file to start automatically $FileNameParts = pathinfo($VpnConfigFilename); $VpnConfigFileBasename = $FileNameParts['filename']; // filename is only since PHP 5.2.0 //Baasename is the filename without the extension workService("openvpn@$VpnConfigFileBasename","enable"); // Clearing out old IP tables rules logThis("[Clear out the old IP tables rules]",true); runThis("iptables -F"); runThis("iptables -t nat -F"); runThis("iptables -t mangle -F"); runThis("iptables -X"); // Allow loopback device (internal communication) logThis("[Updating iptables to allow for loopback device (internal communication)]",true); runThis("iptables -A INPUT -i lo -j ACCEPT"); runThis("iptables -A OUTPUT -o lo -j ACCEPT"); // Allow all local traffic. logThis("[Updating iptables to allow for all local traffic]",true); runThis("iptables -A INPUT -s 10.0.0.0/8 -j ACCEPT"); runThis("iptables -A OUTPUT -d 10.0.0.0/8 -j ACCEPT"); runThis("iptables -A INPUT -s 172.16.0.0/12 -j ACCEPT"); runThis("iptables -A OUTPUT -d 172.16.0.0/12 -j ACCEPT"); runThis("iptables -A INPUT -s 192.168.0.0/16 -j ACCEPT"); runThis("iptables -A OUTPUT -d 192.168.0.0/16 -j ACCEPT"); // Allow VPN establishment with only 2 ports open, 1 for DNS and 1 for VPN // If establishing thru an IP and not DNS, the ones with port 53 can be removed // Port may be different depending on the VPN logThis("[Updating iptables to allow for 2 ports of communication]",true); runThis("iptables -A INPUT -p $Port1Type --sport $Port1Number -j ACCEPT"); runThis("iptables -A OUTPUT -p $Port1Type --dport $Port1Number -j ACCEPT"); runThis("iptables -A INPUT -p $Port2Type --sport $Port2Number -j ACCEPT"); runThis("iptables -A OUTPUT -p $Port2Type --dport $Port2Number -j ACCEPT"); // Accept all TUN connections (tun = VPN tunnel) logThis("[Updating iptables to allow for all TUN connection traffic]",true); runThis("iptables -A OUTPUT -o tun+ -j ACCEPT"); runThis("iptables -A INPUT -i tun+ -j ACCEPT"); // Set default policies to drop all communication unless specifically allowed logThis("[Updating iptable to drop all communication unless specifically allowed]",true); runThis("iptables -P INPUT DROP"); runThis("iptables -P OUTPUT DROP"); runThis("iptables -P FORWARD DROP"); // Bring up and down the network inteface with pauses logThis("[Bringin up and down the network interface]",true); runThis("ip link set $NetworkInterfaceName down"); runThis("ip link set $NetworkInterfaceName up"); // Stopping and starting openvpn service with pauses workService('openvpn','stop'); sleep(5); workService('openvpn','start'); sleep(5); // Installing iptables-persistent to save iptable rules on reboot runThis("echo iptables-persistent iptables-persistent/autosave_v4 boolean true | sudo debconf-set-selections"); runThis("echo iptables-persistent iptables-persistent/autosave_v6 boolean true | sudo debconf-set-selections"); installSoftware('iptables-persistent'); // Turning on netfilter-persistent and setting to start on restart runThis("netfilter-persistent save"); workService('netfilter-persistent','enable'); ?>