Skip to content

Commit

Permalink
Use open2 instead of open for s_server instance
Browse files Browse the repository at this point in the history
  • Loading branch information
fwh-dc committed Jan 22, 2024
1 parent 5158d64 commit 7930db0
Showing 1 changed file with 10 additions and 55 deletions.
65 changes: 10 additions & 55 deletions util/perl/TLSProxy/Proxy.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use strict;
use POSIX ":sys_wait_h";
use IPC::Open2;

package TLSProxy::Proxy;

Expand Down Expand Up @@ -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";
}

Expand All @@ -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 ]
Expand Down Expand Up @@ -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;
Expand All @@ -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";

Expand Down Expand Up @@ -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;
Expand All @@ -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";
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down

0 comments on commit 7930db0

Please sign in to comment.