Add filename randomizing (#17)

* Added filename randomizing code

* Updated the README file

---------

Co-authored-by: getimiskon <getimiskon@disroot.org>
This commit is contained in:
getimiskon 2023-02-13 21:29:06 +00:00 committed by GitHub
parent e62bcfa9d8
commit d0c7060012
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 11 deletions

View file

@ -11,7 +11,7 @@
1. install the dependencies using `cpan`: 1. install the dependencies using `cpan`:
~~~ ~~~
cpan -i Mojolicious::Lite Mojolicious::Routes::Pattern Mojoliciuos::Plugin::RenderFile cpan -i Mojolicious::Lite Mojolicious::Routes::Pattern Mojolicious::Plugin::RenderFile List::MoreUtils MIME::Types
~~~ ~~~
2. Clone the repo and start the daemon: 2. Clone the repo and start the daemon:
@ -22,14 +22,10 @@ cd sakisafe/http
./sakisafe.pl daemon -m production ./sakisafe.pl daemon -m production
~~~ ~~~
3. Create a 'f' directory in the directory sakisafe will run with
`mkdir f`. Make sure that the user which will run sakisafe.pl can
write in that directory.
By default, sakisafe will bind in 127.0.0.1 port 3000. Because that's By default, sakisafe will bind in 127.0.0.1 port 3000. Because that's
the default bind Mojolicious uses. the default bind Mojolicious uses.
4. Create a proxy rule in nginx configuration (If you're using another 3. Create a proxy rule in nginx configuration (If you're using another
HTTP server, you're on your own.) HTTP server, you're on your own.)
~~~conf ~~~conf

View file

@ -12,6 +12,7 @@ use MIME::Types;
use warnings; use warnings;
use experimental 'signatures'; use experimental 'signatures';
use feature 'say'; use feature 'say';
use Encode qw(decode encode);
plugin 'RenderFile'; plugin 'RenderFile';
# OpenBSD promises. # OpenBSD promises.
@ -22,8 +23,9 @@ pledge("stdio cpath rpath wpath inet flock fattr") if $openbsd;
# 100 MBs # 100 MBs
my $MAX_SIZE = 1024 * 1024 * 100; my $MAX_SIZE = 1024 * 1024 * 100;
my @BANNED = qw(); # Add banned IP addresses here my @BANNED = qw(); # Add banned IP addresses here
my $RANDOMIZE_FILENAME = 0; # Enable/disable randomization
my $dirname; my $dirname;
my $link; my $link;
@ -60,6 +62,15 @@ sub handle_file {
my @chars = ( '0' .. '9', 'a' .. 'Z' ); my @chars = ( '0' .. '9', 'a' .. 'Z' );
$dirname .= $chars[ rand @chars ] for 1 .. 5; $dirname .= $chars[ rand @chars ] for 1 .. 5;
my $filename = $filedata->filename; my $filename = $filedata->filename;
my $enc = encode( "UTF-8", $filename );
$filename = $enc;
if ( $RANDOMIZE_FILENAME = 1 ) {
my $extension = $filename;
$extension =~ s/.*\.//;
$filename = "";
$filename .= $chars[ rand @chars ] for 1 .. 5;
$filename = $filename . "." . $extension;
}
carp( color("bold yellow"), carp( color("bold yellow"),
"sakisafe warning: could not create directory: $ERRNO", "sakisafe warning: could not create directory: $ERRNO",
color("reset") ) color("reset") )