Masterserver mods announse, ipv6, better curl errors

stable-0.4
proller 2013-07-04 19:39:26 +04:00
parent fc6725f14e
commit ac7a44792c
10 changed files with 77 additions and 42 deletions

View File

@ -206,6 +206,8 @@
#server_url = http://minetest.net
# Automaticaly report to masterserver
#server_announce = 0
# Announce to this masterserver. if you want to announce your ipv6 address - use serverlist_url = v6.servers.minetest.net
#serverlist_url = servers.minetest.net
# Default game (default when creating a new world)
#default_game = minetest
# World directory (everything in the world is stored here)

View File

@ -250,7 +250,7 @@ void * MediaFetchThread::Thread()
m_file_data.push_back(make_pair(i->name, data));
} else {
m_failed.push_back(*i);
infostream << "cURL request failed for " << i->name << std::endl;
infostream << "cURL request failed for " << i->name << " (" << curl_easy_strerror(res) << ")"<< std::endl;
}
curl_easy_cleanup(curl);
}

View File

@ -59,7 +59,7 @@ Json::Value fetchJsonValue(const std::string url,
res = curl_easy_perform(curl);
if (res != CURLE_OK)
errorstream<<"Jsonreader: "<< url <<" not found (internet connection?)"<<std::endl;
errorstream<<"Jsonreader: "<< url <<" not found (" << curl_easy_strerror(res) << ")" <<std::endl;
curl_easy_cleanup(curl);
}

View File

@ -533,7 +533,7 @@ bool GUIEngine::downloadFile(std::string url,std::string target) {
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
errorstream << "File at url \"" << url
<<"\" not found (internet connection?)" <<std::endl;
<<"\" not found (" << curl_easy_strerror(res) << ")" <<std::endl;
retval = false;
}
fclose(targetfile);

View File

@ -1237,7 +1237,7 @@ void Server::AsyncRunStep()
float &counter = m_masterserver_timer;
if((!counter || counter >= 300.0) && g_settings->getBool("server_announce") == true)
{
ServerList::sendAnnounce(!counter ? "start" : "update", m_clients_number, m_uptime.get(), m_gamespec.id);
ServerList::sendAnnounce(!counter ? "start" : "update", m_clients_number, m_uptime.get(), m_gamespec.id, m_mods);
counter = 0.01;
}
counter += dtime;

View File

