open-proxy.pl 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # Copyright (C) 2005 Sunir Shah <sunir@sunir.org>
  2. # Copyright (C) 2005 Alex Schroeder <alex@emacswiki.org>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the
  16. # Free Software Foundation, Inc.
  17. # 59 Temple Place, Suite 330
  18. # Boston, MA 02111-1307 USA
  19. use strict;
  20. use v5.10;
  21. AddModuleDescription('open-proxy.pl', 'Open Proxy Banning Extension');
  22. # We scan proxies by attempting to self-ban ourselves. If we're
  23. # hitting an open proxy, our request will in fact be forwarded, and
  24. # the proxy has banned himself. Ordinary users should never call the
  25. # self-ban action.
  26. our ($q, %Action, %Page, $Now, $ScriptName, $BannedHosts, $DataDir);
  27. our ($SelfBan, $OpenProxies);
  28. $SelfBan = "xyzzy"; # change this from time to time in your config file
  29. $OpenProxies = "$DataDir/openproxies"; # file storing when what IP got scanned
  30. $Action{$SelfBan} = \&DoSelfBan;
  31. sub DoSelfBan {
  32. my $date = &TimeToText($Now);
  33. my $str = '^' . quotemeta($q->remote_addr());
  34. OpenPage($BannedHosts);
  35. Save ($BannedHosts, $Page{text} . "\n\nself-ban on $date\n $str",
  36. Ts("Self-ban by %s", $q->remote_addr()), 1); # minor edit
  37. ReportError(T("You have banned your own IP."));
  38. }
  39. # Before you can edit a page, we do the open proxy scanning.
  40. *OpenProxyOldDoEdit = \&DoEdit;
  41. *DoEdit = \&OpenProxyNewDoEdit;
  42. sub OpenProxyNewDoEdit {
  43. BanOpenProxy();
  44. OpenProxyOldDoEdit(@_);
  45. }
  46. sub BanOpenProxy {
  47. my ($force) = @_;
  48. my $ip = $q->remote_addr();
  49. my $limit = 60*60*24*30; # rescan after 30 days
  50. # Only check each IP address once a month
  51. my %proxy = split(/\s+/, ReadFile($OpenProxies));
  52. return if $Now - $proxy{$ip} < $limit;
  53. # If possible, do the scanning in a forked process so that the user
  54. # does not have to wait.
  55. return if !$force && fork;
  56. require LWP::UserAgent;
  57. my @ports = qw/23 80 81 1080 3128 8080 8081 scx-proxy dproxy sdproxy
  58. funkproxy dpi-proxy proxy-gateway ace-proxy plgproxy
  59. csvr-proxy flamenco-proxy awg-proxy trnsprntproxy
  60. castorproxy ttlpriceproxy privoxy ezproxy ezproxy-2/;
  61. my $browser = LWP::UserAgent->new(
  62. timeout =>10,
  63. max_size =>2048,
  64. requests_redirectable => []
  65. );
  66. foreach my $port (@ports) {
  67. $browser->proxy("http","http://$ip:".$port);
  68. my $response = $browser->head("$ScriptName?action=$SelfBan");
  69. last unless defined $response;
  70. last unless $response->is_error;
  71. }
  72. # Now update the list
  73. $proxy{$ip} = $Now;
  74. my $data = '';
  75. foreach (keys %proxy) {
  76. $data .= $_ . ' ' . $proxy{$_} . "\n";
  77. }
  78. WriteStringToFile($OpenProxies, $data);
  79. exit unless $force; # exit if we're in the fork
  80. }