Skip to content

Commit

Permalink
PHP Proxy Optim. #65
Browse files Browse the repository at this point in the history
  • Loading branch information
kimbtech committed Oct 24, 2024
1 parent 538be1e commit cb0c277
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
2.9.2
2.9.3
2.9
2
33 changes: 19 additions & 14 deletions php/classes/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,27 @@ class Helper {
* @param $link the url to follow its redirects
*/
public static function getFinalUrl( string $link ) : string {
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD');
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

if( curl_exec($ch) ){
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
} else{
$url = $link;
if( function_exists('curl_init') ){
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD');
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

if( curl_exec($ch) ){
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
} else{
$url = $link;
}

curl_close($ch);

return $url;
}

curl_close($ch);

return $url;
error_log("Warning: Please install CURL for better support of Proxy");
return $link;
}

public static function checkFilename( $n ) : bool {
Expand Down
3 changes: 3 additions & 0 deletions php/classes/SimpleProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ public static function open( $url ){

if( $f !== false ){
self::sendHeader($header);
// unset the execution limit (long time proxying)
set_time_limit(0);
// while something to proxy and still a client
while(!feof($f) && !connection_aborted() ){
echo fread($f, 128);
flush();
Expand Down
7 changes: 3 additions & 4 deletions php/stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,14 @@
if( !empty($url) && filter_var( $url, FILTER_VALIDATE_URL) !== false ){
$url = filter_var($url, FILTER_SANITIZE_URL); //clean url

// the proxy does not support redirects!, so do them before
$url = Helper::getFinalUrl($url);

if(!DOCKER_MODE){ // use a PHP based proxy
SimpleProxy::open( $url );
die();
}

// the proxy does not support redirects!, so do them before
$url = Helper::getFinalUrl($url);


// get hostname and url parts before and after
$matches = array();
$matchok = preg_match( '/^(https?:\/\/)([^\/]+\.?[a-zA-Z]+)((?::[0-9]+)?(?:\/.*)?)$/', $url, $matches ); // get host
Expand Down

0 comments on commit cb0c277

Please sign in to comment.