Move master server to seperate repository

experimental
ShadowNinja 2014-06-30 17:08:22 -04:00
parent bfaba2c12a
commit 567dea29ee
6 changed files with 0 additions and 564 deletions

View File

@ -1,49 +0,0 @@
Minetest server list
====================
Setting up the webpage
----------------------
You will have to install node.js, doT.js and their dependencies to compile
the serverlist webpage template.
First install node.js, eg:
# apt-get install nodejs
# pacman -S nodejs
# emerge nodejs
Then install doT.js and its dependencies:
$ cd ~/code
$ git clone https://github.com/olado/doT.git
$ cd doT
$ npm install
Or with npm:
$ npm install dot commander mkdirp
And finally compile the template:
$ cd ~/minetest/util/master
$ ~/code/doT/bin/dot-packer -s . -d .
or
$ ~/node_modules/dot/bin/dot-packer -s . -d .
Embedding to any page
----------------------
<script>
var master = {
root: 'http://servers.minetest.net/',
limit: 10,
clients_min: 1,
no_flags: 1,
no_ping: 1,
no_uptime: 1
};
</script>
<script src="http://servers.minetest.net/list.js"></script>

View File

@ -1,11 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Minetest server list</title>
</head>
<body>
<div id="server_list"></div>
</body>
</html>
<script src="list.js"></script>

View File

@ -1,100 +0,0 @@
var master;
if (!master) master = {};
if (typeof(master.root)=='undefined') master.root = "http://servers.minetest.net/";
if (!master.output) master.output = '#server_list';
if (!master.list) master.list = "list";
if (!master.list_root) master.list_root = master.root;
if (!master.list_url) master.list_url = master.list_root + master.list;
function humanTime(seconds) {
if (!seconds) return '?';
var conv = {
y: 31536000,
d: 86400,
h: 3600,
m: 60
}
for (var i in conv) {
if (seconds >= conv[i]) {
return (seconds / conv[i]).toFixed(1) + i;
}
}
return seconds + 's';
}
function escapeHTML(str) {
if(!str) return str;
return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}
function addressString(server) {
var isIPv6 = server.address.indexOf(":") != -1;
var addrStr = (isIPv6 ? '[' : '') +
escapeHTML(server.address) +
(isIPv6 ? ']' : '');
var shortStr = addrStr;
addrStr += ':' + server.port;
var str = '<span'
if (shortStr.length > 25) {
shortStr = shortStr.substr(0, 23) + "&hellip;";
str += ' class="mts_tooltip" title="' + addrStr + '"'
}
if (server.port != 30000)
shortStr += ':' + server.port;
return str + '>' + shortStr + '</span>';
}
function tooltipString(str, maxLen) {
str = escapeHTML(str);
var shortStr = str;
var ret = '<span';
if (shortStr.length > maxLen) {
shortStr = shortStr.substr(0, maxLen - 2) + "&hellip;";
ret += ' class="mts_tooltip" title="' + str + '"';
}
return ret + '>' + shortStr + '</span>';
}
function hoverList(name, list) {
if (!list || list.length == 0) return '';
var str = '<div class="mts_hover_list">'
str += name + '(' + list.length + ')<br />';
for (var i in list) {
str += escapeHTML(list[i]) + '<br />';
}
return str + '</div>';
}
function hoverString(name, string) {
if (!string) return '';
return '<div class="mts_hover_list">'
+ name + ':<br />'
+ escapeHTML(string) + '<br />'
+ '</div>';
}
function draw(json) {
html = window.render.servers(json);
jQuery(master.output).html(html);
}
function get() {
jQuery.getJSON(master.list_url, draw);
}
function loaded(){
if (!master.no_refresh) {
setInterval(get, 60 * 1000);
}
get();
}
// https://github.com/pyrsmk/toast
this.toast=function(){var e=document,t=e.getElementsByTagName("head")[0],n=this.setTimeout,r="createElement",i="appendChild",s="addEventListener",o="onreadystatechange",u="styleSheet",a=10,f=0,l=function(){--f},c,h=function(e,r,i,s){if(!t)n(function(){h(e)},a);else if(e.length){c=-1;while(i=e[++c]){if((s=typeof i)=="function"){r=function(){return i(),!0};break}if(s=="string")p(i);else if(i.pop){p(i[0]),r=i[1];break}}d(r,Array.prototype.slice.call(e,c+1))}},p=function(n,s){++f,/\.css$/.test(n)?(s=e[r]("link"),s.rel=u,s.href=n,t[i](s),v(s)):(s=e[r]("script"),s.src=n,t[i](s),s[o]===null?s[o]=m:s.onload=l)},d=function(e,t){if(!f)if(!e||e()){h(t);return}n(function(){d(e,t)},a)},v=function(e){if(e.sheet||e[u]){l();return}n(function(){v(e)},a)},m=function(){/ded|co/.test(this.readyState)&&l()};h(arguments)};
toast(master.root+'style.css', master.root+'servers.js', function() {
if (typeof(jQuery)!='undefined')
loaded();
else
toast('//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', loaded);
});

