Squid sudah banyak dikenal sebagai aplikasi yang sangat membantu dalam mengakselerasi browsing di internet. Tapi
ternyata squid bisa juga digunakan untuk melakukan cache untuk dynamic content. Caranya adalah meng-combo-nya
dengan menggunakan apache dan php5.
Sebenarnya selain menggunakan apache, ada juga teknik lain menggunakan NGINX dan RUBY script untuk melakukan proses
cache youtube (http://www.mieorganik.com/squidtube.html). Tapi teknik ini kurang direkomendasikan untuk mereka yang
memiliki bandwidth terbatas. Karena dengan nginx, akan ada background download yang akan menguras bandwidth anda.
Sementara dengan apache+php ini, kita hanya akan menyimpan youtube video yang sudah terbuka sempurna. Dan apabila
user menghentikannya, proses download akan berhenti dengan sendirinya.
OK…let’s get start
REQUIREMENT
1. Squid 2.7 – 3.1 / Lusca
2. Apache + PHP5
Saya mengasumsikan anda sudah berhasil menginstal APACHE+PHP5 ya..apabila belum pernah menginstall silahkan merujuk
ke link dibawah ini atau bisa searching secara mandiri via google dengan keyword “how to install apache phpâ€.
http://www.lamphowto.com/
http://www.freebsdmadeeasy.com/tutor...eb-hosting.php
INSTALASI :
1. Sisipkan parameter dibawah ini di squid.conf anda :
Code:
acl youtube2 dstdomain .youtube.com # #(lokasi phpredir menyesuaikan) url_rewrite_program /usr/local/etc/squid/phpredir.php url_rewrite_access allow youtube2 url_rewrite_access deny all redirector_bypass on
kemudian ubah permissionnya : chmod 777 phpredir.php
Code:
#!/usr/local/bin/php
<?php
while ( $input = fgets(STDIN) ) {
// Split the output (space delimited) from squid into an array.
$input=explode(" ",$input);
if(preg_match("@youtube@",$input[0])){
$input[0]=urlencode($input[0]);
$input= implode(" ",$input);
echo "http://ip.address/per.php?url=$input"; //URL of my web server
}else
echo ""; // empty line means no re-write by Squid.
}
?>
apache)
kemudian ubah permissionnya : chmod 777 per.php
Code:
<?php
$file_path="/usr/local/www/youtube";
$logfile="$file_path/cache.log";
$url=urldecode($_GET['url']);
$urlptr=fopen($_GET['url'],"r");
$blocksize=32*1024;
//attempt to get. a 404 shouldn't happen, but...
if($urlptr===FALSE){
header("Status: 404 Not Found");
die();
}
//find content type and length
foreach($http_response_header as $line){
if(substr_compare($line,'Content-Type',0,12,true)==0)
$content_type=$line;
else if(substr_compare($line,'Content-Length',0,14,true)==0){
$content_length=$line;
}
}
if(!preg_match("@.*youtube.*videoplayback.*@",$url)){
fpassthru($urlptr);
fclose($urlptr);
exit(0);
}
//send content type and length
header($content_type);
header($content_length);
//find youtube id;
$url_exploded=explode('&',$url);
$id="";
foreach($url_exploded as $line){
if(substr($line,0,3)==='id=')
$id=substr($line,3);
}
//Get the supposed file size
$length=intval(substr($content_length,16));
file_put_contents($logfile,"\nFound id=$id, content-type: $content_type contentlength=$
content_length\n",FILE_APPEND);
//Do we have it? delivar if we do
$fname="$file_path/$id-$length";
//Check if we have the file, and it is the correct size. incorrect size implies corruption
if(file_exists($fname) &&filesize($fname)==$length){
// if(file_exists($fname)) {
readfile($fname);
logdata("HIT",$url,$fname);
exit(0);
}
//file not in cache? Get it, send it & save it
logdata("MISS",$url,$fname);
$fileptr=fopen($fname,"w");
while(!feof($urlptr)){
$line=fread($urlptr,$blocksize);
echo $line;
if($fileptr) fwrite($fileptr,$line);
}
fclose($urlptr);
if($fileptr) fclose($fileptr);
function logdata($type,$what, $fname){
$file_path="/usr/local/www/youtube";
$logfile="$file_path/cache.log";
$line="@ ".time()."Cache $type url: $what file: $fname client:".$_SERVER['REMOTE_ADDR']."\n";
file_put_contents($logfile,$line,FILE_APPEND);
}
?>
5. Untuk melihat apakah youtubenya jalan, lihat saja di file log yang ada di folder youtube anda.
6. Apabila kode diatas sulit dibaca, silahkan download saja original filenya di
http://www.mieorganik.com/squidtube.tar.gz












