LocalAuthority / scripts / host_manager.pl
Newer Older
1324 lines | 48.542kb
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});
Xdev Host Manager authored 2 days ago
195
    my @hosts = map { host_payload($_) } @{ $registry->{hosts} };
Xdev Host Manager authored 2 days ago
196
    return {
197
        version => $registry->{version},
198
        updated_at => $registry->{updated_at},
199
        policy => $registry->{policy},
Xdev Host Manager authored 2 days ago
200
        hosts => \@hosts,
Xdev Host Manager authored 2 days ago
201
        problems => $problems,
202
        counts => {
203
            hosts => scalar @{ $registry->{hosts} },
204
            problems => scalar @$problems,
205
        },
206
    };
207
}
208

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

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

            
Xdev Host Manager authored 2 days ago
218
    my @names = remove_derived_names(clean_list($payload->{names}));
Xdev Host Manager authored 2 days ago
219
    return send_json($client, 400, { error => 'missing_names' }) unless @names;
220

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

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

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

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

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

            
Xdev Host Manager authored 2 days ago
287
sub host_payload {
288
    my ($host) = @_;
289
    my %copy = %$host;
290
    $copy{names} = [ effective_names($host) ];
291
    $copy{declared_names} = [ @{ $host->{names} || [] } ];
292
    $copy{derived_names} = [ derived_names($host) ];
293
    return \%copy;
294
}
295

            
296
sub effective_names {
297
    my ($host) = @_;
298
    my @names = @{ $host->{names} || [] };
299
    push @names, derived_names($host);
300
    return unique_preserve(@names);
301
}
302

            
303
sub derived_names {
304
    my ($host) = @_;
305
    my @derived;
306
    for my $name (@{ $host->{names} || [] }) {
307
        next unless $name =~ /^(.+)\.madagascar\.xdev\.ro$/;
308
        push @derived, $1 if length $1;
309
    }
310
    return unique_preserve(@derived);
311
}
312

            
313
sub remove_derived_names {
314
    my @names = @_;
315
    my %derived;
316
    for my $name (@names) {
317
        next unless $name =~ /^(.+)\.madagascar\.xdev\.ro$/;
318
        $derived{$1} = 1;
319
    }
320
    return grep { !$derived{$_} } @names;
321
}
322

            
323
sub unique_preserve {
324
    my @values = @_;
325
    my %seen;
326
    return grep { !$seen{$_}++ } @values;
327
}
328

            
Xdev Host Manager authored 2 days ago
329
sub problem {
330
    my ($host, $code, $message) = @_;
331
    return { host_id => $host->{id}, code => $code, message => $message };
332
}
333

            
334
sub render_local_hosts_tsv {
335
    my ($registry) = @_;
336
    my $out = "# Local DNS manifest for the madagascar network.\n";
337
    $out .= "# Generated by scripts/host_manager.pl from config/hosts.yaml.\n";
338
    $out .= "#\n";
339
    $out .= "# Format:\n";
340
    $out .= "# hosts_ip<TAB>dns_ip<TAB>name [aliases...]\n";
341
    $out .= "#\n";
342
    $out .= "# Priority rule:\n";
343
    $out .= "# - DHCP lease/reservation on 192.168.2.1 is canonical for LAN IP allocation.\n";
344
    $out .= "# - madagascar.json is canonical for cluster roles and service interfaces.\n";
345
    $out .= "# - This file publishes approved local DNS records derived from those sources.\n";
346
    for my $host (sort { $a->{id} cmp $b->{id} } @{ $registry->{hosts} }) {
347
        next unless ($host->{status} || 'active') eq 'active';
Xdev Host Manager authored 2 days ago
348
        my @names = effective_names($host);
349
        next unless @names;
350
        $out .= join("\t", $host->{hosts_ip}, $host->{dns_ip}, join(' ', @names)) . "\n";
Xdev Host Manager authored 2 days ago
351
    }
352
    return $out;
353
}
354

            
355
sub render_monitoring {
356
    my ($registry) = @_;
357
    my @hosts;
358
    for my $host (sort { $a->{id} cmp $b->{id} } @{ $registry->{hosts} }) {
359
        next unless ($host->{status} || 'active') eq 'active';
360
        next if ($host->{monitoring} || 'pending') eq 'disabled';
Xdev Host Manager authored 2 days ago
361
        my @names = effective_names($host);
Xdev Host Manager authored 2 days ago
362
        push @hosts, {
363
            id => $host->{id},
Xdev Host Manager authored 2 days ago
364
            primary_name => $names[0],
Xdev Host Manager authored 2 days ago
365
            address => $host->{dns_ip},
Xdev Host Manager authored 2 days ago
366
            aliases => \@names,
367
            declared_names => [ @{ $host->{names} || [] } ],
368
            derived_names => [ derived_names($host) ],
Xdev Host Manager authored 2 days ago
369
            roles => [ @{ $host->{roles} || [] } ],
370
            monitoring => $host->{monitoring} || 'pending',
371
            notes => $host->{notes} || '',
372
        };
373
    }
374
    return {
375
        version => $registry->{version},
376
        generated_at => iso_now(),
377
        source => 'config/hosts.yaml',
378
        hosts => \@hosts,
379
    };
380
}
381

            
Xdev Host Manager authored 2 days ago
382
sub ca_script_path {
383
    return "$project_dir/scripts/ca_manager.sh";
384
}
385

            
386
sub ca_dir {
387
    return $ENV{HOST_MANAGER_CA_DIR} || "$project_dir/var/ca";
388
}
389

            
390
sub ca_cert_path {
391
    return ca_dir() . "/certs/ca.cert.pem";
392
}
393

            
394
sub ca_manager_json {
395
    my ($command) = @_;
396
    my $script = ca_script_path();
397
    die "CA manager script is missing\n" unless -x $script;
398
    local $ENV{HOST_MANAGER_CA_DIR} = ca_dir();
399
    open my $fh, '-|', $script, $command or die "Cannot run CA manager\n";
400
    local $/;
401
    my $out = <$fh>;
402
    close $fh or die "CA manager failed\n";
403
    return $out || '{}';
404
}
405

            
Xdev Host Manager authored 2 days ago
406
sub parse_hosts_yaml {
407
    my ($text) = @_;
408
    my %registry = (
409
        version => 1,
410
        updated_at => '',
411
        policy => {},
412
        hosts => [],
413
    );
414
    my ($section, $current, $list_key);
415
    for my $line (split /\n/, $text) {
416
        next if $line =~ /^\s*$/ || $line =~ /^\s*#/;
417
        if ($line =~ /^version:\s*(\d+)/) {
418
            $registry{version} = int($1);
419
        } elsif ($line =~ /^updated_at:\s*(.+)$/) {
420
            $registry{updated_at} = yaml_unquote($1);
421
        } elsif ($line =~ /^policy:\s*$/) {
422
            $section = 'policy';
423
        } elsif ($line =~ /^hosts:\s*$/) {
424
            $section = 'hosts';
425
        } elsif (($section || '') eq 'policy' && $line =~ /^  ([A-Za-z0-9_]+):\s*(.+)$/) {
426
            $registry{policy}{$1} = yaml_unquote($2);
427
        } elsif (($section || '') eq 'hosts' && $line =~ /^  - id:\s*(.+)$/) {
428
            $current = {
429
                id => yaml_unquote($1),
430
                status => 'active',
431
                hosts_ip => '',
432
                dns_ip => '',
433
                names => [],
434
                roles => [],
435
                sources => [],
436
                monitoring => 'pending',
437
                notes => '',
438
            };
439
            push @{ $registry{hosts} }, $current;
440
            $list_key = undef;
441
        } elsif ($current && $line =~ /^    ([A-Za-z0-9_]+):\s*$/) {
442
            $list_key = $1;
443
            $current->{$list_key} ||= [];
444
        } elsif ($current && defined $list_key && $line =~ /^      -\s*(.+)$/) {
445
            push @{ $current->{$list_key} }, yaml_unquote($1);
446
        } elsif ($current && $line =~ /^    ([A-Za-z0-9_]+):\s*(.*)$/) {
447
            $current->{$1} = yaml_unquote($2);
448
            $list_key = undef;
449
        }
450
    }
451
    return \%registry;
452
}
453

            
454
sub render_hosts_yaml {
455
    my ($registry) = @_;
456
    my $out = "version: " . int($registry->{version} || 1) . "\n";
457
    $out .= "updated_at: " . yq($registry->{updated_at} || iso_now()) . "\n";
458
    $out .= "policy:\n";
459
    for my $key (sort keys %{ $registry->{policy} || {} }) {
460
        $out .= "  $key: " . yq($registry->{policy}{$key}) . "\n";
461
    }
462
    $out .= "hosts:\n";
463
    for my $host (sort { $a->{id} cmp $b->{id} } @{ $registry->{hosts} || [] }) {
464
        $out .= "  - id: " . yq($host->{id}) . "\n";
465
        for my $key (qw(status hosts_ip dns_ip)) {
466
            $out .= "    $key: " . yq($host->{$key} || '') . "\n";
467
        }
468
        for my $key (qw(names roles sources)) {
469
            $out .= "    $key:\n";
470
            for my $value (@{ $host->{$key} || [] }) {
471
                $out .= "      - " . yq($value) . "\n";
472
            }
473
        }
474
        $out .= "    monitoring: " . yq($host->{monitoring} || 'pending') . "\n";
475
        $out .= "    notes: " . yq($host->{notes} || '') . "\n";
476
    }
477
    return $out;
478
}
479

            
480
sub request_payload {
481
    my ($headers, $body) = @_;
482
    my $type = $headers->{'content-type'} || '';
483
    if ($type =~ m{application/json}) {
484
        return json_decode($body || '{}');
485
    }
486
    return { parse_params($body || '') };
487
}
488

            
489
sub json_bool {
490
    my ($value) = @_;
491
    return bless \(my $bool = $value ? 1 : 0), 'HostManager::JSONBool';
492
}
493

            
494
sub json_encode {
495
    my ($value) = @_;
496
    if (!defined $value) {
497
        return 'null';
498
    }
499
    my $ref = ref($value);
500
    if (!$ref) {
501
        return $value if $value =~ /\A-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?\z/;
502
        return json_string($value);
503
    }
504
    if ($ref eq 'HostManager::JSONBool') {
505
        return $$value ? 'true' : 'false';
506
    }
507
    if ($ref eq 'ARRAY') {
508
        return '[' . join(',', map { json_encode($_) } @$value) . ']';
509
    }
510
    if ($ref eq 'HASH') {
511
        return '{' . join(',', map { json_string($_) . ':' . json_encode($value->{$_}) } sort keys %$value) . '}';
512
    }
513
    return json_string("$value");
514
}
515

            
516
sub json_string {
517
    my ($value) = @_;
518
    $value = '' unless defined $value;
519
    $value =~ s/\\/\\\\/g;
520
    $value =~ s/"/\\"/g;
521
    $value =~ s/\n/\\n/g;
522
    $value =~ s/\r/\\r/g;
523
    $value =~ s/\t/\\t/g;
524
    $value =~ s/([\x00-\x1f])/sprintf("\\u%04x", ord($1))/eg;
525
    return qq("$value");
526
}
527

            
528
sub json_decode {
529
    my ($text) = @_;
530
    my $i = 0;
531
    my $len = length($text);
532
    my ($parse_value, $parse_string, $parse_array, $parse_object, $parse_number, $skip_ws);
533

            
534
    $skip_ws = sub {
535
        $i++ while $i < $len && substr($text, $i, 1) =~ /\s/;
536
    };
537

            
538
    $parse_string = sub {
539
        die "Expected JSON string\n" unless substr($text, $i, 1) eq '"';
540
        $i++;
541
        my $out = '';
542
        while ($i < $len) {
543
            my $ch = substr($text, $i++, 1);
544
            return $out if $ch eq '"';
545
            if ($ch eq "\\") {
546
                die "Bad JSON escape\n" if $i >= $len;
547
                my $esc = substr($text, $i++, 1);
548
                if ($esc eq '"' || $esc eq "\\" || $esc eq '/') {
549
                    $out .= $esc;
550
                } elsif ($esc eq 'b') {
551
                    $out .= "\b";
552
                } elsif ($esc eq 'f') {
553
                    $out .= "\f";
554
                } elsif ($esc eq 'n') {
555
                    $out .= "\n";
556
                } elsif ($esc eq 'r') {
557
                    $out .= "\r";
558
                } elsif ($esc eq 't') {
559
                    $out .= "\t";
560
                } elsif ($esc eq 'u') {
561
                    my $hex = substr($text, $i, 4);
562
                    die "Bad JSON unicode escape\n" unless $hex =~ /\A[0-9A-Fa-f]{4}\z/;
563
                    $out .= chr(hex($hex));
564
                    $i += 4;
565
                } else {
566
                    die "Bad JSON escape\n";
567
                }
568
            } else {
569
                $out .= $ch;
570
            }
571
        }
572
        die "Unterminated JSON string\n";
573
    };
574

            
575
    $parse_number = sub {
576
        my $start = $i;
577
        $i++ if substr($text, $i, 1) eq '-';
578
        $i++ while $i < $len && substr($text, $i, 1) =~ /[0-9]/;
579
        if ($i < $len && substr($text, $i, 1) eq '.') {
580
            $i++;
581
            $i++ while $i < $len && substr($text, $i, 1) =~ /[0-9]/;
582
        }
583
        if ($i < $len && substr($text, $i, 1) =~ /[eE]/) {
584
            $i++;
585
            $i++ if $i < $len && substr($text, $i, 1) =~ /[+-]/;
586
            $i++ while $i < $len && substr($text, $i, 1) =~ /[0-9]/;
587
        }
588
        return 0 + substr($text, $start, $i - $start);
589
    };
590

            
591
    $parse_array = sub {
592
        die "Expected JSON array\n" unless substr($text, $i, 1) eq '[';
593
        $i++;
594
        my @out;
595
        $skip_ws->();
596
        if ($i < $len && substr($text, $i, 1) eq ']') {
597
            $i++;
598
            return \@out;
599
        }
600
        while (1) {
601
            push @out, $parse_value->();
602
            $skip_ws->();
603
            my $ch = substr($text, $i++, 1);
604
            last if $ch eq ']';
605
            die "Expected JSON array comma\n" unless $ch eq ',';
606
        }
607
        return \@out;
608
    };
609

            
610
    $parse_object = sub {
611
        die "Expected JSON object\n" unless substr($text, $i, 1) eq '{';
612
        $i++;
613
        my %out;
614
        $skip_ws->();
615
        if ($i < $len && substr($text, $i, 1) eq '}') {
616
            $i++;
617
            return \%out;
618
        }
619
        while (1) {
620
            $skip_ws->();
621
            my $key = $parse_string->();
622
            $skip_ws->();
623
            die "Expected JSON object colon\n" unless substr($text, $i++, 1) eq ':';
624
            $out{$key} = $parse_value->();
625
            $skip_ws->();
626
            my $ch = substr($text, $i++, 1);
627
            last if $ch eq '}';
628
            die "Expected JSON object comma\n" unless $ch eq ',';
629
        }
630
        return \%out;
631
    };
632

            
633
    $parse_value = sub {
634
        $skip_ws->();
635
        die "Unexpected end of JSON\n" if $i >= $len;
636
        my $ch = substr($text, $i, 1);
637
        return $parse_string->() if $ch eq '"';
638
        return $parse_object->() if $ch eq '{';
639
        return $parse_array->() if $ch eq '[';
640
        if (substr($text, $i, 4) eq 'true') {
641
            $i += 4;
642
            return json_bool(1);
643
        }
644
        if (substr($text, $i, 5) eq 'false') {
645
            $i += 5;
646
            return json_bool(0);
647
        }
648
        if (substr($text, $i, 4) eq 'null') {
649
            $i += 4;
650
            return undef;
651
        }
652
        return $parse_number->() if $ch =~ /[-0-9]/;
653
        die "Unexpected JSON token\n";
654
    };
655

            
656
    my $value = $parse_value->();
657
    $skip_ws->();
658
    die "Trailing JSON content\n" if $i != $len;
659
    return $value;
660
}
661

            
662
sub parse_params {
663
    my ($text) = @_;
664
    my %out;
665
    for my $pair (split /&/, $text) {
666
        next unless length $pair;
667
        my ($k, $v) = split /=/, $pair, 2;
668
        $out{url_decode($k)} = url_decode($v || '');
669
    }
670
    return %out;
671
}
672

            
673
sub clean_id {
674
    my ($value) = @_;
675
    $value = lc clean_scalar($value);
676
    $value =~ s/[^a-z0-9_.-]+/-/g;
677
    $value =~ s/^-+|-+$//g;
678
    return $value;
679
}
680

            
681
sub clean_scalar {
682
    my ($value) = @_;
683
    $value = '' unless defined $value;
684
    $value =~ s/[\r\n\t]+/ /g;
685
    $value =~ s/^\s+|\s+$//g;
686
    return $value;
687
}
688

            
689
sub clean_list {
690
    my ($value) = @_;
691
    return () unless defined $value;
692
    my @items = ref($value) eq 'ARRAY' ? @$value : split /[\s,]+/, $value;
693
    my @clean;
694
    for my $item (@items) {
695
        $item = clean_scalar($item);
696
        push @clean, $item if length $item;
697
    }
698
    return @clean;
699
}
700

            
701
sub yq {
702
    my ($value) = @_;
703
    $value = '' unless defined $value;
704
    $value =~ s/\\/\\\\/g;
705
    $value =~ s/"/\\"/g;
706
    return qq("$value");
707
}
708

            
709
sub yaml_unquote {
710
    my ($value) = @_;
711
    $value = '' unless defined $value;
712
    $value =~ s/^\s+|\s+$//g;
713
    if ($value =~ /^"(.*)"$/) {
714
        $value = $1;
715
        $value =~ s/\\"/"/g;
716
        $value =~ s/\\\\/\\/g;
717
    }
718
    return $value;
719
}
720

            
721
sub verify_totp {
722
    my ($secret, $otp) = @_;
723
    return 0 unless $secret && $otp =~ /^\d{6}$/;
724
    my $key = eval { base32_decode($secret) };
725
    return 0 if $@ || !length $key;
726
    my $counter = int(time() / 30);
727
    for my $offset (-1, 0, 1) {
728
        return 1 if totp_code($key, $counter + $offset) eq $otp;
729
    }
730
    return 0;
731
}
732

            
733
sub totp_code {
734
    my ($key, $counter) = @_;
735
    my $msg = pack('NN', int($counter / 4294967296), $counter & 0xffffffff);
736
    my $hash = hmac_sha1($msg, $key);
737
    my $offset = ord(substr($hash, -1)) & 0x0f;
738
    my $bin = unpack('N', substr($hash, $offset, 4)) & 0x7fffffff;
739
    return sprintf('%06d', $bin % 1_000_000);
740
}
741

            
742
sub base32_decode {
743
    my ($text) = @_;
744
    $text = uc($text || '');
745
    $text =~ s/[^A-Z2-7]//g;
746
    my %map;
747
    my @chars = ('A'..'Z', '2'..'7');
748
    @map{@chars} = (0..31);
749
    my ($bits, $value, $out) = (0, 0, '');
750
    for my $char (split //, $text) {
751
        die "Invalid base32\n" unless exists $map{$char};
752
        $value = ($value << 5) | $map{$char};
753
        $bits += 5;
754
        while ($bits >= 8) {
755
            $bits -= 8;
756
            $out .= chr(($value >> $bits) & 0xff);
757
        }
758
    }
759
    return $out;
760
}
761

            
762
sub create_session {
763
    my $nonce = random_hex(24);
764
    my $expires = int(time() + 8 * 3600);
765
    my $sig = hmac_sha256_hex("$nonce:$expires", $session_secret);
766
    my $token = "$nonce:$expires:$sig";
767
    $sessions{$token} = $expires;
768
    return $token;
769
}
770

            
771
sub is_authenticated {
772
    my ($headers) = @_;
773
    my $token = cookie_value($headers->{'cookie'} || '', 'hm_session');
774
    return 0 unless $token;
775
    my ($nonce, $expires, $sig) = split /:/, $token;
776
    return 0 unless $nonce && $expires && $sig;
777
    return 0 if $expires < time();
778
    return 0 unless hmac_sha256_hex("$nonce:$expires", $session_secret) eq $sig;
779
    return exists $sessions{$token};
780
}
781

            
782
sub expire_session {
783
    my ($headers) = @_;
784
    my $token = cookie_value($headers->{'cookie'} || '', 'hm_session');
785
    delete $sessions{$token} if $token;
786
}
787

            
788
sub cookie_value {
789
    my ($cookie, $name) = @_;
790
    for my $part (split /;\s*/, $cookie) {
791
        my ($k, $v) = split /=/, $part, 2;
792
        return $v if defined $k && $k eq $name;
793
    }
794
    return '';
795
}
796

            
797
sub send_json {
798
    my ($client, $status, $payload, $extra_headers) = @_;
799
    return send_response($client, $status, json_encode($payload), 'application/json; charset=utf-8', $extra_headers);
800
}
801

            
Xdev Host Manager authored 2 days ago
802
sub send_json_raw {
803
    my ($client, $status, $json_body, $extra_headers) = @_;
804
    return send_response($client, $status, $json_body, 'application/json; charset=utf-8', $extra_headers);
805
}
806

            
Xdev Host Manager authored 2 days ago
807
sub send_html {
808
    my ($client, $status, $html) = @_;
809
    return send_response($client, $status, $html, 'text/html; charset=utf-8');
810
}
811

            
812
sub send_text {
813
    my ($client, $status, $text) = @_;
814
    return send_response($client, $status, $text, 'text/plain; charset=utf-8');
815
}
816

            
817
sub send_download {
818
    my ($client, $status, $content, $type, $filename) = @_;
819
    return send_response($client, $status, $content, $type, [ qq(Content-Disposition: attachment; filename="$filename") ]);
820
}
821

            
822
sub send_file {
823
    my ($client, $path, $type, $filename) = @_;
824
    return send_json($client, 404, { error => 'missing_file' }) unless -f $path;
825
    return send_download($client, 200, read_file($path), $type, $filename);
826
}
827

            
828
sub send_response {
829
    my ($client, $status, $body, $type, $extra_headers) = @_;
830
    my %reason = (200 => 'OK', 400 => 'Bad Request', 401 => 'Unauthorized', 404 => 'Not Found', 500 => 'Internal Server Error', 503 => 'Service Unavailable');
831
    $body = '' unless defined $body;
832
    print $client "HTTP/1.1 $status " . ($reason{$status} || 'OK') . "\r\n";
833
    print $client "Content-Type: $type\r\n";
834
    print $client "Content-Length: " . length($body) . "\r\n";
835
    print $client "Cache-Control: no-store\r\n";
836
    print $client "$_\r\n" for @{ $extra_headers || [] };
837
    print $client "Connection: close\r\n\r\n";
838
    print $client $body;
839
}
840

            
841
sub read_file {
842
    my ($path) = @_;
843
    open my $fh, '<', $path or die "Cannot read $path: $!";
844
    local $/;
845
    return <$fh>;
846
}
847

            
848
sub write_file {
849
    my ($path, $content) = @_;
850
    open my $fh, '>', $path or die "Cannot write $path: $!";
851
    print {$fh} $content;
852
    close $fh or die "Cannot close $path: $!";
853
}
854

            
855
sub backup_file {
856
    my ($path) = @_;
857
    return unless -f $path;
858
    my $backup_dir = "$project_dir/backups/host-manager";
859
    make_path($backup_dir) unless -d $backup_dir;
860
    my $name = $path;
861
    $name =~ s{.*/}{};
862
    my $stamp = strftime('%Y%m%d_%H%M%S', localtime);
863
    write_file("$backup_dir/$name.$stamp.bak", read_file($path));
864
}
865

            
866
sub url_decode {
867
    my ($value) = @_;
868
    $value = '' unless defined $value;
869
    $value =~ tr/+/ /;
870
    $value =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
871
    return $value;
872
}
873

            
874
sub random_hex {
875
    my ($bytes) = @_;
876
    if (open my $fh, '<:raw', '/dev/urandom') {
877
        read($fh, my $raw, $bytes);
878
        close $fh;
879
        return unpack('H*', $raw);
880
    }
881
    return sha256_hex(rand() . time() . $$);
882
}
883

            
884
sub iso_now {
885
    return strftime('%Y-%m-%dT%H:%M:%SZ', gmtime);
886
}
887

            
888
sub app_html {
889
    return <<'HTML';
890
<!doctype html>
891
<html lang="ro">
892
<head>
893
  <meta charset="utf-8">
894
  <meta name="viewport" content="width=device-width, initial-scale=1">
895
  <title>Host Manager</title>
896
  <style>
897
    :root {
898
      color-scheme: light;
899
      --ink: #152033;
900
      --muted: #647084;
901
      --line: #d8dee8;
902
      --soft: #f4f6f9;
903
      --panel: #ffffff;
904
      --accent: #1267d8;
905
      --bad: #b42318;
906
      --warn: #946200;
907
      --ok: #137333;
908
    }
909
    * { box-sizing: border-box; }
910
    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
911

            
912
    /* ── Login screen ── */
913
    #login-screen {
914
      display: flex;
915
      align-items: center;
916
      justify-content: center;
917
      min-height: 100dvh;
918
      padding: 24px;
919
      background: #13182a;
920
    }