View File

@ -1,295 +0,0 @@
#!/usr/bin/perl
=info
install:
cpan JSON JSON::XS
touch list_full list log.log
chmod a+rw list_full list log.log
freebsd:
www/fcgiwrap www/nginx
rc.conf.local:
nginx_enable="YES"
fcgiwrap_enable="YES"
fcgiwrap_user="www"
nginx:
location / {
index index.html;
add_header Access-Control-Allow-Origin *;
}
location /announce {
fastcgi_pass unix:/var/run/fcgiwrap/fcgiwrap.sock;
fastcgi_param SCRIPT_FILENAME $document_root/master.cgi;
include fastcgi_params;
}
apache .htaccess:
AddHandler cgi-script .cgi
DirectoryIndex index.html
Options +ExecCGI +FollowSymLinks
Order allow,deny
<FilesMatch (\.(html?|cgi|fcgi|css|js|gif|png|jpe?g|ico)|(^)|\w+)$>
Allow from all
</FilesMatch>
Deny from all
<ifModule mod_headers.c>
Header set Access-Control-Allow-Origin: *
</ifModule>
=cut
use strict;
no strict qw(refs);
use warnings "NONFATAL" => "all";
no warnings qw(uninitialized);
no if $] >= 5.017011, warnings => 'experimental::smartmatch';
use utf8;
use Socket;
BEGIN {
if ($Socket::VERSION ge '2.008') {
eval qq{use Socket qw(getaddrinfo getnameinfo NI_NUMERICHOST NIx_NOSERV)}; # >5.16
} else {
eval qq{use Socket6 qw(getaddrinfo getnameinfo NI_NUMERICHOST NIx_NOSERV)}; # <5.16
}
};
use Time::HiRes qw(time sleep);
use IO::Socket::IP;
use JSON;
use Net::Ping;
#use Data::Dumper;
our $root_path;
($ENV{'SCRIPT_FILENAME'} || $0) =~ m|^(.+)[/\\].+?$|; #v0w
$root_path = $1 . '/' if $1;
$root_path =~ s|\\|/|g;
our %config = (
#debug => 1,
list_full => $root_path . 'list_full',
list_pub => $root_path . 'list',
log => $root_path . 'log.log',
time_purge => 86400 * 1,
time_alive => 650,
source_check => 1,
ping_timeout => 3,
ping => 1,
mineping => 1,
pingable => 1,
trusted => [qw( 176.9.122.10 )], #masterserver self ip - if server on same ip with masterserver doesnt announced
#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
);
do($root_path . 'config.pl');
our $ping = Net::Ping->new("udp", $config{ping_timeout});
$ping->hires();
sub get_params_one(@) {
local %_ = %{ref $_[0] eq 'HASH' ? shift : {}};
for (@_) {
tr/+/ /, s/%([a-f\d]{2})/pack 'H*', $1/gei for my ($k, $v) = /^([^=]+=?)=(.+)$/ ? ($1, $2) : (/^([^=]*)=?$/, /^-/);
$_{$k} = $v;
}
wantarray ? %_ : \%_;
}
sub get_params(;$$) { #v7
my ($string, $delim) = @_;
$delim ||= '&';
read(STDIN, local $_ = '', $ENV{'CONTENT_LENGTH'}) if !$string and $ENV{'CONTENT_LENGTH'};
local %_ =
$string
? get_params_one split $delim, $string
: (get_params_one(@ARGV), map { get_params_one split $delim, $_ } split(/;\s*/, $ENV{'HTTP_COOKIE'}), $ENV{'QUERY_STRING'}, $_);
wantarray ? %_ : \%_;
}
sub get_params_utf8(;$$) {
local $_ = &get_params;
utf8::decode $_ for %$_;
wantarray ? %$_ : $_;
}
sub file_rewrite(;$@) {
local $_ = shift;
return unless open my $fh, '>', $_;
print $fh @_;
}
sub printlog(;@) {
#local $_ = shift;
return unless open my $fh, '>>', $config{log};
print $fh (join ' ', @_), "\n";
}
sub file_read ($) {
open my $f, '<', $_[0] or return;
local $/ = undef;
my $ret = <$f>;
close $f;
return \$ret;
}
sub read_json {
my $ret = {};
eval { $ret = JSON->new->utf8->relaxed(1)->decode(${ref $_[0] ? $_[0] : file_read($_[0]) or \''} || '{}'); }; #'mc
printlog "json error [$@] on [", ${ref $_[0] ? $_[0] : \$_[0]}, "]" if $@;
$ret;
}
sub printu (@) {
for (@_) {
print($_), next unless utf8::is_utf8($_);
my $s = $_;
utf8::encode($s);
print($s);
}
}
sub float {
return ($_[0] < 8 and $_[0] - int($_[0]))
? sprintf('%.' . ($_[0] < 1 ? 3 : ($_[0] < 3 ? 2 : 1)) . 'f', $_[0])
: int($_[0]);
}
sub mineping ($$) {
my ($addr, $port) = @_;
printlog "mineping($addr, $port)" if $config{debug};
my $data;
my $time = time;
eval {
my $socket = IO::Socket::IP->new(
'PeerAddr' => $addr,
'PeerPort' => $port,
'Proto' => 'udp',
'Timeout' => $config{ping_timeout},
);
$socket->send("\x4f\x45\x74\x03\x00\x00\x00\x01");
local $SIG{ALRM} = sub { die "alarm time out"; };
alarm $config{ping_timeout};
$socket->recv($data, POSIX::BUFSIZ) or die "recv: $!";
alarm 0;
1; # return value from eval on normalcy
} or return 0;
return 0 unless length $data;
$time = float(time - $time);
printlog "recvd: ", length $data, " [$time]" if $config{debug};
return $time;
}
sub request (;$) {
my ($r) = @_;
$r ||= \%ENV;
my $param = get_params_utf8;
my $after = sub {
if ($param->{json}) {
my $j = {};
eval { $j = JSON->new->decode($param->{json}) || {} };
$param->{$_} = $j->{$_} for keys %$j;
delete $param->{json};
}
#printlog 'recv', Dumper $param;
if (%$param) {
s/^false$// for values %$param;
$param->{ip} = $r->{REMOTE_ADDR};
$param->{ip} =~ s/^::ffff://;
for (@{$config{blacklist}}) {
#printlog("blacklist", $param->{ip} ~~ $_) if $config{debug};
return if $param->{ip} ~~ $_;
}
$param->{address} ||= $param->{ip};
if ($config{source_check}) {
(my $err, local @_) = getaddrinfo($param->{address});
my $addrs = [ map{(getnameinfo($_->{addr}, NI_NUMERICHOST, NIx_NOSERV))[1]} @_];
if (!($param->{ip} ~~ $addrs) and !($param->{ip} ~~ $config{trusted})) {
printlog("bad address (", @$addrs, ")[$param->{address}] ne [$param->{ip}] [$err]") if $config{debug};
return;
}
}
$param->{port} ||= 30000;
$param->{key} = "$param->{ip}:$param->{port}";
$param->{off} = time if $param->{action} ~~ 'delete';
if ($config{ping} and $param->{action} ne 'delete') {
if ($config{mineping}) {
$param->{ping} = mineping($param->{ip}, $param->{port});
} else {
$ping->port_number($param->{port});
$ping->service_check(0);
my ($pingret, $duration, $ip) = $ping->ping($param->{address});
if ($ip ne $param->{ip} and !($param->{ip} ~~ $config{trusted})) {
printlog "strange ping ip [$ip] != [$param->{ip}]" if $config{debug};
return if $config{source_check} and !($param->{ip} ~~ $config{trusted});
}
$param->{ping} = $duration if $pingret;
printlog " PING t=$config{ping_timeout}, $param->{address}:$param->{port} = ( $pingret, $duration, $ip )" if $config{debug};
}
return if !$param->{ping};
}
my $list = read_json($config{list_full}) || {};
printlog "readed[$config{list_full}] list size=", scalar @{$list->{list}} if $config{debug};
my $listk = {map { $_->{key} => $_ } @{$list->{list}}};
my $old = $listk->{$param->{key}};
$param->{time} = $old->{time} if $param->{off};
$param->{time} ||= int time;
$param->{start} = $param->{action} ~~ 'start' ? $param->{time} : $old->{start} || $param->{time};
delete $param->{start} if $param->{off};
$param->{clients} ||= scalar @{$param->{clients_list}} if ref $param->{clients_list} eq 'ARRAY';
$param->{first} ||= $old->{first} || $old->{time} || $param->{time};
$param->{clients_top} = $old->{clients_top} if $old->{clients_top} > $param->{clients};
$param->{clients_top} ||= $param->{clients} || 0;
# params reported once on start, must be same as src/serverlist.cpp:~221 if(server["action"] == "start") { ...
for (qw(dedicated rollback liquid_finite mapgen mods privs)) {
$param->{$_} ||= $old->{$_} if $old->{$_} and !($param->{action} ~~ 'start');
}
$param->{pop_n} = $old->{pop_n} + 1;
$param->{pop_c} = $old->{pop_c} + $param->{clients};
$param->{pop_v} = $param->{pop_c} / $param->{pop_n};
delete $param->{action};
$listk->{$param->{key}} = $param;
#printlog 'write', Dumper $param if $config{debug};
my $list_full = [grep { $_->{time} > time - $config{time_purge} } values %$listk];
$list->{list} = [
sort { $b->{clients} <=> $a->{clients} || $a->{start} <=> $b->{start} }
grep { $_->{time} > time - $config{time_alive} and !$_->{off} and (!$config{ping} or !$config{pingable} or $_->{ping}) }
@{$list_full}
];
$list->{total} = {clients => 0, servers => 0};
for (@{$list->{list}}) {
$list->{total}{clients} += $_->{clients};
++$list->{total}{servers};
}
$list->{total_max}{clients} = $list->{total}{clients} if $list->{total_max}{clients} < $list->{total}{clients};
$list->{total_max}{servers} = $list->{total}{servers} if $list->{total_max}{servers} < $list->{total}{servers};
file_rewrite($config{list_pub}, JSON->new->encode($list));
printlog "writed[$config{list_pub}] list size=", scalar @{$list->{list}} if $config{debug};
$list->{list} = $list_full;
file_rewrite($config{list_full}, JSON->new->encode($list));
printlog "writed[$config{list_full}] list size=", scalar @{$list->{list}} if $config{debug};
}
};
return [200, ["Content-type", "application/json"], [JSON->new->encode({})]], $after;
}
sub request_cgi {
my ($p, $after) = request(@_);
shift @$p;
printu join "\n", map { join ': ', @$_ } shift @$p;
printu "\n\n";
printu join '', map { join '', @$_ } @$p;
if (fork) {
unless ($config{debug}) {
close STDOUT;
close STDERR;
}
} else {
$after->() if ref $after ~~ 'CODE';
}
}
request_cgi() unless caller;

