| 1 |
#!/usr/bin/env perl |
| 2 |
# Copyright 2000 by Mark Wedel. |
| 3 |
# This script follows the same license as crossfire (GPL). |
| 4 |
|
| 5 |
use Socket; |
| 6 |
use English; |
| 7 |
|
| 8 |
# We periodically generate a nice HTML file that people can't put their |
| 9 |
# web browser at. This is the location of that file. |
| 10 |
$HTML_FILE="/var/apache/htdocs/metaserver.html"; |
| 11 |
|
| 12 |
# Cache file to keep data we ahve collected. This is used so that if |
| 13 |
# the metaserver program crashes/dies, it still has some old data. |
| 14 |
# You may want to set this to a location that will survive across system |
| 15 |
# reboots. |
| 16 |
$CACHE_FILE="/var/tmp/meta_xfire.cache"; |
| 17 |
|
| 18 |
# We remove a server after $REMOVE_SERVER number of seconds of no updates. |
| 19 |
# 600 is 10 minutes - if we haven't gotten an update that fast, the server |
| 20 |
# is almost certainly gone/not available. This reduces congestion when |
| 21 |
# someone on a dhcp connection keeps running a server and it fills |
| 22 |
# up a bunch of the slots. |
| 23 |
$REMOVE_SERVER=600; |
| 24 |
|
| 25 |
# UPDATE_SYNC determines how often we update the HTML_FILE and CACHE_FILE. |
| 26 |
$UPDATE_SYNC=300; |
| 27 |
|
| 28 |
# IP_INTERVAL is how often (in seconds) same IP can request metaserver |
| 29 |
# info. This is to prevent DOS attacks. |
| 30 |
|
| 31 |
$IP_INTERVAL=5; |
| 32 |
|
| 33 |
# For gathering some anonymous statistics. You probably want to use |
| 34 |
# MRTG/RRDTOOL for generating statistics from the file. |
| 35 |
# -- Heikki Hokkanen, 2005-03-26 |
| 36 |
my $STATS_FILE="/var/tmp/meta_xfire.stats"; |
| 37 |
my $stats_updatehits = 0; # COUNTER |
| 38 |
my $stats_requesthits = 0; # COUNTER |
| 39 |
my $stats_totalplayers = 0; # GAUGE |
| 40 |
my $stats_totalservers = 0; # GAUGE |
| 41 |
|
| 42 |
socket(SOCKET, PF_INET, SOCK_DGRAM, getprotobyname("udp")) || |
| 43 |
die("$0: can not open udp socket: $OS_ERROR\n"); |
| 44 |
bind(SOCKET, sockaddr_in(13326, INADDR_ANY)) || |
| 45 |
die("$0: Can not bind to socket: $OS_ERROR\n"); |
| 46 |
|
| 47 |
# Socket1 is used for incoming requests - if we get a connection on this, |
| 48 |
# we dump out our data to that socket in an easily parsible form. |
| 49 |
socket(SOCKET1, PF_INET, SOCK_STREAM, getprotobyname("tcp")) || |
| 50 |
die("$0: can not open tcp socket: $OS_ERROR\n"); |
| 51 |
|
| 52 |
# errors on this not that critical |
| 53 |
setsockopt(SOCKET1, SOL_SOCKET, SO_REUSEADDR, 1); |
| 54 |
|
| 55 |
bind(SOCKET1, sockaddr_in(13326, INADDR_ANY)) || |
| 56 |
die("$0: Can not bind to socket: $OS_ERROR\n"); |
| 57 |
listen(SOCKET1, 10) || |
| 58 |
die("$0: Can not listen on socket: $OS_ERROR\n"); |
| 59 |
|
| 60 |
vec($rin, fileno(SOCKET), 1)=1; |
| 61 |
vec($rin, fileno(SOCKET1), 1)=1; |
| 62 |
|
| 63 |
if (open(CACHE,"<$CACHE_FILE")) { |
| 64 |
while (<CACHE>) { |
| 65 |
chomp; |
| 66 |
($ip, $rest) = split /\|/, $_, 2; |
| 67 |
$data{$ip} = $_; |
| 68 |
} |
| 69 |
} |
| 70 |
close(CACHE); |
| 71 |
|
| 72 |
$last_sync=time; |
| 73 |
|
| 74 |
while (1) { |
| 75 |
$nfound=select($rout=$rin, undef, undef, 60); |
| 76 |
$cur_time=time; |
| 77 |
if ($nfound) { |
| 78 |
if (vec($rout, fileno(SOCKET),1)) { |
| 79 |
$ipaddr = recv(SOCKET, $data, 256, 0) || |
| 80 |
print STDERR "$0: error on recv call: $OS_ERROR\n"; |
| 81 |
($port, $ip) = sockaddr_in($ipaddr); |
| 82 |
$host = inet_ntoa $ip; |
| 83 |
($name, $rest) = split /\|/, $data; |
| 84 |
if ($name ne "put.your.hostname.here") { |
| 85 |
$data{$host} = "$host|$cur_time|$data"; |
| 86 |
$stats_updatehits++; |
| 87 |
} |
| 88 |
} |
| 89 |
if (vec($rout, fileno(SOCKET1),1)) { |
| 90 |
# This is overly basic - if there are enough servers |
| 91 |
# where the output won't fit in the outgoing socket, |
| 92 |
# this will block. However, if we fork, we open |
| 93 |
# ourselves up to someone intentionally designing something |
| 94 |
# that causes these to block, and then have us fork a huge |
| 95 |
# number of process potentially filling up our proc table. |
| 96 |
if ($ipaddr=accept NEWSOCKET, SOCKET1) { |
| 97 |
($port, $ip ) = sockaddr_in( $ipaddr ); |
| 98 |
$dq = join('.',unpack('C4', $ip)); |
| 99 |
if ($ip_times{$dq} > time) { |
| 100 |
close(NEWSOCKET); |
| 101 |
} else { |
| 102 |
$ip_times{$dq} = time + $IP_INTERVAL; |
| 103 |
foreach $i (keys %data) { |
| 104 |
# Report idle time to client, and not last update |
| 105 |
# as we received in seconds since epoch. |
| 106 |
($ip, $time, $rest) = split /\|/, $data{$i}, 3; |
| 107 |
$newtime = $cur_time - $time; |
| 108 |
print NEWSOCKET "$ip|$newtime|$rest\n"; |
| 109 |
} |
| 110 |
close(NEWSOCKET); |
| 111 |
$stats_requesthits++; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
# Need to generate some files. This is also where we remove outdated |
| 118 |
# hosts. |
| 119 |
if ($last_sync+$UPDATE_SYNC < $cur_time) { |
| 120 |
$last_sync = $cur_time; |
| 121 |
open(CACHE,">$CACHE_FILE"); |
| 122 |
open(HTML,">$HTML_FILE"); |
| 123 |
|
| 124 |
print HTML |
| 125 |
'<title>Crossfire Server List</title> |
| 126 |
<h1 align=center>Crossfire Server List</h1><p> |
| 127 |
<table border=1 align=center cellpadding=5> |
| 128 |
<tr> |
| 129 |
<th>IP Address</th><th>Last Update Date/Time</th><th>Last Update Minutes Elapsed</th> |
| 130 |
<th>Hostname</th><th>Number of Players</th><th>Version</th><th>Comment</th> |
| 131 |
</tr> |
| 132 |
'; |
| 133 |
|
| 134 |
$stats_totalplayers = 0; |
| 135 |
$stats_totalservers = 0; |
| 136 |
foreach $i (keys %data) { |
| 137 |
$stats_totalservers++; |
| 138 |
($ip, $time, @rest) = split /\|/, $data{$i}; |
| 139 |
if ($time+$REMOVE_SERVER<$cur_time) { |
| 140 |
delete $data{$i}; |
| 141 |
} else { |
| 142 |
print CACHE "$data{$i}\n"; |
| 143 |
$elapsed = int(($cur_time - $time)/60); |
| 144 |
$gmtime = gmtime($time); |
| 145 |
print HTML "<tr><td>$i</td><td>$gmtime</td><td>$elapsed</td>"; |
| 146 |
print HTML "<td>$rest[0]</td><td>$rest[1]</td><td>$rest[2]</td><td>$rest[3]</td></tr>\n"; |
| 147 |
$stats_totalplayers += int($rest[2]); |
| 148 |
} |
| 149 |
} |
| 150 |
$gmtime = gmtime($cur_time); |
| 151 |
print HTML " |
| 152 |
</table><p> |
| 153 |
The server name is reported by the server, while the ip address is determined by |
| 154 |
the incoming data packet. These values may not resolve to the same thing in the |
| 155 |
case of multi homed hosts or multi ip hosts.<p> |
| 156 |
|
| 157 |
All times are in GMT.<p> |
| 158 |
|
| 159 |
<font size=-2>Last Updated: $gmtime<font size=+2><p>"; |
| 160 |
close(HTML); |
| 161 |
close(CACHE); |
| 162 |
|
| 163 |
open(STATS,">$STATS_FILE"); |
| 164 |
print STATS "$stats_updatehits:$stats_requesthits:$stats_totalservers:$stats_totalplayers\n"; |
| 165 |
close(STATS); |
| 166 |
|
| 167 |
} |
| 168 |
} |