#!/usr/bin/perl
# tonnel.cgi (CGI tunnel) v0.08 (c) Dzianis Kahanovich (AKA mahatma), 2007
##############################################################################
# command line: [daemon [<url> [{<n*>]}]|split [client|daemon]]
=head1 NAME
tonnel.cgi v0.08 - tunnel cgi+daemon for remote cgi execution (for CGIProxy).
=head1 DESCRIPTION
CGI/http reverse tunnel. Mainly used for CGIProxy by James Marshall.
=head1 README
todo.
Using to remote exec of CGIProxy (etc?) and browse CGI from
this daemon+CGIproxy. Using only single-direction requests (daemon->http/cgi).
Mainly written to bypass hoster's firewall, denied outgoing connections,
but may be good solution for HA multichannel browsing.
Daemon command line: [daemon [<url> [{<n*>]}]|split [client|daemon]]
- where n* - threads/connects limits (or 0)
- example:
./nph-tonnel.cgi daemon http://www.yoursite./cgi-bin/nph-tonnel.cgi 20 2 3
James Marshall's CGIProxy: http://www.jmarshall.com/tools/cgiproxy/
Tested with CGIProxy 2.1beta15
=head1 PREREQUISITES
Perl 5
=head1 COREQUISITES
Perl 5, Socket, POSIX
=pod OSNAMES
All
=pod SCRIPT CATEGORIES
Networking
Web
=cut
### daemon
use Socket;
my $split=32768;
my @timeout=(60,1800,180,180); # connect,init,read,write
my $preresolve=0;
my $cgi_proxy="./nph-proxy.cgi";
#$proxy='socks4a://127.0.0.1:9050';
my (@LIM,@CNT,$MR,$CLW,@MW,@CLR);
### client
my $serve="serve";
my $dtype="binary/octet-stream";
my $URL="http://mahatma.eu.by/cgi-bin/nph-tunnel.cgi";
my $temp="/var/tmp";
my $bufsize=1024;
$|=1;
### /daemon
my $fi="$temp/tonnel.fifo";
my $log="$temp/tonnel.log";
my $duplex=1; # independed for cgi & daemon
my $nph=1;
my $pgrp=1;
my ($F,$pid,$ppid,$wr);
### /client
### daemon
goto 'O_'.shift(@ARGV);
O_:
### /daemon
### client
$ENV{QUERY_STRING}=~s/^$serve\=(\-?\d*)$/$pid=abs($ppid=$1)/se;
if(!defined($pid)){
$pid=$$;
$F=openpipe('>>',$fi);
print($F wrx($pid))||&cgidie;
close($F);
push @ff,"$fi.$pid.r";
$F=openpipe('>',"$fi.$pid.r");
while(my ($k,$v)=each %ENV){$v=~tr/\n/ /;print($F "$k: $v\n")||&cgidie}
print($F "\n")||&cgidie;
if(exists($ENV{CONTENT_LENGTH})){
fcopy($ENV{CONTENT_LENGTH},*STDIN,$F)||&cgidie;
}else{
fcopy1(*STDIN,$F)||&cgidie;
}
close($F);
push @ff,"$fi.$pid.c";
NEXT:
fcopy1($F=openpipe('<',"$fi.$pid.c"),*STDOUT)||&cgidie;
close($F);
goto NEXT if(-e "$fi.$pid.r");
=1= |