View File

@ -1,74 +0,0 @@
{{? !master.no_total}}
<div class="total">
{{? it.total_max && it.total_max.clients > 0}}Players: {{=it.total.clients}}/{{=it.total_max.clients}}&nbsp;{{?}}
{{? it.total_max && it.total_max.servers > 0}}Servers: {{=it.total.servers}}/{{=it.total_max.servers}}{{?}}
</div>
{{?}}
<table>
<tr>
{{? !master.no_address}}<th>IP[:Port]</th>{{?}}
{{? !master.no_clients}}<th>Players/Max{{? !master.no_avgtop}}<br/>Avg/Top{{?}}</th>{{?}}
{{? !master.no_version}}<th>Version, Gameid, MapGen</th>{{?}}
{{? !master.no_name}}<th>Name</th>{{?}}
{{? !master.no_description}}<th>Description</th>{{?}}
{{? !master.no_flags}}<th>Flags</th>{{?}}
{{? !master.no_uptime}}<th>Uptime, Age</th>{{?}}
{{? !master.no_ping}}<th>Ping, Lag</th>{{?}}
</tr>
{{~it.list :server:index}}
{{ if (master.limit && index + 1 > master.limit) break;}}
{{ if (master.min_clients && server.clients < master.min_clients) continue;}}
<tr>
{{? !master.no_address}}
<td class ="address">
{{=addressString(server)}}
</td>{{?}}
{{? !master.no_clients}}
<td class="clients{{? server.clients_list && server.clients_list.length > 0}} mts_hover_list_text{{?}}">
{{=server.clients}}/{{=server.clients_max}}{{? !master.no_avgtop}} &nbsp;&nbsp;{{=Math.floor(server.pop_v)}}/{{=server.clients_top}}{{?}}
{{=hoverList("Clients", server.clients_list)}}
</td>{{?}}
{{? !master.no_version}}
<td class="version{{? server.mods && server.mods.length > 0}} mts_hover_list_text{{?}}">
{{=escapeHTML(server.version)}}, {{=escapeHTML(server.gameid)}},&nbsp;
{{=escapeHTML(server.mapgen || '?')}}
{{=hoverList("Mods", server.mods)}}
</td>{{?}}
{{? !master.no_name}}
<td class="name">
{{? server.url}}
<a href="{{=escapeHTML(server.url)}}">{{=tooltipString(server.name, 25)}}</a>
{{??}}
{{=tooltipString(server.name, 25)}}
{{?}}
</td>{{?}}
{{? !master.no_description}}
<td class="description">
{{=tooltipString(server.description, 50)}}
</td>{{?}}
{{? !master.no_flags}}
<td class="flags {{? server.privs}} mts_hover_list_text{{?}}">
{{=hoverString("Privs", server.privs)}}
{{=server.creative ? 'Cre ' : ''}}
{{=server.dedicated ? 'Ded ' : ''}}
{{=server.damage ? 'Dmg ' : ''}}
{{=server.liquid_finite ? 'Liq ' : ''}}
{{=server.pvp ? 'PvP ' : ''}}
{{=server.password ? 'Pwd ' : ''}}
{{=server.rollback ? 'Rol ' : ''}}
{{=server.can_see_far_names ? 'Far ' : ''}}
</td>{{?}}
{{? !master.no_uptime}}
<td class="uptime">
{{=humanTime(server.uptime)}}, {{=humanTime(server.game_time)}}
</td>{{?}}
{{? !master.no_ping}}
<td class="ping">
{{=Math.floor(server.ping * 1000)}}{{? server.lag}}, {{= Math.floor(server.lag * 1000)}}{{?}}
</td>{{?}}
</tr>
{{~}}
</table>
{{? master.min_clients || master.limit}}
<a class="clickable" onclick="delete master.min_clients; delete master.limit; get();">More...</a>
{{?}}

View File

@ -1,35 +0,0 @@
#server_list table {
max-width: 100%;
width: 100%;
background-color: transparent;
border-collapse: collapse;
border-spacing: 0;
font-size: small;
}
#server_list td, #server_list th {
border: 1px solid gray;
}
.mts_hover_list{
visibility: hidden;
border: gray solid 1px;
position: absolute;
z-index: 100;
background-color: white;
padding: 0.5em;
}
td:hover .mts_hover_list {
visibility: visible;
}
.mts_hover_list_text, .mts_tooltip {
text-decoration: underline;
text-decoration-style: dashed;
}
.clickable {
text-decoration: underline;
cursor: pointer;
}