LocalAuthority / scripts / host_manager.pl
Newer Older
1205 lines | 44.541kb
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
    }
150

            
151
    if ($method eq 'POST' && $path =~ m{^/api/}) {
152
        if ($path eq '/api/hosts/upsert') {
153
            my $payload = request_payload(\%headers, $body);
154
            return upsert_host($client, $payload);
155
        }
156
        if ($path eq '/api/hosts/delete') {
157
            my $payload = request_payload(\%headers, $body);
158
            return delete_host($client, $payload->{id} || '');
159
        }
160
        if ($path eq '/api/render/local-hosts-tsv') {
161
            my $registry = load_registry();
162
            my $content = render_local_hosts_tsv($registry);
163
            backup_file($opt{local_hosts_tsv});
164
            write_file($opt{local_hosts_tsv}, $content);
165
            return send_json($client, 200, { ok => json_bool(1), file => $opt{local_hosts_tsv} });
166
        }
167
    }
168

            
169
    return send_json($client, 404, { error => 'not_found' });
170
}
171

            
172
sub load_registry {
173
    return parse_hosts_yaml(read_file($opt{data}));
174
}
175

            
176
sub save_registry {
177
    my ($registry) = @_;
178
    $registry->{updated_at} = iso_now();
179
    backup_file($opt{data});
180
    write_file($opt{data}, render_hosts_yaml($registry));
181
}
182

            
183
sub registry_payload {
184
    my ($registry) = @_;
185
    my $problems = analyze_hosts($registry->{hosts});
186
    return {
187
        version => $registry->{version},
188
        updated_at => $registry->{updated_at},
189
        policy => $registry->{policy},
190
        hosts => $registry->{hosts},
191
        problems => $problems,
192
        counts => {
193
            hosts => scalar @{ $registry->{hosts} },
194
            problems => scalar @$problems,
195
        },
196
    };
197
}
198

            
199
sub upsert_host {
200
    my ($client, $payload) = @_;
201
    my $id = clean_id($payload->{id} || '');
202
    return send_json($client, 400, { error => 'invalid_id' }) unless $id;
203

            
204
    my $hosts_ip = clean_scalar($payload->{hosts_ip} || '');
205
    my $dns_ip = clean_scalar($payload->{dns_ip} || '');
206
    return send_json($client, 400, { error => 'missing_ip' }) unless $hosts_ip && $dns_ip;
207

            
208
    my @names = clean_list($payload->{names});
209
    return send_json($client, 400, { error => 'missing_names' }) unless @names;
210

            
211
    my $registry = load_registry();
212
    my %host = (
213
        id => $id,
214
        status => clean_scalar($payload->{status} || 'active'),
215
        hosts_ip => $hosts_ip,
216
        dns_ip => $dns_ip,
217
        names => \@names,
218
        roles => [ clean_list($payload->{roles}) ],
219
        sources => [ clean_list($payload->{sources}) ],
220
        monitoring => clean_scalar($payload->{monitoring} || 'pending'),
221
        notes => clean_scalar($payload->{notes} || ''),
222
    );
223

            
224
    my $replaced = 0;
225
    for my $i (0 .. $#{ $registry->{hosts} }) {
226
        if ($registry->{hosts}->[$i]{id} eq $id) {
227
            $registry->{hosts}->[$i] = \%host;
228
            $replaced = 1;
229
            last;
230
        }
231
    }
232
    push @{ $registry->{hosts} }, \%host unless $replaced;
233
    save_registry($registry);
234
    return send_json($client, 200, { ok => json_bool(1), host => \%host });
235
}
236

            
237
sub delete_host {
238
    my ($client, $id) = @_;
239
    $id = clean_id($id);
240
    return send_json($client, 400, { error => 'invalid_id' }) unless $id;
241

            
242
    my $registry = load_registry();
243
    my @kept = grep { $_->{id} ne $id } @{ $registry->{hosts} };
244
    return send_json($client, 404, { error => 'not_found' }) if @kept == @{ $registry->{hosts} };
245
    $registry->{hosts} = \@kept;
246
    save_registry($registry);
247
    return send_json($client, 200, { ok => json_bool(1) });
248
}
249

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

            
272
sub problem {
273
    my ($host, $code, $message) = @_;
274
    return { host_id => $host->{id}, code => $code, message => $message };
275
}
276

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

            
297
sub render_monitoring {
298
    my ($registry) = @_;
299
    my @hosts;
300
    for my $host (sort { $a->{id} cmp $b->{id} } @{ $registry->{hosts} }) {
301
        next unless ($host->{status} || 'active') eq 'active';
302
        next if ($host->{monitoring} || 'pending') eq 'disabled';
303
        push @hosts, {
304
            id => $host->{id},
305
            primary_name => $host->{names}[0],
306
            address => $host->{dns_ip},
307
            aliases => [ @{ $host->{names} || [] } ],
308
            roles => [ @{ $host->{roles} || [] } ],
309
            monitoring => $host->{monitoring} || 'pending',
310
            notes => $host->{notes} || '',
311
        };
312
    }
313
    return {
314
        version => $registry->{version},
315
        generated_at => iso_now(),
316
        source => 'config/hosts.yaml',
317
        hosts => \@hosts,
318
    };
319
}
320

            
321
sub parse_hosts_yaml {
322
    my ($text) = @_;
323
    my %registry = (
324
        version => 1,
325
        updated_at => '',
326
        policy => {},
327
        hosts => [],
328
    );
329
    my ($section, $current, $list_key);
330
    for my $line (split /\n/, $text) {
331
        next if $line =~ /^\s*$/ || $line =~ /^\s*#/;
332
        if ($line =~ /^version:\s*(\d+)/) {
333
            $registry{version} = int($1);
334
        } elsif ($line =~ /^updated_at:\s*(.+)$/) {
335
            $registry{updated_at} = yaml_unquote($1);
336
        } elsif ($line =~ /^policy:\s*$/) {
337
            $section = 'policy';
338
        } elsif ($line =~ /^hosts:\s*$/) {
339
            $section = 'hosts';
340
        } elsif (($section || '') eq 'policy' && $line =~ /^  ([A-Za-z0-9_]+):\s*(.+)$/) {
341
            $registry{policy}{$1} = yaml_unquote($2);
342
        } elsif (($section || '') eq 'hosts' && $line =~ /^  - id:\s*(.+)$/) {
343
            $current = {
344
                id => yaml_unquote($1),
345
                status => 'active',
346
                hosts_ip => '',
347
                dns_ip => '',
348
                names => [],
349
                roles => [],
350
                sources => [],
351
                monitoring => 'pending',
352
                notes => '',
353
            };
354
            push @{ $registry{hosts} }, $current;
355
            $list_key = undef;
356
        } elsif ($current && $line =~ /^    ([A-Za-z0-9_]+):\s*$/) {
357
            $list_key = $1;
358
            $current->{$list_key} ||= [];
359
        } elsif ($current && defined $list_key && $line =~ /^      -\s*(.+)$/) {
360
            push @{ $current->{$list_key} }, yaml_unquote($1);
361
        } elsif ($current && $line =~ /^    ([A-Za-z0-9_]+):\s*(.*)$/) {
362
            $current->{$1} = yaml_unquote($2);
363
            $list_key = undef;
364
        }
365
    }
366
    return \%registry;
367
}
368

            
369
sub render_hosts_yaml {
370
    my ($registry) = @_;
371
    my $out = "version: " . int($registry->{version} || 1) . "\n";
372
    $out .= "updated_at: " . yq($registry->{updated_at} || iso_now()) . "\n";
373
    $out .= "policy:\n";
374
    for my $key (sort keys %{ $registry->{policy} || {} }) {
375
        $out .= "  $key: " . yq($registry->{policy}{$key}) . "\n";
376
    }
377
    $out .= "hosts:\n";
378
    for my $host (sort { $a->{id} cmp $b->{id} } @{ $registry->{hosts} || [] }) {
379
        $out .= "  - id: " . yq($host->{id}) . "\n";
380
        for my $key (qw(status hosts_ip dns_ip)) {
381
            $out .= "    $key: " . yq($host->{$key} || '') . "\n";
382
        }
383
        for my $key (qw(names roles sources)) {
384
            $out .= "    $key:\n";
385
            for my $value (@{ $host->{$key} || [] }) {
386
                $out .= "      - " . yq($value) . "\n";
387
            }
388
        }
389
        $out .= "    monitoring: " . yq($host->{monitoring} || 'pending') . "\n";
390
        $out .= "    notes: " . yq($host->{notes} || '') . "\n";
391
    }
392
    return $out;
393
}
394

            
395
sub request_payload {
396
    my ($headers, $body) = @_;
397
    my $type = $headers->{'content-type'} || '';
398
    if ($type =~ m{application/json}) {
399
        return json_decode($body || '{}');
400
    }
401
    return { parse_params($body || '') };
402
}
403

            
404
sub json_bool {
405
    my ($value) = @_;
406
    return bless \(my $bool = $value ? 1 : 0), 'HostManager::JSONBool';
407
}
408

            
409
sub json_encode {
410
    my ($value) = @_;
411
    if (!defined $value) {
412
        return 'null';
413
    }
414
    my $ref = ref($value);
415
    if (!$ref) {
416
        return $value if $value =~ /\A-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?\z/;
417
        return json_string($value);
418
    }
419
    if ($ref eq 'HostManager::JSONBool') {
420
        return $$value ? 'true' : 'false';
421
    }
422
    if ($ref eq 'ARRAY') {
423
        return '[' . join(',', map { json_encode($_) } @$value) . ']';
424
    }
425
    if ($ref eq 'HASH') {
426
        return '{' . join(',', map { json_string($_) . ':' . json_encode($value->{$_}) } sort keys %$value) . '}';
427
    }
428
    return json_string("$value");
429
}
430

            
431
sub json_string {
432
    my ($value) = @_;
433
    $value = '' unless defined $value;
434
    $value =~ s/\\/\\\\/g;
435
    $value =~ s/"/\\"/g;
436
    $value =~ s/\n/\\n/g;
437
    $value =~ s/\r/\\r/g;
438
    $value =~ s/\t/\\t/g;
439
    $value =~ s/([\x00-\x1f])/sprintf("\\u%04x", ord($1))/eg;
440
    return qq("$value");
441
}
442

            
443
sub json_decode {
444
    my ($text) = @_;
445
    my $i = 0;
446
    my $len = length($text);
447
    my ($parse_value, $parse_string, $parse_array, $parse_object, $parse_number, $skip_ws);
448

            
449
    $skip_ws = sub {
450
        $i++ while $i < $len && substr($text, $i, 1) =~ /\s/;
451
    };
452

            
453
    $parse_string = sub {
454
        die "Expected JSON string\n" unless substr($text, $i, 1) eq '"';
455
        $i++;
456
        my $out = '';
457
        while ($i < $len) {
458
            my $ch = substr($text, $i++, 1);
459
            return $out if $ch eq '"';
460
            if ($ch eq "\\") {
461
                die "Bad JSON escape\n" if $i >= $len;
462
                my $esc = substr($text, $i++, 1);
463
                if ($esc eq '"' || $esc eq "\\" || $esc eq '/') {
464
                    $out .= $esc;
465
                } elsif ($esc eq 'b') {
466
                    $out .= "\b";
467
                } elsif ($esc eq 'f') {
468
                    $out .= "\f";
469
                } elsif ($esc eq 'n') {
470
                    $out .= "\n";
471
                } elsif ($esc eq 'r') {
472
                    $out .= "\r";
473
                } elsif ($esc eq 't') {
474
                    $out .= "\t";
475
                } elsif ($esc eq 'u') {
476
                    my $hex = substr($text, $i, 4);
477
                    die "Bad JSON unicode escape\n" unless $hex =~ /\A[0-9A-Fa-f]{4}\z/;
478
                    $out .= chr(hex($hex));
479
                    $i += 4;
480
                } else {
481
                    die "Bad JSON escape\n";
482
                }
483
            } else {
484
                $out .= $ch;
485
            }
486
        }
487
        die "Unterminated JSON string\n";
488
    };
489

            
490
    $parse_number = sub {
491
        my $start = $i;
492
        $i++ if substr($text, $i, 1) eq '-';
493
        $i++ while $i < $len && substr($text, $i, 1) =~ /[0-9]/;
494
        if ($i < $len && substr($text, $i, 1) eq '.') {
495
            $i++;
496
            $i++ while $i < $len && substr($text, $i, 1) =~ /[0-9]/;
497
        }
498
        if ($i < $len && substr($text, $i, 1) =~ /[eE]/) {
499
            $i++;
500
            $i++ if $i < $len && substr($text, $i, 1) =~ /[+-]/;
501
            $i++ while $i < $len && substr($text, $i, 1) =~ /[0-9]/;
502
        }
503
        return 0 + substr($text, $start, $i - $start);
504
    };
505

            
506
    $parse_array = sub {
507
        die "Expected JSON array\n" unless substr($text, $i, 1) eq '[';
508
        $i++;
509
        my @out;
510
        $skip_ws->();
511
        if ($i < $len && substr($text, $i, 1) eq ']') {
512
            $i++;
513
            return \@out;
514
        }
515
        while (1) {
516
            push @out, $parse_value->();
517
            $skip_ws->();
518
            my $ch = substr($text, $i++, 1);
519
            last if $ch eq ']';
520
            die "Expected JSON array comma\n" unless $ch eq ',';
521
        }
522
        return \@out;
523
    };
524

            
525
    $parse_object = sub {
526
        die "Expected JSON object\n" unless substr($text, $i, 1) eq '{';
527
        $i++;
528
        my %out;
529
        $skip_ws->();
530
        if ($i < $len && substr($text, $i, 1) eq '}') {
531
            $i++;
532
            return \%out;
533
        }
534
        while (1) {
535
            $skip_ws->();
536
            my $key = $parse_string->();
537
            $skip_ws->();
538
            die "Expected JSON object colon\n" unless substr($text, $i++, 1) eq ':';
539
            $out{$key} = $parse_value->();
540
            $skip_ws->();
541
            my $ch = substr($text, $i++, 1);
542
            last if $ch eq '}';
543
            die "Expected JSON object comma\n" unless $ch eq ',';
544
        }
545
        return \%out;
546
    };
547

            
548
    $parse_value = sub {
549
        $skip_ws->();
550
        die "Unexpected end of JSON\n" if $i >= $len;
551
        my $ch = substr($text, $i, 1);
552
        return $parse_string->() if $ch eq '"';
553
        return $parse_object->() if $ch eq '{';
554
        return $parse_array->() if $ch eq '[';
555
        if (substr($text, $i, 4) eq 'true') {
556
            $i += 4;
557
            return json_bool(1);
558
        }
559
        if (substr($text, $i, 5) eq 'false') {
560
            $i += 5;
561
            return json_bool(0);
562
        }
563
        if (substr($text, $i, 4) eq 'null') {
564
            $i += 4;
565
            return undef;
566
        }
567
        return $parse_number->() if $ch =~ /[-0-9]/;
568
        die "Unexpected JSON token\n";
569
    };
570

            
571
    my $value = $parse_value->();
572
    $skip_ws->();
573
    die "Trailing JSON content\n" if $i != $len;
574
    return $value;
575
}
576

            
577
sub parse_params {
578
    my ($text) = @_;
579
    my %out;
580
    for my $pair (split /&/, $text) {
581
        next unless length $pair;
582
        my ($k, $v) = split /=/, $pair, 2;
583
        $out{url_decode($k)} = url_decode($v || '');
584
    }
585
    return %out;
586
}
587

            
588
sub clean_id {
589
    my ($value) = @_;
590
    $value = lc clean_scalar($value);
591
    $value =~ s/[^a-z0-9_.-]+/-/g;
592
    $value =~ s/^-+|-+$//g;
593
    return $value;
594
}
595

            
596
sub clean_scalar {
597
    my ($value) = @_;
598
    $value = '' unless defined $value;
599
    $value =~ s/[\r\n\t]+/ /g;
600
    $value =~ s/^\s+|\s+$//g;
601
    return $value;
602
}
603

            
604
sub clean_list {
605
    my ($value) = @_;
606
    return () unless defined $value;
607
    my @items = ref($value) eq 'ARRAY' ? @$value : split /[\s,]+/, $value;
608
    my @clean;
609
    for my $item (@items) {
610
        $item = clean_scalar($item);
611
        push @clean, $item if length $item;
612
    }
613
    return @clean;
614
}
615

            
616
sub yq {
617
    my ($value) = @_;
618
    $value = '' unless defined $value;
619
    $value =~ s/\\/\\\\/g;
620
    $value =~ s/"/\\"/g;
621
    return qq("$value");
622
}
623

            
624
sub yaml_unquote {
625
    my ($value) = @_;
626
    $value = '' unless defined $value;
627
    $value =~ s/^\s+|\s+$//g;
628
    if ($value =~ /^"(.*)"$/) {
629
        $value = $1;
630
        $value =~ s/\\"/"/g;
631
        $value =~ s/\\\\/\\/g;
632
    }
633
    return $value;
634
}
635

            
636
sub verify_totp {
637
    my ($secret, $otp) = @_;
638
    return 0 unless $secret && $otp =~ /^\d{6}$/;
639
    my $key = eval { base32_decode($secret) };
640
    return 0 if $@ || !length $key;
641
    my $counter = int(time() / 30);
642
    for my $offset (-1, 0, 1) {
643
        return 1 if totp_code($key, $counter + $offset) eq $otp;
644
    }
645
    return 0;
646
}
647

            
648
sub totp_code {
649
    my ($key, $counter) = @_;
650
    my $msg = pack('NN', int($counter / 4294967296), $counter & 0xffffffff);
651
    my $hash = hmac_sha1($msg, $key);
652
    my $offset = ord(substr($hash, -1)) & 0x0f;
653
    my $bin = unpack('N', substr($hash, $offset, 4)) & 0x7fffffff;
654
    return sprintf('%06d', $bin % 1_000_000);
655
}
656

            
657
sub base32_decode {
658
    my ($text) = @_;
659
    $text = uc($text || '');
660
    $text =~ s/[^A-Z2-7]//g;
661
    my %map;
662
    my @chars = ('A'..'Z', '2'..'7');
663
    @map{@chars} = (0..31);
664
    my ($bits, $value, $out) = (0, 0, '');
665
    for my $char (split //, $text) {
666
        die "Invalid base32\n" unless exists $map{$char};
667
        $value = ($value << 5) | $map{$char};
668
        $bits += 5;
669
        while ($bits >= 8) {
670
            $bits -= 8;
671
            $out .= chr(($value >> $bits) & 0xff);
672
        }
673
    }
674
    return $out;
675
}
676

            
677
sub create_session {
678
    my $nonce = random_hex(24);
679
    my $expires = int(time() + 8 * 3600);
680
    my $sig = hmac_sha256_hex("$nonce:$expires", $session_secret);
681
    my $token = "$nonce:$expires:$sig";
682
    $sessions{$token} = $expires;
683
    return $token;
684
}
685

            
686
sub is_authenticated {
687
    my ($headers) = @_;
688
    my $token = cookie_value($headers->{'cookie'} || '', 'hm_session');
689
    return 0 unless $token;
690
    my ($nonce, $expires, $sig) = split /:/, $token;
691
    return 0 unless $nonce && $expires && $sig;
692
    return 0 if $expires < time();
693
    return 0 unless hmac_sha256_hex("$nonce:$expires", $session_secret) eq $sig;
694
    return exists $sessions{$token};
695
}
696

            
697
sub expire_session {
698
    my ($headers) = @_;
699
    my $token = cookie_value($headers->{'cookie'} || '', 'hm_session');
700
    delete $sessions{$token} if $token;
701
}
702

            
703
sub cookie_value {
704
    my ($cookie, $name) = @_;
705
    for my $part (split /;\s*/, $cookie) {
706
        my ($k, $v) = split /=/, $part, 2;
707
        return $v if defined $k && $k eq $name;
708
    }
709
    return '';
710
}
711

            
712
sub send_json {
713
    my ($client, $status, $payload, $extra_headers) = @_;
714
    return send_response($client, $status, json_encode($payload), 'application/json; charset=utf-8', $extra_headers);
715
}
716

            
717
sub send_html {
718
    my ($client, $status, $html) = @_;
719
    return send_response($client, $status, $html, 'text/html; charset=utf-8');
720
}
721

            
722
sub send_text {
723
    my ($client, $status, $text) = @_;
724
    return send_response($client, $status, $text, 'text/plain; charset=utf-8');
725
}
726

            
727
sub send_download {
728
    my ($client, $status, $content, $type, $filename) = @_;
729
    return send_response($client, $status, $content, $type, [ qq(Content-Disposition: attachment; filename="$filename") ]);
730
}
731

            
732
sub send_file {
733
    my ($client, $path, $type, $filename) = @_;
734
    return send_json($client, 404, { error => 'missing_file' }) unless -f $path;
735
    return send_download($client, 200, read_file($path), $type, $filename);
736
}
737

            
738
sub send_response {
739
    my ($client, $status, $body, $type, $extra_headers) = @_;
740
    my %reason = (200 => 'OK', 400 => 'Bad Request', 401 => 'Unauthorized', 404 => 'Not Found', 500 => 'Internal Server Error', 503 => 'Service Unavailable');
741
    $body = '' unless defined $body;
742
    print $client "HTTP/1.1 $status " . ($reason{$status} || 'OK') . "\r\n";
743
    print $client "Content-Type: $type\r\n";
744
    print $client "Content-Length: " . length($body) . "\r\n";
745
    print $client "Cache-Control: no-store\r\n";
746
    print $client "$_\r\n" for @{ $extra_headers || [] };
747
    print $client "Connection: close\r\n\r\n";
748
    print $client $body;
749
}
750

            
751
sub read_file {
752
    my ($path) = @_;
753
    open my $fh, '<', $path or die "Cannot read $path: $!";
754
    local $/;
755
    return <$fh>;
756
}
757

            
758
sub write_file {
759
    my ($path, $content) = @_;
760
    open my $fh, '>', $path or die "Cannot write $path: $!";
761
    print {$fh} $content;
762
    close $fh or die "Cannot close $path: $!";
763
}
764

            
765
sub backup_file {
766
    my ($path) = @_;
767
    return unless -f $path;
768
    my $backup_dir = "$project_dir/backups/host-manager";
769
    make_path($backup_dir) unless -d $backup_dir;
770
    my $name = $path;
771
    $name =~ s{.*/}{};
772
    my $stamp = strftime('%Y%m%d_%H%M%S', localtime);
773
    write_file("$backup_dir/$name.$stamp.bak", read_file($path));
774
}
775

            
776
sub url_decode {
777
    my ($value) = @_;
778
    $value = '' unless defined $value;
779
    $value =~ tr/+/ /;
780
    $value =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
781
    return $value;
782
}
783

            
784
sub random_hex {
785
    my ($bytes) = @_;
786
    if (open my $fh, '<:raw', '/dev/urandom') {
787
        read($fh, my $raw, $bytes);
788
        close $fh;
789
        return unpack('H*', $raw);
790
    }
791
    return sha256_hex(rand() . time() . $$);
792
}
793

            
794
sub iso_now {
795
    return strftime('%Y-%m-%dT%H:%M:%SZ', gmtime);
796
}
797

            
798
sub app_html {
799
    return <<'HTML';
800
<!doctype html>
801
<html lang="ro">
802
<head>
803
  <meta charset="utf-8">
804
  <meta name="viewport" content="width=device-width, initial-scale=1">
805
  <title>Host Manager</title>
806
  <style>
807
    :root {
808
      color-scheme: light;
809
      --ink: #152033;
810
      --muted: #647084;
811
      --line: #d8dee8;
812
      --soft: #f4f6f9;
813
      --panel: #ffffff;
814
      --accent: #1267d8;
815
      --bad: #b42318;
816
      --warn: #946200;
817
      --ok: #137333;
818
    }
819
    * { box-sizing: border-box; }
820
    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
821

            
822
    /* ── Login screen ── */
823
    #login-screen {
824
      display: flex;
825
      align-items: center;
826
      justify-content: center;
827
      min-height: 100dvh;
828
      padding: 24px;
829
      background: #13182a;
830
    }
831
    .login-card {
832
      background: #fff;
833
      border-radius: 16px;
834
      padding: 44px 36px 36px;
835
      width: 100%;
836
      max-width: 380px;
837
      display: grid;
838
      gap: 24px;
839
      box-shadow: 0 8px 40px rgba(0,0,0,.28);
840
    }
841
    .login-card .brand { text-align: center; display: grid; gap: 6px; }
842
    .login-card .brand .icon {
843
      margin: 0 auto 4px;
844
      width: 52px; height: 52px; border-radius: 14px;
845
      background: #e8f0fe; display: flex; align-items: center; justify-content: center;
846
    }
847
    .login-card .brand .icon svg { width: 26px; height: 26px; fill: var(--accent); }
848
    .login-card .brand h1 { margin: 0; font-size: 22px; font-weight: 700; color: var(--ink); }
849
    .login-card .brand p { margin: 0; color: var(--muted); font-size: 13px; }
850
    .login-card form { display: grid; gap: 16px; }
851
    .login-card .field-label { font-size: 13px; font-weight: 600; color: var(--ink); }
852
    /* 6 separate OTP digit boxes */
853
    .otp-row { display: flex; gap: 8px; justify-content: center; }
854
    .otp-row input {
855
      width: 48px; height: 56px; border: 1.5px solid #dde2ec; border-radius: 10px;
856
      font-size: 22px; font-weight: 600; text-align: center; color: var(--ink);
857
      background: #f8fafc; caret-color: transparent; outline: none;
858
      transition: border-color .15s, background .15s;
859
    }