921
    .login-card {
922
      background: #fff;
923
      border-radius: 16px;
924
      padding: 44px 36px 36px;
925
      width: 100%;
926
      max-width: 380px;
927
      display: grid;
928
      gap: 24px;
929
      box-shadow: 0 8px 40px rgba(0,0,0,.28);
930
    }
931
    .login-card .brand { text-align: center; display: grid; gap: 6px; }
932
    .login-card .brand .icon {
933
      margin: 0 auto 4px;
934
      width: 52px; height: 52px; border-radius: 14px;
935
      background: #e8f0fe; display: flex; align-items: center; justify-content: center;
936
    }
937
    .login-card .brand .icon svg { width: 26px; height: 26px; fill: var(--accent); }
938
    .login-card .brand h1 { margin: 0; font-size: 22px; font-weight: 700; color: var(--ink); }
939
    .login-card .brand p { margin: 0; color: var(--muted); font-size: 13px; }
940
    .login-card form { display: grid; gap: 16px; }
941
    .login-card .field-label { font-size: 13px; font-weight: 600; color: var(--ink); }
942
    /* 6 separate OTP digit boxes */
943
    .otp-row { display: flex; gap: 8px; justify-content: center; }
944
    .otp-row input {
945
      width: 48px; height: 56px; border: 1.5px solid #dde2ec; border-radius: 10px;
946
      font-size: 22px; font-weight: 600; text-align: center; color: var(--ink);
947
      background: #f8fafc; caret-color: transparent; outline: none;
948
      transition: border-color .15s, background .15s;
949
    }
