用php實(shí)現(xiàn)proxy功能的示范 [已升級(jí)至v0.0.2]
Jun 21, 2016 am 09:15 AM剛剛寫的練手的作品。感覺還是有些實(shí)用價(jià)值的。。放出來給大家試試。
功能:通過php實(shí)現(xiàn)代理功能
適用用戶:在家下載較慢。且懶得找proxy。且有高速主機(jī),且主機(jī)有php,且主機(jī)支持socket。
使用方法:
1。把本程序傳到主機(jī)上
2。通過瀏覽器瀏覽主機(jī)上的此文件
3。在url中填入欲下載的文件地址(如果有必要,可以填入referer)
4。點(diǎn)擊get proxy url
5。新打開的頁面中的鏈接即為通過proxy下載的鏈接
注意:
1。支持多線程下載,斷點(diǎn)續(xù)傳
2。僅支持http協(xié)議
3。不支持服務(wù)器端header重定向
4。不支持http auth
5。下載較大的文件時(shí),可以修改程序中set_time_limit的值,以減少斷線的現(xiàn)象(默認(rèn)為180,即3分鐘)
PHP代碼:--------------------------------------------------------------------------------
/***************************************/
/* */
/* Php Http Proxy */
/* version: 0.0.2 */
/* last modify: 2005.1.12 */
/* author: q3boy
/* */
/***************************************/
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 = "調(diào)用 $mixArgv 方法時(shí)參數(shù)傳遞出錯(cuò), 枚舉值不存在";
}elseif($intError==1) {
$this->_strError = "url 錯(cuò)誤, url 格式不正確.\n".$mixArgvs;
}elseif($intError == 2) {
$this->_strError = "url 錯(cuò)誤, 目前尚未支持此協(xié)議.\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 默認(rèn)值
$this->_arrDefaultArrayUrl = array('protocol'=>'http','file'=>'index.htm');
//默認(rèn)端口
$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 {
//拆解匹配數(shù)組
list($tmp, $tmp, $arr['protocol'], $arr['user'], $arr['pass'], $arr['host'], $arr['port'], $arr['path'], $arr['query'], $arr['anchor']) = $arrUrl;
//默認(rèn)協(xié)議
if(!$arr['protocol']) {
$arrUrl[1] = $this->_arrDefaultArrayUrl['protocol'].'://';
}
//設(shè) filename
$arr['file'] = basename($arr['path']);
//默認(rèn)值
foreach($this->_arrDefaultArrayUrl as $key=>$val) {
if($arr[$key] == '') {
$arr[$key] = $val;
}
}
//默認(rèn)端口
if(is_null($this->_arrDefaultPort[$arr['protocol']])) {
Return $this->setError(2,$arr['protocol']);
}elseif(!$arr['port']) {
$arr['port'] = $this->_arrDefaultPort[$arr['protocol']];
}
//設(shè) uri
$arr['uri'] = ($arr['path']?$arr['path']:'/') . ($arr['query']?'?'.$arr['query']:'') . ($arr['anchor']?'#'.$arr['anchor']:'');
//設(shè) url
unset($arrUrl[0]);
unset($arrUrl[2]);
$this->_strUrl = implode('',$arrUrl);
//設(shè) 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('
');
}
$strUrl = trim(${$strGetArrayName}['u']);
if($strUrl=='') {
die('請(qǐng)輸入 url 地址.');
}
//get referer
$strRefTmp = trim(${$strGetArrayName}['r']);
//初始化 proxy 類
$objProxy = new CPhpProxy();
//設(shè)置 url 和 refer
$objProxy->setUrl($strUrl);
$objProxy->setRefer($strRef);
//錯(cuò)誤輸出
if($objProxy->getError()) {
die($objProxy->getError(PP_ERROR_MESSAGE));
}
//echo url for download
if(${$strGetArrayName}['act']=='make') {
die("點(diǎn)擊此處通過 PhpProxy 下載文件
}
//取文件
$objProxy->proxy();
//錯(cuò)誤處理
if($objProxy->getError()) {
die($objProxy->getError(PP_ERROR_MESSAGE));
}
?>

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

The usage of return in C language is: 1. For functions whose return value type is void, you can use the return statement to end the execution of the function early; 2. For functions whose return value type is not void, the function of the return statement is to end the execution of the function. The result is returned to the caller; 3. End the execution of the function early. Inside the function, we can use the return statement to end the execution of the function early, even if the function does not return a value.

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

Source code: publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#Output The output of the above code can simply conclude: return is executed before finally. Let's take a look at what happens at the bytecode level. The following intercepts part of the bytecode of the case1 method, and compares the source code to annotate the meaning of each instruction in

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore
