open-proxy.pl 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 3 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, see <http://www.gnu.org/licenses/>.
  16. use strict;
  17. use v5.10;
  18. AddModuleDescription('open-proxy.pl', 'Open Proxy Banning Extension');
  19. # We scan proxies by attempting to self-ban ourselves. If we're
  20. # hitting an open proxy, our request will in fact be forwarded, and
  21. # the proxy has banned himself. Ordinary users should never call the
  22. # self-ban action.
  23. our ($q, %Action, %Page, $Now, $ScriptName, $BannedHosts, $DataDir);
  24. our ($SelfBan, $OpenProxies);
  25. $SelfBan = "xyzzy"; # change this from time to time in your config file
  26. $OpenProxies = "$DataDir/openproxies"; # file storing when what IP got scanned
  27. $Action{$SelfBan} = \&DoSelfBan;
  28. sub DoSelfBan {
  29. my $date = &TimeToText($Now);
  30. my $str = '^' . quotemeta($q->remote_addr());
  31. OpenPage($BannedHosts);
  32. Save ($BannedHosts, $Page{text} . "\n\nself-ban on $date\n $str",
  33. Ts("Self-ban by %s", $q->remote_addr()), 1); # minor edit
  34. ReportError(T("You have banned your own IP."));
  35. }
  36. # Before you can edit a page, we do the open proxy scanning.
  37. *OpenProxyOldDoEdit = \&DoEdit;
  38. *DoEdit = \&OpenProxyNewDoEdit;
  39. sub OpenProxyNewDoEdit {
  40. BanOpenProxy();
  41. OpenProxyOldDoEdit(@_);
  42. }
  43. sub BanOpenProxy {
  44. my ($force) = @_;
  45. my $ip = $q->remote_addr();
  46. my $limit = 60*60*24*30; # rescan after 30 days
  47. # Only check each IP address once a month
  48. my %proxy = split(/\s+/, ReadFile($OpenProxies));
  49. return if $Now - $proxy{$ip} < $limit;
  50. # If possible, do the scanning in a forked process so that the user
  51. # does not have to wait.
  52. return if !$force && fork;
  53. require LWP::UserAgent;
  54. my @ports = qw/23 80 81 1080 3128 8080 8081 scx-proxy dproxy sdproxy
  55. funkproxy dpi-proxy proxy-gateway ace-proxy plgproxy
  56. csvr-proxy flamenco-proxy awg-proxy trnsprntproxy
  57. castorproxy ttlpriceproxy privoxy ezproxy ezproxy-2/;
  58. my $browser = LWP::UserAgent->new(
  59. timeout =>10,
  60. max_size =>2048,
  61. requests_redirectable => []
  62. );
  63. foreach my $port (@ports) {
  64. $browser->proxy("http","http://$ip:".$port);
  65. my $response = $browser->head("$ScriptName?action=$SelfBan");
  66. last unless defined $response;
  67. last unless $response->is_error;
  68. }
  69. # Now update the list
  70. $proxy{$ip} = $Now;
  71. my $data = '';
  72. foreach (keys %proxy) {
  73. $data .= $_ . ' ' . $proxy{$_} . "\n";
  74. }
  75. WriteStringToFile($OpenProxies, $data);
  76. exit unless $force; # exit if we're in the fork
  77. }