From cc04457c99952083ee095d6eae069cb5d8307837 Mon Sep 17 00:00:00 2001 From: Emil Williams Date: Wed, 20 Aug 2025 14:23:06 +0000 Subject: [PATCH] Gorillas Break Down Your House And Sell It Wholesale --- .gitignore | 1 + config.pl.example | 6 +++ gorillanest | 115 ------------------------------------------- gorillanest.pl | 121 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 128 insertions(+), 115 deletions(-) create mode 100644 config.pl.example delete mode 100755 gorillanest create mode 100755 gorillanest.pl diff --git a/.gitignore b/.gitignore index 0c4032e..ee90409 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.sqlite3 +config.pl git/* !git/public/.gitkeep !git/private/.gitkeep diff --git a/config.pl.example b/config.pl.example new file mode 100644 index 0000000..fd39608 --- /dev/null +++ b/config.pl.example @@ -0,0 +1,6 @@ +# always assume anything to do with files is from project root +use constant GIT_ROOT => 'git'; # directory containing all browsable $users/$repositories +use constant DB_FILE => 'gorillanest.sqlite3'; # sqlite3 database file location +use constant TEMPLATE_ROOT => 'template'; # template directory +use constant USER_REPOSITORY => qr{^/([a-zA-Z0-9_.]+)(?:/([a-zA-Z0-9_.]+))?}; # $1 = username, $2 = repository fullname +1; diff --git a/gorillanest b/gorillanest deleted file mode 100755 index f5c26f7..0000000 --- a/gorillanest +++ /dev/null @@ -1,115 +0,0 @@ -#!/usr/bin/env perl - -use strict; -use warnings; - -use CGI; -use FCGI; -use Switch::Back; -use Syntax::Keyword::Try; -use Template; -use URI::Escape; -use Cwd; -# use Git::Repository; - -use Data::Dumper; -sub info { - warn join(' ', @_); -} - -# significant dirs only -sub GN::directories { - my $root = $_[0]; - opendir my $dir, $root or die "Cannot open directory: $!"; - my @directories; - my %drop = ( - '.' => 0, - '..' => 0, - ); - foreach (readdir $dir) { - push(@directories, $_) if (-d join('/', $_[0], $_) && ($drop{$_} // 1)); - } - closedir $dir; - return \@directories; -} - -# probably should output all repos recursively, currently just outputs list of users -sub GN::index { # / - my ($root, $dataref) = @_; - my %data = %$dataref; - my @directories = map { my $i = $_; map { join('/', $i, $_) } @{GN::directories(join('/', $root, $i))} } @{GN::directories($root)}; - $data{directories} = \@directories; - if ($data{directories}) { $data{found} = 1; } - return \%data; -} - -sub GN::user { # /$username/ - my ($root, $dataref) = @_; - my %data = %$dataref; - my @directories = @{GN::directories(join('/', $root, $data{username}))}; - $data{directories} = \@directories; - if ($data{directories}) { $data{found} = 1; } - return \%data; -} - -sub GN::repository { # /$username/$repository - my ($root, $dataref) = @_; - my %data = %$dataref; - $data{found} = 0; - return \%data; -} - -open STDERR, '>', '/tmp/gorillanest.log' or die "You Will Never Ever See This Message Hopefully: $!"; -my $sock = FCGI::OpenSocket('/tmp/gorillanest.socket', 100); -try { - my $public = 'git/public'; - my $dbfile = 'gorillanest.sqlite3'; - my %data = ( - found => 0, - access => 0, - ); - - my $request = FCGI::Request(\*STDIN, \*STDOUT, \*STDERR, \%ENV, $sock); - my $template = Template->new({INCLUDE_PATH => 'template'}); - my $head = 0; - my $a_template; - while($request->Accept() >= 0) { - $data{access} += 1; - my $cgi = CGI->new; - my %header = ( - -Content_Type => 'text/html', - -charset => 'UTF-8', - ); - my $method = $ENV{'REQUEST_METHOD'} || ''; - my $uri = $ENV{'REQUEST_URI'} || '/'; - if ($method eq 'HEAD') { - $head = 1; - } elsif ($method eq 'GET') { - ($data{username}, $data{repository}) = $uri =~ m{^/([a-zA-Z0-9_.]+)(?:/([a-zA-Z0-9_.]+))?}; - info("name:", $data{username} || '', "repo:", $data{repository} || ''); - if ($uri eq '/') { - %data = %{GN::index($public, \%data)}; - $a_template = "index.tt"; - } elsif ($data{repository}) { - %data = %{GN::repository($public, \%data)}; - $a_template = "repository.tt"; - } elsif ($data{username}) { - %data = %{GN::user($public, \%data)}; - $a_template = "index_user.tt"; - } - if (!$data{found}) { - $header{-status} = '404 Not Found'; - $a_template = "404.tt"; - } - print $cgi->header(%header); - if ($head) { $head = 0; continue; } - $template->process($a_template, \%data) or info("Template: " . $template->error()); - } else { - $header{-status} = '405 Method Not Allowed'; - print $cgi->header(%header); - } - } -} catch ($error) { - info("Crashed: $error"); -} -FCGI::CloseSocket($sock); diff --git a/gorillanest.pl b/gorillanest.pl new file mode 100755 index 0000000..53d3a9d --- /dev/null +++ b/gorillanest.pl @@ -0,0 +1,121 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use CGI; +use FCGI; +use Switch::Back; +use Syntax::Keyword::Try; +use Template; +use URI::Escape; +use Cwd; +use Data::Dumper; +use Git::Repository; + +use lib '.'; +BEGIN { require 'config.pl'; } + +sub info { + warn join(' ', @_); +} + +# significant dirs only +sub GN::directories { + my $root = $_[0]; + opendir my $dir, $root or die "Cannot open directory: $!"; + my @directories; + my %drop = ( + '.' => 0, + '..' => 0, + ); + foreach (readdir $dir) { + push(@directories, $_) if (-d join('/', $_[0], $_) && ($drop{$_} // 1)); + } + closedir $dir; + return \@directories; +} + +# probably should output all repos recursively, currently just outputs list of users +sub GN::index { # / + my ($root, $dataref) = @_; + my %data = %$dataref; + my @directories = map { my $i = $_; map { join('/', $i, $_) } @{GN::directories(join('/', $root, $i))} } @{GN::directories($root)}; + $data{directories} = \@directories; + if ($data{directories}) { $data{found} = 1; } + return \%data; +} + +sub GN::user { # /$username/ + my ($root, $dataref) = @_; + my %data = %$dataref; + my @directories = @{GN::directories(join('/', $root, $data{username}))}; + $data{directories} = \@directories; + if ($data{directories}) { $data{found} = 1; } + return \%data; +} + +sub GN::repository { # /$username/$repository + my ($root, $dataref) = @_; + my %data = %$dataref; + $data{found} = 0; + return \%data; +} + +sub GN::main { + open STDERR, '>', '/tmp/gorillanest.log' or die "You Will Never Ever See This Message Hopefully: $!"; + my $sock = FCGI::OpenSocket('/tmp/gorillanest.socket', 100); + try { + my $root = GIT_ROOT; + my $dbfile = DB_FILE; + my %data = ( + found => 0, + access => 0, + ); + + my $request = FCGI::Request(\*STDIN, \*STDOUT, \*STDERR, \%ENV, $sock); + my $template = Template->new({INCLUDE_PATH => TEMPLATE_ROOT}); + my $head = 0; + my $a_template; + while($request->Accept() >= 0) { + $data{access} += 1; + my $cgi = CGI->new; + my %header = ( + -Content_Type => 'text/html', + -charset => 'UTF-8', + ); + my $method = $ENV{'REQUEST_METHOD'} || ''; + my $uri = $ENV{'REQUEST_URI'} || '/'; + if ($method eq 'HEAD') { + $head = 1; + } elsif ($method eq 'GET') { + ($data{username}, $data{repository}) = $uri =~ USER_REPOSITORY; + info("name:", $data{username} || '', "repo:", $data{repository} || ''); + if ($uri eq '/') { + %data = %{GN::index($root, \%data)}; + $a_template = "index.tt"; + } elsif ($data{repository}) { + %data = %{GN::repository($root, \%data)}; + $a_template = "repository.tt"; + } elsif ($data{username}) { + %data = %{GN::user($root, \%data)}; + $a_template = "index_user.tt"; + } + if (!$data{found}) { + $header{-status} = '404 Not Found'; + $a_template = "404.tt"; + } + print $cgi->header(%header); + if ($head) { $head = 0; continue; } + $template->process($a_template, \%data) or info("Template: " . $template->error()); + } else { + $header{-status} = '405 Method Not Allowed'; + print $cgi->header(%header); + } + } + } catch ($error) { + info("Crashed: $error"); + } + FCGI::CloseSocket($sock); +} + +GN::main(); -- 2.39.5