proxy
刚刚写的练手的作品。感觉还是有些实用价值的。。放出来给大家试试。
功能:通过php实现代理功能
适用用户:在家下载较慢。且懒得找proxy。且有高速主机,且主机有php,且主机支持socket。
使用方法:
1。把本程序传到主机上
2。通过浏览器浏览主机上的此文件
3。在url中填入欲下载的文件地址(如果有必要,可以填入referer)
4。点击get proxy url
5。新打开的页面中的链接即为通过proxy下载的链接
注意:
1。支持多线程下载,断点续传
2。仅支持http协议
3。不支持服务器端header重定向
4。不支持http auth
5。下载较大的文件时,可以修改程序中set_time_limit的值,以减少断线的现象(默认为180,即3分钟)
php代码:--------------------------------------------------------------------------------
init();
is_null($strurl)?':$this->seturl($strurl);
is_null($arrargv['refer'])?':$this->setref($arrargv['refer']);
$this->proxy($strurl,$arrargv);
}
function seterror($interror,$mixargv=null) {
$this->_interror = $interror;
if($interror==1001) {
$this->_strerror = 调用 $mixargv 方法时参数传递出错, 枚举值不存在;
}elseif($interror==1) {
$this->_strerror = url 错误, url 格式不正确.\n.$mixargvs;
}elseif($interror == 2) {
$this->_strerror = url 错误, 目前尚未支持此协议.\n.$mixargvs;
}elseif($interror == 3) {
$this->_strerror = 无法打开 .$this->_arrurl['host'].':'.$this->_arrurl['port'].\n.$mixargv[0].': '.$mixargv[1];
}
return false;
}
function geterror($enumreturntype=pp_error_code) {
if($enumreturntype==pp_error_all) {
return array(str_pad($this->_interror,5,'0',str_pad_left),$this->_strerror);
}elseif($enumreturntype==pp_error_message) {
return $this->_strerror;
}elseif($enumreturntype==pp_error_code) {
return $this->_interror;
}else {
return false;
}
}
function init() {
$this->_strurl = null;
$this->_arrargv = array();
$this->_strref = null;
$this->_arrurl = array();
$this->_ressocket = null;
$this->_interror = 0;
$this->_strerror = 'there is no error';
//arrurl 默认值
$this->_arrdefaultarrayurl = array('protocol'=>'http','file'=>'index.htm');
//默认端口
$this->_arrdefaultport =array('http'=>'80');
return true;
}
function seturl($strurl) {
$strurl = trim($strurl);
//正则
$reg = /^(([\w]+):\/\/)?([^:|\/|@]*:)?([^:|\/|@]*@)?([\w|\-|\.]+)(:\d+)?(\/[^\?|#]*)?(\?[^#]*)?(#.*)?$/is;
if(!preg_match($reg,$strurl,$arrurl)) {
return $this->seterror(1,$strurl);
}else {
//拆解匹配数组
list($tmp, $tmp, $arr['protocol'], $arr['user'], $arr['pass'], $arr['host'], $arr['port'], $arr['path'], $arr['query'], $arr['anchor']) = $arrurl;
//默认协议
if(!$arr['protocol']) {
$arrurl[1] = $this->_arrdefaultarrayurl['protocol'].'://';
}
//设 filename
$arr['file'] = basename($arr['path']);
//默认值
foreach($this->_arrdefaultarrayurl as $key=>$val) {
if($arr[$key] == ') {
$arr[$key] = $val;
}
}
//默认端口
if(is_null($this->_arrdefaultport[$arr['protocol']])) {
return $this->seterror(2,$arr['protocol']);
}elseif(!$arr['port']) {
$arr['port'] = $this->_arrdefaultport[$arr['protocol']];
}
//设 uri
$arr['uri'] = ($arr['path']?$arr['path']:'/') . ($arr['query']?'?'.$arr['query']:') . ($arr['anchor']?'#'.$arr['anchor']:');
//设 url
unset($arrurl[0]);
unset($arrurl[2]);
$this->_strurl = implode(',$arrurl);
//设 arrurl
$this->_arrurl = $arr;
return true;
}
}
function geturl($enumreturntype = pp_return_array) {
if($enumreturntype==pp_return_string) {
return $this->_strurl;
}elseif($enumreturntype==pp_return_array) {
return $this->_arrurl;
}if($enumreturntype==pp_return_all) {
return array($this->_strurl,$this->_arrurl);
}else {
return $this->seterror(1001);
}
}
function setrefer($strref) {
$this->_strref = trim($strref);
return true;
}
function getrefer() {
return $this->_strref;
}
function getproxyurl() {
global $_server;
list($strprocotol) = explode('/',strtolower(trim($_server['server_protocol'])));
$str = $strprocotol.'://'.$_server['http_host'].$_server['php_self'].
'?u='.urlencode($this->geturl(pp_return_string)).
($this->getrefer()?('&r='.urlencode($this->getrefer())):');
return $str;
}
function opensocket() {
$arr = $this->geturl();
$this->_ressocket = @fsockopen($arr['host'],$arr['port'],$interror, $strerror, 30);
if(!$this->_ressocket) {
$this->_ressocket = null;
return $this->seterror(3,array($interror, $strerror));
}else {
return true;
}
}
function getrequest() {
return $this->{'get'.ucfirst($this->_arrurl['protocol']).'request'}();
}
function sendrequest() {
return fwrite($this->_ressocket,$this->getrequest());
}
function flushresponse() {
return $this->{'flush'.ucfirst($this->_arrurl['protocol']).'response'}();
}
function gethttprequest() {
$arr = $this->geturl();
$arrrequest = getallheaders();
$arrrequest['host'] = $arr['host'];
$strrequest = get .$arr['uri']. http/1.1\r\n;
foreach($arrrequest as $key=>$val) {
$strrequest .=$key: $val\r\n;
}
$strrequest .=\r\n;
return $strrequest;
}
function flushhttpresponse() {
$bolheader=true;
while (!feof($this->_ressocket)) {
$str = fgets($this->_ressocket, 4096);
if($bolheader) {
@header($str);
}else {
echo($str);
}
if($bolheader && $str==\r\n) {
$bolheader=false;
@header(content-type: application/gzip);
@header('content-disposition: attachment; filename='.$this->_arrurl['file'].'');
}
}
return true;
}
function close() {
fclose($this->_ressocket);
$this->_ressocket = null;
return true;
}
function proxy($strurl = null,$arrargv = array()) {
if(!is_null($this->geturl(pp_return_string)) && $this->opensocket() && $this->sendrequest() && $this->flushresponse()) {
echo(123);
$this->close();
}else {
return false;
}
}
}
if(sizeof($_get)) {
$strgetarrayname = '_get';
}elseif(sizeof($http_get_vars)) {
$strgetarrayname = 'http_get_vars';
}else {
die('
phpproxy
phpproxy
url:
referer:
');
}
$strurl = trim(${$strgetarrayname}['u']);
if($strurl==') {
die('请输入 url 地址.');
}
//get referer
$strreftmp = trim(${$strgetarrayname}['r']);
//初始化 proxy 类
$objproxy = new cphpproxy();
//设置 url 和 refer
$objproxy->seturl($strurl);
$objproxy->setrefer($strref);
//错误输出
if($objproxy->geterror()) {
die($objproxy->geterror(pp_error_message));
}
//echo url for download
if(${$strgetarrayname}['act']=='make') {
die(getproxyurl().%22>点击此处通过 phpproxy 下载文件);
}
//取文件
$objproxy->proxy();
//错误处理
if($objproxy->geterror()) {
die($objproxy->geterror(pp_error_message));
}
?>