File Coverage

blib/lib/App/MatrixTool/Command/resolve.pm
Criterion Covered Total %
statement 21 37 56.7
branch 0 4 0.0
condition n/a
subroutine 7 9 77.7
pod 0 1 0.0
total 28 51 54.9


line stmt bran cond sub pod time code
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     1   862 use strict;
  1         2  
  1         28  
9 1     1   3 use warnings;
  1         2  
  1         25  
10 1     1   9 use base qw( App::MatrixTool );
  1         2  
  1         92  
11              
12             our $VERSION = '0.08';
13              
14 1     1   4 use Future::Utils qw( try_repeat );
  1         7  
  1         52  
15              
16 1     1   4 use constant DESCRIPTION => "Look up hostnames or IP addresses of a server";
  1         1  
  1         49  
17 1     1   4 use constant ARGUMENTS => ( "server_name" );
  1         2  
  1         54  
18 1         324 use constant OPTIONS => (
19             'a|address' => 'Resolve hostnames into IP addressese',
20 1     1   4 );
  1         1  
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     0 0   my $self = shift;
55 0           my ( $opts, $server_name ) = @_;
56              
57             $self->http_client->resolve_matrix( $server_name )->then( sub {
58 0     0     my @res = @_;
59             # SRV records yield a 'weight' field, A/AAAA-based backup does not
60             defined $res[0]->{weight}
61 0 0         ? $self->output_info( "Resolved $server_name by SRV" )
62             : $self->output_info( "Using legacy IP address fallback" );
63              
64             try_repeat {
65 0           my $res = shift;
66              
67 0           $self->output( "target=$res->{target} port=$res->{port}" );
68              
69             $opts->{address} or
70 0 0         return Future->done;
71              
72             $self->http_client->resolve_addr( $res->{target} )
73             ->then( sub {
74 0           my @addrs = @_;
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]]" );
82 0           Future->done;
83 0           });
84 0           } foreach => \@res;
85 0           });
86             }
87              
88             =head1 EXAMPLES
89              
90             For example, retrieving the F 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 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
115              
116             =cut
117              
118             0x55AA;