Added feature to ban IP addresses and also a logger
This commit is contained in:
parent
80ef5e6ba2
commit
e361669b87
1 changed files with 50 additions and 28 deletions
|
@ -3,52 +3,74 @@
|
||||||
|
|
||||||
use Mojolicious::Lite -signatures;
|
use Mojolicious::Lite -signatures;
|
||||||
use Mojolicious::Routes::Pattern;
|
use Mojolicious::Routes::Pattern;
|
||||||
|
|
||||||
use v5.36;
|
use v5.36;
|
||||||
plugin 'RenderFile';
|
plugin 'RenderFile';
|
||||||
|
|
||||||
my $MAX_SIZE = 1024 * 1024 * 100;
|
my $MAX_SIZE = 1024 * 1024 * 100;
|
||||||
|
|
||||||
|
my @BANNED = qw(); # Add banned IP addresses here
|
||||||
my $dirname;
|
my $dirname;
|
||||||
my $host;
|
my $host;
|
||||||
|
|
||||||
sub handle_file {
|
# Function to handle file uploads
|
||||||
my $c = shift;
|
|
||||||
my $filedata = $c->param("file");
|
|
||||||
if ( $filedata->size > $MAX_SIZE ) {
|
|
||||||
return $c->render(
|
|
||||||
text => "Max upload size: $MAX_SIZE",
|
|
||||||
status => 400
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
# Generate random string for the directory
|
sub logger ( $level, $address, $message ) {
|
||||||
my @chars = ( '0' .. '9', 'a' .. 'Z' );
|
open( my $fh, ">>", "sakisafe.log" );
|
||||||
$dirname .= $chars[ rand @chars ] for 1 .. 5;
|
printf( $fh "[%s]: %s has uploaded file %s\n", $level, $address, $message );
|
||||||
my $filename = $filedata->filename;
|
close($fh);
|
||||||
mkdir( "f/" . $dirname );
|
}
|
||||||
$filename .= ".txt" if $filename eq "-";
|
|
||||||
$filedata->move_to( "f/" . $dirname . "/" . $filename );
|
sub handle_file {
|
||||||
my $host = $c->req->url->to_abs->host;
|
my $c = shift;
|
||||||
$c->res->headers->header(
|
my $filedata = $c->param("file");
|
||||||
'Location' => "http://$host/$dirname/" . $filedata->filename );
|
if ( $filedata->size > $MAX_SIZE ) {
|
||||||
$c->render(
|
return $c->render(
|
||||||
text => "http://$host/f/$dirname/" . $filename,
|
text => "Max upload size: $MAX_SIZE",
|
||||||
status => 201,
|
status => 400
|
||||||
);
|
);
|
||||||
$dirname = "";
|
}
|
||||||
|
if ( $c->tx->remote_address ~~ @BANNED ) {
|
||||||
|
$c->render(
|
||||||
|
text =>
|
||||||
|
"Hi! Seems like the server admin added your IP address to the banned IP array." .
|
||||||
|
"As the developer of sakisafe, I can't do anything.",
|
||||||
|
status => 403
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Generate random string for the directory
|
||||||
|
my @chars = ( '0' .. '9', 'a' .. 'Z' );
|
||||||
|
$dirname .= $chars[ rand @chars ] for 1 .. 5;
|
||||||
|
my $filename = $filedata->filename;
|
||||||
|
mkdir( "f/" . $dirname );
|
||||||
|
$filename .= ".txt" if $filename eq "-";
|
||||||
|
$filedata->move_to( "f/" . $dirname . "/" . $filename );
|
||||||
|
my $host = $c->req->url->to_abs->host;
|
||||||
|
$c->res->headers->header(
|
||||||
|
'Location' => "http://$host/$dirname/" . $filename );
|
||||||
|
$c->render(
|
||||||
|
text => "http://$host/f/$dirname/" . $filename,
|
||||||
|
status => 201,
|
||||||
|
);
|
||||||
|
logger( "INFO", $c->tx->remote_address, $dirname . "/" . $filename );
|
||||||
|
$dirname = "";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Function to log uploaded files
|
||||||
|
|
||||||
get '/' => 'index';
|
get '/' => 'index';
|
||||||
post '/' => sub ($c) { handle_file($c) };
|
post '/' => sub ($c) { handle_file($c) };
|
||||||
|
|
||||||
# Allow files to be downloaded.
|
# Allow files to be downloaded.
|
||||||
|
|
||||||
get '/f/:dir/:name' => sub ($c) {
|
get '/f/:dir/:name' => sub ($c) {
|
||||||
my $captures = $c->req->url;
|
my $captures = $c->req->url;
|
||||||
$captures =~ s/^.//;
|
$captures =~ s/^.//;
|
||||||
my $filerender = Mojolicious::Plugin::RenderFile->new;
|
my $filerender = Mojolicious::Plugin::RenderFile->new;
|
||||||
$c->render_file( filepath => $captures );
|
$c->render_file( filepath => $captures );
|
||||||
};
|
};
|
||||||
|
|
||||||
app->max_request_size( 1024 * 1024 * 100 );
|
app->max_request_size( 1024 * 1024 * 100 );
|
||||||
|
|
Loading…
Add table
Reference in a new issue