2 contributors
#!/usr/bin/env perl
#
# host_manager.pl - Minimal host registry web app with no CPAN dependencies.
#
use strict;
use warnings;
use Cwd qw(abs_path);
use Digest::SHA qw(hmac_sha1 hmac_sha256_hex sha256_hex);
use File::Basename qw(dirname);
use File::Path qw(make_path);
use IO::Socket::INET;
use POSIX qw(strftime);
use Time::HiRes qw(time);
my $script_dir = dirname(abs_path($0));
my $project_dir = dirname($script_dir);
my %opt = (
bind => $ENV{HOST_MANAGER_BIND} || '127.0.0.1',
port => $ENV{HOST_MANAGER_PORT} || 8088,
data => $ENV{HOST_MANAGER_DATA} || "$project_dir/config/hosts.yaml",
local_hosts_tsv => $ENV{HOST_MANAGER_LOCAL_HOSTS_TSV} || "$project_dir/config/local-hosts.tsv",
work_orders => $ENV{HOST_MANAGER_WORK_ORDERS} || "$project_dir/config/work-orders.yaml",
);
while (@ARGV) {
my $arg = shift @ARGV;
if ($arg eq '--bind') {
$opt{bind} = shift @ARGV;
} elsif ($arg eq '--port') {
$opt{port} = shift @ARGV;
} elsif ($arg eq '--data') {
$opt{data} = shift @ARGV;
} elsif ($arg eq '--local-hosts-tsv') {
$opt{local_hosts_tsv} = shift @ARGV;
} elsif ($arg eq '--work-orders') {
$opt{work_orders} = shift @ARGV;
} elsif ($arg eq '--help' || $arg eq '-h') {
usage();
exit 0;
} else {
die "Unknown option: $arg\n";
}
}
my $session_secret = $ENV{HOST_MANAGER_SESSION_SECRET} || random_hex(32);
my %sessions;
my $server = IO::Socket::INET->new(
LocalHost => $opt{bind},
LocalPort => $opt{port},
Proto => 'tcp',
Listen => 10,
ReuseAddr => 1,
) or die "Cannot listen on $opt{bind}:$opt{port}: $!\n";
print "host-manager listening on http://$opt{bind}:$opt{port}\n";
print "data file: $opt{data}\n";
print "OTP login: " . ($ENV{HOST_MANAGER_TOTP_SECRET} ? "enabled\n" : "disabled; set HOST_MANAGER_TOTP_SECRET\n");
while (my $client = $server->accept) {
eval {
$client->autoflush(1);
handle_client($client);
};
if ($@) {
eval { send_json($client, 500, { error => 'internal_error', detail => "$@" }); };
}
close $client;
}
sub usage {
print <<"EOF";
Usage: perl scripts/host_manager.pl [--bind 127.0.0.1] [--port 8088]
Environment:
HOST_MANAGER_TOTP_SECRET Base32 TOTP secret required for write access.
HOST_MANAGER_SESSION_SECRET Optional session signing secret.
HOST_MANAGER_DATA Defaults to config/hosts.yaml.
HOST_MANAGER_LOCAL_HOSTS_TSV Defaults to config/local-hosts.tsv.
HOST_MANAGER_WORK_ORDERS Defaults to config/work-orders.yaml.
The nginx vhost keeps registry, CA, work order and download endpoints behind OTP.
EOF
}
sub handle_client {
my ($client) = @_;
my $request_line = <$client>;
return unless defined $request_line;
$request_line =~ s/\r?\n$//;
my ($method, $target) = $request_line =~ m{^([A-Z]+)\s+(\S+)\s+HTTP/};
return send_text($client, 400, 'bad request') unless $method && $target;
my %headers;
while (my $line = <$client>) {
$line =~ s/\r?\n$//;
last if $line eq '';
my ($k, $v) = split /:\s*/, $line, 2;
$headers{lc $k} = $v if defined $k && defined $v;
}
my $body = '';
if (($headers{'content-length'} || 0) > 0) {
read($client, $body, int($headers{'content-length'}));
}
my ($path, $query) = split /\?/, $target, 2;
my %query = parse_params($query || '');
if ($method eq 'GET' && app_page_path($path)) {
return send_html($client, 200, app_html());
}
if ($method eq 'GET' && $path eq '/healthz') {
return send_json($client, 200, { ok => json_bool(1) });
}
if ($method eq 'GET' && $path eq '/api/session') {
return send_json($client, 200, { authenticated => is_authenticated(\%headers) ? json_bool(1) : json_bool(0) });
}
if ($method eq 'POST' && $path eq '/api/login') {
return send_json($client, 503, { error => 'otp_not_configured' }) unless $ENV{HOST_MANAGER_TOTP_SECRET};
my $payload = request_payload(\%headers, $body);
my $otp = $payload->{otp} || '';
if (!verify_totp($ENV{HOST_MANAGER_TOTP_SECRET} || '', $otp)) {
return send_json($client, 401, { error => 'invalid_otp' });
}
my $token = create_session();
return send_json($client, 200, { ok => json_bool(1) }, [ "Set-Cookie: hm_session=$token; HttpOnly; SameSite=Strict; Path=/" ]);
}
if ($method eq 'POST' && $path eq '/api/logout') {
expire_session(\%headers);
return send_json($client, 200, { ok => json_bool(1) }, [ "Set-Cookie: hm_session=deleted; Max-Age=0; Path=/" ]);
}
return send_json($client, 401, { error => 'authentication_required' }) unless is_authenticated(\%headers);
if ($method eq 'GET' && $path eq '/api/hosts') {
my $registry = load_registry();
return send_json($client, 200, registry_payload($registry));
}
if ($method eq 'GET' && $path eq '/api/work-orders') {
return send_json($client, 200, work_orders_payload(load_work_orders()));
}
if ($method eq 'GET' && $path eq '/download/hosts.yaml') {
return send_file($client, $opt{data}, 'application/x-yaml; charset=utf-8', 'hosts.yaml');
}
if ($method eq 'GET' && $path eq '/download/local-hosts.tsv') {
my $registry = load_registry();
return send_download($client, 200, render_local_hosts_tsv($registry), 'text/tab-separated-values; charset=utf-8', 'local-hosts.tsv');
}
if ($method eq 'GET' && $path eq '/download/monitoring.json') {
my $registry = load_registry();
return send_download($client, 200, json_encode(render_monitoring($registry)), 'application/json; charset=utf-8', 'monitoring-hosts.json');
}
if ($method eq 'GET' && $path eq '/api/ca/status') {
return send_json_raw($client, 200, ca_manager_json('status-json'));
}
if ($method eq 'GET' && $path eq '/api/ca/certificates') {
return send_json_raw($client, 200, ca_manager_json('list-json'));
}
if ($method eq 'GET' && $path eq '/download/ca.crt') {
return send_file($client, ca_cert_path(), 'application/x-pem-file; charset=utf-8', 'xdev-madagascar-host-ca.crt');
}
if ($method eq 'GET' && $path =~ m{\A/download/ca/cert/([A-Za-z0-9_.-]+)\.crt\z}) {
my $name = $1;
return send_file($client, ca_issued_cert_path($name), 'application/x-pem-file; charset=utf-8', "$name.crt");
}
if ($method eq 'POST' && $path =~ m{^/api/}) {
if ($path eq '/api/hosts/upsert') {
my $payload = request_payload(\%headers, $body);
return upsert_host($client, $payload);
}
if ($path eq '/api/hosts/delete') {
my $payload = request_payload(\%headers, $body);
return delete_host($client, $payload->{id} || '');
}
if ($path eq '/api/work-orders/confirm') {
my $payload = request_payload(\%headers, $body);
return confirm_work_order($client, $payload);
}
if ($path eq '/api/work-orders/checklist') {
my $payload = request_payload(\%headers, $body);
return update_work_order_checklist($client, $payload);
}
if ($path eq '/api/render/local-hosts-tsv') {
my $registry = load_registry();
my $content = render_local_hosts_tsv($registry);
backup_file($opt{local_hosts_tsv});
write_file($opt{local_hosts_tsv}, $content);
return send_json($client, 200, { ok => json_bool(1), file => $opt{local_hosts_tsv} });
}
}
return send_json($client, 404, { error => 'not_found' });
}
sub app_page_path {
my ($path) = @_;
return $path =~ m{\A/(?:|overview|hosts|dns|work-orders|ca)\z};
}
sub load_registry {
return parse_hosts_yaml(read_file($opt{data}));
}
sub save_registry {
my ($registry) = @_;
$registry->{updated_at} = iso_now();
backup_file($opt{data});
write_file($opt{data}, render_hosts_yaml($registry));
}
sub load_work_orders {
return { version => 1, work_orders => [] } unless -f $opt{work_orders};
return parse_work_orders_yaml(read_file($opt{work_orders}));
}
sub save_work_orders {
my ($orders) = @_;
backup_file($opt{work_orders});
write_file($opt{work_orders}, render_work_orders_yaml($orders));
}
sub work_orders_payload {
my ($orders) = @_;
my $pending = 0;
for my $wo (@{ $orders->{work_orders} || [] }) {
$pending++ if ($wo->{status} || 'pending') eq 'pending';
}
return {
version => $orders->{version},
work_orders => $orders->{work_orders} || [],
counts => {
work_orders => scalar @{ $orders->{work_orders} || [] },
pending => $pending,
},
};
}
sub confirm_work_order {
my ($client, $payload) = @_;
my $id = clean_scalar($payload->{id} || '');
return send_json($client, 400, { error => 'invalid_work_order_id' }) unless $id =~ /\AWO-[A-Za-z0-9_.-]+\z/;
return send_json($client, 400, { error => 'confirmation_required' }) unless clean_scalar($payload->{confirm} || '') eq $id;
my $orders = load_work_orders();
my $work_order;
for my $wo (@{ $orders->{work_orders} || [] }) {
if (($wo->{id} || '') eq $id) {
$work_order = $wo;
last;
}
}
return send_json($client, 404, { error => 'work_order_not_found' }) unless $work_order;
return send_json($client, 409, { error => 'work_order_not_pending' }) unless ($work_order->{status} || 'pending') eq 'pending';
my $incomplete = incomplete_work_order_items($work_order);
return send_json($client, 409, {
error => 'work_order_incomplete',
incomplete => $incomplete,
}) if @$incomplete;
my $registry = load_registry();
my $results = apply_work_order($registry, $work_order);
$work_order->{status} = 'confirmed';
$work_order->{confirmed_at} = iso_now();
$work_order->{result} = scalar(@$results) . ' action(s) applied';
save_registry($registry);
save_work_orders($orders);
backup_file($opt{local_hosts_tsv});
write_file($opt{local_hosts_tsv}, render_local_hosts_tsv($registry));
return send_json($client, 200, {
ok => json_bool(1),
work_order => $work_order,
results => $results,
local_hosts_tsv => $opt{local_hosts_tsv},
});
}
sub update_work_order_checklist {
my ($client, $payload) = @_;
my $id = clean_scalar($payload->{id} || '');
my $item_id = clean_scalar($payload->{item_id} || '');
my $status = clean_scalar($payload->{status} || '');
my $notes = clean_scalar($payload->{notes} || '');
return send_json($client, 400, { error => 'invalid_work_order_id' }) unless $id =~ /\AWO-[A-Za-z0-9_.-]+\z/;
return send_json($client, 400, { error => 'invalid_checklist_item' }) unless $item_id =~ /\A[A-Za-z0-9_.-]+\z/;
return send_json($client, 400, { error => 'invalid_checklist_status' }) unless $status =~ /\A(?:pending|done|blocked)\z/;
my $orders = load_work_orders();
my $work_order;
for my $wo (@{ $orders->{work_orders} || [] }) {
if (($wo->{id} || '') eq $id) {
$work_order = $wo;
last;
}
}
return send_json($client, 404, { error => 'work_order_not_found' }) unless $work_order;
return send_json($client, 409, { error => 'work_order_not_pending' }) unless ($work_order->{status} || 'pending') eq 'pending';
my $item;
for my $candidate (@{ $work_order->{checklist} || [] }) {
if (($candidate->{id} || '') eq $item_id) {
$item = $candidate;
last;
}
}
return send_json($client, 404, { error => 'checklist_item_not_found' }) unless $item;
$item->{status} = $status;
$item->{updated_at} = iso_now();
$item->{notes} = $notes if length $notes;
save_work_orders($orders);
return send_json($client, 200, { ok => json_bool(1), work_order => $work_order });
}
sub incomplete_work_order_items {
my ($work_order) = @_;
my @incomplete;
for my $item (@{ $work_order->{checklist} || [] }) {
push @incomplete, $item unless ($item->{status} || 'pending') eq 'done';
}
return \@incomplete;
}
sub apply_work_order {
my ($registry, $work_order) = @_;
my @results;
for my $action (@{ $work_order->{actions} || [] }) {
my $type = $action->{type} || '';
if ($type eq 'remove_name') {
my $host_id = $action->{host_id} || '';
my $name = $action->{name} || '';
my $removed = 0;
for my $host (@{ $registry->{hosts} || [] }) {
next unless ($host->{id} || '') eq $host_id;
my @kept = grep { $_ ne $name } @{ $host->{names} || [] };
$removed = @kept != @{ $host->{names} || [] };
$host->{names} = \@kept;
last;
}
push @results, {
type => $type,
host_id => $host_id,
name => $name,
removed => json_bool($removed),
};
} else {
die "Unsupported work order action: $type\n";
}
}
return \@results;
}
sub registry_payload {
my ($registry) = @_;
my $problems = analyze_hosts($registry->{hosts});
my @hosts = map { host_payload($_) } @{ $registry->{hosts} };
return {
version => $registry->{version},
updated_at => $registry->{updated_at},
policy => $registry->{policy},
hosts => \@hosts,
problems => $problems,
counts => {
hosts => scalar @{ $registry->{hosts} },
problems => scalar @$problems,
},
};
}
sub upsert_host {
my ($client, $payload) = @_;
my $id = clean_id($payload->{id} || '');
return send_json($client, 400, { error => 'invalid_id' }) unless $id;
my $hosts_ip = clean_scalar($payload->{hosts_ip} || '');
my $dns_ip = clean_scalar($payload->{dns_ip} || '');
return send_json($client, 400, { error => 'missing_ip' }) unless $hosts_ip && $dns_ip;
my @names = remove_derived_names(clean_list($payload->{names}));
return send_json($client, 400, { error => 'missing_names' }) unless @names;
my $registry = load_registry();
my %host = (
id => $id,
status => clean_scalar($payload->{status} || 'active'),
hosts_ip => $hosts_ip,
dns_ip => $dns_ip,
names => \@names,
roles => [ clean_list($payload->{roles}) ],
sources => [ clean_list($payload->{sources}) ],
monitoring => clean_scalar($payload->{monitoring} || 'pending'),
notes => clean_scalar($payload->{notes} || ''),
);
my $replaced = 0;
for my $i (0 .. $#{ $registry->{hosts} }) {
if ($registry->{hosts}->[$i]{id} eq $id) {
$registry->{hosts}->[$i] = \%host;
$replaced = 1;
last;
}
}
push @{ $registry->{hosts} }, \%host unless $replaced;
save_registry($registry);
return send_json($client, 200, { ok => json_bool(1), host => \%host });
}
sub delete_host {
my ($client, $id) = @_;
$id = clean_id($id);
return send_json($client, 400, { error => 'invalid_id' }) unless $id;
my $registry = load_registry();
my @kept = grep { $_->{id} ne $id } @{ $registry->{hosts} };
return send_json($client, 404, { error => 'not_found' }) if @kept == @{ $registry->{hosts} };
$registry->{hosts} = \@kept;
save_registry($registry);
return send_json($client, 200, { ok => json_bool(1) });
}
sub analyze_hosts {
my ($hosts) = @_;
my @problems;
my (%names, %ids);
for my $host (@$hosts) {
push @problems, problem($host, 'duplicate-id', "Duplicate id $host->{id}") if $ids{ $host->{id} }++;
my @fqdn = grep { /\.madagascar\.xdev\.ro$/ } @{ $host->{names} || [] };
push @problems, problem($host, 'missing-fqdn', 'No madagascar.xdev.ro FQDN') unless @fqdn || ($host->{status} || '') ne 'active';
push @problems, problem($host, 'deprecated-vad-is', 'Deprecated vad.is.xdev.ro name present')
if grep { /\.vad\.is\.xdev\.ro$/ } @{ $host->{names} || [] };
push @problems, problem($host, 'legacy-prefix', 'Legacy prefix should be normalized out')
if grep { /^(is|vad|b)-/ } @{ $host->{names} || [] };
for my $name (@{ $host->{names} || [] }) {
push @problems, problem($host, 'duplicate-name', "Duplicate name $name") if $names{$name}++;
}
my %declared = map { $_ => 1 } @{ $host->{names} || [] };
for my $derived (derived_names($host)) {
push @problems, problem($host, 'redundant-derived-name', "Name $derived is derived from madagascar.xdev.ro")
if $declared{$derived};
}
if (($host->{hosts_ip} || '') ne ($host->{dns_ip} || '') && ($host->{hosts_ip} || '') ne '127.0.0.1') {
push @problems, problem($host, 'split-ip', 'hosts_ip differs from dns_ip; check that this is intentional');
}
}
return \@problems;
}
sub host_payload {
my ($host) = @_;
my %copy = %$host;
$copy{names} = [ effective_names($host) ];
$copy{declared_names} = [ @{ $host->{names} || [] } ];
$copy{derived_names} = [ derived_names($host) ];
return \%copy;
}
sub effective_names {
my ($host) = @_;
my @names = @{ $host->{names} || [] };
push @names, derived_names($host);
return unique_preserve(@names);
}
sub derived_names {
my ($host) = @_;
my @derived;
for my $name (@{ $host->{names} || [] }) {
next unless $name =~ /^(.+)\.madagascar\.xdev\.ro$/;
push @derived, $1 if length $1;
}
return unique_preserve(@derived);
}
sub remove_derived_names {
my @names = @_;
my %derived;
for my $name (@names) {
next unless $name =~ /^(.+)\.madagascar\.xdev\.ro$/;
$derived{$1} = 1;
}
return grep { !$derived{$_} } @names;
}
sub unique_preserve {
my @values = @_;
my %seen;
return grep { !$seen{$_}++ } @values;
}
sub problem {
my ($host, $code, $message) = @_;
return { host_id => $host->{id}, code => $code, message => $message };
}
sub render_local_hosts_tsv {
my ($registry) = @_;
my $out = "# Local DNS manifest for the madagascar network.\n";
$out .= "# Generated by scripts/host_manager.pl from config/hosts.yaml.\n";
$out .= "#\n";
$out .= "# Format:\n";
$out .= "# hosts_ip<TAB>dns_ip<TAB>name [aliases...]\n";
$out .= "#\n";
$out .= "# Priority rule:\n";
$out .= "# - DHCP lease/reservation on 192.168.2.1 is canonical for LAN IP allocation.\n";
$out .= "# - madagascar.json is canonical for cluster roles and service interfaces.\n";
$out .= "# - This file publishes approved local DNS records derived from those sources.\n";
for my $host (sort { $a->{id} cmp $b->{id} } @{ $registry->{hosts} }) {
next unless ($host->{status} || 'active') eq 'active';
my @names = effective_names($host);
next unless @names;
$out .= join("\t", $host->{hosts_ip}, $host->{dns_ip}, join(' ', @names)) . "\n";
}
return $out;
}
sub render_monitoring {
my ($registry) = @_;
my @hosts;
for my $host (sort { $a->{id} cmp $b->{id} } @{ $registry->{hosts} }) {
next unless ($host->{status} || 'active') eq 'active';
next if ($host->{monitoring} || 'pending') eq 'disabled';
my @names = effective_names($host);
push @hosts, {
id => $host->{id},
primary_name => $names[0],
address => $host->{dns_ip},
aliases => \@names,
declared_names => [ @{ $host->{names} || [] } ],
derived_names => [ derived_names($host) ],
roles => [ @{ $host->{roles} || [] } ],
monitoring => $host->{monitoring} || 'pending',
notes => $host->{notes} || '',
};
}
return {
version => $registry->{version},
generated_at => iso_now(),
source => 'config/hosts.yaml',
hosts => \@hosts,
};
}
sub ca_script_path {
return "$project_dir/scripts/ca_manager.sh";
}
sub ca_dir {
return $ENV{HOST_MANAGER_CA_DIR} || "$project_dir/var/ca";
}
sub ca_cert_path {
return ca_dir() . "/certs/ca.cert.pem";
}
sub ca_issued_cert_path {
my ($name) = @_;
die "unsafe certificate name\n" unless $name =~ /\A[A-Za-z0-9_.-]+\z/;
return ca_dir() . "/issued/$name.cert.pem";
}
sub ca_manager_json {
my ($command) = @_;
my $script = ca_script_path();
die "CA manager script is missing\n" unless -x $script;
local $ENV{HOST_MANAGER_CA_DIR} = ca_dir();
open my $fh, '-|', $script, $command or die "Cannot run CA manager\n";
local $/;
my $out = <$fh>;
close $fh or die "CA manager failed\n";
return $out || '{}';
}
sub parse_hosts_yaml {
my ($text) = @_;
my %registry = (
version => 1,
updated_at => '',
policy => {},
hosts => [],
);
my ($section, $current, $list_key);
for my $line (split /\n/, $text) {
next if $line =~ /^\s*$/ || $line =~ /^\s*#/;
if ($line =~ /^version:\s*(\d+)/) {
$registry{version} = int($1);
} elsif ($line =~ /^updated_at:\s*(.+)$/) {
$registry{updated_at} = yaml_unquote($1);
} elsif ($line =~ /^policy:\s*$/) {
$section = 'policy';
} elsif ($line =~ /^hosts:\s*$/) {
$section = 'hosts';
} elsif (($section || '') eq 'policy' && $line =~ /^ ([A-Za-z0-9_]+):\s*(.+)$/) {
$registry{policy}{$1} = yaml_unquote($2);
} elsif (($section || '') eq 'hosts' && $line =~ /^ - id:\s*(.+)$/) {
$current = {
id => yaml_unquote($1),
status => 'active',
hosts_ip => '',
dns_ip => '',
names => [],
roles => [],
sources => [],
monitoring => 'pending',
notes => '',
};
push @{ $registry{hosts} }, $current;
$list_key = undef;
} elsif ($current && $line =~ /^ ([A-Za-z0-9_]+):\s*$/) {
$list_key = $1;
$current->{$list_key} ||= [];
} elsif ($current && defined $list_key && $line =~ /^ -\s*(.+)$/) {
push @{ $current->{$list_key} }, yaml_unquote($1);
} elsif ($current && $line =~ /^ ([A-Za-z0-9_]+):\s*(.*)$/) {
$current->{$1} = yaml_unquote($2);
$list_key = undef;
}
}
return \%registry;
}
sub render_hosts_yaml {
my ($registry) = @_;
my $out = "version: " . int($registry->{version} || 1) . "\n";
$out .= "updated_at: " . yq($registry->{updated_at} || iso_now()) . "\n";
$out .= "policy:\n";
for my $key (sort keys %{ $registry->{policy} || {} }) {
$out .= " $key: " . yq($registry->{policy}{$key}) . "\n";
}
$out .= "hosts:\n";
for my $host (sort { $a->{id} cmp $b->{id} } @{ $registry->{hosts} || [] }) {
$out .= " - id: " . yq($host->{id}) . "\n";
for my $key (qw(status hosts_ip dns_ip)) {
$out .= " $key: " . yq($host->{$key} || '') . "\n";
}
for my $key (qw(names roles sources)) {
$out .= " $key:\n";
for my $value (@{ $host->{$key} || [] }) {
$out .= " - " . yq($value) . "\n";
}
}
$out .= " monitoring: " . yq($host->{monitoring} || 'pending') . "\n";
$out .= " notes: " . yq($host->{notes} || '') . "\n";
}
return $out;
}
sub parse_work_orders_yaml {
my ($text) = @_;
my %orders = (
version => 1,
work_orders => [],
);
my ($section, $current, $list_section, $current_action, $current_item);
for my $line (split /\n/, $text) {
next if $line =~ /^\s*$/ || $line =~ /^\s*#/;
if ($line =~ /^version:\s*(\d+)/) {
$orders{version} = int($1);
} elsif ($line =~ /^work_orders:\s*$/) {
$section = 'work_orders';
} elsif (($section || '') eq 'work_orders' && $line =~ /^ - id:\s*(.+)$/) {
$current = {
id => yaml_unquote($1),
status => 'pending',
checklist => [],
actions => [],
};
push @{ $orders{work_orders} }, $current;
$list_section = '';
$current_action = undef;
$current_item = undef;
} elsif ($current && $line =~ /^ checklist:\s*$/) {
$list_section = 'checklist';
$current->{checklist} ||= [];
} elsif ($current && $list_section eq 'checklist' && $line =~ /^ - id:\s*(.+)$/) {
$current_item = { id => yaml_unquote($1), status => 'pending' };
push @{ $current->{checklist} }, $current_item;
$current_action = undef;
} elsif ($current_item && $list_section eq 'checklist' && $line =~ /^ ([A-Za-z0-9_]+):\s*(.*)$/) {
$current_item->{$1} = yaml_unquote($2);
} elsif ($current && $line =~ /^ actions:\s*$/) {
$list_section = 'actions';
$current->{actions} ||= [];
} elsif ($current && $list_section eq 'actions' && $line =~ /^ - type:\s*(.+)$/) {
$current_action = { type => yaml_unquote($1) };
push @{ $current->{actions} }, $current_action;
$current_item = undef;
} elsif ($current_action && $list_section eq 'actions' && $line =~ /^ ([A-Za-z0-9_]+):\s*(.*)$/) {
$current_action->{$1} = yaml_unquote($2);
} elsif ($current && $line =~ /^ ([A-Za-z0-9_]+):\s*(.*)$/) {
$current->{$1} = yaml_unquote($2);
$list_section = '';
$current_action = undef;
$current_item = undef;
}
}
return \%orders;
}
sub render_work_orders_yaml {
my ($orders) = @_;
my $out = "version: " . int($orders->{version} || 1) . "\n";
$out .= "work_orders:\n";
for my $wo (@{ $orders->{work_orders} || [] }) {
$out .= " - id: " . yq($wo->{id}) . "\n";
for my $key (qw(status title reason created_at confirmed_at result)) {
next unless exists $wo->{$key} && length($wo->{$key} || '');
$out .= " $key: " . yq($wo->{$key}) . "\n";
}
$out .= " checklist:\n";
for my $item (@{ $wo->{checklist} || [] }) {
$out .= " - id: " . yq($item->{id}) . "\n";
for my $key (qw(text status owner notes updated_at)) {
next unless exists $item->{$key} && length($item->{$key} || '');
$out .= " $key: " . yq($item->{$key}) . "\n";
}
}
$out .= " actions:\n";
for my $action (@{ $wo->{actions} || [] }) {
$out .= " - type: " . yq($action->{type}) . "\n";
for my $key (qw(host_id name)) {
next unless exists $action->{$key} && length($action->{$key} || '');
$out .= " $key: " . yq($action->{$key}) . "\n";
}
}
}
return $out;
}
sub request_payload {
my ($headers, $body) = @_;
my $type = $headers->{'content-type'} || '';
if ($type =~ m{application/json}) {
return json_decode($body || '{}');
}
return { parse_params($body || '') };
}
sub json_bool {
my ($value) = @_;
return bless \(my $bool = $value ? 1 : 0), 'HostManager::JSONBool';
}
sub json_encode {
my ($value) = @_;
if (!defined $value) {
return 'null';
}
my $ref = ref($value);
if (!$ref) {
return $value if $value =~ /\A-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?\z/;
return json_string($value);
}
if ($ref eq 'HostManager::JSONBool') {
return $$value ? 'true' : 'false';
}
if ($ref eq 'ARRAY') {
return '[' . join(',', map { json_encode($_) } @$value) . ']';
}
if ($ref eq 'HASH') {
return '{' . join(',', map { json_string($_) . ':' . json_encode($value->{$_}) } sort keys %$value) . '}';
}
return json_string("$value");
}
sub json_string {
my ($value) = @_;
$value = '' unless defined $value;
$value =~ s/\\/\\\\/g;
$value =~ s/"/\\"/g;
$value =~ s/\n/\\n/g;
$value =~ s/\r/\\r/g;
$value =~ s/\t/\\t/g;
$value =~ s/([\x00-\x1f])/sprintf("\\u%04x", ord($1))/eg;
return qq("$value");
}
sub json_decode {
my ($text) = @_;
my $i = 0;
my $len = length($text);
my ($parse_value, $parse_string, $parse_array, $parse_object, $parse_number, $skip_ws);
$skip_ws = sub {
$i++ while $i < $len && substr($text, $i, 1) =~ /\s/;
};
$parse_string = sub {
die "Expected JSON string\n" unless substr($text, $i, 1) eq '"';
$i++;
my $out = '';
while ($i < $len) {
my $ch = substr($text, $i++, 1);
return $out if $ch eq '"';
if ($ch eq "\\") {
die "Bad JSON escape\n" if $i >= $len;
my $esc = substr($text, $i++, 1);
if ($esc eq '"' || $esc eq "\\" || $esc eq '/') {
$out .= $esc;
} elsif ($esc eq 'b') {
$out .= "\b";
} elsif ($esc eq 'f') {
$out .= "\f";
} elsif ($esc eq 'n') {
$out .= "\n";
} elsif ($esc eq 'r') {
$out .= "\r";
} elsif ($esc eq 't') {
$out .= "\t";
} elsif ($esc eq 'u') {
my $hex = substr($text, $i, 4);
die "Bad JSON unicode escape\n" unless $hex =~ /\A[0-9A-Fa-f]{4}\z/;
$out .= chr(hex($hex));
$i += 4;
} else {
die "Bad JSON escape\n";
}
} else {
$out .= $ch;
}
}
die "Unterminated JSON string\n";
};
$parse_number = sub {
my $start = $i;
$i++ if substr($text, $i, 1) eq '-';
$i++ while $i < $len && substr($text, $i, 1) =~ /[0-9]/;
if ($i < $len && substr($text, $i, 1) eq '.') {
$i++;
$i++ while $i < $len && substr($text, $i, 1) =~ /[0-9]/;
}
if ($i < $len && substr($text, $i, 1) =~ /[eE]/) {
$i++;
$i++ if $i < $len && substr($text, $i, 1) =~ /[+-]/;
$i++ while $i < $len && substr($text, $i, 1) =~ /[0-9]/;
}
return 0 + substr($text, $start, $i - $start);
};
$parse_array = sub {
die "Expected JSON array\n" unless substr($text, $i, 1) eq '[';
$i++;
my @out;
$skip_ws->();
if ($i < $len && substr($text, $i, 1) eq ']') {
$i++;
return \@out;
}
while (1) {
push @out, $parse_value->();
$skip_ws->();
my $ch = substr($text, $i++, 1);
last if $ch eq ']';
die "Expected JSON array comma\n" unless $ch eq ',';
}
return \@out;
};
$parse_object = sub {
die "Expected JSON object\n" unless substr($text, $i, 1) eq '{';
$i++;
my %out;
$skip_ws->();
if ($i < $len && substr($text, $i, 1) eq '}') {
$i++;
return \%out;
}
while (1) {
$skip_ws->();
my $key = $parse_string->();
$skip_ws->();
die "Expected JSON object colon\n" unless substr($text, $i++, 1) eq ':';
$out{$key} = $parse_value->();
$skip_ws->();
my $ch = substr($text, $i++, 1);
last if $ch eq '}';
die "Expected JSON object comma\n" unless $ch eq ',';
}
return \%out;
};
$parse_value = sub {
$skip_ws->();
die "Unexpected end of JSON\n" if $i >= $len;
my $ch = substr($text, $i, 1);
return $parse_string->() if $ch eq '"';
return $parse_object->() if $ch eq '{';
return $parse_array->() if $ch eq '[';
if (substr($text, $i, 4) eq 'true') {
$i += 4;
return json_bool(1);
}
if (substr($text, $i, 5) eq 'false') {
$i += 5;
return json_bool(0);
}
if (substr($text, $i, 4) eq 'null') {
$i += 4;
return undef;
}
return $parse_number->() if $ch =~ /[-0-9]/;
die "Unexpected JSON token\n";
};
my $value = $parse_value->();
$skip_ws->();
die "Trailing JSON content\n" if $i != $len;
return $value;
}
sub parse_params {
my ($text) = @_;
my %out;
for my $pair (split /&/, $text) {
next unless length $pair;
my ($k, $v) = split /=/, $pair, 2;
$out{url_decode($k)} = url_decode($v || '');
}
return %out;
}
sub clean_id {
my ($value) = @_;
$value = lc clean_scalar($value);
$value =~ s/[^a-z0-9_.-]+/-/g;
$value =~ s/^-+|-+$//g;
return $value;
}
sub clean_scalar {
my ($value) = @_;
$value = '' unless defined $value;
$value =~ s/[\r\n\t]+/ /g;
$value =~ s/^\s+|\s+$//g;
return $value;
}
sub clean_list {
my ($value) = @_;
return () unless defined $value;
my @items = ref($value) eq 'ARRAY' ? @$value : split /[\s,]+/, $value;
my @clean;
for my $item (@items) {
$item = clean_scalar($item);
push @clean, $item if length $item;
}
return @clean;
}
sub yq {
my ($value) = @_;
$value = '' unless defined $value;
$value =~ s/\\/\\\\/g;
$value =~ s/"/\\"/g;
return qq("$value");
}
sub yaml_unquote {
my ($value) = @_;
$value = '' unless defined $value;
$value =~ s/^\s+|\s+$//g;
if ($value =~ /^"(.*)"$/) {
$value = $1;
$value =~ s/\\"/"/g;
$value =~ s/\\\\/\\/g;
}
return $value;
}
sub verify_totp {
my ($secret, $otp) = @_;
return 0 unless $secret && $otp =~ /^\d{6}$/;
my $key = eval { base32_decode($secret) };
return 0 if $@ || !length $key;
my $counter = int(time() / 30);
for my $offset (-1, 0, 1) {
return 1 if totp_code($key, $counter + $offset) eq $otp;
}
return 0;
}
sub totp_code {
my ($key, $counter) = @_;
my $msg = pack('NN', int($counter / 4294967296), $counter & 0xffffffff);
my $hash = hmac_sha1($msg, $key);
my $offset = ord(substr($hash, -1)) & 0x0f;
my $bin = unpack('N', substr($hash, $offset, 4)) & 0x7fffffff;
return sprintf('%06d', $bin % 1_000_000);
}
sub base32_decode {
my ($text) = @_;
$text = uc($text || '');
$text =~ s/[^A-Z2-7]//g;
my %map;
my @chars = ('A'..'Z', '2'..'7');
@map{@chars} = (0..31);
my ($bits, $value, $out) = (0, 0, '');
for my $char (split //, $text) {
die "Invalid base32\n" unless exists $map{$char};
$value = ($value << 5) | $map{$char};
$bits += 5;
while ($bits >= 8) {
$bits -= 8;
$out .= chr(($value >> $bits) & 0xff);
}
}
return $out;
}
sub create_session {
my $nonce = random_hex(24);
my $expires = int(time() + 8 * 3600);
my $sig = hmac_sha256_hex("$nonce:$expires", $session_secret);
my $token = "$nonce:$expires:$sig";
$sessions{$token} = $expires;
return $token;
}
sub is_authenticated {
my ($headers) = @_;
my $token = cookie_value($headers->{'cookie'} || '', 'hm_session');
return 0 unless $token;
my ($nonce, $expires, $sig) = split /:/, $token;
return 0 unless $nonce && $expires && $sig;
return 0 if $expires < time();
return 0 unless hmac_sha256_hex("$nonce:$expires", $session_secret) eq $sig;
return exists $sessions{$token};
}
sub expire_session {
my ($headers) = @_;
my $token = cookie_value($headers->{'cookie'} || '', 'hm_session');
delete $sessions{$token} if $token;
}
sub cookie_value {
my ($cookie, $name) = @_;
for my $part (split /;\s*/, $cookie) {
my ($k, $v) = split /=/, $part, 2;
return $v if defined $k && $k eq $name;
}
return '';
}
sub send_json {
my ($client, $status, $payload, $extra_headers) = @_;
return send_response($client, $status, json_encode($payload), 'application/json; charset=utf-8', $extra_headers);
}
sub send_json_raw {
my ($client, $status, $json_body, $extra_headers) = @_;
return send_response($client, $status, $json_body, 'application/json; charset=utf-8', $extra_headers);
}
sub send_html {
my ($client, $status, $html) = @_;
return send_response($client, $status, $html, 'text/html; charset=utf-8');
}
sub send_text {
my ($client, $status, $text) = @_;
return send_response($client, $status, $text, 'text/plain; charset=utf-8');
}
sub send_download {
my ($client, $status, $content, $type, $filename) = @_;
return send_response($client, $status, $content, $type, [ qq(Content-Disposition: attachment; filename="$filename") ]);
}
sub send_file {
my ($client, $path, $type, $filename) = @_;
return send_json($client, 404, { error => 'missing_file' }) unless -f $path;
return send_download($client, 200, read_file($path), $type, $filename);
}
sub send_response {
my ($client, $status, $body, $type, $extra_headers) = @_;
my %reason = (200 => 'OK', 400 => 'Bad Request', 401 => 'Unauthorized', 404 => 'Not Found', 409 => 'Conflict', 500 => 'Internal Server Error', 503 => 'Service Unavailable');
$body = '' unless defined $body;
print $client "HTTP/1.1 $status " . ($reason{$status} || 'OK') . "\r\n";
print $client "Content-Type: $type\r\n";
print $client "Content-Length: " . length($body) . "\r\n";
print $client "Cache-Control: no-store\r\n";
print $client "$_\r\n" for @{ $extra_headers || [] };
print $client "Connection: close\r\n\r\n";
print $client $body;
}
sub read_file {
my ($path) = @_;
open my $fh, '<', $path or die "Cannot read $path: $!";
local $/;
return <$fh>;
}
sub write_file {
my ($path, $content) = @_;
open my $fh, '>', $path or die "Cannot write $path: $!";
print {$fh} $content;
close $fh or die "Cannot close $path: $!";
}
sub backup_file {
my ($path) = @_;
return unless -f $path;
my $backup_dir = "$project_dir/backups/host-manager";
make_path($backup_dir) unless -d $backup_dir;
my $name = $path;
$name =~ s{.*/}{};
my $stamp = strftime('%Y%m%d_%H%M%S', localtime);
write_file("$backup_dir/$name.$stamp.bak", read_file($path));
}
sub url_decode {
my ($value) = @_;
$value = '' unless defined $value;
$value =~ tr/+/ /;
$value =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
return $value;
}
sub random_hex {
my ($bytes) = @_;
if (open my $fh, '<:raw', '/dev/urandom') {
read($fh, my $raw, $bytes);
close $fh;
return unpack('H*', $raw);
}
return sha256_hex(rand() . time() . $$);
}
sub iso_now {
return strftime('%Y-%m-%dT%H:%M:%SZ', gmtime);
}
sub build_info {
my %info = (
revision => '',
branch => '',
built_at => '',
deployed_at => '',
dirty => '',
);
if ($ENV{HOST_MANAGER_BUILD}) {
$info{revision} = clean_scalar($ENV{HOST_MANAGER_BUILD});
return \%info;
}
my $build_file = "$project_dir/BUILD";
if (-f $build_file) {
for my $line (split /\n/, read_file($build_file)) {
next unless $line =~ /\A([A-Za-z0-9_.-]+)=(.*)\z/;
$info{$1} = clean_scalar($2);
}
return \%info if $info{revision} || $info{built_at};
}
my $revision = git_value('rev-parse --short=12 HEAD');
my $branch = git_value('rev-parse --abbrev-ref HEAD');
$info{revision} = $revision if $revision;
$info{branch} = $branch if $branch && $branch ne 'HEAD';
return \%info;
}
sub git_value {
my ($args) = @_;
return '' unless -d "$project_dir/.git";
open my $fh, '-|', "git -C '$project_dir' $args 2>/dev/null" or return '';
my $value = <$fh> || '';
close $fh;
chomp $value;
return clean_scalar($value);
}
sub build_label {
my $info = build_info();
my $revision = $info->{revision} || 'unknown';
my $branch = $info->{branch} || '';
$branch = '' if $branch eq 'HEAD';
my $label = $branch ? "$branch $revision" : $revision;
$label .= '+dirty' if ($info->{dirty} || '') eq '1';
return $label;
}
sub build_title {
my $info = build_info();
my $label = build_label();
my $stamp = $info->{deployed_at} || $info->{built_at} || '';
return $stamp ? "$label deployed $stamp" : $label;
}
sub html_escape {
my ($value) = @_;
$value = '' unless defined $value;
$value =~ s/&/&/g;
$value =~ s/</</g;
$value =~ s/>/>/g;
$value =~ s/"/"/g;
$value =~ s/'/'/g;
return $value;
}
sub app_html {
my $build = html_escape(build_label());
my $build_title = html_escape(build_title());
my $html = <<'HTML';
<!doctype html>
<html lang="ro">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="xdev-build" content="__HOST_MANAGER_BUILD_TITLE__">
<title>Madagascar Local Authority</title>
<style>
:root {
color-scheme: light;
--ink: #152033;
--muted: #647084;
--line: #d8dee8;
--soft: #f4f6f9;
--panel: #ffffff;
--accent: #1267d8;
--bad: #b42318;
--warn: #946200;
--ok: #137333;
}
* { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; color: var(--ink); background: #eef2f6; font-size: 14px; }
/* ── Login screen ── */
#login-screen {
display: flex;
align-items: flex-start;
justify-content: center;
min-height: 100dvh;
padding: clamp(48px, 10vh, 96px) 24px clamp(140px, 20vh, 220px);
background: #13182a;
overflow: auto;
}
.login-card {
--otp-size: 48px;
--otp-gap: 18px;
--login-form-width: calc((var(--otp-size) * 6) + (var(--otp-gap) * 5));
background: #fff;
border-radius: 16px;
padding: 54px 64px 34px;
width: 100%;
max-width: 680px;
min-height: 360px;
display: grid;
align-content: start;
justify-items: center;
gap: 28px;
box-shadow: 0 8px 40px rgba(0,0,0,.28);
}
.login-card .brand { text-align: center; display: grid; gap: 8px; justify-items: center; }
.login-card .brand .icon {
margin: 0 0 8px;
width: 64px; height: 64px; border-radius: 18px;
background: #e8f0fe; display: flex; align-items: center; justify-content: center;
}
.login-card .brand .icon svg { width: 38px; height: 38px; fill: none; stroke: var(--accent); stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.login-card .brand h1 { margin: 0; font-size: 32px; line-height: 1.05; font-weight: 750; color: var(--ink); }
.login-card .brand p { margin: 0; color: var(--muted); font-size: 16px; }
.login-card form {
display: grid;
width: min(100%, var(--login-form-width));
justify-self: center;
padding-bottom: 0;
}
.login-card form.busy { opacity: .72; pointer-events: none; }
.pm-helper-fields {
position: absolute;
left: -10000px;
top: auto;
width: 1px;
height: 1px;
overflow: hidden;
opacity: 0.01;
}
.pm-helper-fields input {
width: 1px;
height: 1px;
padding: 0;
border: 0;
}
/* 6 separate OTP digit boxes */
.otp-row {
display: flex;
gap: var(--otp-gap);
justify-content: center;
}
.otp-row input {
width: var(--otp-size); height: 56px; border: 1.5px solid #dde2ec; border-radius: 10px;
font-size: 22px; font-weight: 600; text-align: center; color: var(--ink);
background: #f8fafc; caret-color: transparent; outline: none;
transition: border-color .15s, background .15s;
}
.otp-row input:focus { border-color: var(--accent); background: #fff; }
.otp-row input.filled { border-color: #b3c6f0; background: #fff; }
#login-error {
color: var(--bad); font-size: 13px; text-align: center;
min-height: 18px; margin-top: -52px;
}
@media (max-width: 760px) {
.login-card {
max-width: 520px;
min-height: 0;
padding: 48px 36px 44px;
gap: 26px;
}
.login-card .brand h1 { font-size: 24px; }
.login-card .brand p { font-size: 14px; }
.login-card form { padding-bottom: 0; }
#login-error { margin-top: -24px; }
}
@media (max-width: 430px) {
#login-screen { padding: 24px 16px 120px; }
.login-card {
--otp-size: 42px;
--otp-gap: 12px;
padding: 36px 22px 34px;
}
.otp-row input { height: 52px; }
.login-card form { padding-bottom: 0; }
}
@media (max-height: 720px) {
#login-screen { padding-top: 28px; padding-bottom: 96px; }
.login-card { padding-top: 34px; padding-bottom: 34px; gap: 20px; }
.login-card form { padding-bottom: 0; }
#login-error { margin-top: -32px; }
}
/* ── App shell (hidden until authenticated) ── */
#app { display: none; }
header { display: grid; grid-template-columns: minmax(180px, auto) 1fr auto; align-items: center; gap: 16px; padding: 12px 18px; background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 2; }
h1 { margin: 0; font-size: 17px; font-weight: 700; }
nav { display: flex; align-items: center; gap: 4px; min-width: 0; overflow-x: auto; }
nav a { color: var(--muted); text-decoration: none; padding: 7px 10px; border-radius: 6px; white-space: nowrap; font-weight: 650; }
nav a:hover { color: var(--ink); background: var(--soft); }
nav a.active { color: var(--accent); background: #e8f0fe; }
.header-right { display: flex; align-items: center; justify-content: flex-end; gap: 10px; min-width: 0; }
#message { max-width: 260px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
main { padding: 16px; display: grid; gap: 16px; max-width: 1280px; margin: 0 auto; }
.page { display: grid; gap: 16px; }
.page[hidden] { display: none; }
.toolbar, .panel { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; }
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 10px; }
.panel { overflow: hidden; }
.panel-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--line); background: #fafbfc; }
.panel-head h2 { margin: 0; font-size: 14px; }
.stats { display: flex; gap: 8px; flex-wrap: wrap; }
.stat { padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--soft); font-size: 12px; color: var(--muted); }
button, input, select, textarea { font: inherit; }
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; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button:disabled { opacity: .45; cursor: not-allowed; }
button.danger { color: var(--bad); }
button:disabled, .linkbtn[aria-disabled="true"] { opacity: .55; cursor: not-allowed; pointer-events: none; }
input, select, textarea { width: 100%; border: 1px solid var(--line); border-radius: 6px; padding: 8px; background: #fff; color: var(--ink); }
textarea { min-height: 74px; resize: vertical; }
table { width: 100%; border-collapse: collapse; table-layout: fixed; }
th, td { padding: 9px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; overflow-wrap: anywhere; }
th { color: var(--muted); font-size: 12px; font-weight: 700; background: #fafbfc; }
tr:hover td { background: #f8fafc; }
.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; }
.pill.ok { color: var(--ok); border-color: #b7dfc1; background: #edf8ef; }
.pill.warn { color: var(--warn); border-color: #f1d184; background: #fff7df; }
.pill.bad { color: var(--bad); border-color: #f0b8b3; background: #fff0ee; }
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; padding: 14px; }
.span2 { grid-column: 1 / -1; }
label { display: grid; gap: 5px; color: var(--muted); font-size: 12px; font-weight: 650; }
.muted { color: var(--muted); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-size: 12px; }
.ca-detail { display: grid; gap: 6px; min-width: 0; }
.ca-fingerprint { overflow-wrap: anywhere; }
.ca-empty { padding: 12px 14px; }
.build-badge {
position: fixed;
right: 10px;
bottom: 8px;
z-index: 5;
color: rgba(255,255,255,.46);
background: rgba(19,24,42,.28);
border: 1px solid rgba(255,255,255,.08);
border-radius: 4px;
padding: 2px 5px;
font-size: 10px;
line-height: 1.2;
pointer-events: none;
user-select: none;
}
body.is-app .build-badge {
color: rgba(100,112,132,.58);
background: rgba(255,255,255,.72);
border-color: rgba(216,222,232,.72);
}
.problems { padding: 10px 14px; display: grid; gap: 8px; }
.problem { border-left: 3px solid var(--warn); padding: 7px 9px; background: #fffaf0; }
.work-order-card { display: grid; gap: 8px; min-width: 0; }
.work-order-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.work-order-title { color: var(--ink); font-size: 14px; font-weight: 650; }
.work-order-checklist, .work-order-actions { display: grid; gap: 6px; min-width: 0; }
.work-order-actions { gap: 4px; }
.work-order-checkitem { display: flex; align-items: flex-start; gap: 8px; min-width: 0; color: var(--ink); font-size: 13px; font-weight: 400; }
.work-order-checkitem input[type="checkbox"] { width: auto; flex: 0 0 auto; margin: 2px 0 0; }
.work-order-checkitem span { min-width: 0; overflow-wrap: anywhere; }
.host-tools { display: flex; align-items: center; justify-content: flex-end; gap: 8px; min-width: 0; }
.host-tools input { max-width: 240px; }
.modal-backdrop {
position: fixed;
inset: 0;
z-index: 10;
display: grid;
align-items: start;
justify-items: center;
padding: 72px 16px 24px;
background: rgba(21,32,51,.48);
overflow: auto;
}
.modal-backdrop[hidden] { display: none; }
.modal {
width: min(840px, 100%);
max-height: calc(100dvh - 96px);
overflow: auto;
background: var(--panel);
border: 1px solid var(--line);
border-radius: 8px;
box-shadow: 0 20px 60px rgba(21,32,51,.26);
}
.modal-head {
position: sticky;
top: 0;
z-index: 1;
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 12px 14px;
border-bottom: 1px solid var(--line);
background: #fafbfc;
}
.modal-head h2 { margin: 0; font-size: 14px; }
.modal-close { min-width: 34px; justify-content: center; padding: 7px; }
.form-message { min-height: 18px; color: var(--muted); font-size: 13px; }
.form-message.error { color: var(--bad); }
.form-actions { display: flex; flex-wrap: wrap; gap: 8px; }
@media (max-width: 760px) {
header { grid-template-columns: 1fr; align-items: stretch; gap: 10px; }
.header-right { justify-content: flex-start; flex-wrap: wrap; }
#message { max-width: 100%; }
.panel-head { align-items: stretch; flex-direction: column; }
.host-tools { justify-content: flex-start; flex-wrap: wrap; }
.host-tools input { max-width: none; }
.modal-backdrop { padding-top: 16px; }
.modal { max-height: calc(100dvh - 32px); }
.grid { grid-template-columns: 1fr; }
table { min-width: 760px; }
.table-wrap { overflow-x: auto; }
}
</style>
</head>
<body class="is-login">
<!-- ── Login screen ── -->
<div id="login-screen">
<div class="login-card">
<div class="brand">
<div class="icon">
<svg viewBox="0 0 64 64" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
<rect x="16" y="10" width="32" height="44" rx="4"/>
<rect x="21" y="16" width="22" height="8" rx="2"/>
<rect x="21" y="28" width="22" height="8" rx="2"/>
<rect x="21" y="40" width="22" height="8" rx="2"/>
<path d="M26 20h8M26 32h8M26 44h8"/>
<path d="M40 20h.01M40 32h.01M40 44h.01"/>
</svg>
</div>
<h1>Madagascar Local Authority</h1>
<p>Hosts, DNS & Local CA</p>
</div>
<form id="login-form" method="post" action="/api/login" autocomplete="on" novalidate>
<div class="pm-helper-fields" aria-hidden="true">
<input type="text" id="login-account" name="username" autocomplete="username" autocapitalize="off" spellcheck="false">
<input type="text" id="otp-autofill" name="code" inputmode="numeric" pattern="[0-9]*" autocomplete="one-time-code">
<input type="hidden" id="otp-hidden" name="otp">
</div>
<div class="otp-row">
<input type="text" inputmode="numeric" pattern="[0-9]*" class="otp-digit" aria-label="Digit 1">
<input type="text" inputmode="numeric" pattern="[0-9]*" class="otp-digit" aria-label="Digit 2">
<input type="text" inputmode="numeric" pattern="[0-9]*" class="otp-digit" aria-label="Digit 3">
<input type="text" inputmode="numeric" pattern="[0-9]*" class="otp-digit" aria-label="Digit 4">
<input type="text" inputmode="numeric" pattern="[0-9]*" class="otp-digit" aria-label="Digit 5">
<input type="text" inputmode="numeric" pattern="[0-9]*" class="otp-digit" aria-label="Digit 6">
</div>
</form>
<div id="login-error"></div>
</div>
</div>
<!-- ── App (shown after login) ── -->
<div id="app">
<header>
<h1>Madagascar Local Authority</h1>
<nav aria-label="Sections">
<a href="/overview" data-page-link="overview">Overview</a>
<a href="/hosts" data-page-link="hosts">Hosts</a>
<a href="/dns" data-page-link="dns">DNS</a>
<a href="/work-orders" data-page-link="work-orders">Work Orders</a>
<a href="/ca" data-page-link="ca">Local CA</a>
</nav>
<div class="header-right">
<span class="muted" id="app-updated"></span>
<span id="message" class="muted"></span>
<button id="refresh">Refresh</button>
<button type="button" id="logout">Logout</button>
</div>
</header>
<main>
<section class="page" id="page-overview" data-page="overview">
<section class="panel">
<div class="panel-head">
<h2>Overview</h2>
<div class="stats" id="stats"></div>
</div>
<div class="problems" id="problems"></div>
</section>
</section>
<section class="page" id="page-hosts" data-page="hosts" hidden>
<section class="panel">
<div class="panel-head">
<h2>Hosts</h2>
<div class="host-tools">
<input id="filter" placeholder="filter">
<button type="button" id="new-host">New host</button>
</div>
</div>
<div class="table-wrap">
<table>
<thead>
<tr>
<th style="width: 120px">ID</th>
<th style="width: 130px">hosts_ip</th>
<th style="width: 130px">dns_ip</th>
<th>Names</th>
<th style="width: 150px">Roles</th>
<th style="width: 110px">Monitoring</th>
<th style="width: 90px">Status</th>
</tr>
</thead>
<tbody id="hosts"></tbody>
</table>
</div>
</section>
</section>
<section class="page" id="page-dns" data-page="dns" hidden>
<section class="toolbar">
<a class="linkbtn" href="/download/hosts.yaml">hosts.yaml</a>
<a class="linkbtn" href="/download/local-hosts.tsv">local-hosts.tsv</a>
<a class="linkbtn" href="/download/monitoring.json">monitoring.json</a>
<button id="write-tsv">Write local-hosts.tsv</button>
</section>
</section>
<section class="page" id="page-work-orders" data-page="work-orders" hidden>
<section class="panel">
<div class="panel-head">
<h2>Work Orders</h2>
<div class="stats" id="wo-stats"></div>
</div>
<div class="problems" id="work-orders"></div>
</section>
</section>
<section class="page" id="page-ca" data-page="ca" hidden>
<section class="panel">
<div class="panel-head">
<h2>Local Certificate Authority</h2>
<a class="linkbtn" href="/download/ca.crt">ca.crt</a>
</div>
<div class="problems" id="ca-status"></div>
</section>
<section class="panel">
<div class="panel-head">
<h2>Issued Certificates</h2>
<div class="stats" id="ca-certs-summary"></div>
</div>
<div class="table-wrap">
<table>
<thead>
<tr>
<th style="width: 150px">Name</th>
<th>DNS names</th>
<th style="width: 210px">Validity</th>
<th style="width: 180px">Serial</th>
<th>Fingerprint</th>
<th style="width: 110px">Download</th>
</tr>
</thead>
<tbody id="ca-certs"></tbody>
</table>
</div>
</section>
</section>
</main>
<div id="host-modal" class="modal-backdrop" hidden>
<section class="modal" role="dialog" aria-modal="true" aria-labelledby="host-modal-title">
<div class="modal-head">
<h2 id="host-modal-title">Edit host</h2>
<button type="button" id="close-host-modal" class="modal-close" aria-label="Close host editor">x</button>
</div>
<form id="host-form" class="grid">
<label>ID<input name="id" required></label>
<label>Status<select name="status"><option>active</option><option>planned</option><option>retired</option></select></label>
<label>hosts_ip<input name="hosts_ip" required></label>
<label>dns_ip<input name="dns_ip" required></label>
<label class="span2">Names<textarea name="names" required></textarea></label>
<label>Roles<input name="roles"></label>
<label>Sources<input name="sources"></label>
<label>Monitoring<select name="monitoring"><option>pending</option><option>enabled</option><option>disabled</option></select></label>
<label>Notes<input name="notes"></label>
<div id="host-form-message" class="span2 form-message" aria-live="polite"></div>
<div class="span2 form-actions">
<button class="primary" type="submit" id="save-host">Save host</button>
<button class="danger" type="button" id="delete-host">Delete host</button>
</div>
</form>
</section>
</div>
</div>
<div class="build-badge" title="Running build __HOST_MANAGER_BUILD_TITLE__">build __HOST_MANAGER_BUILD__</div>
<script>
let state = { hosts: [], problems: [], workOrders: [], authenticated: false };
let hostFormSnapshot = '';
const $ = (id) => document.getElementById(id);
const msg = (text) => { $('message').textContent = text || ''; };
const PAGE_PATHS = {
'/': 'overview',
'/overview': 'overview',
'/hosts': 'hosts',
'/dns': 'dns',
'/work-orders': 'work-orders',
'/ca': 'ca',
};
async function api(path, options = {}) {
const res = await fetch(path, options);
const body = await res.json();
if (!res.ok) throw new Error(body.error || res.statusText);
return body;
}
function currentPage() {
return PAGE_PATHS[window.location.pathname] || 'overview';
}
function showPage(page, push = false) {
const target = page || 'overview';
document.querySelectorAll('[data-page]').forEach(section => {
section.hidden = section.dataset.page !== target;
});
document.querySelectorAll('[data-page-link]').forEach(link => {
link.classList.toggle('active', link.dataset.pageLink === target);
link.setAttribute('aria-current', link.dataset.pageLink === target ? 'page' : 'false');
});
if (push) {
const href = target === 'overview' ? '/overview' : '/' + target;
history.pushState({ page: target }, '', href);
}
}
function showLogin(errorText) {
document.body.classList.remove('is-app');
document.body.classList.add('is-login');
$('app').style.display = 'none';
$('login-screen').style.display = 'flex';
$('login-error').textContent = errorText || '';
clearOtp();
}
function showApp() {
document.body.classList.remove('is-login');
document.body.classList.add('is-app');
$('login-screen').style.display = 'none';
$('app').style.display = 'block';
showPage(currentPage());
}
async function refresh() {
const session = await api('/api/session');
state.authenticated = session.authenticated;
if (!state.authenticated) { showLogin(); return; }
showApp();
const data = await api('/api/hosts');
state.hosts = data.hosts || [];
state.problems = data.problems || [];
render(data);
await renderCa();
await renderWorkOrders();
}
function render(data) {
$('app-updated').textContent = data.updated_at ? 'updated ' + data.updated_at : '';
$('stats').innerHTML = [
['hosts', data.counts.hosts],
['problems', data.counts.problems],
].map(([k, v]) => `<span class="stat">${k}: ${escapeHtml(String(v))}</span>`).join('');
$('problems').innerHTML = state.problems.length
? state.problems.map(p => `<div class="problem"><strong>${escapeHtml(p.host_id)}</strong> ${escapeHtml(p.code)}: ${escapeHtml(p.message)}</div>`).join('')
: '<div class="muted" style="padding: 8px 0">No registry problems detected.</div>';
renderHosts();
}
async function renderCa() {
try {
const status = await api('/api/ca/status');
if (!status.initialized) {
$('ca-status').innerHTML = '<div class="problem"><strong>not initialized</strong> Run <code>sudo scripts/ca_manager.sh init</code> on jumper.</div>';
$('ca-certs-summary').innerHTML = '';
$('ca-certs').innerHTML = '<tr><td colspan="6" class="muted">CA is not initialized.</td></tr>';
return;
}
const certs = await api('/api/ca/certificates');
const caDays = daysUntil(status.not_after);
$('ca-status').innerHTML = `
<div class="muted ca-detail">
<div><strong>${escapeHtml(status.subject || '')}</strong></div>
<div>SHA256 <span class="mono ca-fingerprint">${escapeHtml(status.fingerprint_sha256 || '')}</span></div>
<div>valid ${escapeHtml(status.not_before || '')} - ${escapeHtml(status.not_after || '')}</div>
<div>
<span class="pill ${certStatusClass(caDays)}">${escapeHtml(certStatusLabel(caDays))}</span>
<span>${certs.length} issued certificate(s)</span>
</div>
</div>`;
$('ca-certs-summary').innerHTML = [
['issued', certs.length],
['expiring', certs.filter(cert => {
const days = daysUntil(cert.not_after);
return days !== null && days >= 0 && days <= 30;
}).length],
['expired', certs.filter(cert => {
const days = daysUntil(cert.not_after);
return days !== null && days < 0;
}).length],
].map(([k, v]) => `<span class="stat">${k}: ${escapeHtml(String(v))}</span>`).join('');
$('ca-certs').innerHTML = certs.length ? certs.map(cert => {
const days = daysUntil(cert.not_after);
const dnsNames = cert.dns_names || [];
const dnsHtml = dnsNames.length
? dnsNames.map(name => `<span class="pill">${escapeHtml(name)}</span>`).join('')
: '<span class="muted">No DNS SANs reported.</span>';
return `<tr>
<td><strong>${escapeHtml(cert.name || '')}</strong></td>
<td>${dnsHtml}</td>
<td>
<div class="ca-detail">
<span class="pill ${certStatusClass(days)}">${escapeHtml(certStatusLabel(days))}</span>
<span class="muted">until ${escapeHtml(cert.not_after || '')}</span>
</div>
</td>
<td class="mono">${escapeHtml(cert.serial || '')}</td>
<td class="mono ca-fingerprint">${escapeHtml(cert.fingerprint_sha256 || '')}</td>
<td><a class="linkbtn" href="/download/ca/cert/${encodeURIComponent(cert.name || '')}.crt">crt</a></td>
</tr>`;
}).join('') : '<tr><td colspan="6" class="muted">No issued certificates.</td></tr>';
} catch (e) {
$('ca-status').innerHTML = `<div class="problem"><strong>CA status unavailable</strong> ${escapeHtml(e.message)}</div>`;
$('ca-certs-summary').innerHTML = '';
$('ca-certs').innerHTML = '<tr><td colspan="6" class="muted">Certificate list unavailable.</td></tr>';
}
}
function daysUntil(dateText) {
const time = Date.parse(dateText || '');
if (!Number.isFinite(time)) return null;
return Math.ceil((time - Date.now()) / 86400000);
}
function certStatusClass(days) {
if (days === null) return '';
if (days < 0) return 'bad';
if (days <= 30) return 'warn';
return 'ok';
}
function certStatusLabel(days) {
if (days === null) return 'validity unknown';
if (days < 0) return 'expired';
if (days === 0) return 'expires today';
return `${days}d remaining`;
}
async function renderWorkOrders() {
try {
const data = await api('/api/work-orders');
state.workOrders = data.work_orders || [];
$('wo-stats').innerHTML = [
['pending', data.counts.pending],
['total', data.counts.work_orders],
].map(([k, v]) => `<span class="stat">${k}: ${escapeHtml(String(v))}</span>`).join('');
if (!state.workOrders.length) {
$('work-orders').innerHTML = '<div class="muted" style="padding: 8px 0">No work orders.</div>';
return;
}
$('work-orders').innerHTML = state.workOrders.map(wo => {
const checklist = wo.checklist || [];
const doneItems = checklist.filter(item => (item.status || 'pending') === 'done').length;
const checklistComplete = checklist.length === 0 || doneItems === checklist.length;
const checklistHtml = checklist.map(item => {
const checked = (item.status || 'pending') === 'done' ? 'checked' : '';
return `<label class="work-order-checkitem">
<input type="checkbox" data-wo-checklist="${escapeHtml(wo.id)}" data-item-id="${escapeHtml(item.id || '')}" ${checked}>
<span><strong>${escapeHtml(item.id || '')}</strong> ${escapeHtml(item.text || '')}</span>
</label>`;
}).join('');
const actions = (wo.actions || []).map(a => {
const target = [a.host_id, a.name].filter(Boolean).join(' ');
return `<div><span class="pill">${escapeHtml(a.type || '')}</span> ${escapeHtml(target)}</div>`;
}).join('');
const statusClass = (wo.status || 'pending') === 'pending' ? 'warn' : 'ok';
const button = (wo.status || 'pending') === 'pending'
? `<button type="button" class="primary" data-confirm-wo="${escapeHtml(wo.id)}" ${checklistComplete ? '' : 'disabled'}>Confirm</button>`
: '';
return `<div class="problem work-order-card">
<div class="work-order-head">
<div><strong>${escapeHtml(wo.id || '')}</strong> <span class="pill ${statusClass}">${escapeHtml(wo.status || 'pending')}</span> <span class="pill">${doneItems}/${checklist.length} done</span></div>
${button}
</div>
<div class="work-order-title">${escapeHtml(wo.title || '')}</div>
<div class="muted">${escapeHtml(wo.reason || '')}</div>
<div class="work-order-checklist">${checklistHtml}</div>
<div class="work-order-actions">${actions}</div>
${wo.confirmed_at ? `<div class="muted">confirmed ${escapeHtml(wo.confirmed_at)}</div>` : ''}
</div>`;
}).join('');
document.querySelectorAll('[data-wo-checklist]').forEach(input => input.addEventListener('change', () => updateWorkOrderChecklist(input.dataset.woChecklist, input.dataset.itemId, input.checked)));
document.querySelectorAll('[data-confirm-wo]').forEach(button => button.addEventListener('click', () => confirmWorkOrder(button.dataset.confirmWo)));
} catch (e) {
$('work-orders').innerHTML = `<div class="problem"><strong>Work orders unavailable</strong> ${escapeHtml(e.message)}</div>`;
}
}
async function updateWorkOrderChecklist(id, itemId, checked) {
try {
await api('/api/work-orders/checklist', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ id, item_id: itemId, status: checked ? 'done' : 'pending' })
});
msg('work order updated');
await refresh();
} catch (e) { msg(e.message); await refresh(); }
}
async function confirmWorkOrder(id) {
const typed = prompt(`Type ${id} to confirm this work order`);
if (typed !== id) return;
try {
await api('/api/work-orders/confirm', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ id, confirm: typed })
});
msg('work order confirmed; local-hosts.tsv written');
await refresh();
} catch (e) { msg(e.message); }
}
function renderHosts() {
const filter = $('filter').value.toLowerCase();
$('hosts').innerHTML = state.hosts
.filter(h => JSON.stringify(h).toLowerCase().includes(filter))
.map(h => {
const problems = state.problems.filter(p => p.host_id === h.id);
const cls = problems.length ? 'warn' : 'ok';
return `<tr data-id="${escapeHtml(h.id)}">
<td><button type="button" data-edit="${escapeHtml(h.id)}">${escapeHtml(h.id)}</button></td>
<td>${escapeHtml(h.hosts_ip || '')}</td>
<td>${escapeHtml(h.dns_ip || '')}</td>
<td>${(h.names || []).map(n => `<span class="pill">${escapeHtml(n)}</span>`).join('')}</td>
<td>${(h.roles || []).map(n => `<span class="pill">${escapeHtml(n)}</span>`).join('')}</td>
<td><span class="pill ${cls}">${escapeHtml(h.monitoring || '')}</span></td>
<td>${escapeHtml(h.status || '')}</td>
</tr>`;
}).join('');
document.querySelectorAll('[data-edit]').forEach(button => button.addEventListener('click', () => editHost(button.dataset.edit)));
}
function editHost(id) {
const host = state.hosts.find(h => h.id === id);
if (!host) return;
const form = $('host-form');
clearHostFormMessage();
for (const key of ['id', 'status', 'hosts_ip', 'dns_ip', 'monitoring', 'notes']) hostField(key).value = host[key] || '';
hostField('names').value = (host.names || []).join('\n');
hostField('roles').value = (host.roles || []).join(' ');
hostField('sources').value = (host.sources || []).join(' ');
openHostModal('Edit host');
}
function newHost() {
const form = $('host-form');
form.reset();
clearHostFormMessage();
hostField('status').value = 'active';
hostField('monitoring').value = 'pending';
openHostModal('New host');
}
function openHostModal(title) {
$('host-modal-title').textContent = title || 'Edit host';
$('host-modal').hidden = false;
document.body.style.overflow = 'hidden';
hostFormSnapshot = hostFormState();
hostField('id').focus();
}
function requestCloseHostModal() {
if ($('save-host').disabled) return;
if (hostFormDirty() && !confirm('Discard unsaved host changes?')) return;
closeHostModal();
}
function closeHostModal() {
$('host-modal').hidden = true;
document.body.style.overflow = '';
setHostFormBusy(false);
clearHostFormMessage();
hostFormSnapshot = '';
}
function hostField(name) {
return $('host-form').elements.namedItem(name);
}
function hostFormState() {
return JSON.stringify(formObject($('host-form')));
}
function hostFormDirty() {
return !$('host-modal').hidden && hostFormSnapshot && hostFormState() !== hostFormSnapshot;
}
function setHostFormBusy(busy) {
$('save-host').disabled = busy;
$('delete-host').disabled = busy;
$('close-host-modal').disabled = busy;
}
function setHostFormMessage(text, isError = false) {
const message = $('host-form-message');
message.textContent = text || '';
message.classList.toggle('error', !!isError);
}
function clearHostFormMessage() {
setHostFormMessage('');
}
function formObject(form) {
return Object.fromEntries(new FormData(form).entries());
}
function escapeHtml(value) {
value = value == null ? '' : String(value);
return value.replace(/[&<>"']/g, ch => ({'&':'&','<':'<','>':'>','"':'"',"'":'''}[ch]));
}
const ACCOUNT_STORAGE_KEY = 'mla_last_account';
// OTP digit boxes — auto-advance, backspace, paste
const otpDigits = Array.from(document.querySelectorAll('.otp-digit'));
const otpAutofill = $('otp-autofill');
const otpHidden = $('otp-hidden');
const loginAccount = $('login-account');
if (loginAccount) {
const rememberedAccount = localStorage.getItem(ACCOUNT_STORAGE_KEY);
if (rememberedAccount && !loginAccount.value) loginAccount.value = rememberedAccount;
loginAccount.addEventListener('input', () => {
const value = (loginAccount.value || '').trim();
if (value) localStorage.setItem(ACCOUNT_STORAGE_KEY, value);
});
}
otpDigits[0].focus();
otpDigits.forEach((input, idx) => {
input.addEventListener('keydown', (e) => {
if (e.key === 'Backspace') {
if (input.value) setOtpDigit(idx, '');
else if (idx > 0) { setOtpDigit(idx - 1, ''); otpDigits[idx - 1].focus(); }
syncOtpFields();
e.preventDefault();
}
});
input.addEventListener('input', () => {
const digits = input.value.replace(/\D/g, '');
if (digits.length > 1) {
fillOtp(digits, digits.length >= otpDigits.length ? 0 : idx);
return;
}
setOtpDigit(idx, digits);
syncOtpFields();
if (digits && idx < otpDigits.length - 1) otpDigits[idx + 1].focus();
maybeSubmitOtp();
});
input.addEventListener('paste', (e) => {
const text = (e.clipboardData || window.clipboardData).getData('text').replace(/\D/g, '');
e.preventDefault();
fillOtp(text, text.length >= otpDigits.length ? 0 : idx);
});
});
function setOtpDigit(idx, value) {
const digit = (value || '').replace(/\D/g, '').slice(0, 1);
otpDigits[idx].value = digit;
otpDigits[idx].classList.toggle('filled', !!digit);
}
function fillOtp(text, startIdx = 0) {
const digits = (text || '').replace(/\D/g, '').slice(0, otpDigits.length);
if (!digits) return;
if (digits.length >= otpDigits.length) {
otpDigits.forEach((_, i) => setOtpDigit(i, digits[i] || ''));
} else {
digits.split('').forEach((ch, offset) => {
const targetIdx = startIdx + offset;
if (targetIdx < otpDigits.length) setOtpDigit(targetIdx, ch);
});
}
syncOtpFields();
const next = Math.min((digits.length >= otpDigits.length ? digits.length : startIdx + digits.length), otpDigits.length - 1);
otpDigits[next].focus();
maybeSubmitOtp();
}
function getOtp() { return otpDigits.map(i => i.value).join(''); }
function syncOtpFields() {
const otp = getOtp();
if (otpHidden) otpHidden.value = otp;
if (otpAutofill && otpAutofill.value !== otp) otpAutofill.value = otp;
}
function otpReady() { return otpDigits.every(i => /^\d$/.test(i.value)); }
function maybeSubmitOtp() { if (otpReady()) $('login-form').requestSubmit(); }
function clearOtp() {
otpDigits.forEach(i => { i.value = ''; i.classList.remove('filled'); });
if (otpAutofill) otpAutofill.value = '';
if (otpHidden) otpHidden.value = '';
otpDigits[0].focus();
}
if (otpAutofill) {
const handleAutofill = () => fillOtp(otpAutofill.value, 0);
otpAutofill.addEventListener('input', handleAutofill);
otpAutofill.addEventListener('change', handleAutofill);
}
document.querySelectorAll('[data-page-link]').forEach(link => {
link.addEventListener('click', (event) => {
event.preventDefault();
showPage(link.dataset.pageLink, true);
});
});
window.addEventListener('popstate', () => showPage(currentPage()));
$('login-form').addEventListener('submit', async (event) => {
event.preventDefault();
if (!otpReady()) return;
$('login-form').classList.add('busy');
$('login-error').textContent = '';
try {
await api('/api/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ otp: getOtp() }) });
await refresh();
} catch (e) {
showLogin(e.message === 'invalid_otp' ? 'Cod incorect.' : e.message);
} finally {
$('login-form').classList.remove('busy');
}
});
$('logout').addEventListener('click', async () => {
await api('/api/logout', { method: 'POST' }).catch(() => {});
window.location.replace('/?logged_out=' + Date.now());
});
$('refresh').addEventListener('click', () => refresh().catch(e => msg(e.message)));
$('filter').addEventListener('input', renderHosts);
$('new-host').addEventListener('click', newHost);
$('close-host-modal').addEventListener('click', requestCloseHostModal);
window.addEventListener('keydown', (event) => {
if (event.key === 'Escape' && !$('host-modal').hidden) requestCloseHostModal();
});
$('host-form').addEventListener('submit', async (event) => {
event.preventDefault();
setHostFormBusy(true);
setHostFormMessage('Saving...');
try {
await api('/api/hosts/upsert', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(formObject(event.target)) });
hostFormSnapshot = hostFormState();
closeHostModal();
msg('host saved');
await refresh();
} catch (e) {
setHostFormMessage(e.message, true);
msg(e.message);
} finally {
setHostFormBusy(false);
}
});
$('host-form').addEventListener('invalid', (event) => {
setHostFormMessage('Complete the required host fields before saving.', true);
}, true);
$('host-form').addEventListener('input', () => {
if ($('host-form-message').classList.contains('error')) clearHostFormMessage();
});
$('delete-host').addEventListener('click', async () => {
const id = hostField('id').value;
if (!id || !confirm(`Delete ${id}?`)) return;
setHostFormBusy(true);
setHostFormMessage('Deleting...');
try {
await api('/api/hosts/delete', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id }) });
$('host-form').reset();
hostFormSnapshot = hostFormState();
closeHostModal();
msg('host deleted');
await refresh();
} catch (e) {
setHostFormMessage(e.message, true);
msg(e.message);
} finally {
setHostFormBusy(false);
}
});
$('write-tsv').addEventListener('click', async () => {
if (!confirm('Write config/local-hosts.tsv from hosts.yaml?')) return;
try {
await api('/api/render/local-hosts-tsv', { method: 'POST' });
msg('local-hosts.tsv written');
} catch (e) { msg(e.message); }
});
refresh().catch(() => showLogin());
</script>
</body>
</html>
HTML
$html =~ s/__HOST_MANAGER_BUILD_TITLE__/$build_title/g;
$html =~ s/__HOST_MANAGER_BUILD__/$build/g;
return $html;
}