www2.0/www-swagg.pl

114 lines
2.8 KiB
Perl
Raw Normal View History

#!/usr/bin/env perl
# Daniel Bowling <swaggboi@slackware.uk>
# Mar 2021
use Mojolicious::Lite;
use Regexp::Common qw{net};
use Digest::SHA qw{sha1_hex};
2021-03-06 22:20:56 +00:00
use Number::Format qw{format_number};
2021-03-13 04:22:50 +00:00
## Let's set some things up first ##
plugin 'Config';
# CGI scripts
plugin CGI => ['/cgi-bin/guest' => './cgi-bin/guest_mm.cgi'];
plugin CGI => ['/cgi-bin/whoami' => './cgi-bin/whoami.cgi' ];
2021-03-08 01:44:58 +00:00
2021-03-13 04:22:50 +00:00
# Handle the GDPR non-compliance banner via session cookie
helper swaggSession => sub {
my ($c) = @_;
if ($c->cookie('banner') eq 'seen') {
$c->session->{banner} = 'seen' unless $c->session->{banner}
}
};
## Begin routes ##
# The main landing page; pass in the output of the fortune command
get '/' => sub {
2021-02-28 07:27:38 +00:00
my ($c) = @_;
2021-03-08 01:55:34 +00:00
my $count = format_number time; # Grab epoch and add commas
2021-03-07 01:24:16 +00:00
my $fortune = `/usr/games/fortune` || `fortune` || "huh??\n";
2021-03-06 22:20:56 +00:00
$c->stash(
2021-03-06 22:20:56 +00:00
count => $count,
fortune => $fortune
);
2021-03-13 04:27:18 +00:00
$c->swaggSession();
$c->render();
} => 'index';
# Process mac address to ula6 ala the ol' ula6.cgi script:
# - Current time of day in 64-bit NTP format
# - Obtain EUI64
# - Cat first with second
# - Compute SHA-1 of third and use least significant 40 bits
# TODO: This ancient code could certainly be cleaned up
get '/ula6' => sub {
2021-03-08 01:57:42 +00:00
my ($c) = @_;
2021-03-08 01:44:58 +00:00
my ($mac, $ula6);
# Check the MAC
2021-03-08 01:44:58 +00:00
$mac = ($c->param('macaddr')) ? lc $c->param('macaddr') : '';
2021-03-13 04:22:50 +00:00
if ($mac =~ /$RE{net}{MAC}/) {
# Local vars for this bit
my (
$binfield,
$decfield,
$digesty,
$epoch,
@fields,
$fulleui,
$halfone,
$halftwo,
$hexfield,
$uniqueid
);
# EUI64
@fields = split(/:/, $mac);
$halfone = $fields[0] . $fields[1] . $fields[2] . "ff";
$halftwo = "fe" . $fields[3] . $fields[4] . $fields[5];
$binfield = sprintf "%b", hex $fields[0];
$decfield = sprintf "%d", hex $fields[0];
2021-03-06 03:31:48 +00:00
$decfield = ($binfield =~ /1[01]$/) ? $decfield - 2 : $decfield + 2;
$hexfield = sprintf "%x", int $decfield;
$halfone = $hexfield . $fields[1] . $fields[2] . "ff";
$fulleui = $halfone . $halftwo;
# Current time of day
$epoch = time() + 2208988800;
# Cat with the time
$digesty = sha1_hex($epoch . $fulleui);
2021-03-06 03:31:48 +00:00
($uniqueid = "fd" . substr $digesty, -10) =~ s/(.{1,4})/$1:/g;
# Set the ULA
$ula6 = $uniqueid . ':/48';
}
2021-03-13 04:22:50 +00:00
$c->swaggSession();
$c->render(ula6 => $ula6);
};
# Catch any other route
get '/:route' => sub {
my ($c) = @_;
my $route = $c->stash('route');
2021-03-13 04:22:50 +00:00
$c->swaggSession();
$c->render(template => $route);
};
2021-03-13 04:22:50 +00:00
## Send it ##
app->secrets(app->config->{secrets}) || die $!;
app->start();