Skip to content

Commit 62da801

Browse files
committed
hack/test-port-forwarding.pl: add reporting "failed to listen tcp"
Add reporting for the "failed to listen tcp" error. Additionally, it should print information about the processes listening on the failed ports. e.g. ```console $ socat TCP-LISTEN:2020,bind=127.0.0.1,fork /dev/null& $ socat TCP-LISTEN:4040,bind=127.0.0.1,fork /dev/null& $ hack/test-port-forwarding.pl default ✅ Not forwarding TCP 127.0.0.2:3000 ✅ Forwarding TCP from 127.0.0.3:3001 to 127.0.0.1:2001 ✅ Forwarding TCP from 0.0.0.0:3002 to 127.0.0.1:2002 ✅ Not forwarding TCP 0.0.0.0:3010 ✅ Not forwarding TCP 127.0.0.1:3011 ❌ Forwarding TCP from 127.0.0.2:3020 to 127.0.0.1:2020 Guest received: '' ✅ Forwarding TCP from 127.0.0.1:3021 to 127.0.0.1:2021 ✅ Forwarding TCP from 0.0.0.0:3022 to 127.0.0.1:2022 ✅ Forwarding TCP from [::]:3023 to 127.0.0.1:2023 ✅ Forwarding TCP from [::1]:3024 to 127.0.0.1:2024 ✅ Forwarding TCP from 127.0.0.1:3030 to 192.168.12.19:2030 ✅ Forwarding TCP from 0.0.0.0:3031 to 192.168.12.19:2031 ✅ Forwarding TCP from [::]:3032 to 192.168.12.19:2032 ✅ Forwarding TCP from [::1]:3033 to 192.168.12.19:2033 ✅ Forwarding TCP from 127.0.0.1:300 to 127.0.0.1:300 ✅ Forwarding TCP from 127.0.0.1:310 to 0.0.0.0:310 ✅ Forwarding TCP from 192.168.5.15:4000 to 192.168.12.19:4000 ✅ Forwarding TCP from [::1]:4010 to [::]:4010 ✅ Forwarding TCP from 127.0.0.1:4020 to 192.168.12.19:4020 ✅ Forwarding TCP from 127.0.0.2:4021 to 192.168.12.19:4021 ✅ Forwarding TCP from 192.168.5.15:4022 to 192.168.12.19:4022 ✅ Forwarding TCP from 0.0.0.0:4023 to 192.168.12.19:4023 ✅ Forwarding TCP from [::]:4024 to 192.168.12.19:4024 ✅ Forwarding TCP from [::1]:4025 to 192.168.12.19:4025 ✅ Forwarding TCP from 127.0.0.1:4030 to 192.168.12.19:4030 ✅ Forwarding TCP from 127.0.0.2:4031 to 192.168.12.19:4031 ✅ Forwarding TCP from 192.168.5.15:4032 to 192.168.12.19:4032 ✅ Forwarding TCP from 0.0.0.0:4033 to 192.168.12.19:4033 ✅ Forwarding TCP from [::]:4034 to 192.168.12.19:4034 ✅ Forwarding TCP from [::1]:4035 to 192.168.12.19:4035 ❌ Forwarding TCP from 0.0.0.0:4040 to 127.0.0.1:4040 Guest received: '' ✅ Forwarding TCP from [::]:4041 to 127.0.0.1:4041 ✅ Not forwarding TCP 127.0.0.1:4043 ✅ Not forwarding TCP 192.168.5.15:4044 ✅ Forwarding TCP from 127.0.0.1:5000 to /Users/norio/.lima/default/sock/port5000.sock ⚠️ failed to listen tcp: listen tcp 127.0.0.1:2020: bind: address already in use ⚠️ failed to listen tcp: listen tcp 127.0.0.1:4040: bind: address already in use COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME socat1 76346 norio 5u IPv4 0x4a9052b6dc39e98b 0t0 TCP localhost:2020 (LISTEN) socat1 76381 norio 5u IPv4 0x35a18a4bd36fb075 0t0 TCP localhost:4040 (LISTEN) ``` Signed-off-by: Norio Nomura <norio.nomura@gmail.com>
1 parent 8944d8b commit 62da801

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

hack/test-port-forwarding.pl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,11 @@
187187
open(my $log, "< $ha_log") or die "Can't read $ha_log: $!";
188188
seek($log, $ha_log_size, 0) or die "Can't seek $ha_log to $ha_log_size: $!";
189189
my %seen;
190+
my %failed_to_listen_tcp;
190191
while (<$log>) {
191192
$seen{$1}++ if /(Forwarding TCP from .*? to ((\d.*?|\[.*?\]):\d+|\/[^"]+))/;
192193
$seen{$1}++ if /(Not forwarding TCP .*?:\d+)/;
194+
$failed_to_listen_tcp{$2}=$1 if /(failed to listen tcp: listen tcp (.*?:\d+):[^"]+)/;
193195
}
194196
close $log or die;
195197

@@ -220,6 +222,24 @@
220222
print "😕 Unexpected log message: $_\n";
221223
}
222224

225+
if (%failed_to_listen_tcp) {
226+
foreach (keys %failed_to_listen_tcp) {
227+
print "⚠️ $failed_to_listen_tcp{$_}\n";
228+
}
229+
my @tcp_list = keys %failed_to_listen_tcp;
230+
if ($Config{osname} eq "darwin") {
231+
my @lsof_args = map { "-iTCP\@$_" } @tcp_list;
232+
print `lsof -P @lsof_args`;
233+
} elsif ($Config{osname} eq "linux") {
234+
my @lss_args = map { "src = $_" } @tcp_list;
235+
my $ss_expression = join(" or ", @lss_args);
236+
print `sudo ss -lnpt "$ss_expression"`;
237+
} elsif ($Config{osname} eq "cygwin") {
238+
my @awk_args = map { "-e'/$_/'" } @tcp_list;
239+
print `netstat -aon | awk -e'/^ +Proto/' @awk_args`;
240+
}
241+
}
242+
223243
# Cleanup remaining netcat instances (and port forwards)
224244
print $lima "sudo pkill -x nc";
225245

0 commit comments

Comments
 (0)