From e3d15dad1394faa15affa29350348477c534b790 Mon Sep 17 00:00:00 2001 From: Stephan Hradek Date: Wed, 12 Dec 2018 13:52:00 +0100 Subject: [PATCH 1/2] Update port-proxy --- src/port-proxy | 54 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/src/port-proxy b/src/port-proxy index 2614e41..452b343 100755 --- a/src/port-proxy +++ b/src/port-proxy @@ -24,10 +24,15 @@ my $detach=0; # detach (unix only) #-- options -- my %opts; -getopt('d:c:D',\%opts); +getopts('dc:Dp:',\%opts); if (exists $opts{'d'}) { $debug=1; } if (exists $opts{'c'}) { $configFile=$opts{'c'}; } if (exists $opts{'D'}) { $detach=1; } +# -p filename.pid will put +# kill \ +# PID +# rm filename.pid +# into the file. Usefull for killing the daemon and removing the pidfile #---- Global Vars --- my @allow_proxy_to=(); @@ -172,22 +177,32 @@ sub ReadConfig() @allow_proxy_to=(); open(F,"<$configFile") || die "can't open file $configFile\n"; while() { - $line=$_; - $line=~s/#.*$//g; # remove comments - if ($debug) { print "Conf>$line"; } - if ( $line=~/^\s*(.+?)=(.*?)\s*$/) { - $param=$1; $val=$2; - if ($param eq 'forward') { - @aval=split(/ *, */,$val); - CreateListenSock($aval[0],$aval[1],$aval[2],$aval[3]); - } elsif($param eq 'allow_proxy_to') { - push(@allow_proxy_to,$val) - } - } + $line= $_; + $line=~ s/#.*$//g; # remove comments + $line=~ s/\$(\w+|\{\w+\})/envvar($1)/ge; + if ($debug) { print "Conf>$line"; } + if ( $line=~/^\s*(.+?)=(.*?)\s*$/) { + $param=$1; $val=$2; + if ($param eq 'forward') { + @aval=split(/ *, */,$val); + CreateListenSock($aval[0],$aval[1],$aval[2],$aval[3]); + } elsif($param eq 'allow_proxy_to') { + push(@allow_proxy_to,$val) + } + } } close(F); } +sub envvar { + my($varname)= @_; + for ($varname) { + s/^\{//; + s/\}$// + } + return defined($ENV{$varname}) ? $ENV{$varname} : $_[0]; +} + #--- # Handle some (unix) signals; HUP: restart with reading config; TERM/KILL: shut down sub sigHandler($) # sigHandler(signame) @@ -374,8 +389,15 @@ sub daemonize { open STDIN, '/dev/null' or die "Can't read /dev/null: $!"; open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!"; defined(my $pid = fork) or die "Can't fork: $!"; - exit if $pid; - setsid or die "Can't start a new session: $!"; - open STDERR, '>&STDOUT' or die "Can't dup stdout: $!"; + if ($pid) { + if ($opts{'p'}) { + open my $pidfile, '>', $opts{'p'} or exit; + print $pidfile "kill \\\n$pid\nrm ".$opts{'p'}."\n"; + close $pidfile; + } + exit; + } + setsid or die "Can't start a new session: $!"; + open STDERR, '>&STDOUT' or die "Can't dup stdout: $!"; } From e73edfac83cd4fc1edbaa3090d2b1dd2d94d5cec Mon Sep 17 00:00:00 2001 From: Stephan Hradek Date: Wed, 12 Dec 2018 13:58:47 +0100 Subject: [PATCH 2/2] Update README.md --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fc431b8..8bf757f 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,11 @@ Configuration ============= -All the configuration is done in the file port-proxy.conf, which is readed from the current directory when starting port-proxy +All the configuration is done in the file port-proxy.conf, which is readed from the current directory when starting port-proxy. + +It is possible to use environment variables in the configuration file. Any $WORD or ${WORD} will be replaced by the value of the environment variable WORD, provided that the variable exists. + +**NOTE**: There is currently no way to use the character "$" when it is followed by a word character or a "{". There are the following parameters which can apear multiple times: @@ -117,6 +121,8 @@ perl port-proxy [-d] [-D] [-c conffile] -d Enable debug output -D Become a background process (detach don't work on windows) -c conffile Specify an config file +-p PIDFILE Create a file containing th PID plus commands to + to send a kill to the process and remove the pidfile ``` **Example:**