| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||
| 3 | # | ||||||||||
| 4 | # (C) Paul Evans, 2015-2016 -- leonerd@leonerd.org.uk | ||||||||||
| 5 | |||||||||||
| 6 | package App::MatrixTool::Command::resolve; | ||||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use base qw( App::MatrixTool ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 1 | our $VERSION = '0.06'; | |||||||||
| 13 | |||||||||||
| 14 | 1 | ▶ | use Future::Utils qw( try_repeat ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 1 | ▶ | use constant DESCRIPTION => "Look up hostnames or IP addresses of a server"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 1 | ▶ | use constant ARGUMENTS => ( "server_name" ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 1 | use constant OPTIONS => ( | |||||||||
| 19 | 'a|address' => 'Resolve hostnames into IP addressese', | ||||||||||
| 20 | 1 | ▶ | ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | |||||||||||
| 22 | =head1 NAME | ||||||||||
| 23 | |||||||||||
| 24 | matrixtool resolve - Look up hostnames or IP addresses of a server | ||||||||||
| 25 | |||||||||||
| 26 | =head1 SYNOPSIS | ||||||||||
| 27 | |||||||||||
| 28 | $ matrixtool resolve my-server.org | ||||||||||
| 29 | |||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||
| 31 | |||||||||||
| 32 | This command attempts to find the hostnames or IP addresses to use to | ||||||||||
| 33 | communicate with a given Matrix homeserver. It does not attempt to actually | ||||||||||
| 34 | talk to the server, it merely queries in DNS for information on how to find | ||||||||||
| 35 | the server. It is typically the first diagnostic command to use to determine | ||||||||||
| 36 | whether Matrix server federation is working, and if not where it is failing. | ||||||||||
| 37 | |||||||||||
| 38 | =head1 OPTIONS | ||||||||||
| 39 | |||||||||||
| 40 | The following additional options are recognised: | ||||||||||
| 41 | |||||||||||
| 42 | =over 4 | ||||||||||
| 43 | |||||||||||
| 44 | =item C<--address>, C<-a> | ||||||||||
| 45 | |||||||||||
| 46 | Also resolve hostnames into IP addresses in printed output. | ||||||||||
| 47 | |||||||||||
| 48 | =back | ||||||||||
| 49 | |||||||||||
| 50 | =cut | ||||||||||
| 51 | |||||||||||
| 52 | sub run | ||||||||||
| 53 | { | ||||||||||
| 54 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
run: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 55 | 0 | my ( $opts, $server_name ) = @_; | |||||||||
| 56 | |||||||||||
| 57 | $self->http_client->resolve_matrix( $server_name )->then( sub { | ||||||||||
| 58 | 0 | ▶ | my @res = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 59 | # SRV records yield a 'weight' field, A/AAAA-based backup does not | ||||||||||
| 60 | defined $res[0]->{weight} | ||||||||||
| 61 | 0 | ▶ | ? $self->output_info( "Resolved $server_name by SRV" ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | : $self->output_info( "Using legacy IP address fallback" ); | ||||||||||
| 63 | |||||||||||
| 64 | try_repeat { | ||||||||||
| 65 | 0 | ▶ | my $res = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 66 | |||||||||||
| 67 | 0 | $self->output( "target=$res->{target} port=$res->{port}" ); | |||||||||
| 68 | |||||||||||
| 69 | $opts->{address} or | ||||||||||
| 70 | 0 | ▶ | return Future->done; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | |||||||||||
| 72 | $self->http_client->resolve_addr( $res->{target} ) | ||||||||||
| 73 | ->then( sub { | ||||||||||
| 74 | 0 | ▶ | my @addrs = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 75 | 0 | foreach my $addr ( @addrs ) { | |||||||||
| 76 | 0 | $self->output( " " . $self->format_hostport( $addr, $res->{port} ) ); | |||||||||
| 77 | } | ||||||||||
| 78 | 0 | Future->done; | |||||||||
| 79 | }) | ||||||||||
| 80 | ->else( sub { | ||||||||||
| 81 | 0 | ▶ | $self->output( " address unknown [$_[0]]" ); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 82 | 0 | Future->done; | |||||||||
| 83 | 0 | }); | |||||||||
| 84 | 0 | } foreach => \@res; | |||||||||
| 85 | 0 | }); | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | =head1 EXAMPLES | ||||||||||
| 89 | |||||||||||
| 90 | For example, retrieving the F<matrix.org> server's information: | ||||||||||
| 91 | |||||||||||
| 92 | $ matrixtool resolve matrix.org | ||||||||||
| 93 | [INFO] Resolved matrix.org by SRV | ||||||||||
| 94 | target=matrix.org port=8448 | ||||||||||
| 95 | |||||||||||
| 96 | We can also see the IP addresses associated with these hostnames: | ||||||||||
| 97 | |||||||||||
| 98 | $ matrixtool resolve -a matrix.org | ||||||||||
| 99 | [INFO] Resolved matrix.org by SRV | ||||||||||
| 100 | target=matrix.org port=8448 | ||||||||||
| 101 | 83.166.64.33:8448 | ||||||||||
| 102 | |||||||||||
| 103 | Some networks don't publish a C<SRV> record in DNS, so for those the hostname | ||||||||||
| 104 | is implied directly from the server name. For example: | ||||||||||
| 105 | |||||||||||
| 106 | $ matrixtool resolve example.org | ||||||||||
| 107 | [INFO] Using legacy IP address fallback | ||||||||||
| 108 | target=example.org port=8448 | ||||||||||
| 109 | |||||||||||
| 110 | =cut | ||||||||||
| 111 | |||||||||||
| 112 | =head1 AUTHOR | ||||||||||
| 113 | |||||||||||
| 114 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||
| 115 | |||||||||||
| 116 | =cut | ||||||||||
| 117 | |||||||||||
| 118 | 1 | 0x55AA; | |||||||||