860
    .otp-row input:focus { border-color: var(--accent); background: #fff; }
861
    .otp-row input.filled { border-color: #b3c6f0; background: #fff; }
862
    .login-card button.primary {
863
      width: 100%; border: none; background: var(--accent); color: #fff;
864
      border-radius: 10px; padding: 13px; font: inherit; font-size: 15px;
865
      font-weight: 600; cursor: pointer; min-height: 48px;
866
      display: flex; align-items: center; justify-content: center; gap: 8px;
867
    }
868
    .login-card button.primary:hover { background: #0f52b8; }
869
    .login-card button.primary:disabled { opacity: .55; cursor: not-allowed; }
870
    #login-error {
871
      color: var(--bad); font-size: 13px; text-align: center;
872
      min-height: 18px; margin-top: -8px;
873
    }
874

            
875
    /* ── App shell (hidden until authenticated) ── */
876
    #app { display: none; }
877
    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; }
878
    h1 { margin: 0; font-size: 17px; font-weight: 700; }
879
    .header-right { display: flex; align-items: center; gap: 10px; }
Xdev Host Manager authored 2 days ago
880
    main { padding: 16px; display: grid; gap: 16px; max-width: 1280px; margin: 0 auto; }
881
    .toolbar, .panel { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; }
882
    .toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 10px; }
