#!/usr/bin/env perl # Daniel Bowling # Mar 2021 use Mojolicious::Lite; use Regexp::Common qw{net}; use Digest::SHA qw{sha1_hex}; use Fcntl qw{:flock :seek}; use Number::Format qw{format_number}; # The main landing page; pass in the output of the fortune command get '/' => sub { my ($c) = @_; my $fortune = `fortune` || "huh?? no fortune for u\n"; my ($count, %serverSide); # Visitor counter ala the ol' count.cgi script # TODO: clean up and variable-ize filehandles open(IN,"+<.counts"); # Open it flock(IN,LOCK_EX); # Lock the file (exclusive lock) seek(IN,0,SEEK_SET); # Rewind it to the beginning $count = ; # Read only the first line $count = ++$count; # Increment counter truncate(IN,0); # This erases the file to length=0 seek(IN,0,SEEK_SET); # Rewind it to the beginning print IN "$count\n"; # Write out the new count close(IN); # Close the file. $count = format_number($count); # Add commas # Gather all our server-side stuff %serverSide = ( count => $count, fortune => $fortune ); $c->render(%serverSide); } => 'index'; # Deprecation of IE page get '/die'; # guest.cgi script plugin CGI => ['/cgi-bin/guest' => './cgi-bin/guest_mm.cgi']; get '/me'; get '/news'; # 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 { my ($c) = @_; my $mac = lc $c->param('macaddr'); # Lower-case plz my $ula6; # Check the MAC 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]; $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); ($uniqueid = "fd" . substr $digesty, -10) =~ s/(.{1,4})/$1:/g; # Set the ULA $ula6 = $uniqueid . ':/48'; } $c->render(ula6 => $ula6); }; # whoami.cgi script plugin CGI => ['/cgi-bin/whoami' => './cgi-bin/whoami.cgi']; # Send it app->start();