刚刚写的练手的作品。感觉还是有些实用价值的。。放出来给大家试试。
功能:通过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代码:——————————————————————————–
<?php
/***************************************/
/* */
/* php http proxy */
/* version: 0.0.2 */
/* last modify: 2005.1.12 */
/* author: q3boy<q3boy@sina.com> */
/* */
/***************************************/
set_time_limit(180);
define(pp_error_code,1);
define(pp_error_message,2);
define(pp_error_all,3);
define(pp_return_string,1);
define(pp_return_array,2);
define(pp_return_all,3);
class cphpproxy {
var $_strurl;
var $_arrargv;
var $_strref;
var $_arrurl;
var $_ressocket;
var $_interror;
var $_strerror;
var $_arrdefaultarrayurl;
var $_arrdefaultport;
function cphpproxy($strurl = null, $arrargv = array()) {
$this->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(<!doctype html public "-//w3c//dtd html 4.01 transitional//en""http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html">
<title>phpproxy</title>
<style type="text/css">
<!–body,td {font-family: verdana; font-size: 12px;}th,input {font-family: verdana;}–>
</style>
</head>
<body>
<form name="form1" method="get" action=".$_server[php_self].">
<table width="500" border="1" align="center" cellpadding="5" cellspacing="1">
<tr>
<th colspan="2">phpproxy</th>
</tr>
<tr>
<td width="39" nowrap>url:</td>
<td width="432"><input name="u" type="text" id="u" size="60"></td>
</tr>
<tr>
<td nowrap>referer:</td>
<td><input name="r" type="text" id="r" size="60"></td>
</tr>
<tr>
<td colspan="2" align="center"><input name="act" value="make" type="hidden">
<input type="submit" value="get proxy url">
<input type="reset" value="reset"></td>
</tr>
</table>
</form>
</body>
</html>);
}
$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("<center><h4><a href=".$objproxy->getproxyurl().">点击此处通过 phpproxy 下载文件</a></h2></center>");
}
//取文件
$objproxy->proxy();
//错误处理
if($objproxy->geterror()) {
die($objproxy->geterror(pp_error_message));
}
?>