@ -194,34 +194,40 @@ std::string serializeJson(std::vector<ServerListSpec> serverlist)
#if USE_CURL
static size_t ServerAnnounceCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
return 0;
//((std::string*)userp)->append((char*)contents, size * nmemb);
//return size * nmemb;
return size * nmemb;
}
void sendAnnounce(std::string action, u16 clients, double uptime, std::string gameid) {
void sendAnnounce(std::string action, u16 clients, double uptime, std::string gameid, std::vector<ModSpec> m_mods) {
Json::Value server;
if (action.size())
server["action"] = action;
server["port"] = g_settings->get("port");
if (action != "del") {
server["address"] = g_settings->get("server_address");
if (action != "delete") {
server["name"] = g_settings->get("server_name");
server["description"] = g_settings->get("server_description");
server["address"] = g_settings->get("server_address");
server["version"] = VERSION_STRING;
server["url"] = g_settings->get("server_url");
server["creative"] = g_settings->get("creative_mode");
server["damage"] = g_settings->get("enable_damage");
server["dedicated"] = g_settings->get("server_dedicated");
server["rollback"] = g_settings->getBool("enable_rollback_recording");
server["password"] = g_settings->getBool("disallow_empty_password");
server["pvp"] = g_settings->getBool("enable_pvp");
server["clients"] = clients;
server["clients_max"] = g_settings->get("max_users");
if (uptime >=1) server["uptime"] = (int)uptime;
if (gameid!="") server["gameid"] = gameid;
}
if(server["action"] == "start")
if(server["action"] == "start") {
server["mods"] = Json::Value(Json::arrayValue);
for(std::vector<ModSpec>::iterator m = m_mods.begin(); m != m_mods.end(); m++) {
server["mods"].append(m->name);
}
actionstream << "announcing to " << g_settings->get("serverlist_url") << std::endl;
}
Json::StyledWriter writer;
CURL *curl;
curl = curl_easy_init();
@ -236,8 +242,8 @@ void sendAnnounce(std::string action, u16 clients, double uptime, std::string ga
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 1);
res = curl_easy_perform(curl);
//if (res != CURLE_OK)
// errorstream<<"Serverlist at url "<<g_settings->get("serverlist_url")<<" not found (internet connection?)"<<std::endl;
if (res != CURLE_OK)
errorstream<<"Serverlist at url "<<g_settings->get("serverlist_url")<<" error ("<<curl_easy_strerror(res)<<")"<<std::endl;
curl_easy_cleanup(curl);
}

View File

@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <iostream>
#include "config.h"
#include "mods.h"
#include "json/json.h"
#ifndef SERVERLIST_HEADER
@ -39,7 +40,7 @@ namespace ServerList
std::vector<ServerListSpec> deSerializeJson(std::string liststring);
std::string serializeJson(std::vector<ServerListSpec>);
#if USE_CURL
void sendAnnounce(std::string action = "", u16 clients = 0, double uptime = 0, std::string gameid = "");
void sendAnnounce(std::string action = "", u16 clients = 0, double uptime = 0, std::string gameid = "", std::vector<ModSpec> m_mods = std::vector<ModSpec>());
#endif
} //ServerList namespace

View File

@ -45,17 +45,28 @@ function success(r) {
for (var i = 0; i < r.list.length; ++i) {
var s = r.list[i];
if (!s) continue;
if (/:/.test(s.address)) s.address = '[' + s.address + ']';
h += '<tr class="mts_row">';
h += '<td class="mts_address">' + e(s.address) + (s.port != 30000 ? (':' + e(s.port)) : '') + '</td>';
h += '<td class="mts_clients">' + e(s.clients) + (s.clients_max ? '/' + e(s.clients_max) : '') + (s.clients_top ? ', ' + s.clients_top : '') + '</td>';
h += '<td class="mts_version">' + e(s.version) + ' ' + e(s.gameid) + '</td>';
h += '<td class="mts_version">' + e(s.version) + ' ' + e(s.gameid);
if (s.mods && jQuery.isArray(s.mods)) {
h += '<div class="mts_mods">Mods:<br/>';
for (m in s.mods) {
h += s.mods[m] + '<br/>';
}
h += '</div>';
}
h += '</td>';
h += '<td class="mts_url">';
if (s.url) h += '<a href="' + e(s.url) + '">';
h += e(s.name || s.url);
if (s.url) h += '</a>';
h += '</td>';
h += '<td class="mts_description">' + e(s.description) + '</td>';
h += '<td class="mts_flags">' + e(s.password ? 'Pwd ' : '') + (s.creative ? 'Cre ' : '') + (s.damage ? 'Dmg ' : '') + (s.pvp ? 'Pvp ' : '') + (s.dedicated ? 'Ded ' : '') + '</td>';
h += '<td class="mts_flags">' + e(s.password ? 'Pwd ' : '') + (s.creative ? 'Cre ' : '') + (s.damage ? 'Dmg ' : '') + (s.pvp ? 'Pvp ' : '') + (s.dedicated ? 'Ded ' : '') + (s.rollback ? 'Rol ' : '') + '</td>';
if (!s.start || s.start < 0) s.start = 0;
h += '<td class="mts_time">' + (s.uptime ? human_time(s.uptime, 1) : s.start ? human_time(s.start) : '') + '</td>';
h += '<td class="mts_ping">' + (s.ping ? parseFloat(s.ping).toFixed(3) * 1000 : '') + '</td>';

View File

@ -4,7 +4,7 @@
install:
cpan JSON JSON::XS
touch list_full list
chmod a+rw list_full list
chmod a+rw list_full list log.log
freebsd:
www/fcgiwrap www/nginx
@ -50,10 +50,18 @@ use warnings "NONFATAL" => "all";
no warnings qw(uninitialized);
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::INET;
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;
@ -63,6 +71,7 @@ our %config = (
#debug => 1,
list_full => $root_path . 'list_full',
list_pub => $root_path . 'list',
log => $root_path . 'log.log',
time_purge => 86400 * 30,
time_alive => 650,
source_check => 1,
@ -109,6 +118,12 @@ sub file_rewrite(;$@) {
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;
@ -120,7 +135,7 @@ sub file_read ($) {
sub read_json {
my $ret = {};
eval { $ret = JSON->new->utf8->relaxed(1)->decode(${ref $_[0] ? $_[0] : file_read($_[0]) or \''} || '{}'); }; #'mc
warn "json error [$@] on [", ${ref $_[0] ? $_[0] : \$_[0]}, "]" if $@;
printlog "json error [$@] on [", ${ref $_[0] ? $_[0] : \$_[0]}, "]" if $@;
$ret;
}
@ -133,16 +148,6 @@ sub printu (@) {
}
}
sub name_to_ip_noc($) {
my ($name) = @_;
unless ($name =~ /^\d+\.\d+\.\d+\.\d+$/) {
local $_ = (gethostbyname($name))[4];
return ($name, 1) unless length($_) == 4;
$name = inet_ntoa($_);
}
return $name;
}
sub float {
return ($_[0] < 8 and $_[0] - int($_[0]))
? sprintf('%.' . ($_[0] < 1 ? 3 : ($_[0] < 3 ? 2 : 1)) . 'f', $_[0])
@ -152,11 +157,11 @@ sub float {
sub mineping ($$) {
my ($addr, $port) = @_;
warn "mineping($addr, $port)" if $config{debug};
printlog "mineping($addr, $port)" if $config{debug};
my $data;
my $time = time;
eval {
my $socket = IO::Socket::INET->new(
my $socket = IO::Socket::IP->new(
'PeerAddr' => $addr,
'PeerPort' => $port,
'Proto' => 'udp',
@ -171,7 +176,7 @@ sub mineping ($$) {
} or return 0;
return 0 unless length $data;
$time = float(time - $time);
warn "recvd: ", length $data, " [$time]" if $config{debug};
printlog "recvd: ", length $data, " [$time]" if $config{debug};
return $time;
}
@ -189,18 +194,22 @@ sub request (;$) {
if (%$param) {
s/^false$// for values %$param;
$param->{ip} = $r->{REMOTE_ADDR};
$param->{ip} =~ s/^::ffff://;
for (@{$config{blacklist}}) {
return if $param->{ip} ~~ $_;
}
$param->{address} ||= $param->{ip};
if ($config{source_check} and name_to_ip_noc($param->{address}) ne $param->{ip} and !($param->{ip} ~~ $config{trusted})) {
warn("bad address [$param->{address}] ne [$param->{ip}]") if $config{debug};
return;
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});
@ -209,15 +218,15 @@ sub request (;$) {
$ping->service_check(0);
my ($pingret, $duration, $ip) = $ping->ping($param->{address});
if ($ip ne $param->{ip} and !($param->{ip} ~~ $config{trusted})) {
warn "strange ping ip [$ip] != [$param->{ip}]" if $config{debug};
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;
warn " PING t=$config{ping_timeout}, $param->{address}:$param->{port} = ( $pingret, $duration, $ip )" if $config{debug};
printlog " PING t=$config{ping_timeout}, $param->{address}:$param->{port} = ( $pingret, $duration, $ip )" if $config{debug};
}
}
my $list = read_json($config{list_full}) || {};
warn "readed[$config{list_full}] list size=", scalar @{$list->{list}};
printlog "readed[$config{list_full}] list size=", scalar @{$list->{list}};
my $listk = {map { $_->{key} => $_ } @{$list->{list}}};
my $old = $listk->{$param->{key}};
$param->{time} = $old->{time} if $param->{off};
@ -227,18 +236,20 @@ sub request (;$) {
$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;
$param->{mods} ||= $old->{mods};
delete $param->{action};
$listk->{$param->{key}} = $param;
#printlog Dumper $param;
$list->{list} = [grep { $_->{time} > time - $config{time_purge} } values %$listk];
file_rewrite($config{list_full}, JSON->new->encode($list));
warn "writed[$config{list_full}] list size=", scalar @{$list->{list}};
printlog "writed[$config{list_full}] list size=", scalar @{$list->{list}} if $config{debug};
$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->{list}}
];
file_rewrite($config{list_pub}, JSON->new->encode($list));
warn "writed[$config{list_pub}] list size=", scalar @{$list->{list}};
printlog "writed[$config{list_pub}] list size=", scalar @{$list->{list}} if $config{debug};
}
};
return [200, ["Content-type", "application/json"], [JSON->new->encode({})]], $after;

View File

@ -11,4 +11,8 @@ td, th {
div#table table {
width: 100%;
}
}
.mts_mods {visibility: hidden; border:gray solid 1px; position:absolute; z-index:100; background-color:white; padding:.5em;}
.mts_version:hover .mts_mods {visibility: visible;}
.mts_version {text-decoration:underline; text-decoration-style:dashed;}