6 touch list_full list log.log
7 chmod a+rw list_full list log.log
10 www/fcgiwrap www/nginx
21 add_header Access-Control-Allow-Origin *;
24 fastcgi_pass unix:/var/run/fcgiwrap/fcgiwrap.sock;
25 fastcgi_param SCRIPT_FILENAME $document_root/master.cgi;
26 include fastcgi_params;
31 AddHandler cgi-script .cgi
32 DirectoryIndex index.html
33 Options +ExecCGI +FollowSymLinks
35 <FilesMatch (\.(html?|cgi|fcgi|css|js|gif|png|jpe?g|ico)|(^)|\w+)$>
39 <ifModule mod_headers.c>
40 Header set Access-Control-Allow-Origin: *
49 use warnings "NONFATAL" => "all";
50 no warnings qw(uninitialized);
51 no if $] >= 5.017011, warnings => 'experimental::smartmatch';
55 if ($Socket::VERSION ge '2.008') {
56 eval qq{use Socket qw(getaddrinfo getnameinfo NI_NUMERICHOST NIx_NOSERV)}; # >5.16
58 eval qq{use Socket6 qw(getaddrinfo getnameinfo NI_NUMERICHOST NIx_NOSERV)}; # <5.16
61 use Time::HiRes qw(time sleep);
67 ($ENV{'SCRIPT_FILENAME'} || $0) =~ m|^(.+)[/\\].+?$|; #v0w
68 $root_path = $1 . '/' if $1;
69 $root_path =~ s|\\|/|g;
73 list_full => $root_path . 'list_full',
74 list_pub => $root_path . 'list',
75 log => $root_path . 'log.log',
76 time_purge => 86400 * 1,
83 trusted => [qw( 176.9.122.10 )], #masterserver self ip - if server on same ip with masterserver doesnt announced
84 #blacklist => [], # [qw(2.3.4.5 4.5.6.7 8.9.0.1), '1.2.3.4', qr/^10\.20\.30\./, ], # list, or quoted, ips, or regex
86 do($root_path . 'config.pl');
87 our $ping = Net::Ping->new("udp", $config{ping_timeout});
90 sub get_params_one(@) {
91 local %_ = %{ref $_[0] eq 'HASH' ? shift : {}};
93 tr/+/ /, s/%([a-f\d]{2})/pack 'H*', $1/gei for my ($k, $v) = /^([^=]+=?)=(.+)$/ ? ($1, $2) : (/^([^=]*)=?$/, /^-/);
99 sub get_params(;$$) { #v7
100 my ($string, $delim) = @_;
102 read(STDIN, local $_ = '', $ENV{'CONTENT_LENGTH'}) if !$string and $ENV{'CONTENT_LENGTH'};
105 ? get_params_one split $delim, $string
106 : (get_params_one(@ARGV), map { get_params_one split $delim, $_ } split(/;\s*/, $ENV{'HTTP_COOKIE'}), $ENV{'QUERY_STRING'}, $_);
107 wantarray ? %_ : \%_;
110 sub get_params_utf8(;$$) {
111 local $_ = &get_params;
112 utf8::decode $_ for %$_;
113 wantarray ? %$_ : $_;
116 sub file_rewrite(;$@) {
118 return unless open my $fh, '>', $_;
124 return unless open my $fh, '>>', $config{log};
125 print $fh (join ' ', @_), "\n";
129 open my $f, '<', $_[0] or return;
138 eval { $ret = JSON->new->utf8->relaxed(1)->decode(${ref $_[0] ? $_[0] : file_read($_[0]) or \''} || '{}'); }; #'mc
139 printlog "json error [$@] on [", ${ref $_[0] ? $_[0] : \$_[0]}, "]" if $@;
145 print($_), next unless utf8::is_utf8($_);
153 return ($_[0] < 8 and $_[0] - int($_[0]))
154 ? sprintf('%.' . ($_[0] < 1 ? 3 : ($_[0] < 3 ? 2 : 1)) . 'f', $_[0])
159 my ($addr, $port) = @_;
160 printlog "mineping($addr, $port)" if $config{debug};
164 my $socket = IO::Socket::IP->new(
168 'Timeout' => $config{ping_timeout},
170 $socket->send("\x4f\x45\x74\x03\x00\x00\x00\x01");
171 local $SIG{ALRM} = sub { die "alarm time out"; };
172 alarm $config{ping_timeout};
173 $socket->recv($data, POSIX::BUFSIZ) or die "recv: $!";
175 1; # return value from eval on normalcy
177 return 0 unless length $data;
178 $time = float(time - $time);
179 printlog "recvd: ", length $data, " [$time]" if $config{debug};
186 my $param = get_params_utf8;
188 if ($param->{json}) {
190 eval { $j = JSON->new->decode($param->{json}) || {} };
191 $param->{$_} = $j->{$_} for keys %$j;
192 delete $param->{json};
194 #printlog 'recv', Dumper $param;
196 s/^false$// for values %$param;
197 $param->{ip} = $r->{REMOTE_ADDR};
198 $param->{ip} =~ s/^::ffff://;
199 for (@{$config{blacklist}}) {
200 #printlog("blacklist", $param->{ip} ~~ $_) if $config{debug};
201 return if $param->{ip} ~~ $_;
203 $param->{address} ||= $param->{ip};
204 if ($config{source_check}) {
205 (my $err, local @_) = getaddrinfo($param->{address});
206 my $addrs = [ map{(getnameinfo($_->{addr}, NI_NUMERICHOST, NIx_NOSERV))[1]} @_];
207 if (!($param->{ip} ~~ $addrs) and !($param->{ip} ~~ $config{trusted})) {
208 printlog("bad address (", @$addrs, ")[$param->{address}] ne [$param->{ip}] [$err]") if $config{debug};
212 $param->{port} ||= 30000;
213 $param->{key} = "$param->{ip}:$param->{port}";
214 $param->{off} = time if $param->{action} ~~ 'delete';
215 if ($config{ping} and $param->{action} ne 'delete') {
216 if ($config{mineping}) {
217 $param->{ping} = mineping($param->{ip}, $param->{port});
219 $ping->port_number($param->{port});
220 $ping->service_check(0);
221 my ($pingret, $duration, $ip) = $ping->ping($param->{address});
222 if ($ip ne $param->{ip} and !($param->{ip} ~~ $config{trusted})) {
223 printlog "strange ping ip [$ip] != [$param->{ip}]" if $config{debug};
224 return if $config{source_check} and !($param->{ip} ~~ $config{trusted});
226 $param->{ping} = $duration if $pingret;
227 printlog " PING t=$config{ping_timeout}, $param->{address}:$param->{port} = ( $pingret, $duration, $ip )" if $config{debug};
229 return if !$param->{ping};
231 my $list = read_json($config{list_full}) || {};
232 printlog "readed[$config{list_full}] list size=", scalar @{$list->{list}};
233 my $listk = {map { $_->{key} => $_ } @{$list->{list}}};
234 my $old = $listk->{$param->{key}};
235 $param->{time} = $old->{time} if $param->{off};
236 $param->{time} ||= int time;
237 $param->{start} = $param->{action} ~~ 'start' ? $param->{time} : $old->{start} || $param->{time};
238 delete $param->{start} if $param->{off};
239 $param->{clients} ||= scalar @{$param->{clients_list}} if ref $param->{clients_list} eq 'ARRAY';
240 $param->{first} ||= $old->{first} || $old->{time} || $param->{time};
241 $param->{clients_top} = $old->{clients_top} if $old->{clients_top} > $param->{clients};
242 $param->{clients_top} ||= $param->{clients} || 0;
243 # params reported once on start, must be same as src/serverlist.cpp:~221 if(server["action"] == "start") { ...
244 for (qw(dedicated rollback liquid_finite mapgen mods)) {
245 $param->{$_} ||= $old->{$_} if $old->{$_} and !($param->{action} ~~ 'start');
247 $param->{pop_n} = $old->{pop_n} + 1;
248 $param->{pop_c} = $old->{pop_c} + $param->{clients};
249 $param->{pop_v} = $param->{pop_c} / $param->{pop_n};
250 delete $param->{action};
251 $listk->{$param->{key}} = $param;
252 #printlog 'write', Dumper $param if $config{debug};
253 $list->{list} = [grep { $_->{time} > time - $config{time_purge} } values %$listk];
254 file_rewrite($config{list_full}, JSON->new->encode($list));
255 printlog "writed[$config{list_full}] list size=", scalar @{$list->{list}} if $config{debug};
257 sort { $b->{clients} <=> $a->{clients} || $a->{start} <=> $b->{start} }
258 grep { $_->{time} > time - $config{time_alive} and !$_->{off} and (!$config{ping} or !$config{pingable} or $_->{ping}) }
261 $list->{total} = {clients => 0, servers => 0};
262 for (@{$list->{list}}) {
263 $list->{total}{clients} += $_->{clients};
264 ++$list->{total}{servers};
266 $list->{total_max}{clients} = $list->{total}{clients} if $list->{total_max}{clients} < $list->{total}{clients};
267 $list->{total_max}{servers} = $list->{total}{servers} if $list->{total_max}{servers} < $list->{total}{servers};
268 file_rewrite($config{list_pub}, JSON->new->encode($list));
269 printlog "writed[$config{list_pub}] list size=", scalar @{$list->{list}} if $config{debug};
272 return [200, ["Content-type", "application/json"], [JSON->new->encode({})]], $after;
276 my ($p, $after) = request(@_);
278 printu join "\n", map { join ': ', @$_ } shift @$p;
280 printu join '', map { join '', @$_ } @$p;
282 unless ($config{debug}) {
287 $after->() if ref $after ~~ 'CODE';
290 request_cgi() unless caller;