From 7930db0d41f6b3cdec627def8663c18e909bfcb6 Mon Sep 17 00:00:00 2001 From: Frederik Wedel-Heinen Date: Mon, 22 Jan 2024 14:12:06 +0100 Subject: [PATCH] Use open2 instead of open for s_server instance --- util/perl/TLSProxy/Proxy.pm | 65 ++++++------------------------------- 1 file changed, 10 insertions(+), 55 deletions(-) diff --git a/util/perl/TLSProxy/Proxy.pm b/util/perl/TLSProxy/Proxy.pm index 0084328a5f17a..08c82c0c52d06 100644 --- a/util/perl/TLSProxy/Proxy.pm +++ b/util/perl/TLSProxy/Proxy.pm @@ -7,6 +7,7 @@ use strict; use POSIX ":sys_wait_h"; +use IPC::Open2; package TLSProxy::Proxy; @@ -203,7 +204,7 @@ sub connect_to_server Proto => $self->{isdtls} ? 'udp' : 'tcp'); if (!defined($sock)) { my $err = $!; - kill(3, $self->{real_serverpid}); + kill(3, $self->{serverpid}); die "unable to connect: $err\n"; } @@ -215,7 +216,6 @@ sub start my ($self) = shift; my $pid; - # Create the Proxy socket my $proxaddr = $self->{proxy_addr}; $proxaddr =~ s/[\[\]]//g; # Remove [ and ] @@ -290,17 +290,13 @@ sub start print STDERR "Server command: $execcmd\n"; } - open(my $savedin, "<&STDIN"); - - # Temporarily replace STDIN so that sink process can inherit it... - $pid = open(STDIN, "$execcmd 2>&1 |") or die "Failed to $execcmd: $!\n"; - $self->{real_serverpid} = $pid; + $pid = IPC::Open2::open2(my $sout, my $sin, $execcmd) or die "Failed to $execcmd: $!\n"; + $self->{serverpid} = $pid; # Process the output from s_server until we find the ACCEPT line, which # tells us what the accepting address and port are. - while (<>) { - print; - s/\R$//; # Better chomp + while (<$sout>) { + chomp; next unless (/^ACCEPT\s.*:(\d+)$/); $self->{server_port} = $1; last; @@ -313,38 +309,6 @@ sub start die "no ACCEPT detected in '$execcmd' output: $?\n"; } - # Just make sure everything else is simply printed [as separate lines]. - # The sub process simply inherits our STD* and will keep consuming - # server's output and printing it as long as there is anything there, - # out of our way. - my $error; - $pid = undef; - if (eval { require Win32::Process; 1; }) { - if (Win32::Process::Create(my $h, $^X, "perl -ne print", 0, 0, ".")) { - $pid = $h->GetProcessID(); - $self->{proc_handle} = $h; # hold handle till next round [or exit] - } else { - $error = Win32::FormatMessage(Win32::GetLastError()); - } - } else { - if (defined($pid = fork)) { - $pid or exec("$^X -ne print") or exit($!); - } else { - $error = $!; - } - } - - # Change back to original stdin - open(STDIN, "<&", $savedin); - close($savedin); - - if (!defined($pid)) { - kill(3, $self->{real_serverpid}); - die "Failed to capture s_server's output: $error\n"; - } - - $self->{serverpid} = $pid; - print STDERR "Server responds on ", "$self->{server_addr}:$self->{server_port}\n"; @@ -401,7 +365,7 @@ sub clientstart # dead-lock... if (!($pid = open(STDOUT, "| $execcmd"))) { my $err = $!; - kill(3, $self->{real_serverpid}); + kill(3, $self->{serverpid}); die "Failed to $execcmd: $err\n"; } $self->{clientpid} = $pid; @@ -417,7 +381,7 @@ sub clientstart # Wait for incoming connection from client my $fdset = IO::Select->new($self->{proxy_sock}); if (!$fdset->can_read(60)) { - kill(3, $self->{real_serverpid}); + kill(3, $self->{serverpid}); die "s_client didn't try to connect\n"; } @@ -476,14 +440,14 @@ sub clientstart $server_sock->shutdown(SHUT_WR); } } else { - kill(3, $self->{real_serverpid}); + kill(3, $self->{serverpid}); die "Unexpected handle"; } } } if ($ctr >= 10) { - kill(3, $self->{real_serverpid}); + kill(3, $self->{serverpid}); print "No progress made\n"; $succes = 0; } @@ -502,15 +466,6 @@ sub clientstart my $pid; if (--$self->{serverconnects} == 0) { $pid = $self->{serverpid}; - print "Waiting for 'perl -ne print' process to close: $pid...\n"; - $pid = waitpid($pid, 0); - if ($pid > 0) { - die "exit code $? from 'perl -ne print' process\n" if $? != 0; - } elsif ($pid == 0) { - kill(3, $self->{real_serverpid}); - die "lost control over $self->{serverpid}?"; - } - $pid = $self->{real_serverpid}; print "Waiting for s_server process to close: $pid...\n"; # it's done already, just collect the exit code [and reap]... waitpid($pid, 0);