950
    .otp-row input:focus { border-color: var(--accent); background: #fff; }
951
    .otp-row input.filled { border-color: #b3c6f0; background: #fff; }
952
    .login-card button.primary {
953
      width: 100%; border: none; background: var(--accent); color: #fff;
954
      border-radius: 10px; padding: 13px; font: inherit; font-size: 15px;
955
      font-weight: 600; cursor: pointer; min-height: 48px;
956
      display: flex; align-items: center; justify-content: center; gap: 8px;
957
    }
958
    .login-card button.primary:hover { background: #0f52b8; }
959
    .login-card button.primary:disabled { opacity: .55; cursor: not-allowed; }
960
    #login-error {
961
      color: var(--bad); font-size: 13px; text-align: center;
962
      min-height: 18px; margin-top: -8px;
963
    }
964

            
965
    /* ── App shell (hidden until authenticated) ── */
966
    #app { display: none; }
967
    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; }
968
    h1 { margin: 0; font-size: 17px; font-weight: 700; }
969
    .header-right { display: flex; align-items: center; gap: 10px; }
Xdev Host Manager authored 2 days ago
970
    main { padding: 16px; display: grid; gap: 16px; max-width: 1280px; margin: 0 auto; }
971
    .toolbar, .panel { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; }
972
    .toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 10px; }
