1

This post intend to refine caching photos and videos by squid. As you know, store-id.pl is cache key that used by squid proxy. If your store-id.pl located at /etc/squid/store-id.pl, just change it with this one. 

Note: This store-id.pl has paired acl. See here !

Before changing the store-id.pl, these are the steps:

1. Backup Old Store-id.pl
# cp /etc/squid/store-id.pl /etc/squid/old-store-id.pl

2. Making Changes to Store-id.pl.
Use putty for simple editing. You just copy the script, right click mouse on putty screen, and your script will be added automatically.
# squid -k shutdown
# nano /etc/squid/store-id.pl
Remove all scripts inside, or you can adjust it if needed, than insert the new configuration.


#!/usr/bin/perl
# storeid.pl with debug opt - based on storeurl.pl
# @ http://www2.fh-lausitz.de/launic/comp/misc/squid/projekt_youtube/
# modified by cyberscie.com
use IO::File;
$|=1;
STDOUT->autoflush(1);
$debug=0; ## recommended:0
$bypassallrules=0; ## recommended:0
$sucks=""; ## unused
$sucks="sucks" if ($debug>=1);
$timenow="";
$printtimenow=1;   ## print timenow: 0|1
my $logfile = '/tmp/storeid.log';
open my $logfh, '>>', $logfile
    or die "Couldn't open $logfile for appending: $!\n" if $debug;