883
    .panel { overflow: hidden; }
884
    .panel-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--line); background: #fafbfc; }
885
    .panel-head h2 { margin: 0; font-size: 14px; }
886
    .stats { display: flex; gap: 8px; flex-wrap: wrap; }
887
    .stat { padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--soft); font-size: 12px; color: var(--muted); }
888
    button, input, select, textarea { font: inherit; }
889
    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; }
890
    button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
891
    button.danger { color: var(--bad); }
Xdev Host Manager authored 2 days ago
892
    button:disabled, .linkbtn[aria-disabled="true"] { opacity: .55; cursor: not-allowed; pointer-events: none; }
Xdev Host Manager authored 2 days ago
893
    input, select, textarea { width: 100%; border: 1px solid var(--line); border-radius: 6px; padding: 8px; background: #fff; color: var(--ink); }
894
    textarea { min-height: 74px; resize: vertical; }
895
    table { width: 100%; border-collapse: collapse; table-layout: fixed; }
896
    th, td { padding: 9px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; overflow-wrap: anywhere; }
897
    th { color: var(--muted); font-size: 12px; font-weight: 700; background: #fafbfc; }
898
    tr:hover td { background: #f8fafc; }
899
    .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; }
900
    .pill.ok { color: var(--ok); border-color: #b7dfc1; background: #edf8ef; }
901
    .pill.warn { color: var(--warn); border-color: #f1d184; background: #fff7df; }
902
    .pill.bad { color: var(--bad); border-color: #f0b8b3; background: #fff0ee; }
903
    .grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; padding: 14px; }
904
    .span2 { grid-column: 1 / -1; }
905
    label { display: grid; gap: 5px; color: var(--muted); font-size: 12px; font-weight: 650; }
906
    .muted { color: var(--muted); }
907
    .problems { padding: 10px 14px; display: grid; gap: 8px; }
908
    .problem { border-left: 3px solid var(--warn); padding: 7px 9px; background: #fffaf0; }
909
    @media (max-width: 760px) {
910
      .grid { grid-template-columns: 1fr; }
911
      table { min-width: 760px; }
912
      .table-wrap { overflow-x: auto; }
913
    }
914
  </style>
915
</head>
916
<body>
917

            
Xdev Host Manager authored 2 days ago
918
  <!-- ── Login screen ── -->
919
  <div id="login-screen">
920
    <div class="login-card">
921
      <div class="brand">
922
        <div class="icon">
923
          <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
924
            <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"/>
925
            <circle cx="17" cy="7" r="1"/><circle cx="19.5" cy="7" r="1"/>
926
            <circle cx="17" cy="15.5" r="1"/><circle cx="19.5" cy="15.5" r="1"/>
927
          </svg>
928
        </div>
929
        <h1>Host Manager</h1>
930
        <p>madagascar.xdev.ro</p>
Xdev Host Manager authored 2 days ago
931
      </div>
Xdev Host Manager authored 2 days ago
932
      <form id="login-form">
933
        <div class="field-label">Cod Authenticator</div>
934
        <div class="otp-row">
935
          <input type="text" inputmode="numeric" maxlength="1" pattern="[0-9]" class="otp-digit" autocomplete="one-time-code">
936
          <input type="text" inputmode="numeric" maxlength="1" pattern="[0-9]" class="otp-digit">
937
          <input type="text" inputmode="numeric" maxlength="1" pattern="[0-9]" class="otp-digit">
938
          <input type="text" inputmode="numeric" maxlength="1" pattern="[0-9]" class="otp-digit">
939
          <input type="text" inputmode="numeric" maxlength="1" pattern="[0-9]" class="otp-digit">
940
          <input type="text" inputmode="numeric" maxlength="1" pattern="[0-9]" class="otp-digit">
941
        </div>
942
        <button class="primary" type="submit" id="login-btn">Autentifică-te</button>
943
      </form>
944
      <div id="login-error"></div>
945
    </div>
946
  </div>
947

            
948
  <!-- ── App (shown after login) ── -->
949
  <div id="app">
950
    <header>
951
      <h1>Host Manager</h1>
952
      <div class="header-right">
953
        <span class="muted" id="app-updated"></span>
954
        <button type="button" id="logout">Logout</button>
Xdev Host Manager authored 2 days ago
955
      </div>
Xdev Host Manager authored 2 days ago
956
    </header>
957
    <main>
958
      <section class="toolbar">
959
        <button id="refresh">Refresh</button>
960
        <a class="linkbtn" href="/download/hosts.yaml">hosts.yaml</a>
961
        <a class="linkbtn" href="/download/local-hosts.tsv">local-hosts.tsv</a>
962
        <a class="linkbtn" href="/download/monitoring.json">monitoring.json</a>
963
        <button id="write-tsv">Write local-hosts.tsv</button>
964
        <span id="message" class="muted"></span>
965
      </section>
966

            
967
      <section class="panel">
968
        <div class="panel-head">
969
          <h2>Overview</h2>
970
          <div class="stats" id="stats"></div>
971
        </div>
972
        <div class="problems" id="problems"></div>
973
      </section>
Xdev Host Manager authored 2 days ago
974

            
Xdev Host Manager authored 2 days ago
975
      <section class="panel">
976
        <div class="panel-head">
977
          <h2>Hosts</h2>
978
          <input id="filter" placeholder="filter" style="max-width: 240px">
Xdev Host Manager authored 2 days ago
979
        </div>
Xdev Host Manager authored 2 days ago
980
        <div class="table-wrap">
981
          <table>
982
            <thead>
983
              <tr>
984
                <th style="width: 120px">ID</th>
985
                <th style="width: 130px">hosts_ip</th>
986
                <th style="width: 130px">dns_ip</th>
987
                <th>Names</th>
988
                <th style="width: 150px">Roles</th>
989
                <th style="width: 110px">Monitoring</th>
990
                <th style="width: 90px">Status</th>
991
              </tr>
992
            </thead>
993
            <tbody id="hosts"></tbody>
994
          </table>
995
        </div>
996
      </section>
997

            
998
      <section class="panel">
999
        <div class="panel-head">
1000
          <h2>Edit host</h2>
1001
        </div>
1002
        <form id="host-form" class="grid">
1003
          <label>ID<input name="id" required></label>
1004
          <label>Status<select name="status"><option>active</option><option>planned</option><option>retired</option></select></label>
1005
          <label>hosts_ip<input name="hosts_ip" required></label>
1006
          <label>dns_ip<input name="dns_ip" required></label>
1007
          <label class="span2">Names<textarea name="names" required></textarea></label>
1008
          <label>Roles<input name="roles"></label>
1009
          <label>Sources<input name="sources"></label>
1010
          <label>Monitoring<select name="monitoring"><option>pending</option><option>enabled</option><option>disabled</option></select></label>
1011
          <label>Notes<input name="notes"></label>
1012
          <div class="span2">
1013
            <button class="primary" type="submit">Save host</button>
1014
            <button class="danger" type="button" id="delete-host">Delete host</button>
1015
          </div>
1016
        </form>
1017
      </section>
1018
    </main>
1019
  </div>
1020

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

            
1024
    const $ = (id) => document.getElementById(id);
1025
    const msg = (text) => { $('message').textContent = text || ''; };
1026

            
1027
    async function api(path, options = {}) {
1028
      const res = await fetch(path, options);
1029
      const body = await res.json();
1030
      if (!res.ok) throw new Error(body.error || res.statusText);
1031
      return body;
1032
    }
1033

            
Xdev Host Manager authored 2 days ago
1034
    function showLogin(errorText) {
1035
      $('app').style.display = 'none';
1036
      $('login-screen').style.display = 'flex';
1037
      $('login-error').textContent = errorText || '';
1038
      document.querySelectorAll('.otp-digit').forEach(i => { i.value = ''; i.classList.remove('filled'); });
1039
      const first = document.querySelector('.otp-digit');
1040
      if (first) first.focus();
1041
    }
1042

            
1043
    function showApp() {
1044
      $('login-screen').style.display = 'none';
1045
      $('app').style.display = 'block';
1046
    }
1047

            
Xdev Host Manager authored 2 days ago
1048
    async function refresh() {
1049
      const session = await api('/api/session');
1050
      state.authenticated = session.authenticated;
Xdev Host Manager authored 2 days ago
1051
      if (!state.authenticated) { showLogin(); return; }
1052
      showApp();
Xdev Host Manager authored 2 days ago
1053
      const data = await api('/api/hosts');
1054
      state.hosts = data.hosts || [];
1055
      state.problems = data.problems || [];
1056
      render(data);
1057
    }
1058

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

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

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

            
1071
      renderHosts();
1072
    }
1073

            
1074
    function renderHosts() {
1075
      const filter = $('filter').value.toLowerCase();
1076
      $('hosts').innerHTML = state.hosts
1077
        .filter(h => JSON.stringify(h).toLowerCase().includes(filter))
1078
        .map(h => {
1079
          const problems = state.problems.filter(p => p.host_id === h.id);
1080
          const cls = problems.length ? 'warn' : 'ok';
1081
          return `<tr data-id="${escapeHtml(h.id)}">
1082
            <td><button type="button" data-edit="${escapeHtml(h.id)}">${escapeHtml(h.id)}</button></td>
1083
            <td>${escapeHtml(h.hosts_ip || '')}</td>
1084
            <td>${escapeHtml(h.dns_ip || '')}</td>
1085
            <td>${(h.names || []).map(n => `<span class="pill">${escapeHtml(n)}</span>`).join('')}</td>
1086
            <td>${(h.roles || []).map(n => `<span class="pill">${escapeHtml(n)}</span>`).join('')}</td>
1087
            <td><span class="pill ${cls}">${escapeHtml(h.monitoring || '')}</span></td>
1088
            <td>${escapeHtml(h.status || '')}</td>
1089
          </tr>`;
1090
        }).join('');
1091
      document.querySelectorAll('[data-edit]').forEach(button => button.addEventListener('click', () => editHost(button.dataset.edit)));
1092
    }
1093

            
1094
    function editHost(id) {
1095
      const host = state.hosts.find(h => h.id === id);
1096
      if (!host) return;
1097
      const form = $('host-form');
1098
      for (const key of ['id', 'status', 'hosts_ip', 'dns_ip', 'monitoring', 'notes']) form.elements[key].value = host[key] || '';
1099
      form.elements.names.value = (host.names || []).join('\n');
1100
      form.elements.roles.value = (host.roles || []).join(' ');
1101
      form.elements.sources.value = (host.sources || []).join(' ');
Xdev Host Manager authored 2 days ago
1102
      form.scrollIntoView({ behavior: 'smooth', block: 'start' });
Xdev Host Manager authored 2 days ago
1103
    }
1104

            
1105
    function formObject(form) {
1106
      return Object.fromEntries(new FormData(form).entries());
1107
    }
1108

            
1109
    function escapeHtml(value) {
1110
      return value.replace(/[&<>"']/g, ch => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#039;'}[ch]));
1111
    }
1112

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

            
1117
    otpDigits.forEach((input, idx) => {
1118
      input.addEventListener('keydown', (e) => {
1119
        if (e.key === 'Backspace') {
1120
          if (input.value) { input.value = ''; input.classList.remove('filled'); }
1121
          else if (idx > 0) { otpDigits[idx - 1].value = ''; otpDigits[idx - 1].classList.remove('filled'); otpDigits[idx - 1].focus(); }
1122
          e.preventDefault();
1123
        }
1124
      });
1125
      input.addEventListener('input', (e) => {
1126
        const val = input.value.replace(/\D/g, '').slice(-1);
1127
        input.value = val;
1128
        input.classList.toggle('filled', !!val);
1129
        if (val && idx < otpDigits.length - 1) otpDigits[idx + 1].focus();
1130
        if (val && idx === otpDigits.length - 1) $('login-form').requestSubmit();
1131
      });
1132
      input.addEventListener('paste', (e) => {
1133
        const text = (e.clipboardData || window.clipboardData).getData('text').replace(/\D/g, '');
1134
        e.preventDefault();
1135
        text.split('').slice(0, otpDigits.length).forEach((ch, i) => {
1136
          otpDigits[i].value = ch;
1137
          otpDigits[i].classList.add('filled');
1138
        });
1139
        const next = Math.min(text.length, otpDigits.length - 1);
1140
        otpDigits[next].focus();
1141
        if (text.length >= otpDigits.length) $('login-form').requestSubmit();
1142
      });
1143
    });
1144

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

            
1148
    $('login-form').addEventListener('submit', async (event) => {
1149
      event.preventDefault();
Xdev Host Manager authored 2 days ago
1150
      const btn = $('login-btn');
1151
      btn.disabled = true;
1152
      $('login-error').textContent = '';
Xdev Host Manager authored 2 days ago
1153
      try {
Xdev Host Manager authored 2 days ago
1154
        await api('/api/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ otp: getOtp() }) });
Xdev Host Manager authored 2 days ago
1155
        await refresh();
Xdev Host Manager authored 2 days ago
1156
      } catch (e) {
1157
        showLogin(e.message === 'invalid_otp' ? 'Cod incorect.' : e.message);
1158
      } finally {
1159
        btn.disabled = false;
1160
      }
Xdev Host Manager authored 2 days ago
1161
    });
1162

            
1163
    $('logout').addEventListener('click', async () => {
1164
      await api('/api/logout', { method: 'POST' }).catch(() => {});
Xdev Host Manager authored 2 days ago
1165
      clearOtp();
1166
      showLogin();
Xdev Host Manager authored 2 days ago
1167
    });
1168

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

            
Xdev Host Manager authored 2 days ago
1172
    $('host-form').addEventListener('submit', async (event) => {
1173
      event.preventDefault();
1174
      try {
1175
        await api('/api/hosts/upsert', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(formObject(event.target)) });
1176
        msg('host saved');
1177
        await refresh();
1178
      } catch (e) { msg(e.message); }
1179
    });
1180

            
1181
    $('delete-host').addEventListener('click', async () => {
1182
      const id = $('host-form').elements.id.value;
1183
      if (!id || !confirm(`Delete ${id}?`)) return;
1184
      try {
1185
        await api('/api/hosts/delete', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id }) });
1186
        $('host-form').reset();
1187
        msg('host deleted');
1188
        await refresh();
1189
      } catch (e) { msg(e.message); }
1190
    });
1191

            
1192
    $('write-tsv').addEventListener('click', async () => {
1193
      if (!confirm('Write config/local-hosts.tsv from hosts.yaml?')) return;
1194
      try {
1195
        await api('/api/render/local-hosts-tsv', { method: 'POST' });
1196
        msg('local-hosts.tsv written');
1197
      } catch (e) { msg(e.message); }
1198
    });
1199

            
Xdev Host Manager authored 2 days ago
1200
    refresh().catch(() => showLogin());
Xdev Host Manager authored 2 days ago
1201
  </script>
1202
</body>
1203
</html>
1204
HTML
1205
}