973
    .panel { overflow: hidden; }
974
    .panel-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--line); background: #fafbfc; }
975
    .panel-head h2 { margin: 0; font-size: 14px; }
976
    .stats { display: flex; gap: 8px; flex-wrap: wrap; }
977
    .stat { padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--soft); font-size: 12px; color: var(--muted); }
978
    button, input, select, textarea { font: inherit; }
979
    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; }
980
    button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
981
    button.danger { color: var(--bad); }
Xdev Host Manager authored 2 days ago
982
    button:disabled, .linkbtn[aria-disabled="true"] { opacity: .55; cursor: not-allowed; pointer-events: none; }
Xdev Host Manager authored 2 days ago
983
    input, select, textarea { width: 100%; border: 1px solid var(--line); border-radius: 6px; padding: 8px; background: #fff; color: var(--ink); }
984
    textarea { min-height: 74px; resize: vertical; }
985
    table { width: 100%; border-collapse: collapse; table-layout: fixed; }
986
    th, td { padding: 9px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; overflow-wrap: anywhere; }
987
    th { color: var(--muted); font-size: 12px; font-weight: 700; background: #fafbfc; }
988
    tr:hover td { background: #f8fafc; }
989
    .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; }
990
    .pill.ok { color: var(--ok); border-color: #b7dfc1; background: #edf8ef; }
991
    .pill.warn { color: var(--warn); border-color: #f1d184; background: #fff7df; }
992
    .pill.bad { color: var(--bad); border-color: #f0b8b3; background: #fff0ee; }
993
    .grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; padding: 14px; }
994
    .span2 { grid-column: 1 / -1; }
995
    label { display: grid; gap: 5px; color: var(--muted); font-size: 12px; font-weight: 650; }
996
    .muted { color: var(--muted); }
997
    .problems { padding: 10px 14px; display: grid; gap: 8px; }
998
    .problem { border-left: 3px solid var(--warn); padding: 7px 9px; background: #fffaf0; }
999
    @media (max-width: 760px) {
1000
      .grid { grid-template-columns: 1fr; }
1001
      table { min-width: 760px; }
1002
      .table-wrap { overflow-x: auto; }
1003
    }
1004
  </style>
1005
</head>
1006
<body>
1007

            
Xdev Host Manager authored 2 days ago
1008
  <!-- ── Login screen ── -->
1009
  <div id="login-screen">
1010
    <div class="login-card">
1011
      <div class="brand">
1012
        <div class="icon">
1013
          <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
1014
            <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"/>
1015
            <circle cx="17" cy="7" r="1"/><circle cx="19.5" cy="7" r="1"/>
1016
            <circle cx="17" cy="15.5" r="1"/><circle cx="19.5" cy="15.5" r="1"/>
1017
          </svg>
1018
        </div>
1019
        <h1>Host Manager</h1>
1020
        <p>madagascar.xdev.ro</p>
Xdev Host Manager authored 2 days ago
1021
      </div>
Xdev Host Manager authored 2 days ago
1022
      <form id="login-form">
1023
        <div class="field-label">Cod Authenticator</div>
1024
        <div class="otp-row">
1025
          <input type="text" inputmode="numeric" maxlength="1" pattern="[0-9]" class="otp-digit" autocomplete="one-time-code">
1026
          <input type="text" inputmode="numeric" maxlength="1" pattern="[0-9]" class="otp-digit">
1027
          <input type="text" inputmode="numeric" maxlength="1" pattern="[0-9]" class="otp-digit">
1028
          <input type="text" inputmode="numeric" maxlength="1" pattern="[0-9]" class="otp-digit">
1029
          <input type="text" inputmode="numeric" maxlength="1" pattern="[0-9]" class="otp-digit">
1030
          <input type="text" inputmode="numeric" maxlength="1" pattern="[0-9]" class="otp-digit">
1031
        </div>
1032
        <button class="primary" type="submit" id="login-btn">Autentifică-te</button>
1033
      </form>
1034
      <div id="login-error"></div>
1035
    </div>
1036
  </div>
1037

            
1038
  <!-- ── App (shown after login) ── -->
1039
  <div id="app">
1040
    <header>
1041
      <h1>Host Manager</h1>
1042
      <div class="header-right">
1043
        <span class="muted" id="app-updated"></span>
1044
        <button type="button" id="logout">Logout</button>
Xdev Host Manager authored 2 days ago
1045
      </div>
Xdev Host Manager authored 2 days ago
1046
    </header>
1047
    <main>
1048
      <section class="toolbar">
1049
        <button id="refresh">Refresh</button>
1050
        <a class="linkbtn" href="/download/hosts.yaml">hosts.yaml</a>
1051
        <a class="linkbtn" href="/download/local-hosts.tsv">local-hosts.tsv</a>
1052
        <a class="linkbtn" href="/download/monitoring.json">monitoring.json</a>
1053
        <button id="write-tsv">Write local-hosts.tsv</button>
1054
        <span id="message" class="muted"></span>
1055
      </section>
1056

            
1057
      <section class="panel">
1058
        <div class="panel-head">
1059
          <h2>Overview</h2>
1060
          <div class="stats" id="stats"></div>
1061
        </div>
1062
        <div class="problems" id="problems"></div>
1063
      </section>
Xdev Host Manager authored 2 days ago
1064

            
Xdev Host Manager authored 2 days ago
1065
      <section class="panel">
1066
        <div class="panel-head">
1067
          <h2>Certificate Authority</h2>
1068
          <a class="linkbtn" href="/download/ca.crt">ca.crt</a>
1069
        </div>
1070
        <div class="problems" id="ca-status"></div>
1071
      </section>
1072

            
Xdev Host Manager authored 2 days ago
1073
      <section class="panel">
1074
        <div class="panel-head">
1075
          <h2>Hosts</h2>
1076
          <input id="filter" placeholder="filter" style="max-width: 240px">
Xdev Host Manager authored 2 days ago
1077
        </div>
Xdev Host Manager authored 2 days ago
1078
        <div class="table-wrap">
1079
          <table>
1080
            <thead>
1081
              <tr>
1082
                <th style="width: 120px">ID</th>
1083
                <th style="width: 130px">hosts_ip</th>
1084
                <th style="width: 130px">dns_ip</th>
1085
                <th>Names</th>
1086
                <th style="width: 150px">Roles</th>
1087
                <th style="width: 110px">Monitoring</th>
1088
                <th style="width: 90px">Status</th>
1089
              </tr>
1090
            </thead>
1091
            <tbody id="hosts"></tbody>
1092
          </table>
1093
        </div>
1094
      </section>
1095

            
1096
      <section class="panel">
1097
        <div class="panel-head">
1098
          <h2>Edit host</h2>
1099
        </div>
1100
        <form id="host-form" class="grid">
1101
          <label>ID<input name="id" required></label>
1102
          <label>Status<select name="status"><option>active</option><option>planned</option><option>retired</option></select></label>
1103
          <label>hosts_ip<input name="hosts_ip" required></label>
1104
          <label>dns_ip<input name="dns_ip" required></label>
1105
          <label class="span2">Names<textarea name="names" required></textarea></label>
1106
          <label>Roles<input name="roles"></label>
1107
          <label>Sources<input name="sources"></label>
1108
          <label>Monitoring<select name="monitoring"><option>pending</option><option>enabled</option><option>disabled</option></select></label>
1109
          <label>Notes<input name="notes"></label>
1110
          <div class="span2">
1111
            <button class="primary" type="submit">Save host</button>
1112
            <button class="danger" type="button" id="delete-host">Delete host</button>
1113
          </div>
1114
        </form>
1115
      </section>
1116
    </main>
1117
  </div>
1118

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

            
1122
    const $ = (id) => document.getElementById(id);
1123
    const msg = (text) => { $('message').textContent = text || ''; };
1124

            
1125
    async function api(path, options = {}) {
1126
      const res = await fetch(path, options);
1127
      const body = await res.json();
1128
      if (!res.ok) throw new Error(body.error || res.statusText);
1129
      return body;
1130
    }
1131

            
Xdev Host Manager authored 2 days ago
1132
    function showLogin(errorText) {
1133
      $('app').style.display = 'none';
1134
      $('login-screen').style.display = 'flex';
1135
      $('login-error').textContent = errorText || '';
1136
      document.querySelectorAll('.otp-digit').forEach(i => { i.value = ''; i.classList.remove('filled'); });
1137
      const first = document.querySelector('.otp-digit');
1138
      if (first) first.focus();
1139
    }
1140

            
1141
    function showApp() {
1142
      $('login-screen').style.display = 'none';
1143
      $('app').style.display = 'block';
1144
    }
1145

            
Xdev Host Manager authored 2 days ago
1146
    async function refresh() {
1147
      const session = await api('/api/session');
1148
      state.authenticated = session.authenticated;
Xdev Host Manager authored 2 days ago
1149
      if (!state.authenticated) { showLogin(); return; }
1150
      showApp();
Xdev Host Manager authored 2 days ago
1151
      const data = await api('/api/hosts');
1152
      state.hosts = data.hosts || [];
1153
      state.problems = data.problems || [];
1154
      render(data);
Xdev Host Manager authored 2 days ago
1155
      await renderCa();
Xdev Host Manager authored 2 days ago
1156
    }
1157

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

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

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

            
1170
      renderHosts();
1171
    }
1172

            
Xdev Host Manager authored 2 days ago
1173
    async function renderCa() {
1174
      try {
1175
        const status = await api('/api/ca/status');
1176
        if (!status.initialized) {
1177
          $('ca-status').innerHTML = '<div class="problem"><strong>not initialized</strong> Run <code>sudo scripts/ca_manager.sh init</code> on jumper.</div>';
1178
          return;
1179
        }
1180
        const certs = await api('/api/ca/certificates');
1181
        $('ca-status').innerHTML = `
1182
          <div class="muted" style="display:grid;gap:6px">
1183
            <div><strong>${escapeHtml(status.subject || '')}</strong></div>
1184
            <div>SHA256 ${escapeHtml(status.fingerprint_sha256 || '')}</div>
1185
            <div>valid ${escapeHtml(status.not_before || '')} - ${escapeHtml(status.not_after || '')}</div>
1186
            <div>${certs.length} issued certificate(s)</div>
1187
          </div>`;
1188
      } catch (e) {
1189
        $('ca-status').innerHTML = `<div class="problem"><strong>CA status unavailable</strong> ${escapeHtml(e.message)}</div>`;
1190
      }
1191
    }
1192

            
Xdev Host Manager authored 2 days ago
1193
    function renderHosts() {
1194
      const filter = $('filter').value.toLowerCase();
1195
      $('hosts').innerHTML = state.hosts
1196
        .filter(h => JSON.stringify(h).toLowerCase().includes(filter))
1197
        .map(h => {
1198
          const problems = state.problems.filter(p => p.host_id === h.id);
1199
          const cls = problems.length ? 'warn' : 'ok';
1200
          return `<tr data-id="${escapeHtml(h.id)}">
1201
            <td><button type="button" data-edit="${escapeHtml(h.id)}">${escapeHtml(h.id)}</button></td>
1202
            <td>${escapeHtml(h.hosts_ip || '')}</td>
1203
            <td>${escapeHtml(h.dns_ip || '')}</td>
1204
            <td>${(h.names || []).map(n => `<span class="pill">${escapeHtml(n)}</span>`).join('')}</td>
1205
            <td>${(h.roles || []).map(n => `<span class="pill">${escapeHtml(n)}</span>`).join('')}</td>
1206
            <td><span class="pill ${cls}">${escapeHtml(h.monitoring || '')}</span></td>
1207
            <td>${escapeHtml(h.status || '')}</td>
1208
          </tr>`;
1209
        }).join('');
1210
      document.querySelectorAll('[data-edit]').forEach(button => button.addEventListener('click', () => editHost(button.dataset.edit)));
1211
    }
1212

            
1213
    function editHost(id) {
1214
      const host = state.hosts.find(h => h.id === id);
1215
      if (!host) return;
1216
      const form = $('host-form');
1217
      for (const key of ['id', 'status', 'hosts_ip', 'dns_ip', 'monitoring', 'notes']) form.elements[key].value = host[key] || '';
1218
      form.elements.names.value = (host.names || []).join('\n');
1219
      form.elements.roles.value = (host.roles || []).join(' ');
1220
      form.elements.sources.value = (host.sources || []).join(' ');
Xdev Host Manager authored 2 days ago
1221
      form.scrollIntoView({ behavior: 'smooth', block: 'start' });
Xdev Host Manager authored 2 days ago
1222
    }
1223

            
1224
    function formObject(form) {
1225
      return Object.fromEntries(new FormData(form).entries());
1226
    }
1227

            
1228
    function escapeHtml(value) {
1229
      return value.replace(/[&<>"']/g, ch => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#039;'}[ch]));
1230
    }
1231

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

            
1236
    otpDigits.forEach((input, idx) => {
1237
      input.addEventListener('keydown', (e) => {
1238
        if (e.key === 'Backspace') {
1239
          if (input.value) { input.value = ''; input.classList.remove('filled'); }
1240
          else if (idx > 0) { otpDigits[idx - 1].value = ''; otpDigits[idx - 1].classList.remove('filled'); otpDigits[idx - 1].focus(); }
1241
          e.preventDefault();
1242
        }
1243
      });
1244
      input.addEventListener('input', (e) => {
1245
        const val = input.value.replace(/\D/g, '').slice(-1);
1246
        input.value = val;
1247
        input.classList.toggle('filled', !!val);
1248
        if (val && idx < otpDigits.length - 1) otpDigits[idx + 1].focus();
1249
        if (val && idx === otpDigits.length - 1) $('login-form').requestSubmit();
1250
      });
1251
      input.addEventListener('paste', (e) => {
1252
        const text = (e.clipboardData || window.clipboardData).getData('text').replace(/\D/g, '');
1253
        e.preventDefault();
1254
        text.split('').slice(0, otpDigits.length).forEach((ch, i) => {
1255
          otpDigits[i].value = ch;
1256
          otpDigits[i].classList.add('filled');
1257
        });
1258
        const next = Math.min(text.length, otpDigits.length - 1);
1259
        otpDigits[next].focus();
1260
        if (text.length >= otpDigits.length) $('login-form').requestSubmit();
1261
      });
1262
    });
1263

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

            
1267
    $('login-form').addEventListener('submit', async (event) => {
1268
      event.preventDefault();
Xdev Host Manager authored 2 days ago
1269
      const btn = $('login-btn');
1270
      btn.disabled = true;
1271
      $('login-error').textContent = '';
Xdev Host Manager authored 2 days ago
1272
      try {
Xdev Host Manager authored 2 days ago
1273
        await api('/api/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ otp: getOtp() }) });
Xdev Host Manager authored 2 days ago
1274
        await refresh();
Xdev Host Manager authored 2 days ago
1275
      } catch (e) {
1276
        showLogin(e.message === 'invalid_otp' ? 'Cod incorect.' : e.message);
1277
      } finally {
1278
        btn.disabled = false;
1279
      }
Xdev Host Manager authored 2 days ago
1280
    });
1281

            
1282
    $('logout').addEventListener('click', async () => {
1283
      await api('/api/logout', { method: 'POST' }).catch(() => {});
Xdev Host Manager authored 2 days ago
1284
      clearOtp();
1285
      showLogin();
Xdev Host Manager authored 2 days ago
1286
    });
1287

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

            
Xdev Host Manager authored 2 days ago
1291
    $('host-form').addEventListener('submit', async (event) => {
1292
      event.preventDefault();
1293
      try {
1294
        await api('/api/hosts/upsert', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(formObject(event.target)) });
1295
        msg('host saved');
1296
        await refresh();
1297
      } catch (e) { msg(e.message); }
1298
    });
1299

            
1300
    $('delete-host').addEventListener('click', async () => {
1301
      const id = $('host-form').elements.id.value;
1302
      if (!id || !confirm(`Delete ${id}?`)) return;
1303
      try {
1304
        await api('/api/hosts/delete', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id }) });
1305
        $('host-form').reset();
1306
        msg('host deleted');
1307
        await refresh();
1308
      } catch (e) { msg(e.message); }
1309
    });
1310

            
1311
    $('write-tsv').addEventListener('click', async () => {
1312
      if (!confirm('Write config/local-hosts.tsv from hosts.yaml?')) return;
1313
      try {
1314
        await api('/api/render/local-hosts-tsv', { method: 'POST' });
1315
        msg('local-hosts.tsv written');
1316
      } catch (e) { msg(e.message); }
1317
    });
1318

            
Xdev Host Manager authored 2 days ago
1319
    refresh().catch(() => showLogin());
Xdev Host Manager authored 2 days ago
1320
  </script>
1321
</body>
1322
</html>
1323
HTML
1324
}