StoreID – Support Caching Youtube dan Facebook HTTPS

Ini catatan pribadi, isinya storeid untuk squid-3.x Support caching youtube (thanks Ces Pun), Facebook HTTPS, serta beberapa web lainnya (playstore, reverbnation, dan sebagainya). Untuk mengaktifkan log storeid, silakan set $debug=1; pada storeid, dan buat file “/tmp/storeid.log” yang sudah di chown ke user proxy/squid.

Untuk mengecek log, silakan “tail -f /tmp/storeid.log”.

Note : untuk konfigurasi membutuhkan squid-3.4.7 yang sudah di patch untuk bug # 3937 (storeid loop).

Konfigurasi

# storeid *test*
acl urlrewrite dstdomain .fbcdn.net .akamaihd.net
acl speedtest url_regex -i speedtest\/.*\.(jpg|txt)\?.*
acl reverbnation url_regex -i reverbnation.*audio_player.*ec_stream_song.*$
acl utmgif url_regex -i utm.gif.*
acl playstoreandroid url_regex -i c.android.clients.google.com.market.GetBinary.GetBinary.*
acl idyoutube url_regex -i youtube.*(ptracking|stream_204|player_204).*(v\=|docid\=|video_id\=).*$
acl videoyoutube url_regex -i (youtube|googlevideo).*videoplayback\?
acl videoyoutube url_regex -i (youtube|googlevideo).*videoplayback\?
acl loop_302 http_status 302
acl getmethod method GET
 
store_id_program /usr/bin/perl /etc/squid/storeid
store_id_children 20 startup=0 idle=1 concurrency=5
store_id_access deny !getmethod
store_id_access allow urlrewrite
store_id_access allow speedtest
store_id_access allow reverbnation
store_id_access allow utmgif
store_id_access allow playstoreandroid
store_id_access allow idyoutube
store_id_access allow videoyoutube
store_id_access deny all

store_miss deny videoyoutube loop_302
send_hit deny videoyoutube loop_302

refresh_pattern (akamaihd|fbcdn)\.net 14400 99% 518400  ignore-no-store ignore-private ignore-reload ignore-must-revalidate store-stale
refresh_pattern -i squid\.internal 14400 99% 518400  ignore-no-store ignore-private ignore-reload ignore-must-revalidate store-stale
refresh_pattern \.(jpg|png|gif|css|ico)($|\?) 14400 99% 518400  ignore-no-store ignore-private reload-into-ims ignore-must-revalidate store-stale
refresh_pattern . 0 99% 518400  ignore-no-store ignore-private reload-into-ims store-stale

Isi storeid.pl

#!/usr/bin/perl
#
# storeid.pl with debug opt - based on storeurl.pl
# @ http://www2.fh-lausitz.de/launic/comp/misc/squid/projekt_youtube/
#
# mods by cespun and fajar @ ipfire.id
#  
 
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/" . $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?\:\/\/.*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/^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).*videoplayback.*/){
	@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://video-srv.squid.internal/id=@id@mime@range";

} else {
	$out="ERR";
}
	print $logfh "$timenow"."out: $a $out\n" if ($debug>=1);
	print "$a $out\n";
}
close $logfh if ($debug);

Tested untuk cache Facebook HTTPS dan youtube. Silakan di copas kalo pengen :D

About Fajar R. 297 Articles
Seseorang yang antusias terhadap dunia internet, IT, networking dan komputer. Tipikal introvert tapi suka berorganisasi dan adventuring ke tempat-tempat baru.

