#!/usr/bin/perl # tinyweb - authored by pal.ekran@nhn.no, Thu Nov 2 10:59:49 PM CET 2023 # ------------------------------------------------------------------------------ # starts a tiny web-server on specified port. the web-server only serves one web-page # which can be edited at the bottom of the script. # # usage: tinyweb {port1,port2,..} # example: tinyweb start 8080,8180,8280 # use strict; use IO::Socket::INET; # prototypes sub catlog(); sub logx($); sub launch($$); sub webpage($$); my $cmd=""; my $lports=""; my $hostname= `hostname`; chomp $hostname; $cmd=$ARGV[0]; $lports=$ARGV[1]; if (!$lports) { $lports="8080"; } # default port if (!$cmd) { $cmd=""; } # default port die ("bad numbers of parameters: $0 {ports} - RTFM ERROR!") if scalar @ARGV > 2; if ($cmd eq "catlog") { catlog(); exit 0; } if ($cmd eq "stop") { logx ("$0 stopped."); system ("killall tinyweb"); exit 0; } if ($cmd eq "status") { print "Current running processes:\n\n"; system ("ps axu | grep tinyweb | grep perl | grep -v grep"); exit 0; } if ($cmd eq "start") { my $pid=fork(); if (!$pid) { launch($lports,$hostname); } exit 0; } print "$0 {port,}\n\n"; print "Creates a web-service on one or more ports, used for testing of firewalls, routing, loadbalancers, etc.\n\n"; print "Example use:\n"; print "\ttinyweb stop - stop all services.\n"; print "\ttinyweb start - start a webservice on default port.\n"; print "\ttinyweb start 8000,8001,8020 - starts webservices on port 8000,8001 and 8020.\n"; print "\ttinyweb status - list running processes.\n"; print "\ttinyweb catlog - show logfile.\n"; exit 0; sub launch($$) { my ($lports,$hostname)=@_; my $port; my @socket; my @ports=split /,/,$lports; foreach $port (@ports) { if ($socket[$port]=IO::Socket::INET->new( LocalPort => $port, Listen=> 1, ReuseAddr => 1,)) { logx ("$0 launched on port ".$port); print ("$0 launched on port ".$port."\n"); } else { logx ("ERROR: Can't listen on port $port socket: $@"); print ("ERROR:\tCan't listen on port $port\n\tsocket: $@\n"); exit (0); } } foreach $port (@ports) { my $pid=fork(); if ($pid) { for (;;) { my $sfd=$socket[$port]->accept(); my ($out)=webpage($hostname,$port); my $response="HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: ".length($out)."\r\n\r\n"; $response.=$out; print $sfd $response; close $sfd; logx("connection on port ".$port." from ".gethostbyaddr($sfd->peeraddr,AF_INET)); } } } } sub logx($) { my ($text)=@_; my $logfile; my $dat=`date`; chomp $dat; # if current user = root, log to /var/log, else, log to homedir. if ($< == 0) { $logfile='/var/log/tinyweb.log'; } else { $logfile=$ENV{'HOME'}.'/tinyweb.log'; } open LOG,">>".$logfile; print LOG $dat.":".$text."\n"; close LOG; } sub catlog() { my $logfile; if ($< == 0) { $logfile='/var/log/tinyweb.log'; } else { $logfile=$ENV{'HOME'}.'/tinyweb.log'; } open LOG,"<".$logfile; while () { print $_; } close LOG; } # web-page output sub webpage($$) { my ($hostname,$port)=@_; my $cbackground='#262625'; my $ctext='#ebf21b'; my $clink='#c7f21b'; return(' tinyweb

tinyweb title

You have reached this web-service running on '.$hostname.' port '.$port.'

'); }