Posted By

paramah on 01/05/09


Tagged

redirect perl proxy squid


Versions (?)

perl redirect for squid


 / Published in: Perl
 

For database inserts must be made some site on "http://redirect_host:port/.../" with nice getip function: http://snipplr.com/view/10946/

  1. #!/usr/bin/perl
  2. #
  3. #
  4. #
  5. # CREATE TABLE `redirects` (
  6. # `id` int(11) NOT NULL auto_increment,
  7. # `ip` varchar(15) collate utf8_polish_ci NOT NULL,
  8. # `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP,
  9. # `choice` varchar(10) collate utf8_polish_ci NOT NULL,
  10. # PRIMARY KEY (`id`),
  11. # UNIQUE KEY `ip` (`ip`)
  12. #) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci AUTO_INCREMENT=3395 ;
  13. #
  14. #
  15.  
  16. use Sys::Syslog qw(:DEFAULT setlogsock);
  17. use DBI;
  18. use Net::IP::Match::Regexp qw( create_iprange_regexp match_ip );
  19.  
  20.  
  21. $dbase = DBI->connect("DBI:mysql:database=DBNAME;host=DBHOST","DBUSER","DBPASS", { RaiseError => 1 });
  22. $utsfmt = "UNIX_TIMESTAMP()";
  23.  
  24.  
  25. my $net1 = create_iprange_regexp( qw( 10.10.0.0/24 10.10.16.0/24 ) );
  26. my $net2 = create_iprange_regexp( qw( 10.21.0.0/17 10.21.128.0/17 ) );
  27.  
  28.  
  29. openlog("redirector");
  30. setlogsock('unix');
  31. syslog('info', 'Script started.');
  32.  
  33. $|=1;
  34. while (<>)
  35. {
  36. @X = split;
  37. my $url = $X[0];
  38. my $oul = $url;
  39. @uri = split(///,$url);
  40. @ip = split(///,$X[1]);
  41. my $sdbq = $dbase->prepare("SELECT * FROM redirects WHERE ip='".$ip[0]."' AND choice IN ('0','1','3')");
  42. $sdbq->execute();
  43. if ($sdbq->rows == 0)
  44. {
  45. if (match_ip($ip[0], $net1))
  46. {
  47. $url="http://redirect_host:port/net1/".$uri[3];
  48. }
  49. elsif(match_ip($ip[0], $net2))
  50. {
  51. $url="http://redirect_host:port/net2/".$uri[3];
  52. }
  53. else
  54. {
  55. $url="http://redirect_host:port/rest/".$uri[3];
  56. }
  57.  
  58. $action ="New";
  59. $all = "[".$ip[0]."] ".$url."[".$action."] - ".$oul." - ".$uri[3]."n";
  60. syslog('info', $all);
  61.  
  62. print "$urln";
  63. }
  64. else
  65. {
  66. $action ="In database";
  67. $all = "[".$ip[0]."] ".$url."[".$action."] - ".$oul." - ".$uri[3]."n";
  68. syslog('info', $all);
  69.  
  70. }
  71. }
  72. closelog();

Report this snippet  

You need to login to post a comment.