17 Komentar

  1. topologi yng digambar salah mas, itu koneksi inet langsung masuk di mikrotik bukan ke proxy.,,, ke geser ke kiri krn cuman pake spasi di kolom komentar

  2. seperti di pengaturan NAT mikrotiknya yang salah mas, saya belum paham betul ttg ini,,, topologi jaringan saya

    internet – mikrotik – client
    |
    proxy

    ether1 mikrotik: 192.168.2.1 – 192.168.2.2 inet
    ether2 mikroitk: 192.168.0.1 – 192.168.0.2 proxy
    ether3 mikrotik : 192.168.137.1 – 192.168.137.0/24 client net
    ether4 mikrotik : 192.168.1.1 – ke hotspot (192.168.1.0/24)

    ini link ss invalid addressnya https://scontent-a-sin.xx.fbcdn.net/hphotos-xap1/v/t1.0-0/10676161_700122290106885_254401823363058951_n.jpg?efg=eyJpIjoiYiJ9&oh=acc236522a83d3f0806722c54ea59571&oe=55385455 ..makasih bantuannya.

    missing terjadi ketika NAT sy enable di mikrotik ,,, port 80,81,8080 di dst ke 192.168.0.2 port 3128

    • error gini sering di squid 3. coba pake mangle mas, jangan pake nat. coba ubek ubek blog ini, ada postingan tentang pengaturan proxy eksternal.

  3. mas, saya baru2 install squid 3.5.4 yang paling baru,,, ane dapat kesulitan dgn pengaturan http_port, yng sudah mendukung proxy protocol….
    kan biasa pake http_port 3128 , klo ini di di nat pada mikrotik, selalu missing html di browser, nanti di set manual proxynya di browser kalo konek.

    trus kalo pake
    http_port 3128
    acl frontend src 192.0.0.x
    dst….. gak bisa digunakan squid, eror trus. tolong dong mas.

    • saya nggak ngerti yang di bagian missing html :)
      apa bisa dikasih screenshot. oia.. jangan lupa juga cek konfigurasi squid dengan cara mengetikkan “squid -k parse”
      kemudian pastekan hasilnya disini atau pastebin.com

  4. mas saya minta tutorial lengkap installasi + setting squid di ubuntu 12.04.5 server yang bisa caching youtube dan https soalnya untuk os linux yang agak faham cuma ubuntu

  5. kok malah jadi gak konek gak kalau dari hp android google playnya gan.. dari PC gak ada masalah google playnya..
    soalnya ane ada AP yang ane hub kan dengan HUB..

    yang gak konek : bbm, googleplay dan beberapa program lainnya seperti game online jg gan.

    tapi kalau debug=0 malah bisa semuanya yg ane sebut di atas gan..

    kenapa yaa…

    SS settingan debug=1
    http://s2.postimg.org/o0hql2rfc/IMG_20141029_211740_HDR.jpg

    SS chown squid:squid yg sudah diaktifkan
    http://s29.postimg.org/inuyfv0ye/IMG_20141029_211628_HDR.jpg

    SS google play yg bisa konek di PC
    http://s7.postimg.org/53gtliwmi/IMG_20141029_211622_HDR.jpg

    SS google play yang gak konek di hp android
    http://s10.postimg.org/ppi1xq22x/Screenshot_2014_10_29_21_12_50.png

    SS bbm yang gak konek di hp android
    http://s8.postimg.org/ls32ehog5/Screenshot_2014_10_29_21_12_08.png

    • debug diaktifkan jika mas ingin memeriksa url pattern apa saja yang di rewrite atau tidak. dalam mesin proxy produksi/sudah berjalan baik, debug sebaiknya di set 0 agar kinerja disk write lebih optimal.

      pada kasus nggak bisa konek, silakan mas cek cache.log dan access.log nya, amati log-log tsb saat mas mencoba mengakses situs/apps yang nggak konek. hal ini perlu dilakukan, karena susah kalo kita mencoba mencari letak masalahnya kalo yang di info kan hanya screenshot nya. demikian dan semoga membantu.

  6. Bang fazar gmn caranya set $debug=1; pada storeid, dan buat file “/tmp/storeid.log” yang sudah di chown ke user proxy/squid nya?mohon pencerahanya..thx sangat membantu

    • pertama set $debug=1; (bisa menggunakan editor seperti vi, nano, dsb) selanjutnya ikuti step berikut di ssh :

      # touch /tmp/storeid.log
      # chown squid:squid /tmp/storeid.log
      # squid -k reconfigure

      selesai, silakan “tail /tmp/storeid.log” :)

Leave a Reply

Alamat email Anda tidak akan dipublikasikan.


*