1 |
#! perl |
2 |
|
3 |
# metaserver2 reports |
4 |
|
5 |
use URI; |
6 |
use URI::Escape (); |
7 |
use Coro::Socket; |
8 |
|
9 |
$cf::CFG{metaserver2_urls} |
10 |
or return; |
11 |
|
12 |
our $UPDATE_METASERVER2 = Event->timer ( |
13 |
reentrant => 0, |
14 |
after => 0, |
15 |
interval => 299, # crossfire itself uses 60something as interval |
16 |
hard => 1, |
17 |
data => cf::WF_AUTOCANCEL, |
18 |
cb => Coro::unblock_sub { |
19 |
my %form = ( |
20 |
hostname => $cf::CFG{metaserver2_hostname}, |
21 |
port => $cf::CFG{metaserver2_port}, |
22 |
|
23 |
html_comment => $cf::CFG{metaserver2_html_comment}, |
24 |
text_comment => $cf::CFG{metaserver2_text_comment}, |
25 |
flags => $cf::CFG{metaserver2_flags} || "K", |
26 |
archbase => $cf::CFG{archbase} || "TRT", |
27 |
mapbase => $cf::CFG{mapbase} || "TRT", |
28 |
codebase => $cf::CFG{codebase} || "TRT", |
29 |
|
30 |
num_players => cf::player::num_playing, |
31 |
in_bytes => 0, # no can do |
32 |
out_bytes => 0, # no can do |
33 |
uptime => time - $cf::UPTIME, |
34 |
|
35 |
version => cf::VERSION, |
36 |
sc_version => cf::VERSION_SC, |
37 |
cs_version => cf::VERSION_CS, |
38 |
); |
39 |
|
40 |
my $content = |
41 |
join "&", |
42 |
map "$_=" . (URI::Escape::uri_escape_utf8 $form{$_}), |
43 |
keys %form; |
44 |
|
45 |
$content .= "\015\012"; # for good form |
46 |
|
47 |
# this is a bit hacky, but hey, invoking LWP on something so trivial feels like a sin |
48 |
|
49 |
for my $url (@{ $cf::CFG{metaserver2_urls} || [] }) { |
50 |
$url = new URI $url |
51 |
or next; |
52 |
$url->scheme eq "http" |
53 |
or next; |
54 |
|
55 |
my $socket = new Coro::Socket |
56 |
Timeout => 60, |
57 |
PeerAddr => $url->host_port, |
58 |
LocalAddr => $cf::CFG{metaserver2_serveraddr} |
59 |
or (warn "$url: connection error: $!"), next; |
60 |
|
61 |
syswrite $socket, join "", |
62 |
"POST ", $url->path, " HTTP/1.0\015\012", |
63 |
"Host: ", $url->host, "\015\012", |
64 |
"Content-Type: application/x-www-form-urlencoded\015\012", |
65 |
"User-Agent: Crossfire TRT Server (http://crossfire.schmorp.de)\015\012", |
66 |
"Content-Length: ", length $content, "\015\012", |
67 |
"\015\012", |
68 |
$content |
69 |
; |
70 |
|
71 |
shutdown $socket, 1; |
72 |
|
73 |
my $response = $socket->readline (undef); |
74 |
|
75 |
unless ($response =~ /^HTTP\/[0-9.]+\s+200\s+/) { |
76 |
warn "$url: $response\n"; |
77 |
} |
78 |
} |
79 |
}, |
80 |
); |
81 |
|