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,14 +3,24 @@
|
|||
|
||||
use Mojolicious::Lite -signatures;
|
||||
use Mojolicious::Routes::Pattern;
|
||||
|
||||
use v5.36;
|
||||
plugin 'RenderFile';
|
||||
|
||||
my $MAX_SIZE = 1024 * 1024 * 100;
|
||||
|
||||
my @BANNED = qw(); # Add banned IP addresses here
|
||||
my $dirname;
|
||||
my $host;
|
||||
|
||||
# Function to handle file uploads
|
||||
|
||||
sub logger ( $level, $address, $message ) {
|
||||
open( my $fh, ">>", "sakisafe.log" );
|
||||
printf( $fh "[%s]: %s has uploaded file %s\n", $level, $address, $message );
|
||||
close($fh);
|
||||
}
|
||||
|
||||
sub handle_file {
|
||||
my $c = shift;
|
||||
my $filedata = $c->param("file");
|
||||
|
@ -20,6 +30,15 @@ sub handle_file {
|
|||
status => 400
|
||||
);
|
||||
}
|
||||
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' );
|
||||
|
@ -30,15 +49,18 @@ sub handle_file {
|
|||
$filedata->move_to( "f/" . $dirname . "/" . $filename );
|
||||
my $host = $c->req->url->to_abs->host;
|
||||
$c->res->headers->header(
|
||||
'Location' => "http://$host/$dirname/" . $filedata->filename );
|
||||
'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';
|
||||
post '/' => sub ($c) { handle_file($c) };
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue