Last active
March 15, 2016 20:07
-
-
Save prigaux/d3435cae95f7005d7194 to your computer and use it in GitHub Desktop.
deprecated, See proxy-broadcast.js for something safer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
# Usage: | |
# | |
# - for now, this script uses JSESSIONID cookie as the stickysession parameter, it could be passed as a ENV parameter | |
# | |
# - put this file in directory (example /usr/local/esup/cgi-bin) | |
# | |
# - allow that directory to run cgi-bins: | |
# | |
# <Directory "/usr/local/esup/cgi-bin"> | |
# Require all granted | |
# SetHandler cgi-script | |
# Options +ExecCGI | |
# </Directory> | |
# | |
# - activate this cgi on requests from CAS server: | |
# | |
# RewriteEngine On | |
# RewriteCond %{REMOTE_ADDR} "=193.55.96.57" [OR] | |
# RewriteCond %{REMOTE_ADDR} "=2001:660:3305::57" | |
# RewriteRule / /usr/local/esup/cgi-bin/proxy-broadcast.pl [E=routes:ent2;ent3,L] | |
# | |
use strict; | |
use LWP::UserAgent; | |
my $logFile = '/tmp/proxy-broadcast.log'; | |
my $timeout = 3; # seconds | |
my $ourUserAgent = 'proxy-broadcast'; | |
$ENV{HTTP_USER_AGENT} ne $ourUserAgent or die "oops, loop :-(\n"; | |
$ENV{routes} or die "missing ENV var 'routes'. Eg: use RewriteRule ... [E=routes:ent1;ent2,L]\n"; | |
my $url = $ENV{SCRIPT_URI}; | |
my $method = $ENV{REQUEST_METHOD}; | |
my @routes = split(';', $ENV{routes}); | |
my $content = join('', <>); | |
print "Content-type: text/plain\n\n"; | |
#use Data::Dumper; print Dumper(\%ENV); | |
daemonize(); | |
my $ua = LWP::UserAgent->new(timeout => $timeout, agent => $ourUserAgent); | |
foreach my $cookie (map { "JSESSIONID=xxx.$_" } @routes) { | |
my $response = | |
$method eq 'POST' ? $ua->post($url, 'Cookie' => $cookie, 'Content' => $content) : | |
$method eq 'GET' ? $ua->get($url, 'Cookie' => $cookie) : | |
die "unhandled method $method\n"; | |
#print "Response\n"; print $response->content, "\n"; | |
#sleep 10; | |
} | |
sub daemonize { | |
open(STDIN, "<", "/dev/null") or die; | |
open(STDOUT, ">>", $logFile) or die; | |
open(STDERR, ">&STDOUT") or die; | |
defined(my $pid = fork()) or die; | |
exit if $pid; # non-zero now means I am the parent | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment