synapse/check.server.pl
author Peter Gervai <grin@grin.hu>
Wed, 03 Aug 2022 10:43:29 +0200
changeset 11 3d9a0d238469
permissions -rwxr-xr-x
Add check.server.pl: checks whether a matrix server is real and responding * uses both spec based checks and MSC1929 and nodeinfo2 * not pretty, not robust, just works
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     1
#!/usr/bin/perl
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     2
#$Id: check.server.pl,v 1d715c5989e8 2022/08/03 08:42:01 grin $
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     3
#
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     4
# (cc)by_sa-4.0 / GPLv3+  peter 'grin' gervai  @grin:grin.hu
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     5
#
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     6
# hastily check a matrix server
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     7
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     8
## requires:
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     9
##  - libjson-perl
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    10
##  - curl
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    11
##  - fping
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    12
##  - dnsutils (dig)
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    13
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    14
## todo:
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    15
##  - apart from complete rewriting?
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    16
##  - check whether curl, fping, dig present
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    17
##  - well, really, check _anything_. no real error handling as of now.
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    18
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    19
# returns:
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    20
#  1: error
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    21
#  0: ok
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    22
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    23
use warnings; use strict;
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    24
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    25
# https://$d/.well-known/matrix/server
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    26
# https://$d/.well-known/matrix/support
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    27
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    28
use JSON;
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    29
use Data::Dumper;
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    30
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    31
# 1: print everything, verbosely, even if it's unparseable.
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    32
# 0: less chat, bad results are fatal; use exit code
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    33
my $VERBOSE=1;
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    34
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    35
my $j = JSON->new->allow_nonref;
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    36
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    37
my $d = $ARGV[0];
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    38
if( !defined( $d ) ) {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    39
    print "Usage: $0 <matrix_domain>\nExample: $0 grin.hu\n";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    40
    exit 1;
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    41
}
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    42
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    43
my $data_source=0;
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    44
# curl parameters for connection
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    45
my $timeout="--connect-timeout 5";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    46
my $follow_redirs="--location --max-redirs 4";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    47
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    48
# collect well-known, infodata, nodeinfo, dns SRV
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    49
my ($srv,$port) = &get_wk($d);
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    50
&get_wk_infodata($d);
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    51
&get_wk_nodeinfo2($d);
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    52
my ($srv2,$port2) = &get_srv($d);
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    53
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    54
if( $srv ) { 
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    55
    # we have well-known data, lovely
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    56
    $data_source=1;
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    57
} elsif( $srv2 ) {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    58
    # we have SRV, it'll do
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    59
    ($srv,$port) = ($srv2,$port2);
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    60
    $data_source=2;
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    61
} else {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    62
    # lazy admin mode
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    63
    ($srv,$port) = ($d, 8448);
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    64
    $data_source=3;
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    65
}
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    66
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    67
print "FINAL result is $srv:$port. Checking.\n";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    68
my $srv_full = "$srv:$port";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    69
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    70
# ping the server first
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    71
print "Ping: ";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    72
my $res = `fping --retry=3 --timeout=500 $srv`; chomp $res;
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    73
print "$res!\n";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    74
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    75
# get server version
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    76
print "SrvVersion: ";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    77
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    78
my $cmd1;
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    79
if( $data_source == 2 ) {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    80
    # SRV uses domain for SNI
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    81
    $cmd1 = "-s $timeout $follow_redirs --connect-to $srv https://$d:$port/_matrix/federation/v1/version";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    82
} else {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    83
    # everyone else go for the hostname
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    84
    $cmd1 = "-s $timeout $follow_redirs https://$srv_full/_matrix/federation/v1/version";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    85
    
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    86
    if( $srv_full eq "" ) {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    87
        # shall not happen (and really, can't now)
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    88
        &fail( "srv_full empty while data_source=$data_source");
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    89
    } 
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    90
}
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    91
my $cmd  = "curl $cmd1";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    92
$res = qx($cmd);
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    93
if( $res =~ /"server"/ ) {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    94
    # seems like valid data
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    95
    my $data = $j->decode($res);
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    96
    my $v = $$data{'server'};
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    97
    
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    98
    print $$v{name} . '/' . $$v{version}."!\n";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    99
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   100
} else {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   101
    # got something, but it's crap.
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   102
    print "no parseable data\n";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   103
    if( !$VERBOSE ) {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   104
        &fail("no paresable data");
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   105
    }
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   106
    
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   107
    print "here's the debug: ";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   108
    $cmd="curl $timeout --verbose $cmd1";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   109
    print "$cmd\n";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   110
    system $cmd;
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   111
    &fail("no parseable data");
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   112
}
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   113
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   114
# sucess
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   115
exit 0;
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   116
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   117
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   118
##########
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   119
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   120
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   121
# get well-known in a na??ve way
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   122
sub get_wk {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   123
    my ($d) = @_;
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   124
    
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   125
    ## try well-known matrix/server (simplistic parser; spec)
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   126
    print "WellKnown: ";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   127
    $cmd = "curl $timeout $follow_redirs -s https://$d/.well-known/matrix/server";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   128
    $res = qx($cmd);
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   129
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   130
    if( $res=~/m.server/ ) {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   131
        my $data = $j->decode($res);
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   132
        my $srv_full = $$data{'m.server'};
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   133
        if( $srv_full =~ /^(.+):(.+)$/ ) {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   134
            # we have proper form
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   135
            ($srv,$port) = ($1,$2);
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   136
            } else {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   137
                # missing port or crap result
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   138
                print Dumper($data);
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   139
                print "$srv_full don't contain port!!\n";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   140
                print "FAKING port 8448\n";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   141
                $port=8448;
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   142
            }
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   143
        print "$srv on port $port!\n";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   144
    } else {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   145
        # I can't even...
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   146
        print " (unparseable response from $cmd )\n";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   147
        print "$res\n" if $VERBOSE;;
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   148
        print "No well-known 'm.server'.\n";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   149
        return;
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   150
    }
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   151
    return ($srv,$port);
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   152
}
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   153
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   154
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   155
## get well-known/matrix/support info (infodata; MSC1929)
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   156
sub get_wk_infodata {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   157
    my ($d) = @_;
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   158
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   159
    print "# Checking https://$d/.well-known/matrix/support\n";    
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   160
    print "WellKnown(MSC1929): ";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   161
    $cmd = "curl $timeout $follow_redirs -s https://$d/.well-known/matrix/support";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   162
    $res = qx($cmd);
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   163
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   164
    if( $res=~/admins/ ) {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   165
        my $data = $j->decode($res);
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   166
        my $admins = $$data{'admins'};
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   167
        
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   168
        for my $entry (@$admins) {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   169
            print $$entry{'role'}.":";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   170
            delete $$entry{'role'};
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   171
            for my $k (sort keys %$entry) {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   172
                print "$k=" . $$entry{$k}." ";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   173
            }
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   174
            print "; ";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   175
        }
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   176
        print "\n";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   177
    } else {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   178
        print "nope\n";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   179
    }
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   180
}
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   181
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   182
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   183
# try get nodeinfo2, https://github.com/jaywink/nodeinfo2
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   184
sub get_wk_nodeinfo2 {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   185
    my ($d) = @_;
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   186
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   187
    print "# checking https://$d/.well-known/x-nodeinfo2\n";    
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   188
    print "nodeinfo2: ";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   189
    $cmd = "curl $timeout $follow_redirs -s https://$d/.well-known/x-nodeinfo2";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   190
    $res = qx($cmd);
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   191
    if( $res =~ /version/ ) {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   192
        my $data = $j->decode($res);
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   193
        if( $$data{'version'} ne '1.0' ) {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   194
            print " (unknown version " . $$data{'version'} . ") ";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   195
        }
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   196
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   197
        if( $$data{'openRegistrations'} ) {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   198
            print "OPENREG! ";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   199
        }
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   200
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   201
        print "proto: " . join(',', @{$$data{'protocols'}}) . " ";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   202
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   203
        print "srv: ";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   204
        for my $field ( qw( baseUrl name software ) ) {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   205
            print "$field=" . $$data{'server'}{$field} . " ";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   206
        }
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   207
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   208
        print "org: ";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   209
        for my $field ( qw( name contact admin ) ) {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   210
            print "$field=" . $$data{'organization'}{$field} . " ";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   211
        }
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   212
    } else {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   213
        print "none";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   214
    }
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   215
    print "\n";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   216
}
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   217
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   218
# DNS SRV in an extremely lazy way
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   219
sub get_srv {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   220
    my ($d) = @_;
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   221
        
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   222
    my ($srv,$port);
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   223
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   224
    ## try srv (simplistic as well)
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   225
    print "SRV (_matrix._tcp.$d): ";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   226
    $res = `dig _matrix._tcp.$d srv +short`;
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   227
    print "$res";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   228
    if( $res =~ /^(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\.$/ ) {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   229
        # looks good
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   230
        my ($pri1,$pri2);
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   231
        ($pri1, $pri2, $port, $srv) = ($1,$2,$3,$4);
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   232
    } else {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   233
        print "No SRV found.\n";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   234
        return;
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   235
    }
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   236
    return ($srv,$port);
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   237
}
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   238
    
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   239
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   240
# guess what?
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   241
sub fail {
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   242
    my ($msg) = @_;
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   243
    print "FAIL: $msg\n";
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   244
    exit(1);
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   245
}
3d9a0d238469 Add check.server.pl: checks whether a matrix server is real and responding
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   246