$logfh->autoflush(1) if $debug;
while (<>) {
$timenow=time()." " if ($printtimenow);
print $logfh "$timenow"."in : $_" if ($debug>=1);
chop; 
my $myURL = $_;
@X = split(" ",$myURL);
$a = $X[0]; ## channel id
$b = $X[1]; ## url
$c = $X[2]; ## ip address
$u = $b; ## url
if ($bypassallrules){
 $out="$u"; ## map 1:1
} elsif ($u=~ m/http.*\.(fbcdn|akamaihd)\.net\/h(profile|photos).*[\d\w].*\/([\w]\d+x\d+\/.*\.[\d\w]{3}).*/) {
$out="OK store-id=http://fbcdn.net.squid.internal/" . $1 . $2 . "/" . $3 ;
} elsif ($u=~ m/http.*\.(fbcdn|akamaihd)\.net\/h(profile|photos).*[\d\w].*\/([\w]\d+x\d+\/.*\.[\d\w]{3}).*/) {
$out="OK store-id=http://fbcdn.net.squid.internal/" . $2 . "/" . $3 ;
} elsif ($u=~ m/^http(.*)static(.*)(akamaihd|fbcdn).net\/rsrc.php\/(.*\/.*\/(.*).(js|css|png|gif))(\?(.*)|$)/) {
$out="OK store-id=http://fbcdn.net.squid.internal/static/" . $5 . "." . $6 ;
} elsif ($u=~ m/^https?:\/\/[a-zA-Z0-9\-\_\.\%]*(fbcdn|akamaihd)[a-zA-Z0-9\-\_\.\%]*net\/rsrc\.php\/(.*)/) { 
$out="OK store-id=http://fbcdn.net.squid.internal/" . $2 ;
} elsif ($u=~ m/^https?\:\/\/.*(profile|photo|creative).*\.ak\.fbcdn\.net\/((h|)(profile|photos)-ak-)(snc|ash|prn)[0-9]?(.*)/) {
$out="OK store-id=http://fbcdn.net.squid.internal/" . $2  . "fb" .  $6 ;
} elsif ($u=~ m/^https?:\/\/.*(profile|photo|creative)*.akamaihd\.net\/((h|)(profile|photos|ads)-ak-)(snc|ash|prn|frc[0-9])[0-9]?(.*)/) {
$out="OK store-id=http://fbcdn.net.squid.internal/" . $2  . $5 .  $6 ;
} elsif ($u=~ m/^https?\:\/\/video\.(.*)\.fbcdn\.net\/(.*?)\/([0-9_]+\.(mp4|flv|avi|mkv|m4v|mov|wmv|3gp|mpg|mpeg)?)(.*)/) {
$out="OK store-id=http://video.fbcdn.net.squid.internal/" . $1 ;
} elsif ($u=~ m/^https?\:\/\/.*utm.gif.*/) {
$out="OK store-id=http://google-analytics.squid.internal/__utm.gif";
} elsif ($u=~ m/^https?\:\/\/.*\/speedtest\/(.*\.(jpg|txt)).*/) {
$out="OK store-id=http://speedtest.squid.internal/" . $1;
} elsif ($u=~ m/^https?\:\/\/.*\/(.*\..*(mp4|3gp|flv))\?.*/) {
$out="OK store-id=http://video-file.squid.internal/" . $1;
} elsif ($u=~ m/^https?\:\/\/c2lo\.reverbnation\.com\/audio_player\/ec_stream_song\/(.*)\?.*/) {
$out="OK store-id=http://reverbnation.squid.internal/" . $1;
} elsif ($u=~ m/^https?\:\/\/.*\.c\.android\.clients\.google\.com\/market\/GetBinary\/GetBinary\/(.*\/.*)\?.*/) {
$out="OK store-id=http://playstore-android.squid.internal/" . $1;
} elsif ($u =~ m/^http:\/\/([a-z])[0-9]?(\.gstatic\.com.*|\.wikimapia\.org.*)/) {
$out="OK store-id=http://gstatic.squid.internal/" . $2;
} elsif ($u =~ m/^https?:\/\/.*(googleusercontent.com|blogspot.com)\/(.*)\/([a-z0-9]+)(-[a-z]-[a-z]-[a-z]+)?\/(.*\.(jpg|png))/) {
$out="OK store-id=http://googleusercontent.squid.internal/" . $5;
} elsif ($_ =~ m/^https?:\/\/([a-z0-9.]*)(\.doubleclick\.net|\.quantserve\.com|.exoclick\.com|interclick.\com|\.googlesyndication\.com|\.auditude\.com|.visiblemeasures\.com|yieldmanager|cpxinteractive)(.*)/) {
$out="OK store-id=http://ads.squid.internal/" . $3;
} elsif ($u=~ m/^http\:\/\/.*\.4shared\.com\/download\/(.*)\/.*/) {
$out="OK store-id=http://4shared.squid.internal/" . $1;
} elsif ($u =~ m/^http:\/\/(www\.ziddu\.com.*\.[^\/]{3,4})\/(.*?)/) {
$out="OK store-id=http://ziddu.squid.internal/" . $1;
} elsif ($u=~ m/^https?\:\/\/i[1-4]\.ytimg\.com\/(.*)/) {
 $out="OK store-id=http://ytimg.com.squid.internal/" . $1 ;
} elsif ($u=~ m/^https?\:\/\/lh[0-9]?.ggpht.com\/(.*?)\/(.*?)\/(.*?)\/(.*)\/(.*)?$/) {
          $out="OK store-id=http://ggpht.squid.internal/"  . $1 . "/" .  $2 . "/" . $4 .  "/" .  $5 ; 
} elsif (($u =~ /filehippo/) && (m/^https?:\/\/(.*?)\.(.*?)\/(.*?)\/(.*)\.([a-z0-9]{3,4})(\?.*)?/)) {
@y = ($1,$2,$4,$5); $y[0] =~ s/[a-z0-9]{2,5}/cdn./;
$out="OK store-id=http://filehippo.squid.internal/" . $3;
} elsif ($u[1] =~ m/^http:\/\/.*dlink__[23]Fdownload_[23]F([\w\d-]+)_3Ftsid.*/) {
$1 =~ s/_5F/_/g;
$out="OK store-id=http://4shared.squid.internal/" . $1;
} elsif ($u=~ m/^https?\:\/\/[^\/]*\.vimeo[^\/]*\.com.*\/([[^\/]*\.(flv|mp4|avi|mkv|mp3|rm|rmvb|m4v|mov|wmv|3gp|mpg|mpeg|web))\?.*/) {
$out="OK store-id=http://vimeo.squid.internal/" . $1 ;
} elsif ($u=~ m/^https?\:\/\/.*youtube.*ptracking.*/){
@video_id = m/[&?]video_id\=([^\&\s]*)/;
@cpn = m/[&?]cpn\=([^\&\s]*)/;
unless (-e "/tmp/@cpn"){
open FILE, ">/tmp/@cpn";
print FILE "@video_id";
close FILE;
}
$out="ERR";
} elsif ($u=~ m/^https?\:\/\/.*youtube.*stream_204.*/){
@docid = m/[&?]docid\=([^\&\s]*)/;
@cpn = m/[&?]cpn\=([^\&\s]*)/;
unless (-e "/tmp/@cpn"){
open FILE, ">/tmp/@cpn";
print FILE "@docid";
close FILE;
}
$out="ERR";
} elsif ($u=~ m/^https?\:\/\/.*youtube.*player_204.*/){
@v = m/[&?]v\=([^\&\s]*)/;
@cpn = m/[&?]cpn\=([^\&\s]*)/;
unless (-e "/tmp/@cpn"){
open FILE, ">/tmp/@cpn";
print FILE "@v";
close FILE;
}
$out="ERR";
} elsif ($u=~ m/^https:\/\/.*\.(youtube|googlevideo)\.com\/(watch|get_video|videoplayback|videoplay)\?.*/){
@itag = m/[&?](itag\=[0-9]*)/;
@range = m/[&?](range\=[^\&\s]*)/;
@cpn = m/[&?]cpn\=([^\&\s]*)/;
@mime = m/[&?](mime\=[^\&\s]*)/;
@id = m/[&?]id\=([^\&\s]*)/;
if (defined(@cpn[0])){
if (-e "/tmp/@cpn"){
open FILE, "/tmp/@cpn";
@id = <FILE>;
close FILE;}
}
$out="OK store-id=http://googlevideo.squid.internal/id=@id@mime@range";
} elsif (m/^https?\:\/\/(.*?)\/(.*?)\.(jp(e?g|e|2)|gif|png|tiff?|bmp|ico|flv|wmv|3gp|mp(4|3)|exe|msi|zip|on2|mar|rar|cab|amf|swf)(.*)/) {
$out="OK store-id=http://allcdn1.squid.internal/" . $1 ;
} elsif ($u=~ m/^https?\:\/\/([0-9.]*?)\/\/(.*?)\.(.*)\?(.*?)/) {
    $out="OK store-id=http://allcdn2.squid.internal/" . $2  . "." . $3 ;
} else {
$out="ERR";
}
print $logfh "$timenow"."out: $a $out\n" if ($debug>=1);
print "$a $out\n";
}

close $logfh if ($debug);

(Use ctrl + x than y, to end the editing scripts and get back to terminal)

3. Try The New Configuration
# Squid start
Or restart your proxy machine.

# reboot

Post a Comment

 
Top