⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demo_archive.php

📁 vpn虚拟专用网络的一个开源搭建工具
💻 PHP
字号:
<?php
require('lib/archive.php');
include('lib/function.php');
require('lib/IPv4.php');

$name = $_POST["name"];
$description = $_POST["description"];
$netclass = $_POST["netclass"];
$max_usr = $_POST["max_usr"];
$type_vpn = $_POST["type_vpn"];
$file_key = $_POST["file_key"];
$host_srv = $_POST["host_srv"];
$port_srv = $_POST["port_srv"];

if ($name == "") {
	echo "Invalid name<br>";
	echo "<a href=\"javascript:history.back();\">Go back</a>";
	exit();
}

if ($netclass == "A") { 
	$max_usr = $_POST["max_usrA"];
	$num1 = $_POST["num1A"];
	$num2 = $_POST["num2A"];
	$num3 = $_POST["num3A"];
	$num4 = $_POST["num4A"];
}
if ($netclass == "C") {
	$max_usr = $_POST["max_usrC"];
	$num1 = $_POST["num1C"];
	$num2 = $_POST["num2C"];
	$num3 = $_POST["num3C"];
	$num4 = $_POST["num4C"];
}
// create ip subnet
$subnet = "$num1"."."."$num2"."."."$num3"."."."$num4";

// create ip netmask
if ($max_usr == "16777214") $netmask="255.0.0.0";
if ($max_usr == "8388606") $netmask="255.128.0.0";
if ($max_usr == "4194302") $netmask="255.192.0.0";
if ($max_usr == "2097150") $netmask="255.224.0.0";
if ($max_usr == "1048574") $netmask="255.240.0.0";
if ($max_usr == "524286") $netmask="255.248.0.0";
if ($max_usr == "262142") $netmask="255.252.0.0";
if ($max_usr == "131070") $netmask="255.254.0.0";
if ($max_usr == "65534") $netmask="255.255.0.0";
if ($max_usr == "32766") $netmask="255.255.128.0";
if ($max_usr == "16382") $netmask="255.255.192.0";
if ($max_usr == "8190") $netmask="255.255.224.0";
if ($max_usr == "4094") $netmask="255.255.240.0";
if ($max_usr == "2046") $netmask="255.255.248.0";
if ($max_usr == "1022") $netmask="255.255.252.0";
if ($max_usr == "510") $netmask="255.255.254.0";
if ($max_usr == "254") $netmask="255.255.255.0";
if ($max_usr == "126") $netmask="255.255.255.128";
if ($max_usr == "62") $netmask="255.255.255.192";
if ($max_usr == "30") $netmask="255.255.255.224";
if ($max_usr == "14") $netmask="255.255.255.240";
if ($max_usr == "6") $netmask="255.255.255.248";
if ($max_usr == "2") $netmask="255.255.255.252";

// create IPv4 object
$ip_calc = new Net_IPv4();
// set variables
$ip_calc->ip = $subnet;
$ip_calc->netmask = $netmask;
// calculate
$error = $ip_calc->calculate();
if (is_object($error)) {
	echo "An error occured: ".$error->getMessage();
	exit();
}

if ($ip_calc->network != $subnet) {
	echo "Invalid subnet<br>";
	echo "<a href=\"javascript:history.back();\">Go back</a>";
	exit();
}

if ($type_vpn == "NOTCRYPT") {
	$file_key="NULL";
	$key="NULL";
}
if (($type_vpn == "CRYPT") && ($file_key == "")) {
	echo "Invalid filename or null value of the key, please enter a valid name<br>";
	echo "<a href=\"javascript:history.back();\">Go back</a>";
	exit();
}
if ($type_vpn == "CRYPT") {
	if(($fhandle = fopen('/dev/urandom','rb')) != FALSE) {
		set_magic_quotes_runtime(0);
		for($i=0;$i<256;$i++) {
			$val = ord(fgetc($fhandle));
			if($val <= 0x0f) $rb = sprintf("0%X",$val);
			else $rb = sprintf("%X",$val);
			$rs = strtolower($rs);
			if (($i!=0) && ($i%16==0)) $rs .= "
";
			$rs .= "$rb";
		}
		fclose($fhandle);
	}
	$key = "#
# 2048 bit OpenVPN static key
#
-----BEGIN OpenVPN Static key V1-----
";
$key = $key. "$rs
-----END OpenVPN Static key V1-----
";
}

if ($host_srv != "my_vpn.dyndns.org" || $host_srv != "") {
	if ($host_srv != gethostbyname($host_srv)) {
		$ip_host = new Net_IPv4();
		if (! $ip_host->validateIP(gethostbyname($host_srv))) {
			echo "Invalid Hostname or IP Address<br>";
			echo "<a href=\"javascript:history.back();\">Go back</a>";
			exit();
		}
	}
	else {
		$ip_host = new Net_IPv4();
		if (! $ip_host->validateIP($host_srv)) {
			echo "Invalid Hostname or IP Address<br>";
			echo "<a href=\"javascript:history.back();\">Go back</a>";
			exit();
		}
	}
}
else {
	echo "Invalid Hostname or IP Address<br>";
	echo "<a href=\"javascript:history.back();\">Go back</a>";
	exit();
}

// ip_num generator
$ip = ip2long($subnet);
$nm = ip2long($netmask);
$nw = ($ip & $nm);
$bc = $nw | (~$nm);
$broadcast_addr = long2ip($bc);
$ip_num=CalculateIPRange($subnet, $broadcast_addr, 1);

$openvpn = "#remote ".$host_srv;
$openvpn = $openvpn. "
port ".$port_srv."
dev tap
ifconfig ".$ip_num." ".$netmask;
if ($type_vpn!="NOTCRYPT") $openvpn = $openvpn. "
secret ".$file_key;
$openvpn = $openvpn. "
persist-tun
ping 10
comp-lzo
verb 4
mute 10
";

@mkdir("/tmp/conf", 0766);
$d = opendir("/tmp/conf");
while ($file = readdir($d)) @unlink("/tmp/conf/$file");
closedir($d);

$file=fopen("/tmp/conf/$name.ovpn", "w+");
if (flock($file, LOCK_EX)) {
	fputs($file,$openvpn);
	flock($file, LOCK_UN);
} else echo "lock file !";
fclose($file);

	$test = new zip_file("/tmp/conf/$name.zip");
	$test->set_options(array('inmemory' => 1, 'recurse' => 0, 'storepaths' => 0));
	$test->add_files("/tmp/conf/$name.ovpn");
	if ($type_vpn=="CRYPT") {
		$file=fopen("/tmp/conf/$file_key", "w+");
		if (flock($file, LOCK_EX)) {
			fputs($file,$key);
			flock($file, LOCK_UN);
		} else echo "lock file !";
		fclose($file);
		$test->add_files("/tmp/conf/$file_key");
	}
	$test->create_archive();

@unlink("/tmp/conf/$name.ovpn");
@unlink("/tmp/conf/$file_key");

$test->download_file();

exit();
?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -