LocalAuthority / scripts / host_manager.pl
Newer Older
1272 lines | 47.044kb
Xdev Host Manager authored 2 days ago
1
#!/usr/bin/env perl
2
#
3
# host_manager.pl - Minimal host registry web app with no CPAN dependencies.
4
#
5

            
6
use strict;
7
use warnings;
8

            
9
use Cwd qw(abs_path);
10
use Digest::SHA qw(hmac_sha1 hmac_sha256_hex sha256_hex);
11
use File::Basename qw(dirname);
12
use File::Path qw(make_path);
13
use IO::Socket::INET;
14
use POSIX qw(strftime);
15
use Time::HiRes qw(time);
16

            
17
my $script_dir = dirname(abs_path($0));
18
my $project_dir = dirname($script_dir);
19

            
20
my %opt = (
21
    bind => $ENV{HOST_MANAGER_BIND} || '127.0.0.1',
22
    port => $ENV{HOST_MANAGER_PORT} || 8088,
23
    data => $ENV{HOST_MANAGER_DATA} || "$project_dir/config/hosts.yaml",
24
    local_hosts_tsv => $ENV{HOST_MANAGER_LOCAL_HOSTS_TSV} || "$project_dir/config/local-hosts.tsv",
25
);
26

            
27
while (@ARGV) {
28
    my $arg = shift @ARGV;
29
    if ($arg eq '--bind') {
30
        $opt{bind} = shift @ARGV;
31
    } elsif ($arg eq '--port') {
32
        $opt{port} = shift @ARGV;
33
    } elsif ($arg eq '--data') {
34
        $opt{data} = shift @ARGV;
35
    } elsif ($arg eq '--local-hosts-tsv') {
36
        $opt{local_hosts_tsv} = shift @ARGV;
37
    } elsif ($arg eq '--help' || $arg eq '-h') {
38
        usage();
39
        exit 0;
40
    } else {
41
        die "Unknown option: $arg\n";
42
    }
43
}
44

            
45
my $session_secret = $ENV{HOST_MANAGER_SESSION_SECRET} || random_hex(32);
46
my %sessions;
47

            
48
my $server = IO::Socket::INET->new(
49
    LocalHost => $opt{bind},
50
    LocalPort => $opt{port},
51
    Proto => 'tcp',
52
    Listen => 10,
53
    ReuseAddr => 1,
54
) or die "Cannot listen on $opt{bind}:$opt{port}: $!\n";
55

            
56
print "host-manager listening on http://$opt{bind}:$opt{port}\n";
57
print "data file: $opt{data}\n";
58
print "OTP login: " . ($ENV{HOST_MANAGER_TOTP_SECRET} ? "enabled\n" : "disabled; set HOST_MANAGER_TOTP_SECRET\n");
59

            
60
while (my $client = $server->accept) {
61
    eval {
62
        $client->autoflush(1);
63
        handle_client($client);
64
    };
65
    if ($@) {
66
        eval { send_json($client, 500, { error => 'internal_error', detail => "$@" }); };
67
    }
68
    close $client;
69
}
70

            
71
sub usage {
72
    print <<"EOF";
73
Usage: perl scripts/host_manager.pl [--bind 127.0.0.1] [--port 8088]
74

            
75
Environment:
76
  HOST_MANAGER_TOTP_SECRET      Base32 TOTP secret required for write access.
77
  HOST_MANAGER_SESSION_SECRET   Optional session signing secret.
78
  HOST_MANAGER_DATA             Defaults to config/hosts.yaml.
79
  HOST_MANAGER_LOCAL_HOSTS_TSV  Defaults to config/local-hosts.tsv.
80

            
81
Read-only endpoints do not require authentication.
82
EOF
83
}
84

            
85
sub handle_client {
86
    my ($client) = @_;
87
    my $request_line = <$client>;
88
    return unless defined $request_line;
89
    $request_line =~ s/\r?\n$//;
90
    my ($method, $target) = $request_line =~ m{^([A-Z]+)\s+(\S+)\s+HTTP/};
91
    return send_text($client, 400, 'bad request') unless $method && $target;
92

            
93
    my %headers;
94
    while (my $line = <$client>) {
95
        $line =~ s/\r?\n$//;
96
        last if $line eq '';
97
        my ($k, $v) = split /:\s*/, $line, 2;
98
        $headers{lc $k} = $v if defined $k && defined $v;
99
    }
100

            
101
    my $body = '';
102
    if (($headers{'content-length'} || 0) > 0) {
103
        read($client, $body, int($headers{'content-length'}));
104
    }
105

            
106
    my ($path, $query) = split /\?/, $target, 2;
107
    my %query = parse_params($query || '');
108

            
109
    if ($method eq 'GET' && $path eq '/') {
110
        return send_html($client, 200, app_html());
111
    }
112
    if ($method eq 'GET' && $path eq '/healthz') {
Xdev Host Manager authored 2 days ago
113
        return send_json($client, 200, { ok => json_bool(1) });
Xdev Host Manager authored 2 days ago
114
    }
115
    if ($method eq 'GET' && $path eq '/api/session') {
116
        return send_json($client, 200, { authenticated => is_authenticated(\%headers) ? json_bool(1) : json_bool(0) });
117
    }
Xdev Host Manager authored 2 days ago
118
    if ($method eq 'POST' && $path eq '/api/login') {
119
        return send_json($client, 503, { error => 'otp_not_configured' }) unless $ENV{HOST_MANAGER_TOTP_SECRET};
120
        my $payload = request_payload(\%headers, $body);
121
        my $otp = $payload->{otp} || '';
122
        if (!verify_totp($ENV{HOST_MANAGER_TOTP_SECRET} || '', $otp)) {
123
            return send_json($client, 401, { error => 'invalid_otp' });
124
        }
125
        my $token = create_session();
126
        return send_json($client, 200, { ok => json_bool(1) }, [ "Set-Cookie: hm_session=$token; HttpOnly; SameSite=Strict; Path=/" ]);
127
    }
128
    if ($method eq 'POST' && $path eq '/api/logout') {
129
        expire_session(\%headers);
130
        return send_json($client, 200, { ok => json_bool(1) }, [ "Set-Cookie: hm_session=deleted; Max-Age=0; Path=/" ]);
131
    }
132

            
133
    return send_json($client, 401, { error => 'authentication_required' }) unless is_authenticated(\%headers);
134

            
Xdev Host Manager authored 2 days ago
135
    if ($method eq 'GET' && $path eq '/api/hosts') {
136
        my $registry = load_registry();
137
        return send_json($client, 200, registry_payload($registry));
138
    }
139
    if ($method eq 'GET' && $path eq '/download/hosts.yaml') {
140
        return send_file($client, $opt{data}, 'application/x-yaml; charset=utf-8', 'hosts.yaml');
141
    }
142
    if ($method eq 'GET' && $path eq '/download/local-hosts.tsv') {
143
        my $registry = load_registry();
144
        return send_download($client, 200, render_local_hosts_tsv($registry), 'text/tab-separated-values; charset=utf-8', 'local-hosts.tsv');
145
    }
146
    if ($method eq 'GET' && $path eq '/download/monitoring.json') {
147
        my $registry = load_registry();
148
        return send_download($client, 200, json_encode(render_monitoring($registry)), 'application/json; charset=utf-8', 'monitoring-hosts.json');
149
    }
Xdev Host Manager authored 2 days ago
150
    if ($method eq 'GET' && $path eq '/api/ca/status') {
151
        return send_json_raw($client, 200, ca_manager_json('status-json'));
152
    }
153
    if ($method eq 'GET' && $path eq '/api/ca/certificates') {
154
        return send_json_raw($client, 200, ca_manager_json('list-json'));
155
    }
156
    if ($method eq 'GET' && $path eq '/download/ca.crt') {
157
        return send_file($client, ca_cert_path(), 'application/x-pem-file; charset=utf-8', 'xdev-madagascar-host-ca.crt');
158
    }
Xdev Host Manager authored 2 days ago
159

            
160
    if ($method eq 'POST' && $path =~ m{^/api/}) {
161
        if ($path eq '/api/hosts/upsert') {
162
            my $payload = request_payload(\%headers, $body);
163
            return upsert_host($client, $payload);
164
        }
165
        if ($path eq '/api/hosts/delete') {
166
            my $payload = request_payload(\%headers, $body);
167
            return delete_host($client, $payload->{id} || '');
168
        }
169
        if ($path eq '/api/render/local-hosts-tsv') {
170
            my $registry = load_registry();
171
            my $content = render_local_hosts_tsv($registry);
172
            backup_file($opt{local_hosts_tsv});
173
            write_file($opt{local_hosts_tsv}, $content);
174
            return send_json($client, 200, { ok => json_bool(1), file => $opt{local_hosts_tsv} });
175
        }
176
    }
177

            
178
    return send_json($client, 404, { error => 'not_found' });
179
}
180

            
181
sub load_registry {
182
    return parse_hosts_yaml(read_file($opt{data}));
183
}
184

            
185
sub save_registry {
186
    my ($registry) = @_;
187
    $registry->{updated_at} = iso_now();
188
    backup_file($opt{data});
189
    write_file($opt{data}, render_hosts_yaml($registry));
190
}
191

            
192
sub registry_payload {
193
    my ($registry) = @_;
194
    my $problems = analyze_hosts($registry->{hosts});
195
    return {
196
        version => $registry->{version},
197
        updated_at => $registry->{updated_at},
198
        policy => $registry->{policy},
199
        hosts => $registry->{hosts},
200
        problems => $problems,
201
        counts => {
202
            hosts => scalar @{ $registry->{hosts} },
203
            problems => scalar @$problems,
204
        },
205
    };
206
}
207

            
208
sub upsert_host {
209
    my ($client, $payload) = @_;
210
    my $id = clean_id($payload->{id} || '');
211
    return send_json($client, 400, { error => 'invalid_id' }) unless $id;
212

            
213
    my $hosts_ip = clean_scalar($payload->{hosts_ip} || '');
214
    my $dns_ip = clean_scalar($payload->{dns_ip} || '');
215
    return send_json($client, 400, { error => 'missing_ip' }) unless $hosts_ip && $dns_ip;
216

            
217
    my @names = clean_list($payload->{names});
218
    return send_json($client, 400, { error => 'missing_names' }) unless @names;
219

            
220
    my $registry = load_registry();
221
    my %host = (
222
        id => $id,
223
        status => clean_scalar($payload->{status} || 'active'),
224
        hosts_ip => $hosts_ip,
225
        dns_ip => $dns_ip,
226
        names => \@names,
227
        roles => [ clean_list($payload->{roles}) ],
228
        sources => [ clean_list($payload->{sources}) ],
229
        monitoring => clean_scalar($payload->{monitoring} || 'pending'),
230
        notes => clean_scalar($payload->{notes} || ''),
231
    );
232

            
233
    my $replaced = 0;
234
    for my $i (0 .. $#{ $registry->{hosts} }) {
235
        if ($registry->{hosts}->[$i]{id} eq $id) {
236
            $registry->{hosts}->[$i] = \%host;
237
            $replaced = 1;
238
            last;
239
        }
240
    }
241
    push @{ $registry->{hosts} }, \%host unless $replaced;
242
    save_registry($registry);
243
    return send_json($client, 200, { ok => json_bool(1), host => \%host });
244
}
245

            
246
sub delete_host {
247
    my ($client, $id) = @_;
248
    $id = clean_id($id);
249
    return send_json($client, 400, { error => 'invalid_id' }) unless $id;
250

            
251
    my $registry = load_registry();
252
    my @kept = grep { $_->{id} ne $id } @{ $registry->{hosts} };
253
    return send_json($client, 404, { error => 'not_found' }) if @kept == @{ $registry->{hosts} };
254
    $registry->{hosts} = \@kept;
255
    save_registry($registry);
256
    return send_json($client, 200, { ok => json_bool(1) });
257
}
258

            
259
sub analyze_hosts {
260
    my ($hosts) = @_;
261
    my @problems;
262
    my (%names, %ids);
263
    for my $host (@$hosts) {
264
        push @problems, problem($host, 'duplicate-id', "Duplicate id $host->{id}") if $ids{ $host->{id} }++;
265
        my @fqdn = grep { /\.madagascar\.xdev\.ro$/ } @{ $host->{names} || [] };
266
        push @problems, problem($host, 'missing-fqdn', 'No madagascar.xdev.ro FQDN') unless @fqdn || ($host->{status} || '') ne 'active';
267
        push @problems, problem($host, 'deprecated-vad-is', 'Deprecated vad.is.xdev.ro name present')
268
            if grep { /\.vad\.is\.xdev\.ro$/ } @{ $host->{names} || [] };
269
        push @problems, problem($host, 'legacy-prefix', 'Legacy prefix should be normalized out')
270
            if grep { /^(is|vad|b)-/ } @{ $host->{names} || [] };
271
        for my $name (@{ $host->{names} || [] }) {
272
            push @problems, problem($host, 'duplicate-name', "Duplicate name $name") if $names{$name}++;
273
        }
274
        if (($host->{hosts_ip} || '') ne ($host->{dns_ip} || '') && ($host->{hosts_ip} || '') ne '127.0.0.1') {
275
            push @problems, problem($host, 'split-ip', 'hosts_ip differs from dns_ip; check that this is intentional');
276
        }
277
    }
278
    return \@problems;
279
}
280

            
281
sub problem {
282
    my ($host, $code, $message) = @_;
283
    return { host_id => $host->{id}, code => $code, message => $message };
284
}
285

            
286
sub render_local_hosts_tsv {
287
    my ($registry) = @_;
288
    my $out = "# Local DNS manifest for the madagascar network.\n";
289
    $out .= "# Generated by scripts/host_manager.pl from config/hosts.yaml.\n";
290
    $out .= "#\n";
291
    $out .= "# Format:\n";
292
    $out .= "# hosts_ip<TAB>dns_ip<TAB>name [aliases...]\n";
293
    $out .= "#\n";
294
    $out .= "# Priority rule:\n";
295
    $out .= "# - DHCP lease/reservation on 192.168.2.1 is canonical for LAN IP allocation.\n";
296
    $out .= "# - madagascar.json is canonical for cluster roles and service interfaces.\n";
297
    $out .= "# - This file publishes approved local DNS records derived from those sources.\n";
298
    for my $host (sort { $a->{id} cmp $b->{id} } @{ $registry->{hosts} }) {
299
        next unless ($host->{status} || 'active') eq 'active';
300
        next unless @{ $host->{names} || [] };
301
        $out .= join("\t", $host->{hosts_ip}, $host->{dns_ip}, join(' ', @{ $host->{names} })) . "\n";
302
    }
303
    return $out;
304
}
305

            
306
sub render_monitoring {
307
    my ($registry) = @_;
308
    my @hosts;
309
    for my $host (sort { $a->{id} cmp $b->{id} } @{ $registry->{hosts} }) {
310
        next unless ($host->{status} || 'active') eq 'active';
311
        next if ($host->{monitoring} || 'pending') eq 'disabled';
312
        push @hosts, {
313
            id => $host->{id},
314
            primary_name => $host->{names}[0],
315
            address => $host->{dns_ip},
316
            aliases => [ @{ $host->{names} || [] } ],
317
            roles => [ @{ $host->{roles} || [] } ],
318
            monitoring => $host->{monitoring} || 'pending',
319
            notes => $host->{notes} || '',
320
        };
321
    }
322
    return {
323
        version => $registry->{version},
324
        generated_at => iso_now(),
325
        source => 'config/hosts.yaml',
326
        hosts => \@hosts,
327
    };
328
}
329

            
Xdev Host Manager authored 2 days ago
330
sub ca_script_path {
331
    return "$project_dir/scripts/ca_manager.sh";
332
}
333

            
334
sub ca_dir {
335
    return $ENV{HOST_MANAGER_CA_DIR} || "$project_dir/var/ca";
336
}
337

            
338
sub ca_cert_path {
339
    return ca_dir() . "/certs/ca.cert.pem";
340
}
341

            
342
sub ca_manager_json {
343
    my ($command) = @_;
344
    my $script = ca_script_path();
345
    die "CA manager script is missing\n" unless -x $script;
346
    local $ENV{HOST_MANAGER_CA_DIR} = ca_dir();
347
    open my $fh, '-|', $script, $command or die "Cannot run CA manager\n";
348
    local $/;
349
    my $out = <$fh>;
350
    close $fh or die "CA manager failed\n";
351
    return $out || '{}';
352
}
353

            
Xdev Host Manager authored 2 days ago
354
sub parse_hosts_yaml {
355
    my ($text) = @_;
356
    my %registry = (
357
        version => 1,
358
        updated_at => '',
359
        policy => {},
360
        hosts => [],
361
    );
362
    my ($section, $current, $list_key);
363
    for my $line (split /\n/, $text) {
364
        next if $line =~ /^\s*$/ || $line =~ /^\s*#/;
365
        if ($line =~ /^version:\s*(\d+)/) {
366
            $registry{version} = int($1);
367
        } elsif ($line =~ /^updated_at:\s*(.+)$/) {
368
            $registry{updated_at} = yaml_unquote($1);
369
        } elsif ($line =~ /^policy:\s*$/) {
370
            $section = 'policy';
371
        } elsif ($line =~ /^hosts:\s*$/) {
372
            $section = 'hosts';
373
        } elsif (($section || '') eq 'policy' && $line =~ /^  ([A-Za-z0-9_]+):\s*(.+)$/) {
374
            $registry{policy}{$1} = yaml_unquote($2);
375
        } elsif (($section || '') eq 'hosts' && $line =~ /^  - id:\s*(.+)$/) {
376
            $current = {
377
                id => yaml_unquote($1),
378
                status => 'active',
379
                hosts_ip => '',
380
                dns_ip => '',
381
                names => [],
382
                roles => [],
383
                sources => [],
384
                monitoring => 'pending',
385
                notes => '',
386
            };
387
            push @{ $registry{hosts} }, $current;
388
            $list_key = undef;
389
        } elsif ($current && $line =~ /^    ([A-Za-z0-9_]+):\s*$/) {
390
            $list_key = $1;
391
            $current->{$list_key} ||= [];
392
        } elsif ($current && defined $list_key && $line =~ /^      -\s*(.+)$/) {
393
            push @{ $current->{$list_key} }, yaml_unquote($1);
394
        } elsif ($current && $line =~ /^    ([A-Za-z0-9_]+):\s*(.*)$/) {
395
            $current->{$1} = yaml_unquote($2);
396
            $list_key = undef;
397
        }
398
    }
399
    return \%registry;
400
}
401

            
402
sub render_hosts_yaml {
403
    my ($registry) = @_;
404
    my $out = "version: " . int($registry->{version} || 1) . "\n";
405
    $out .= "updated_at: " . yq($registry->{updated_at} || iso_now()) . "\n";
406
    $out .= "policy:\n";
407
    for my $key (sort keys %{ $registry->{policy} || {} }) {
408
        $out .= "  $key: " . yq($registry->{policy}{$key}) . "\n";
409
    }
410
    $out .= "hosts:\n";
411
    for my $host (sort { $a->{id} cmp $b->{id} } @{ $registry->{hosts} || [] }) {
412
        $out .= "  - id: " . yq($host->{id}) . "\n";
413
        for my $key (qw(status hosts_ip dns_ip)) {
414
            $out .= "    $key: " . yq($host->{$key} || '') . "\n";
415
        }
416
        for my $key (qw(names roles sources)) {
417
            $out .= "    $key:\n";
418
            for my $value (@{ $host->{$key} || [] }) {
419
                $out .= "      - " . yq($value) . "\n";
420
            }
421
        }
422
        $out .= "    monitoring: " . yq($host->{monitoring} || 'pending') . "\n";
423
        $out .= "    notes: " . yq($host->{notes} || '') . "\n";
424
    }
425
    return $out;
426
}
427

            
428
sub request_payload {
429
    my ($headers, $body) = @_;
430
    my $type = $headers->{'content-type'} || '';
431
    if ($type =~ m{application/json}) {
432
        return json_decode($body || '{}');
433
    }
434
    return { parse_params($body || '') };
435
}
436

            
437
sub json_bool {
438
    my ($value) = @_;
439
    return bless \(my $bool = $value ? 1 : 0), 'HostManager::JSONBool';
440
}
441

            
442
sub json_encode {
443
    my ($value) = @_;
444
    if (!defined $value) {
445
        return 'null';
446
    }
447
    my $ref = ref($value);
448
    if (!$ref) {
449
        return $value if $value =~ /\A-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?\z/;
450
        return json_string($value);
451
    }
452
    if ($ref eq 'HostManager::JSONBool') {
453
        return $$value ? 'true' : 'false';
454
    }
455
    if ($ref eq 'ARRAY') {
456
        return '[' . join(',', map { json_encode($_) } @$value) . ']';
457
    }
458
    if ($ref eq 'HASH') {
459
        return '{' . join(',', map { json_string($_) . ':' . json_encode($value->{$_}) } sort keys %$value) . '}';
460
    }
461
    return json_string("$value");
462
}
463

            
464
sub json_string {
465
    my ($value) = @_;
466
    $value = '' unless defined $value;
467
    $value =~ s/\\/\\\\/g;
468
    $value =~ s/"/\\"/g;
469
    $value =~ s/\n/\\n/g;
470
    $value =~ s/\r/\\r/g;
471
    $value =~ s/\t/\\t/g;
472
    $value =~ s/([\x00-\x1f])/sprintf("\\u%04x", ord($1))/eg;
473
    return qq("$value");
474
}
475

            
476
sub json_decode {
477
    my ($text) = @_;
478
    my $i = 0;
479
    my $len = length($text);
480
    my ($parse_value, $parse_string, $parse_array, $parse_object, $parse_number, $skip_ws);
481

            
482
    $skip_ws = sub {
483
        $i++ while $i < $len && substr($text, $i, 1) =~ /\s/;
484
    };
485

            
486
    $parse_string = sub {
487
        die "Expected JSON string\n" unless substr($text, $i, 1) eq '"';
488
        $i++;
489
        my $out = '';
490
        while ($i < $len) {
491
            my $ch = substr($text, $i++, 1);
492
            return $out if $ch eq '"';
493
            if ($ch eq "\\") {
494
                die "Bad JSON escape\n" if $i >= $len;
495
                my $esc = substr($text, $i++, 1);
496
                if ($esc eq '"' || $esc eq "\\" || $esc eq '/') {
497
                    $out .= $esc;
498
                } elsif ($esc eq 'b') {
499
                    $out .= "\b";
500
                } elsif ($esc eq 'f') {
501
                    $out .= "\f";
502
                } elsif ($esc eq 'n') {
503
                    $out .= "\n";
504
                } elsif ($esc eq 'r') {
505
                    $out .= "\r";
506
                } elsif ($esc eq 't') {
507
                    $out .= "\t";
508
                } elsif ($esc eq 'u') {
509
                    my $hex = substr($text, $i, 4);
510
                    die "Bad JSON unicode escape\n" unless $hex =~ /\A[0-9A-Fa-f]{4}\z/;
511
                    $out .= chr(hex($hex));
512
                    $i += 4;
513
                } else {
514
                    die "Bad JSON escape\n";
515
                }
516
            } else {
517
                $out .= $ch;
518
            }
519
        }
520
        die "Unterminated JSON string\n";
521
    };
522

            
523
    $parse_number = sub {
524
        my $start = $i;
525
        $i++ if substr($text, $i, 1) eq '-';
526
        $i++ while $i < $len && substr($text, $i, 1) =~ /[0-9]/;
527
        if ($i < $len && substr($text, $i, 1) eq '.') {
528
            $i++;
529
            $i++ while $i < $len && substr($text, $i, 1) =~ /[0-9]/;
530
        }
531
        if ($i < $len && substr($text, $i, 1) =~ /[eE]/) {
532
            $i++;
533
            $i++ if $i < $len && substr($text, $i, 1) =~ /[+-]/;
534
            $i++ while $i < $len && substr($text, $i, 1) =~ /[0-9]/;
535
        }
536
        return 0 + substr($text, $start, $i - $start);
537
    };
538

            
539
    $parse_array = sub {
540
        die "Expected JSON array\n" unless substr($text, $i, 1) eq '[';
541
        $i++;
542
        my @out;
543
        $skip_ws->();
544
        if ($i < $len && substr($text, $i, 1) eq ']') {
545
            $i++;
546
            return \@out;
547
        }
548
        while (1) {
549
            push @out, $parse_value->();
550
            $skip_ws->();
551
            my $ch = substr($text, $i++, 1);
552
            last if $ch eq ']';
553
            die "Expected JSON array comma\n" unless $ch eq ',';
554
        }
555
        return \@out;
556
    };
557

            
558
    $parse_object = sub {
559
        die "Expected JSON object\n" unless substr($text, $i, 1) eq '{';
560
        $i++;
561
        my %out;
562
        $skip_ws->();
563
        if ($i < $len && substr($text, $i, 1) eq '}') {
564
            $i++;
565
            return \%out;
566
        }
567
        while (1) {
568
            $skip_ws->();
569
            my $key = $parse_string->();
570
            $skip_ws->();
571
            die "Expected JSON object colon\n" unless substr($text, $i++, 1) eq ':';
572
            $out{$key} = $parse_value->();
573
            $skip_ws->();
574
            my $ch = substr($text, $i++, 1);
575
            last if $ch eq '}';
576
            die "Expected JSON object comma\n" unless $ch eq ',';
577
        }
578
        return \%out;
579
    };
580

            
581
    $parse_value = sub {
582
        $skip_ws->();
583
        die "Unexpected end of JSON\n" if $i >= $len;
584
        my $ch = substr($text, $i, 1);
585
        return $parse_string->() if $ch eq '"';
586
        return $parse_object->() if $ch eq '{';
587
        return $parse_array->() if $ch eq '[';
588
        if (substr($text, $i, 4) eq 'true') {
589
            $i += 4;
590
            return json_bool(1);
591
        }
592
        if (substr($text, $i, 5) eq 'false') {
593
            $i += 5;
594
            return json_bool(0);
595
        }
596
        if (substr($text, $i, 4) eq 'null') {
597
            $i += 4;
598
            return undef;
599
        }
600
        return $parse_number->() if $ch =~ /[-0-9]/;
601
        die "Unexpected JSON token\n";
602
    };
603

            
604
    my $value = $parse_value->();
605
    $skip_ws->();
606
    die "Trailing JSON content\n" if $i != $len;
607
    return $value;
608
}
609

            
610
sub parse_params {
611
    my ($text) = @_;
612
    my %out;
613
    for my $pair (split /&/, $text) {
614
        next unless length $pair;
615
        my ($k, $v) = split /=/, $pair, 2;
616
        $out{url_decode($k)} = url_decode($v || '');
617
    }
618
    return %out;
619
}
620

            
621
sub clean_id {
622
    my ($value) = @_;
623
    $value = lc clean_scalar($value);
624
    $value =~ s/[^a-z0-9_.-]+/-/g;
625
    $value =~ s/^-+|-+$//g;
626
    return $value;
627
}
628

            
629
sub clean_scalar {
630
    my ($value) = @_;
631
    $value = '' unless defined $value;
632
    $value =~ s/[\r\n\t]+/ /g;
633
    $value =~ s/^\s+|\s+$//g;
634
    return $value;
635
}
636

            
637
sub clean_list {
638
    my ($value) = @_;
639
    return () unless defined $value;
640
    my @items = ref($value) eq 'ARRAY' ? @$value : split /[\s,]+/, $value;
641
    my @clean;
642
    for my $item (@items) {
643
        $item = clean_scalar($item);
644
        push @clean, $item if length $item;
645
    }
646
    return @clean;
647
}
648

            
649
sub yq {
650
    my ($value) = @_;
651
    $value = '' unless defined $value;
652
    $value =~ s/\\/\\\\/g;
653
    $value =~ s/"/\\"/g;
654
    return qq("$value");
655
}
656

            
657
sub yaml_unquote {
658
    my ($value) = @_;
659
    $value = '' unless defined $value;
660
    $value =~ s/^\s+|\s+$//g;
661
    if ($value =~ /^"(.*)"$/) {
662
        $value = $1;
663
        $value =~ s/\\"/"/g;
664
        $value =~ s/\\\\/\\/g;
665
    }
666
    return $value;
667
}
668

            
669
sub verify_totp {
670
    my ($secret, $otp) = @_;
671
    return 0 unless $secret && $otp =~ /^\d{6}$/;
672
    my $key = eval { base32_decode($secret) };
673
    return 0 if $@ || !length $key;
674
    my $counter = int(time() / 30);
675
    for my $offset (-1, 0, 1) {
676
        return 1 if totp_code($key, $counter + $offset) eq $otp;
677
    }
678
    return 0;
679
}
680

            
681
sub totp_code {
682
    my ($key, $counter) = @_;
683
    my $msg = pack('NN', int($counter / 4294967296), $counter & 0xffffffff);
684
    my $hash = hmac_sha1($msg, $key);
685
    my $offset = ord(substr($hash, -1)) & 0x0f;
686
    my $bin = unpack('N', substr($hash, $offset, 4)) & 0x7fffffff;
687
    return sprintf('%06d', $bin % 1_000_000);
688
}
689

            
690
sub base32_decode {
691
    my ($text) = @_;
692
    $text = uc($text || '');
693
    $text =~ s/[^A-Z2-7]//g;
694
    my %map;
695
    my @chars = ('A'..'Z', '2'..'7');
696
    @map{@chars} = (0..31);
697
    my ($bits, $value, $out) = (0, 0, '');
698
    for my $char (split //, $text) {
699
        die "Invalid base32\n" unless exists $map{$char};
700
        $value = ($value << 5) | $map{$char};
701
        $bits += 5;
702
        while ($bits >= 8) {
703
            $bits -= 8;
704
            $out .= chr(($value >> $bits) & 0xff);
705
        }
706
    }
707
    return $out;
708
}
709

            
710
sub create_session {
711
    my $nonce = random_hex(24);
712
    my $expires = int(time() + 8 * 3600);
713
    my $sig = hmac_sha256_hex("$nonce:$expires", $session_secret);
714
    my $token = "$nonce:$expires:$sig";
715
    $sessions{$token} = $expires;
716
    return $token;
717
}
718

            
719
sub is_authenticated {
720
    my ($headers) = @_;
721
    my $token = cookie_value($headers->{'cookie'} || '', 'hm_session');
722
    return 0 unless $token;
723
    my ($nonce, $expires, $sig) = split /:/, $token;
724
    return 0 unless $nonce && $expires && $sig;
725
    return 0 if $expires < time();
726
    return 0 unless hmac_sha256_hex("$nonce:$expires", $session_secret) eq $sig;
727
    return exists $sessions{$token};
728
}
729

            
730
sub expire_session {
731
    my ($headers) = @_;
732
    my $token = cookie_value($headers->{'cookie'} || '', 'hm_session');
733
    delete $sessions{$token} if $token;
734
}
735

            
736
sub cookie_value {
737
    my ($cookie, $name) = @_;
738
    for my $part (split /;\s*/, $cookie) {
739
        my ($k, $v) = split /=/, $part, 2;
740
        return $v if defined $k && $k eq $name;
741
    }
742
    return '';
743
}
744

            
745
sub send_json {
746
    my ($client, $status, $payload, $extra_headers) = @_;
747
    return send_response($client, $status, json_encode($payload), 'application/json; charset=utf-8', $extra_headers);
748
}
749

            
Xdev Host Manager authored 2 days ago
750
sub send_json_raw {
751
    my ($client, $status, $json_body, $extra_headers) = @_;
752
    return send_response($client, $status, $json_body, 'application/json; charset=utf-8', $extra_headers);
753
}
754

            
Xdev Host Manager authored 2 days ago
755
sub send_html {
756
    my ($client, $status, $html) = @_;
757
    return send_response($client, $status, $html, 'text/html; charset=utf-8');
758
}
759

            
760
sub send_text {
761
    my ($client, $status, $text) = @_;
762
    return send_response($client, $status, $text, 'text/plain; charset=utf-8');
763
}
764

            
765
sub send_download {
766
    my ($client, $status, $content, $type, $filename) = @_;
767
    return send_response($client, $status, $content, $type, [ qq(Content-Disposition: attachment; filename="$filename") ]);
768
}
769

            
770
sub send_file {
771
    my ($client, $path, $type, $filename) = @_;
772
    return send_json($client, 404, { error => 'missing_file' }) unless -f $path;
773
    return send_download($client, 200, read_file($path), $type, $filename);
774
}
775

            
776
sub send_response {
777
    my ($client, $status, $body, $type, $extra_headers) = @_;
778
    my %reason = (200 => 'OK', 400 => 'Bad Request', 401 => 'Unauthorized', 404 => 'Not Found', 500 => 'Internal Server Error', 503 => 'Service Unavailable');
779
    $body = '' unless defined $body;
780
    print $client "HTTP/1.1 $status " . ($reason{$status} || 'OK') . "\r\n";
781
    print $client "Content-Type: $type\r\n";
782
    print $client "Content-Length: " . length($body) . "\r\n";
783
    print $client "Cache-Control: no-store\r\n";
784
    print $client "$_\r\n" for @{ $extra_headers || [] };
785
    print $client "Connection: close\r\n\r\n";
786
    print $client $body;
787
}
788

            
789
sub read_file {
790
    my ($path) = @_;
791
    open my $fh, '<', $path or die "Cannot read $path: $!";
792
    local $/;
793
    return <$fh>;
794
}
795

            
796
sub write_file {
797
    my ($path, $content) = @_;
798
    open my $fh, '>', $path or die "Cannot write $path: $!";
799
    print {$fh} $content;
800
    close $fh or die "Cannot close $path: $!";
801
}
802

            
803
sub backup_file {
804
    my ($path) = @_;
805
    return unless -f $path;
806
    my $backup_dir = "$project_dir/backups/host-manager";
807
    make_path($backup_dir) unless -d $backup_dir;
808
    my $name = $path;
809
    $name =~ s{.*/}{};
810
    my $stamp = strftime('%Y%m%d_%H%M%S', localtime);
811
    write_file("$backup_dir/$name.$stamp.bak", read_file($path));
812
}
813

            
814
sub url_decode {
815
    my ($value) = @_;
816
    $value = '' unless defined $value;
817
    $value =~ tr/+/ /;
818
    $value =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
819
    return $value;
820
}
821

            
822
sub random_hex {
823
    my ($bytes) = @_;
824
    if (open my $fh, '<:raw', '/dev/urandom') {
825
        read($fh, my $raw, $bytes);
826
        close $fh;
827
        return unpack('H*', $raw);
828
    }
829
    return sha256_hex(rand() . time() . $$);
830
}
831

            
832
sub iso_now {
833
    return strftime('%Y-%m-%dT%H:%M:%SZ', gmtime);
834
}
835

            
836
sub app_html {
837
    return <<'HTML';
838
<!doctype html>
839
<html lang="ro">
840
<head>
841
  <meta charset="utf-8">
842
  <meta name="viewport" content="width=device-width, initial-scale=1">
843
  <title>Host Manager</title>
844
  <style>
845
    :root {
846
      color-scheme: light;
847
      --ink: #152033;
848
      --muted: #647084;
849
      --line: #d8dee8;
850
      --soft: #f4f6f9;
851
      --panel: #ffffff;
852
      --accent: #1267d8;
853
      --bad: #b42318;
854
      --warn: #946200;
855
      --ok: #137333;
856
    }
857
    * { box-sizing: border-box; }
858
    body { margin: 0; font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; color: var(--ink); background: #eef2f6; font-size: 14px; }
Xdev Host Manager authored 2 days ago
859

            
860
    /* ── Login screen ── */
861
    #login-screen {
862
      display: flex;
863
      align-items: center;
864
      justify-content: center;
865
      min-height: 100dvh;
866
      padding: 24px;
867
      background: #13182a;
868
    }
869
    .login-card {
870
      background: #fff;
871
      border-radius: 16px;
872
      padding: 44px 36px 36px;
873
      width: 100%;
874
      max-width: 380px;
875
      display: grid;
876
      gap: 24px;
877
      box-shadow: 0 8px 40px rgba(0,0,0,.28);
878
    }
879
    .login-card .brand { text-align: center; display: grid; gap: 6px; }
880
    .login-card .brand .icon {
881
      margin: 0 auto 4px;
882
      width: 52px; height: 52px; border-radius: 14px;
883
      background: #e8f0fe; display: flex; align-items: center; justify-content: center;
884
    }
885
    .login-card .brand .icon svg { width: 26px; height: 26px; fill: var(--accent); }
886
    .login-card .brand h1 { margin: 0; font-size: 22px; font-weight: 700; color: var(--ink); }
887
    .login-card .brand p { margin: 0; color: var(--muted); font-size: 13px; }
888
    .login-card form { display: grid; gap: 16px; }
889
    .login-card .field-label { font-size: 13px; font-weight: 600; color: var(--ink); }
890
    /* 6 separate OTP digit boxes */
891
    .otp-row { display: flex; gap: 8px; justify-content: center; }
892
    .otp-row input {
893
      width: 48px; height: 56px; border: 1.5px solid #dde2ec; border-radius: 10px;
894
      font-size: 22px; font-weight: 600; text-align: center; color: var(--ink);
895
      background: #f8fafc; caret-color: transparent; outline: none;
896
      transition: border-color .15s, background .15s;
897
    }
898
    .otp-row input:focus { border-color: var(--accent); background: #fff; }
899
    .otp-row input.filled { border-color: #b3c6f0; background: #fff; }
900
    .login-card button.primary {
901
      width: 100%; border: none; background: var(--accent); color: #fff;
902
      border-radius: 10px; padding: 13px; font: inherit; font-size: 15px;
903
      font-weight: 600; cursor: pointer; min-height: 48px;
904
      display: flex; align-items: center; justify-content: center; gap: 8px;
905
    }
906
    .login-card button.primary:hover { background: #0f52b8; }
907
    .login-card button.primary:disabled { opacity: .55; cursor: not-allowed; }
908
    #login-error {
909
      color: var(--bad); font-size: 13px; text-align: center;
910
      min-height: 18px; margin-top: -8px;
911
    }
912

            
913
    /* ── App shell (hidden until authenticated) ── */
914
    #app { display: none; }
915
    header { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 12px 18px; background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 2; }
916
    h1 { margin: 0; font-size: 17px; font-weight: 700; }
917
    .header-right { display: flex; align-items: center; gap: 10px; }
Xdev Host Manager authored 2 days ago
918
    main { padding: 16px; display: grid; gap: 16px; max-width: 1280px; margin: 0 auto; }
919
    .toolbar, .panel { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; }
920
    .toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 10px; }
921
    .panel { overflow: hidden; }
922
    .panel-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--line); background: #fafbfc; }
923
    .panel-head h2 { margin: 0; font-size: 14px; }
924
    .stats { display: flex; gap: 8px; flex-wrap: wrap; }
925
    .stat { padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--soft); font-size: 12px; color: var(--muted); }
926
    button, input, select, textarea { font: inherit; }
927
    button, .linkbtn { border: 1px solid var(--line); background: #fff; color: var(--ink); border-radius: 6px; padding: 7px 10px; min-height: 34px; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; gap: 6px; }
928
    button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
929
    button.danger { color: var(--bad); }
Xdev Host Manager authored 2 days ago
930
    button:disabled, .linkbtn[aria-disabled="true"] { opacity: .55; cursor: not-allowed; pointer-events: none; }
Xdev Host Manager authored 2 days ago
931
    input, select, textarea { width: 100%; border: 1px solid var(--line); border-radius: 6px; padding: 8px; background: #fff; color: var(--ink); }
932
    textarea { min-height: 74px; resize: vertical; }
933
    table { width: 100%; border-collapse: collapse; table-layout: fixed; }
934
    th, td { padding: 9px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; overflow-wrap: anywhere; }
935
    th { color: var(--muted); font-size: 12px; font-weight: 700; background: #fafbfc; }
936
    tr:hover td { background: #f8fafc; }
937
    .pill { display: inline-block; padding: 2px 6px; border-radius: 999px; background: var(--soft); border: 1px solid var(--line); color: var(--muted); font-size: 12px; margin: 0 4px 4px 0; }
938
    .pill.ok { color: var(--ok); border-color: #b7dfc1; background: #edf8ef; }
939
    .pill.warn { color: var(--warn); border-color: #f1d184; background: #fff7df; }
940
    .pill.bad { color: var(--bad); border-color: #f0b8b3; background: #fff0ee; }
941
    .grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; padding: 14px; }
942
    .span2 { grid-column: 1 / -1; }
943
    label { display: grid; gap: 5px; color: var(--muted); font-size: 12px; font-weight: 650; }
944
    .muted { color: var(--muted); }
945
    .problems { padding: 10px 14px; display: grid; gap: 8px; }
946
    .problem { border-left: 3px solid var(--warn); padding: 7px 9px; background: #fffaf0; }
947
    @media (max-width: 760px) {
948
      .grid { grid-template-columns: 1fr; }
949
      table { min-width: 760px; }
950
      .table-wrap { overflow-x: auto; }
951
    }
952
  </style>
953
</head>
954
<body>
955

            
Xdev Host Manager authored 2 days ago
956
  <!-- ── Login screen ── -->
957
  <div id="login-screen">
958
    <div class="login-card">
959
      <div class="brand">
960
        <div class="icon">
961
          <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
962
            <path d="M20 3H4a1 1 0 0 0-1 1v5a1 1 0 0 0 1 1h16a1 1 0 0 0 1-1V4a1 1 0 0 0-1-1zm-1 5H5V5h14v3zm1 5H4a1 1 0 0 0-1 1v5a1 1 0 0 0 1 1h16a1 1 0 0 0 1-1v-5a1 1 0 0 0-1-1zm-1 5H5v-3h14v3z"/>
963
            <circle cx="17" cy="7" r="1"/><circle cx="19.5" cy="7" r="1"/>
964
            <circle cx="17" cy="15.5" r="1"/><circle cx="19.5" cy="15.5" r="1"/>
965
          </svg>
966
        </div>
967
        <h1>Host Manager</h1>
968
        <p>madagascar.xdev.ro</p>
Xdev Host Manager authored 2 days ago
969
      </div>
Xdev Host Manager authored 2 days ago
970
      <form id="login-form">
971
        <div class="field-label">Cod Authenticator</div>
972
        <div class="otp-row">
973
          <input type="text" inputmode="numeric" maxlength="1" pattern="[0-9]" class="otp-digit" autocomplete="one-time-code">
974
          <input type="text" inputmode="numeric" maxlength="1" pattern="[0-9]" class="otp-digit">
975
          <input type="text" inputmode="numeric" maxlength="1" pattern="[0-9]" class="otp-digit">
976
          <input type="text" inputmode="numeric" maxlength="1" pattern="[0-9]" class="otp-digit">
977
          <input type="text" inputmode="numeric" maxlength="1" pattern="[0-9]" class="otp-digit">
978
          <input type="text" inputmode="numeric" maxlength="1" pattern="[0-9]" class="otp-digit">
979
        </div>
980
        <button class="primary" type="submit" id="login-btn">Autentifică-te</button>
981
      </form>
982
      <div id="login-error"></div>
983
    </div>
984
  </div>
985

            
986
  <!-- ── App (shown after login) ── -->
987
  <div id="app">
988
    <header>
989
      <h1>Host Manager</h1>
990
      <div class="header-right">
991
        <span class="muted" id="app-updated"></span>
992
        <button type="button" id="logout">Logout</button>
Xdev Host Manager authored 2 days ago
993
      </div>
Xdev Host Manager authored 2 days ago
994
    </header>
995
    <main>
996
      <section class="toolbar">
997
        <button id="refresh">Refresh</button>
998
        <a class="linkbtn" href="/download/hosts.yaml">hosts.yaml</a>
999
        <a class="linkbtn" href="/download/local-hosts.tsv">local-hosts.tsv</a>
1000
        <a class="linkbtn" href="/download/monitoring.json">monitoring.json</a>
1001
        <button id="write-tsv">Write local-hosts.tsv</button>
1002
        <span id="message" class="muted"></span>
1003
      </section>
1004

            
1005
      <section class="panel">
1006
        <div class="panel-head">
1007
          <h2>Overview</h2>
1008
          <div class="stats" id="stats"></div>
1009
        </div>
1010
        <div class="problems" id="problems"></div>
1011
      </section>
Xdev Host Manager authored 2 days ago
1012

            
Xdev Host Manager authored 2 days ago
1013
      <section class="panel">
1014
        <div class="panel-head">
1015
          <h2>Certificate Authority</h2>
1016
          <a class="linkbtn" href="/download/ca.crt">ca.crt</a>
1017
        </div>
1018
        <div class="problems" id="ca-status"></div>
1019
      </section>
1020

            
Xdev Host Manager authored 2 days ago
1021
      <section class="panel">
1022
        <div class="panel-head">
1023
          <h2>Hosts</h2>
1024
          <input id="filter" placeholder="filter" style="max-width: 240px">
Xdev Host Manager authored 2 days ago
1025
        </div>
Xdev Host Manager authored 2 days ago
1026
        <div class="table-wrap">
1027
          <table>
1028
            <thead>
1029
              <tr>
1030
                <th style="width: 120px">ID</th>
1031
                <th style="width: 130px">hosts_ip</th>
1032
                <th style="width: 130px">dns_ip</th>
1033
                <th>Names</th>
1034
                <th style="width: 150px">Roles</th>
1035
                <th style="width: 110px">Monitoring</th>
1036
                <th style="width: 90px">Status</th>
1037
              </tr>
1038
            </thead>
1039
            <tbody id="hosts"></tbody>
1040
          </table>
1041
        </div>
1042
      </section>
1043

            
1044
      <section class="panel">
1045
        <div class="panel-head">
1046
          <h2>Edit host</h2>
1047
        </div>
1048
        <form id="host-form" class="grid">
1049
          <label>ID<input name="id" required></label>
1050
          <label>Status<select name="status"><option>active</option><option>planned</option><option>retired</option></select></label>
1051
          <label>hosts_ip<input name="hosts_ip" required></label>
1052
          <label>dns_ip<input name="dns_ip" required></label>
1053
          <label class="span2">Names<textarea name="names" required></textarea></label>
1054
          <label>Roles<input name="roles"></label>
1055
          <label>Sources<input name="sources"></label>
1056
          <label>Monitoring<select name="monitoring"><option>pending</option><option>enabled</option><option>disabled</option></select></label>
1057
          <label>Notes<input name="notes"></label>
1058
          <div class="span2">
1059
            <button class="primary" type="submit">Save host</button>
1060
            <button class="danger" type="button" id="delete-host">Delete host</button>
1061
          </div>
1062
        </form>
1063
      </section>
1064
    </main>
1065
  </div>
1066

            
Xdev Host Manager authored 2 days ago
1067
  <script>
1068
    let state = { hosts: [], problems: [], authenticated: false };
1069

            
1070
    const $ = (id) => document.getElementById(id);
1071
    const msg = (text) => { $('message').textContent = text || ''; };
1072

            
1073
    async function api(path, options = {}) {
1074
      const res = await fetch(path, options);
1075
      const body = await res.json();
1076
      if (!res.ok) throw new Error(body.error || res.statusText);
1077
      return body;
1078
    }
1079

            
Xdev Host Manager authored 2 days ago
1080
    function showLogin(errorText) {
1081
      $('app').style.display = 'none';
1082
      $('login-screen').style.display = 'flex';
1083
      $('login-error').textContent = errorText || '';
1084
      document.querySelectorAll('.otp-digit').forEach(i => { i.value = ''; i.classList.remove('filled'); });
1085
      const first = document.querySelector('.otp-digit');
1086
      if (first) first.focus();
1087
    }
1088

            
1089
    function showApp() {
1090
      $('login-screen').style.display = 'none';
1091
      $('app').style.display = 'block';
1092
    }
1093

            
Xdev Host Manager authored 2 days ago
1094
    async function refresh() {
1095
      const session = await api('/api/session');
1096
      state.authenticated = session.authenticated;
Xdev Host Manager authored 2 days ago
1097
      if (!state.authenticated) { showLogin(); return; }
1098
      showApp();
Xdev Host Manager authored 2 days ago
1099
      const data = await api('/api/hosts');
1100
      state.hosts = data.hosts || [];
1101
      state.problems = data.problems || [];
1102
      render(data);
Xdev Host Manager authored 2 days ago
1103
      await renderCa();
Xdev Host Manager authored 2 days ago
1104
    }
1105

            
1106
    function render(data) {
Xdev Host Manager authored 2 days ago
1107
      $('app-updated').textContent = data.updated_at ? 'updated ' + data.updated_at : '';
1108

            
Xdev Host Manager authored 2 days ago
1109
      $('stats').innerHTML = [
1110
        ['hosts', data.counts.hosts],
1111
        ['problems', data.counts.problems],
1112
      ].map(([k, v]) => `<span class="stat">${k}: ${escapeHtml(String(v))}</span>`).join('');
1113

            
1114
      $('problems').innerHTML = state.problems.length
1115
        ? state.problems.map(p => `<div class="problem"><strong>${escapeHtml(p.host_id)}</strong> ${escapeHtml(p.code)}: ${escapeHtml(p.message)}</div>`).join('')
1116
        : '<div class="muted" style="padding: 8px 0">No registry problems detected.</div>';
1117

            
1118
      renderHosts();
1119
    }
1120

            
Xdev Host Manager authored 2 days ago
1121
    async function renderCa() {
1122
      try {
1123
        const status = await api('/api/ca/status');
1124
        if (!status.initialized) {
1125
          $('ca-status').innerHTML = '<div class="problem"><strong>not initialized</strong> Run <code>sudo scripts/ca_manager.sh init</code> on jumper.</div>';
1126
          return;
1127
        }
1128
        const certs = await api('/api/ca/certificates');
1129
        $('ca-status').innerHTML = `
1130
          <div class="muted" style="display:grid;gap:6px">
1131
            <div><strong>${escapeHtml(status.subject || '')}</strong></div>
1132
            <div>SHA256 ${escapeHtml(status.fingerprint_sha256 || '')}</div>
1133
            <div>valid ${escapeHtml(status.not_before || '')} - ${escapeHtml(status.not_after || '')}</div>
1134
            <div>${certs.length} issued certificate(s)</div>
1135
          </div>`;
1136
      } catch (e) {
1137
        $('ca-status').innerHTML = `<div class="problem"><strong>CA status unavailable</strong> ${escapeHtml(e.message)}</div>`;
1138
      }
1139
    }
1140

            
Xdev Host Manager authored 2 days ago
1141
    function renderHosts() {
1142
      const filter = $('filter').value.toLowerCase();
1143
      $('hosts').innerHTML = state.hosts
1144
        .filter(h => JSON.stringify(h).toLowerCase().includes(filter))
1145
        .map(h => {
1146
          const problems = state.problems.filter(p => p.host_id === h.id);
1147
          const cls = problems.length ? 'warn' : 'ok';
1148
          return `<tr data-id="${escapeHtml(h.id)}">
1149
            <td><button type="button" data-edit="${escapeHtml(h.id)}">${escapeHtml(h.id)}</button></td>
1150
            <td>${escapeHtml(h.hosts_ip || '')}</td>
1151
            <td>${escapeHtml(h.dns_ip || '')}</td>
1152
            <td>${(h.names || []).map(n => `<span class="pill">${escapeHtml(n)}</span>`).join('')}</td>
1153
            <td>${(h.roles || []).map(n => `<span class="pill">${escapeHtml(n)}</span>`).join('')}</td>
1154
            <td><span class="pill ${cls}">${escapeHtml(h.monitoring || '')}</span></td>
1155
            <td>${escapeHtml(h.status || '')}</td>
1156
          </tr>`;
1157
        }).join('');
1158
      document.querySelectorAll('[data-edit]').forEach(button => button.addEventListener('click', () => editHost(button.dataset.edit)));
1159
    }
1160

            
1161
    function editHost(id) {
1162
      const host = state.hosts.find(h => h.id === id);
1163
      if (!host) return;
1164
      const form = $('host-form');
1165
      for (const key of ['id', 'status', 'hosts_ip', 'dns_ip', 'monitoring', 'notes']) form.elements[key].value = host[key] || '';
1166
      form.elements.names.value = (host.names || []).join('\n');
1167
      form.elements.roles.value = (host.roles || []).join(' ');
1168
      form.elements.sources.value = (host.sources || []).join(' ');
Xdev Host Manager authored 2 days ago
1169
      form.scrollIntoView({ behavior: 'smooth', block: 'start' });
Xdev Host Manager authored 2 days ago
1170
    }
1171

            
1172
    function formObject(form) {
1173
      return Object.fromEntries(new FormData(form).entries());
1174
    }
1175

            
1176
    function escapeHtml(value) {
1177
      return value.replace(/[&<>"']/g, ch => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#039;'}[ch]));
1178
    }
1179

            
Xdev Host Manager authored 2 days ago
1180
    // OTP digit boxes — auto-advance, backspace, paste
1181
    const otpDigits = Array.from(document.querySelectorAll('.otp-digit'));
1182
    otpDigits[0].focus();
1183

            
1184
    otpDigits.forEach((input, idx) => {
1185
      input.addEventListener('keydown', (e) => {
1186
        if (e.key === 'Backspace') {
1187
          if (input.value) { input.value = ''; input.classList.remove('filled'); }
1188
          else if (idx > 0) { otpDigits[idx - 1].value = ''; otpDigits[idx - 1].classList.remove('filled'); otpDigits[idx - 1].focus(); }
1189
          e.preventDefault();
1190
        }
1191
      });
1192
      input.addEventListener('input', (e) => {
1193
        const val = input.value.replace(/\D/g, '').slice(-1);
1194
        input.value = val;
1195
        input.classList.toggle('filled', !!val);
1196
        if (val && idx < otpDigits.length - 1) otpDigits[idx + 1].focus();
1197
        if (val && idx === otpDigits.length - 1) $('login-form').requestSubmit();
1198
      });
1199
      input.addEventListener('paste', (e) => {
1200
        const text = (e.clipboardData || window.clipboardData).getData('text').replace(/\D/g, '');
1201
        e.preventDefault();
1202
        text.split('').slice(0, otpDigits.length).forEach((ch, i) => {
1203
          otpDigits[i].value = ch;
1204
          otpDigits[i].classList.add('filled');
1205
        });
1206
        const next = Math.min(text.length, otpDigits.length - 1);
1207
        otpDigits[next].focus();
1208
        if (text.length >= otpDigits.length) $('login-form').requestSubmit();
1209
      });
1210
    });
1211

            
1212
    function getOtp() { return otpDigits.map(i => i.value).join(''); }
1213
    function clearOtp() { otpDigits.forEach(i => { i.value = ''; i.classList.remove('filled'); }); otpDigits[0].focus(); }
Xdev Host Manager authored 2 days ago
1214

            
1215
    $('login-form').addEventListener('submit', async (event) => {
1216
      event.preventDefault();
Xdev Host Manager authored 2 days ago
1217
      const btn = $('login-btn');
1218
      btn.disabled = true;
1219
      $('login-error').textContent = '';
Xdev Host Manager authored 2 days ago
1220
      try {
Xdev Host Manager authored 2 days ago
1221
        await api('/api/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ otp: getOtp() }) });
Xdev Host Manager authored 2 days ago
1222
        await refresh();
Xdev Host Manager authored 2 days ago
1223
      } catch (e) {
1224
        showLogin(e.message === 'invalid_otp' ? 'Cod incorect.' : e.message);
1225
      } finally {
1226
        btn.disabled = false;
1227
      }
Xdev Host Manager authored 2 days ago
1228
    });
1229

            
1230
    $('logout').addEventListener('click', async () => {
1231
      await api('/api/logout', { method: 'POST' }).catch(() => {});
Xdev Host Manager authored 2 days ago
1232
      clearOtp();
1233
      showLogin();
Xdev Host Manager authored 2 days ago
1234
    });
1235

            
Xdev Host Manager authored 2 days ago
1236
    $('refresh').addEventListener('click', () => refresh().catch(e => msg(e.message)));
1237
    $('filter').addEventListener('input', renderHosts);
1238

            
Xdev Host Manager authored 2 days ago
1239
    $('host-form').addEventListener('submit', async (event) => {
1240
      event.preventDefault();
1241
      try {
1242
        await api('/api/hosts/upsert', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(formObject(event.target)) });
1243
        msg('host saved');
1244
        await refresh();
1245
      } catch (e) { msg(e.message); }
1246
    });
1247

            
1248
    $('delete-host').addEventListener('click', async () => {
1249
      const id = $('host-form').elements.id.value;
1250
      if (!id || !confirm(`Delete ${id}?`)) return;
1251
      try {
1252
        await api('/api/hosts/delete', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id }) });
1253
        $('host-form').reset();
1254
        msg('host deleted');
1255
        await refresh();
1256
      } catch (e) { msg(e.message); }
1257
    });
1258

            
1259
    $('write-tsv').addEventListener('click', async () => {
1260
      if (!confirm('Write config/local-hosts.tsv from hosts.yaml?')) return;
1261
      try {
1262
        await api('/api/render/local-hosts-tsv', { method: 'POST' });
1263
        msg('local-hosts.tsv written');
1264
      } catch (e) { msg(e.message); }
1265
    });
1266

            
Xdev Host Manager authored 2 days ago
1267
    refresh().catch(() => showLogin());
Xdev Host Manager authored 2 days ago
1268
  </script>
1269
</body>
1270
</html>
1271
HTML
1272
}