Skip to content

Commit

Permalink
feat(ipv6): Update test-port-forwarding to obtain system's IPv6 address
Browse files Browse the repository at this point in the history
  • Loading branch information
heyvito committed May 15, 2023
1 parent 17585c2 commit 71ac023
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions hack/test-port-forwarding.pl
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,38 @@

use Config qw(%Config);
use IO::Handle qw();
use Socket qw(inet_ntoa);
use Socket qw(inet_ntop sockaddr_in sockaddr_in6 getaddrinfo AI_CANONNAME SOCK_STREAM AF_INET AF_INET6);
use Sys::Hostname qw(hostname);

my $instance = shift;

my $addr = scalar gethostbyname(hostname());
my $hints = {};
$hints->{flags} = AI_CANONNAME;
$hints->{socktype} = SOCK_STREAM;

# If hostname address cannot be determines, use localhost to trigger fallback to system_profiler lookup
my $ipv4 = length $addr ? inet_ntoa($addr) : "127.0.0.1";
my $ipv6 = ""; # todo
my $ipv4 = "";
my $ipv6 = "";
my ($err, @addrs) = getaddrinfo(hostname(), undef, $hints);

if (@addrs) {
for (@addrs) {
if ($_->{family} == AF_INET && $ipv4 eq "") {
my $addr4 = (sockaddr_in($_->{addr}))[1];
$ipv4 = inet_ntop(AF_INET, $addr4);
} elsif ($_->{family} == AF_INET6 && $ipv6 eq "") {
my $addr6 = (sockaddr_in6($_->{addr}))[1];
$ipv6 = inet_ntop(AF_INET6, $addr6);
}
}
}

# macOS Github runners seem to use "localhost" as the hostname
if ($ipv4 eq "127.0.0.1" && $Config{osname} eq "darwin") {
$ipv4 = qx(system_profiler SPNetworkDataType -json | jq -r 'first(.SPNetworkDataType[] | select(.ip_address) | .ip_address) | first');
$ipv4 = qx(system_profiler SPNetworkDataType -json | jq -r 'first(.SPNetworkDataType[] | select(.IPv4.Addresses)) | .IPv4.Addresses | first');
chomp $ipv4;
$ipv6 = qx(system_profiler SPNetworkDataType -json | jq -r 'first(.SPNetworkDataType[] | select(.IPv6.Addresses)) | .IPv6.Addresses | first');
chomp $ipv6;
}

# If $instance is a filename, add our portForwards to it to enable testing
Expand Down

0 comments on commit 71ac023

Please sign in to comment.