-
Notifications
You must be signed in to change notification settings - Fork 139
/
pool
executable file
·366 lines (308 loc) · 10.2 KB
/
pool
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# -*- cperl -*-
eval 'exec $CBROOT/bin/perl $0 ${1+"$@"}'
unless $running_under_some_shell;
use strict;
use Combust::DB;
use Combust::Config;
use Net::hostent;
use Net::NTP;
use Socket;
use Time::HiRes;
use Data::Dumper;
use Sys::Hostname qw(hostname);
use Net::IP;
use LWP::Simple qw(get);
use Email::Simple;
use Email::Date;
use File::Path qw(mkpath);
use JSON::XS ();
use Math::Random::Secure qw(irand);
my $config = Combust::Config->new;
my $config_ntp = $config->site->{ntppool};
use NP::Model;
my $pool_domain = $config_ntp->{pool_domain}
or die "pool_domain configuration not setup";
$Net::NTP::TIMEOUT = 2;
my $db;
# exit handler
sub closedbconnection {
$db->rollback();
$db->disconnect();
}
# tools
sub ntpdate_local {
my ($host, $error) = @_;
my %ntp = eval { get_ntp_response($host); };
warn Data::Dumper->Dump([\%ntp]);
my @error;
$ntp{Stratum} ||= '';
push @error, "Bad stratum: $ntp{Stratum}"
unless $ntp{Stratum}
and $ntp{Stratum} < 6;
if (@error) {
print @error, "\n";
my @query = `ntpdate -q $host 2>&1`
or die "Error in command execution: ntpdate\n";
print @query;
return undef;
}
return 1;
}
sub ntpdate {
my ($host, $error) = @_;
my $js = get("https://trace2.ntppool.org/ntp/$host");
my $json = JSON::XS->new->utf8;
my %ntp = %{$json->decode($js)};
#warn Data::Dumper->Dump([\%ntp]);
my @error;
$ntp{Stratum} ||= '';
push @error, "Bad stratum: $ntp{Stratum}"
unless $ntp{Stratum}
and $ntp{Stratum} < 6;
if (@error) {
print @error, "\n";
warn Data::Dumper->Dump([\%ntp]);
return undef;
}
return 1;
}
sub server_information {
my $server = shift;
my @zones = $server->zones_display;
my @r;
push @r, sprintf "IP: %s, ID: %i, username: %s email: %s\n",
$server->ip,
$server->id,
$server->admin->username ? $server->admin->username : "-",
$server->admin->email;
push @r, sprintf "score: %.1f, in pool: %i, in server list: %i\n",
$server->score, $server->in_pool, $server->in_server_list;
push @r, "Hostname: " . $server->hostname . "\n";
my $geoip = $ENV{geoip_service} || 'geoip';
my $country = get("http://${geoip}/api/country?ip=" . $server->ip);
push @r, "Country: $country, " if $country;
push @r, "Netspeed: " . $server->netspeed . "\n";
push @r, "Zones: ", join(", ", map { $_->name } @zones), "\n";
push @r, sprintf "in_pool: %i, in_server_list: %i\n", $server->in_pool, $server->in_server_list;
push @r, "Deletion on: ", $server->deletion_on->ymd, "\n"
if $server->deletion_on;
@r;
}
if ($#ARGV < 0) {
die "pool add|report|addmonitor|zone [<args>]\n";
}
# connect to the db
$db = Combust::DB::db_open('ntppool', {AutoCommit => 0})
or die "Couldn't connect to database\n";
sub find_user_by_email {
my $email = shift;
my $user = NP::Model->user->fetch(email => $email);
$user = NP::Model->user->fetch(username => $email) unless $user;
$user = NP::Model->user->fetch(id => $email) unless $user;
$user = NP::Model->user->fetch_or_create(email => $email) unless $user;
$user->save;
return $user;
}
my $cmd = shift @ARGV;
if ($cmd eq "add") {
if ($#ARGV < 1) {
die "pool add <email> <host or IP> [<zone ...>]\n";
}
# look if the email address is already registered
my $email = shift @ARGV;
my $user = find_user_by_email($email);
# do we have a hostname or an IP?
my $hostname = shift @ARGV;
my $ip;
if ($ip = Net::IP->new($hostname)) {
$ip = $ip->ip();
$hostname = undef;
}
else {
my $addrs = gethostbyname($hostname)
or die "$hostname does not resolve.\n";
if ($#{$addrs->addr_list} > 0) {
print "warning: $hostname has multiple IP addresses.\n";
}
$ip = inet_ntoa(${$addrs->addr_list}[0]);
}
# test the timeserver
ntpdate $ip, "Bad server " . ($hostname ? $hostname : "") . "[$ip]:"
or exit;
my $server = NP::Model->server->fetch(ip => $ip);
if ($server) {
die "server $ip already in database for ", $server->user->email, "\n";
}
$server = NP::Model->server->create(
ip => $ip,
hostname => $hostname,
user_id => $user->id,
in_pool => 1,
);
for my $country (@ARGV) {
$server->join_zone($country);
}
$server->save;
}
elsif ($cmd eq "addmonitor") {
if ($#ARGV < 2) {
die "pool addmonitor <email> <host or IP> <ip_version>\n";
}
my ($email, $hostname, $ip_version) = @ARGV;
die "ip_version must be v4 or v6" unless $ip_version =~ m/^v[46]$/;
my $db = NP::Model->db;
my $txn = $db->begin_scoped_work or die $db->error;
# look if the email address is already registered
my $user = find_user_by_email($email);
# do we have a hostname or an IP?
my $ip = Net::IP->new($hostname);
if ($ip) {
$ip = $ip->short;
$hostname = undef;
}
else {
my $addrs = gethostbyname($hostname)
or die "$hostname does not resolve.\n";
if ($#{$addrs->addr_list} > 0) {
print "warning: $hostname has multiple IP addresses.\n";
}
$ip = inet_ntoa(${$addrs->addr_list}[0]);
}
my $monitor = NP::Model->monitor->create(
name => "",
config => "",
created_on => DateTime->now,
ip => $ip,
user_id => $user->id,
ip_version => $ip_version,
);
require Math::BaseCalc;
my $base36 = Math::BaseCalc->new(digits => ['a' .. 'k', 'm' .. 'z', 2 .. 9]);
my @numbers = map { $base36->to_base(irand) } (undef) x 3;
my $api_key = join "", @numbers;
$monitor->api_key($api_key);
$monitor->save;
my $id = $monitor->id;
$db->dbh->do(
q[ insert into server_scores (monitor_id, server_id, score_raw, created_on)
select ?, id, score_raw, NOW() from servers
where ip_version = ? and deletion_on is null
], {}, $monitor->id, $monitor->ip_version
);
for my $f (qw(ip ip_version api_key)) {
printf "%-10s: %s\n", $f, $monitor->$f;
}
$db->commit or die $db->error;
}
elsif ($cmd eq "edit") {
my $arg = shift @ARGV or die "pool zones <host or IP>\n";
my $server = NP::Model->server->find_server($arg);
die "Could not find server $arg\n" unless $server;
print server_information($server);
my $changes = 0;
while (my $k = shift @ARGV) {
$changes++;
print "\n";
my $v = shift @ARGV;
last unless defined $v;
print "Set $k -> $v";
if ($server->can($k)) {
$server->$k($v);
}
else {
die "Don\'t know how to set $k\n";
}
}
if ($changes and $server->save(changes_only => 1) > 0) {
print "\n\nUpdated =============\n";
print server_information($server);
}
}
elsif ($cmd eq "zones") {
my $arg = shift @ARGV or die "pool zones <host or IP>\n";
my $server = NP::Model->server->find_server($arg);
die "Could not find server $arg\n" unless $server;
print "\n";
print server_information($server);
my @zones = $server->zones;
for my $zone_name (@ARGV) {
if ($zone_name =~ s/^-//) {
my ($zone) = grep { $_->name eq $zone_name } @zones;
print "Could not find location '$zone_name' to delete\n" and next
unless $zone;
$server->leave_zone($zone_name);
}
else {
$zone_name =~ s/^\+//;
next if grep { $zone_name eq $_->name } @zones;
my $zone = NP::Model->zone->fetch(name => $zone_name);
print "Could not find '$zone_name'\n" and next unless $zone;
$server->join_zone($zone);
}
}
$server->save;
@zones = $server->zones_display;
print "Zones: ", join(", ", map { $_->name } @zones), "\n";
}
elsif ($cmd eq "rm") {
my $arg = shift @ARGV
or die "pool rm <host or IP>\n";
my $server = NP::Model->server->find_server($arg);
die "Could not find server $arg\n" unless $server;
my $server_desc = (join " / ", grep { defined $_ } $server->ip, $server->hostname);
printf "%i, %s - %s\n", $server->id, $server_desc, $server->user->email;
my $date = DateTime->today;
$server->deletion_on($date);
$server->add_logs(
{ user_id => undef,
type => 'delete',
message => "Deletion scheduled for " . $date->ymd . " by pool admin"
}
);
$server->update;
}
elsif ($cmd eq "ip") {
my ($from, $to) = @ARGV;
$to or die "pool $cmd <host or IP> <IP>\n";
my $server = NP::Model->server->find_server($from);
die "Could not find server $from\n" unless $server;
print server_information($server);
my $server_desc = (join " / ", grep { defined $_ } $server->ip, $server->hostname);
printf "%i, %s (%s) => %s\n", $server->id, $server_desc, $server->admin->email, $to;
$server->add_logs(
{ user_id => undef,
type => 'ip_change',
message => "IP change from " . $server->ip . " to $to",
}
);
$server->ip($to);
$server->score_raw(0);
$server->save;
}
elsif ($cmd eq "zone") {
mkpath "$ENV{CBROOTLOCAL}/dns/", 0, 0711;
my $roots = NP::Model->dns_root->get_objects;
for my $root (@$roots) {
# TODO:
# Make NP::DNS::Root optionally limit how many IPs per zone
$root->populate;
if ($root->origin =~ m{\.{2}} or $root->origin =~ m/[^a-z\.-]/) {
warn "Invalid zone name: " . $root->origin . "\n";
next;
}
my $json = JSON::XS->new->pretty->utf8->convert_blessed;
my $js = $json->encode($root);
my $file = "$ENV{CBROOTLOCAL}/dns/" . $root->origin . ".json";
open(my $js_fh, ">$file.tmp") or die "Could not open $file.tmp: $!";
print $js_fh $js;
close $js_fh or die "could not close $file.json.tmp: $!";
rename "$file.tmp", "$file"
or die "could not rename $file.tmp to $file: $!";
}
}
else {
die "Unknown command: $cmd\n";
}
#$db->commit() or die "Error on commit\n";
$SIG{__DIE__} = 'DEFAULT';
$db->disconnect();