Apache-HTTunnel
view release on metacpan or search on metacpan
Client/httunnel view on Meta::CPAN
-h Prints this usage information.
USAGE
exit(1) ;
}
usage() if (($opts{h})||(! @ARGV[0])) ;
$opts{V} = 2 if $opts{VV} ;
my $DEBUG = $opts{V} || 0 ;
my $SYSLOG = 0 ;
if (($opts{d})&&(Sys::Syslog->can('openlog'))){
Sys::Syslog::setlogsock('unix') ;
Sys::Syslog::openlog('httunnel', 'pid', 'user') or die("Can't open syslog: $!") ;
$SYSLOG = 1 ;
}
my $config = get_conf(@ARGV[0]) ;
my ($select, $listeners) = start_listeners($config) ;
if ($opts{d}){
chdir('/') or sdie("Can't chdir to /: $!") ;
open(STDIN, '</dev/null') or sdie("Can't open /dev/null for reading: $!") ;
open(STDOUT, '>>/dev/null') or sdie("Can't open /dev/null for appending: $!") ;
open(STDERR, '>>/dev/null') or sdie("Can't open /dev/null for appending: $!") ;
setsid() or sdie("Can't start a new session: $!") ;
umask(0) ;
my $pid = fork() ;
sdie("Can't fork: $!") unless defined($pid) ;
exit(0) if ($pid != 0) ;
}
while (1){
my @ready = $select->can_read() ;
foreach my $fh (@ready){
my $cfg = $config->{$listeners->{$fh}} ;
$DEBUG = $cfg->{verbose} ;
my $client = undef ;
if ($cfg->{protocol} eq 'udp'){
$client = $fh ;
$select->remove($fh) ;
}
else {
$client = $fh->accept() ;
}
next if ! defined($client) ;
slog('notice', "Accepted connection on " . $fh->sockhost() . ':' . $fh->sockport()) ;
my $pid = fork() ;
sdie("Can't fork: $!") unless defined($pid) ;
next if ($pid) ;
# child
undef $listeners ;
undef $select ;
$pid = undef ;
my $reader = undef ;
my $hc = new My::HTTunnel::Client($cfg, keep_alive => $cfg->{http_keep_alive}) ;
eval {
local $SIG{TERM} = 'IGNORE' if $cfg->{protocol} ne 'udp' ;
$hc->proxy(['http'], $cfg->{http_proxy}) if $cfg->{http_proxy} ;
$hc->connect($cfg->{protocol}, $cfg->{remote_host}, $cfg->{remote_port}) ;
sdebug("Remote connection to $cfg->{remote_host}:$cfg->{remote_port} established") ;
my $lifeline = new IO::Pipe() ;
($reader, $pid) = start_reader($hc, $cfg, $lifeline) ;
if (! $reader){
$hc->close() ;
die("Can't fork: $!") ;
}
my $sel = new IO::Select($client, $reader) ;
while (1){
my @ready = $sel->can_read() ;
foreach my $fh (@ready){
my $buf = undef ;
if (($fh eq $client)&&($cfg->{protocol} eq 'udp')){
$buf = recv_from($fh, $cfg->{read_length}) ;
}
else {
$buf = read_from($fh, $cfg->{read_length}) ;
}
if (! defined($buf)){
kill 9, $pid ;
$hc->close() ;
exit() ;
}
if ($fh eq $reader){
if ($cfg->{protocol} eq 'udp'){
send_to($client, $buf) ;
}
else {
write_to($client, $buf) ;
}
}
else {
$hc->print($buf) ;
}
}
}
} ;
if ($@){
my $e = $@ ;
kill 9, $pid if defined($pid) ;
eval { $hc->close() ; } ;
slog('err', "$@\n") if $@ ;
sdie("$e\n") ;
}
}
}
###############################################################################
sub get_conf {
my $path = shift ;
my @files = () ;
if (-d $path){
$path =~ s/\/+$// ;
# open dir and get all .conf files.
opendir(CONFD, $path) or die("Can't open configuration directory '$path': $!\n") ;
push @files, map {{name => "$path/$_", lines => []}} grep {$_ =~ /\.conf$/} readdir CONFD ;
die("No configuration files (*.conf) found in '$path'") unless scalar(@files) ;
}
else{
push @files, {name => $path, lines => []} ;
}
foreach my $f (@files){
slog('notice', "Processing configuration file '$f->{name}'") ;
open(CONF, "<$f->{name}") or die("Can't open configuration file '$f->{name}': $!\n") ;
push @{$f->{lines}}, <CONF> ;
close(CONF) ;
}
# Process configuration
my %defaults = (
local_addr => 'localhost',
local_port => undef,
protocol => 'tcp',
remote_port => undef,
remote_host => undef,
url => undef,
http_protocol => 'HTTP/1.1',
http_keep_alive => 1,
http_username => '',
http_password => '',
http_proxy => '',
http_proxy_username => '',
http_proxy_password => '',
read_length => 131072,
read_timeout => 15,
verbose => $DEBUG,
) ;
foreach my $f (@files){
my $cnt = 0 ;
my $section = $f->{name} ;
my $config = {$section => {%defaults}} ;
foreach my $l (@{$f->{lines}}){
chomp($l) ;
$l =~ s/^\s+// ;
$l =~ s/\s+$// ;
$cnt++ ;
if (($l eq '')||($l =~ /^[;#]/)){
next ;
}
elsif ($l =~ /^\[(.+?)\]$/){
$section = "$f->{name}:[$1]" ;
$config->{$section} = {%{$config->{$f->{name}}}} ;
}
elsif (($l =~ /^(\w+)\s*=\s*(.*?)$/)&&(exists $config->{$section}->{$1})){
$config->{$section}->{$1} = $2 ;
}
else{
die("Invalid configuration directive at '$f->{name}' line $cnt") ;
}
}
$f->{config} = $config ;
}
# Validation
my $config = {} ;
foreach my $f (@files){
my $nb_sections = 0 ;
foreach my $s (keys %{$f->{config}}){
if ($s ne $f->{name}){
$nb_sections++ ;
foreach my $k (keys %{$f->{config}->{$s}}){
die("Configuration parameter '$k' is required in section '$s'\n")
unless defined($f->{config}->{$s}->{$k}) ;
}
$config->{$s} = $f->{config}->{$s} ;
$config->{$s}->{__file__} = $f->{name} ;
$config->{$s}->{__section__} = $s ;
}
}
die("No sections declared in configuration file '$f->{name}'\n") unless $nb_sections ;
}
sdebug(Dumper($config)) ;
return $config ;
}
( run in 0.723 second using v1.01-cache-2.11-cpan-df04353d9ac )