Advertisement
punces

youtube.pl

Jun 27th, 2017
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use IO::File;
  3. $|=1;
  4. STDOUT->autoflush(1);
  5. $debug=1; ## recommended:0
  6. $bypassallrules=0; ## recommended:0
  7. $sucks=""; ## unused
  8. $sucks="sucks" if ($debug>=1);
  9. $timenow="";
  10. $printtimenow=1; ## print timenow: 0|1
  11. my $logfile = '/var/log/squid/storeid.log';
  12.  
  13. open my $logfh, '>>', $logfile
  14. or die "Couldn't open $logfile for appending: $!\n" if $debug;
  15. $logfh->autoflush(1) if $debug;
  16. while (<>) {
  17. $timenow=time()." " if ($printtimenow);
  18. print $logfh "$timenow"."in : input=$_" if ($debug>=1);
  19. @X = split;
  20. if ($X[0] =~ m/^http.*/) {
  21. $url = $X[0];
  22. $referer = $X[1];
  23. $urlreferer = $X[0] ." ". $X[1];
  24. } else {
  25. $chanel = $X[0];
  26. $url = $X[1];
  27. $referer = $X[2];
  28. $urlreferer = $X[1] ." ". $X[2];
  29. }
  30.  
  31. if ($bypassallrules){
  32. $out="$url"; ## map 1:1
  33.  
  34. #youtube googlevideo
  35. } elsif ($url =~ m/^https?\:\/\/.*google.*video(playback|goodput).*/){
  36. @cpn = m/[=%&?\/]cpn[=%&?\/]([^\&\s]*)/;
  37. @id = m/[=%&?\/]id[=%&?\/]([^\&\s]*)/;
  38. @itag = m/[=%&?\/]itag[=%&?\/]([\d]*)/;
  39. @range = m/[=%&?\/]range[=%&?\/]([\d]*-[\d]*)/;
  40. @mime = m/[=%&?\/]mime[=%&?\/]([^\&\s]*)/;
  41. if ($referer =~ m/^https?\:\/\/(www|gaming)\.youtube.*\.com\/(watch\?v|embed|v)[=%&?\/]([^\&\s\?]*)/){
  42. @id = $3;
  43. } else {
  44. if (defined(@cpn[0])){
  45. if (-e "/var/log/squid/@cpn"){
  46. open FILE, "/var/log/squid/@cpn";
  47. @id = <FILE>;
  48. close FILE;
  49. }
  50. }
  51. }
  52. $out="OK store-id=http://squid/google/video/id=@id/itag=@itag/mime=@mime/range=@range";
  53.  
  54. #youtube parameter
  55. } elsif (
  56. ($url =~ m/^https?\:\/\/.*youtube.*(stream_204|watchtime|qoe|atr|csi_204|playback).*[=%&?\/]docid[=%&?\/]([^\&\s]*)/) ||
  57. ($url =~ m/^https?\:\/\/.*youtube.*(ptracking|set_awesome).*[=%&?\/]video_id[=%&?\/]([^\&\s]*)/) ||
  58. ($url =~ m/^https?\:\/\/.*youtube.*(player_204).*[=%&?\/]v[=%&?\/]([^\&\s]*)/)
  59. ){
  60. @id = $2;
  61. @cpn = m/[=%&?\/]cpn[=%&?\/]([^\&\s]*)/;
  62. if ($referer !~ m/^https?\:\/\/(www|gaming)\.youtube.*\.com\/(watch\?v|embed|v)[=%&?\/]([^\&\s\?]*)/){
  63. unless (-e "/var/log/squid/@cpn"){
  64. open FILE, ">/var/log/squid/@cpn";
  65. print FILE @id;
  66. close FILE;
  67. }
  68. }
  69. $out = "ERR";
  70.  
  71. if ($X[0] =~ m/^http.*/) {
  72. print $logfh "$timenow"."in : chanel=$chanel referer=$referer\n" if ($debug>=1);
  73. print $logfh "$timenow"."in : url=$url\n" if ($debug>=1);
  74. print $logfh "$timenow"."out: $out\n" if ($debug>=1);
  75. print $logfh "\n" if ($debug>=1);
  76. print "$out\n";
  77. } else {
  78. print $logfh "$timenow"."in : chanel=$chanel referer=$referer\n" if ($debug>=1);
  79. print $logfh "$timenow"."in : url=$url\n" if ($debug>=1);
  80. print $logfh "$timenow"."out: $chanel $out\n" if ($debug>=1);
  81. print $logfh "\n" if ($debug>=1);
  82. print "$chanel $out\n";
  83. }
  84. }
  85. close $logfh if ($debug);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement