1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
package MasterWebInterface::Handler::Tools::AddNew;
use strict;
use warnings;
use Encode;
use Socket;
use IP::Country::Fast;
use TUWF ':html';
TUWF::register(
qr{new} => \&addnewserver,
qr{new/([\.\w]+):(\d+)/(\d+)} => \&valnewserver,
);
################################################################################
# Helper page to add server addresses to the masterserver manually
# Uses the valnewserver page/function to validate an active server.
################################################################################
sub addnewserver {
my $self = shift;
$self->htmlHeader(title => "Add a new server");
div class => "mainbox detail";
div class => "header";
h1 "Manually add a server";
p class => "alttitle", "333networks allows you to add supported servers manually. On this page is explained how to add your server to our masterserver.";
end;
p "You can add your server to our site in two ways:";
ol;
li;
txt "Follow the instructions on the ";
a href => "/masterserver", "MasterServer";
txt " page. This also allows other players to see your server online.";
end;
li "Follow the instructions below. This allows you to share links to your server page.";
end;
p;
txt "To link to your serverstatus, fill in your gameserver's IP and your gameserver's queryport, usually the game port +1. If your server does not show up, check for typos and verify that your firewall is not blocking your server. Your ip is ";
span class => "ext", $ENV{'REMOTE_ADDR'};
txt ".";
end;
table class => "shareopts new";
Tr;
td "IP address:";
td; input type => "text", class => "text", id => "ip", value => $ENV{'REMOTE_ADDR'}; end;
end;
Tr;
td "Query port (game port + 1):";
td; input type => "text", class => "text", id => "port", value => "7778"; end;
end;
Tr;
td "";
td; input type => "submit", class => "submit", value => "Search Server", onclick => "QueryLink()"; end;
end;
end;
p id => "newlink", class => "ext";
txt "Please enter the server's ip and port in the fields above.";
end;
end;
div id => "validate";
end;
$self->htmlFooter;
}
################################################################################
# Query and validate a manually added server.
# Arguments passed on via javascript (unsafe)
# Random number prevents some browsers from caching the request and also
# prevents the /gamename/ip:port regex from catching this function/page.
################################################################################
sub valnewserver {
my ($self, $s_addr, $s_port, $s_rand) = @_;
my ($ip,$port) = $self->valid_address($s_addr, $s_port);
# return "invalid" if no valid ip/port
if (!$ip || !$port){die "invalid";return;}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# DANGEROUS CODE: DO NOT EDIT UNLESS YOU KNOW WHAT YOU ARE DOING
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# Query the game server here on the spot. This may cause timeouts/errors with
# slow or unresponsive servers. Will generate an error in the browser if so.
#
# TODO: consider safer code for this part.
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# prepare target
my $qaddr = sockaddr_in($port, inet_aton($ip) );
my ($data, $buf) = ("","");
eval {
# return error stats on time-out
local $SIG{ALRM} = sub {die "timeout"};
alarm 2;
socket(SERVER, PF_INET, SOCK_DGRAM, getprotobyname("udp")) or die "timeout";
connect(SERVER, $qaddr);
send(SERVER, "\\status\\", 0, $qaddr);
#receive server info
while($data !~ /\\final\\/) {
recv(SERVER, $data, 0xFFFF, 0);
$buf .= $data;
}
shutdown(SERVER, 2);
alarm 0;
};
# turn buffer into hashref
my @a = split /\\/, encode('UTF-8', $buf || "");
shift @a;
my %h = (@a, (scalar @a % 2 == 1) ? "dummy" : () );
%h = map { lc $_ => $h{$_} } keys %h;
my $r = \%h;
# any text received?
# check for some random, supposedly existing tags like gamename, gamever
if ($r->{gamename} || $r->{gamever}) {
div class => "mainbox detail";
div class => "header";
h1 $r->{hostname} || "Unnamed Server";
end;
div class => "container";
div class => "thumbnail";
# find the correct thumbnail, otherwise standard 333 esrb pic
my $mapfig = "$self->{map_url}/default/333esrb.jpg";
# get prefix and mapname
my $mapname = lc $r->{mapname};
my ($pre,$post) = $mapname =~ /^(DM|CTF\-BT|BT|CTF|DOM|AS|JB|TO|SCR|MH)-(.*)/i;
my $prefix = ($pre ? uc $pre : "other");
# if map figure exists, use it
if (-e "$self->{map_dir}/$r->{gamename}/$prefix/$mapname.jpg") {
$mapfig = "$self->{map_url}/$r->{gamename}/$prefix/$mapname.jpg";
}
# if not, game default image
elsif (-e "$self->{map_dir}/default/$r->{gamename}.jpg") {
$mapfig = "$self->{map_url}/default/$r->{gamename}.jpg";
}
img src => $mapfig,
alt => $mapfig,
title => ($r->{mapname} || "Unknown");
span ($r->{maptitle} || "Unknown");
end;
table class => "mapinfo";
if ($r->{maxplayers}) {
Tr;
td class => "wc1", "Players:";
td;
txt $r->{numplayers} || 0;
txt "/";
txt $r->{maxplayers} || 0;
end;
end;
}
if ($r->{botskill} && $r->{minplayers}) {
Tr;
td "Bots:";
td;
txt $r->{minplayers} || 0;
txt " ";
txt ($r->{botskill} || "Standard");
txt " bot"; txt ($r->{minplayers} == 1 ? "" : "s");
end;
end;
}
end;
end; # container
#
# Specific server entry information
#
table class => "serverinfo";
Tr;
th class => "wc1", "Server Info";
th "";
end;
Tr;
td "Address:";
td title => $r->{port}, (($r->{ip} || $ip). ":". ($r->{hostport} || $port));
end;
if ($r->{adminname}) {
Tr;
td "Admin:";
td $r->{adminname};
end;
}
Tr;
td class => "wc1", "Contact:";
td;
if ($r->{adminemail}) {txt $r->{adminemail}}
end;
end;
Tr;
td class => "wc1", "Location:";
my $reg = IP::Country::Fast->new();
my ($flag, $country) = $self->countryflag($reg->inet_atocc($ip) || "");
td;
img class => "flag", src => "/flag/$flag.svg";
txt " ". $country;
end;
end;
end;
#
# Specific game and version information
#
table class => "gameinfo";
Tr;
th class => "wc1", "Game Info";
th "";
end;
Tr;
td "Game:";
td $self->dbGetGameDesc($r->{gamename}) || "unknown game";
end;
if ($r->{gametype}) {
Tr;
td "Type:";
td $r->{gametype};
end;
}
if ($r->{gamestyle}) {
Tr;
td "Style:";
td $r->{gamestyle};
end;
}
if ($r->{gamever}) {
Tr;
td "Version:";
td $r->{gamever};
end;
}
end;
#
# Mutator list
#
table class => "mutators";
Tr;
th "Mutators";
end;
Tr;
td;
if (defined $r->{mutators}) {
txt $r->{mutators};}
else {i "This server does not have any mutators listed.";}
end;
end;
end;
# add this server to the list of pending IPs if it does not exist in the db already
p class => "ext", ( $self->dbAddServer(ip => $ip, port => $port) ? "The server was added to the database." : "The server already exists in the database." );
end; # mainbox detail
}
else {
# else return "invalid" to make AJAX understand that the query failed.
txt "invalid";
}
}
1;
|