VPN Abstracting For On The Fly IP Changing With HMA, PIA etc.
 Page :   [ 1 ]    [ 2 ]    [ 3 ]    [ 4 ]  

Here is the PHP script that does the work:

NOTE: I hardcoded the IPv4 address of the server into this file for the sake of simplicity. (example IP 157.202.243.181)

I'm also simply rewriting the /etc/ppp/peers/piavpn and the /etc/ppp/chap-secrets files with the relevant data passed in via the querystring.  I'm sure there's a more elegant way to do this but it gets the job done! :)

<?php 


function changeVpn($vpn, $originalip){

	$ctx = stream_context_create(array('http'=>
			array(
					'timeout' => 5, // 1 200 Seconds = 20 Minutes
			)
	));

	exec("/usr/bin/sudo /usr/bin/poff piavpn");

	sleep(2);

	exec("/usr/bin/sudo /etc/clean.sh");

	sleep(2);

	$str = "pty \"pptp " . $vpn['ip'] . " --nolaunchpppd\"\nlock\nnoauth\nnobsdcomp\nnodeflate\nname " . $vpn['user'] . "\nremotename piavpn\nipparam piavpn\nrequire-mppe-128\nusepeerdns\ndefaultroute\npersist";

	file_put_contents('/etc/ppp/peers/piavpn', $str);

	$str = "" . $vpn['user'] . "\tpiavpn\t\"" . $vpn['pass'] . "\"\t*";

	file_put_contents('/etc/ppp/chap-secrets', $str);

	$dd = exec("/usr/bin/sudo /usr/bin/pon piavpn");

	$currentip = $originalip;

	$counter = 0;

	while (($currentip == $originalip) || (!strlen($currentip))){

		if ($counter){
			sleep(3);
		}

		$dat = @file_get_contents("http://ipecho.net/plain",false, $ctx);

		if (!(filter_var($dat, FILTER_VALIDATE_IP))) {

			$currentip = "";

		} else {
				
			$currentip = $dat;
		}

		$counter++;

		if ($counter > 5){
			return false;
		}

	}

	return true;

}

$act = isset($_REQUEST['act']) ? $_REQUEST['act'] : '';

$data = array();
$data['status'] = 'fail';

if ($act == 'changeip'){
	
	$ip = isset($_REQUEST['ip']) ? $_REQUEST['ip'] : '';
	$user = isset($_REQUEST['user']) ? $_REQUEST['user'] : '';
	$pass = isset($_REQUEST['pass']) ? $_REQUEST['pass'] : '';
	
	if ($ip == 'off'){

		$ret = shell_exec('/sbin/ifconfig | /bin/grep ppp0');
		
		$data['prior'] = $ret;
		
		exec("/usr/bin/sudo /usr/bin/poff piavpn");
		sleep(2);
		exec("/usr/bin/sudo /etc/clean.sh");
		sleep(1);
				
		$ret = shell_exec('/sbin/ifconfig | /bin/grep ppp0');

		$data['after'] = $ret;

		if (!strlen($ret)){
			$data['status'] = 'success';
		}
		
		header('Content-Type: application/json');
		
		echo json_encode($data);
		
		die();
		
	}	
	
	// hardcoding this.  the IP of the server this is on
	$originalip = '157.202.243.181';
	
	$vpn = array();
	
	$vpn['ip'] = $ip;
	$vpn['user'] = $user;
	$vpn['pass'] = $pass;
	
	$data['status'] = 'success';
	
	$ok = changeVpn($vpn, $originalip);
	
	if ($ok){
		
		$data['result'] = 'changed';

	} else {

		$data['result'] = 'notchanged';
		
		exec("/usr/bin/sudo /usr/bin/poff piavpn");

		sleep(2);
		
		exec("/usr/bin/sudo /etc/clean.sh");		
		
	}	
	
}

header('Content-Type: application/json');

echo json_encode($data);

die();

?>

Example:

Again this is the username and password supplied by PIA for making a PPTP connection

http://yourserver/script.php?user=PIA_USER&pass=PIA_PASS&ip=71.202.44.12&act=changeip

http://yourserver/script.php?ip=off&act=changeip

 

(Page 4 of 4)