Verified Commit b5f02193 authored by Luc Didry's avatar Luc Didry Committed by Luc Didry

Merge branch 'deactivate-urls' into 'development'

Disable URLs instead of removing them

See merge request fiat-tux/hat-softwares/lstu!33
parents e944c18f 7e504f54
Revision history for Perl application Lstu
0.22-0 ????-??-??
- Disable URLs instead of removing them (prevents spammers to reuse a
deleted shortened URL)
0.21-4 2018-12-17
- Add a lockfile to GSB database update to prevent concurrent updates
......
......@@ -43,7 +43,7 @@ sub run {
);
my (@bad, %bad_ips, @bad_from_ips);
my $gsb = $c->app->gsb;
my $deleted = 0;
my $disabled = 0;
$urls->each(sub {
my ($e, $num) = @_;
......@@ -54,7 +54,7 @@ sub run {
if (@matches) {
push @bad, $e->{short};
$bad_ips{$e->{created_by}} = 1 if $e->{created_by};
$deleted += Lstu::DB::URL->new(
$disabled += Lstu::DB::URL->new(
app => $c->app,
short => $e->{short}
)->remove if $remove;
......@@ -65,18 +65,18 @@ sub run {
say sprintf('%d bad URLs detected.', scalar(@bad));
if ($remove) {
say sprintf('%d bad URLs deleted.', $deleted) if $deleted;
say sprintf('%d bad URLs disabled.', $disabled) if $disabled;
} else {
say sprintf("If you want to delete the detected bad URLs, please do:\n carton exec script/lstu url --remove %s", join(' ', @bad)) if @bad;
say sprintf("If you want to disable the detected bad URLs, please do:\n carton exec script/lstu url --remove %s", join(' ', @bad)) if @bad;
}
$deleted = 0;
$disabled = 0;
for my $ip (keys %bad_ips) {
my $u = Lstu::DB::URL->new(app => $c->app)->search_creator($ip);
$u->each(sub {
my ($e, $num) = @_;
push @bad_from_ips, $e->{short};
$deleted += Lstu::DB::URL->new(
$disabled += Lstu::DB::URL->new(
app => $c->app,
short => $e->{short}
)->remove if ($remove && $all);
......@@ -96,9 +96,9 @@ sub run {
}
if ($remove && $all) {
say sprintf('%d URLs from same IPs deleted.', $deleted) if $deleted;
say sprintf('%d URLs from same IPs disabled.', $disabled) if $disabled;
} else {
say sprintf("If you want to delete the URLs created by the same IPs than the detected bad URLs, please do:\n carton exec script/lstu url --remove %s", join(' ', @bad_from_ips)) if @bad_from_ips;
say sprintf("If you want to disable the URLs created by the same IPs than the detected bad URLs, please do:\n carton exec script/lstu url --remove %s", join(' ', @bad_from_ips)) if @bad_from_ips;
}
} else {
say 'It seems that safebrowsing_api_key isn\'t set. Please, check your configuration';
......@@ -113,7 +113,7 @@ sub get_shorts {
for my $short (@shorts) {
my $u = Lstu::DB::URL->new(app => $c->app, short => $short);
if ($u->url) {
if ($u->url && !$u->disabled) {
push @results, $u->to_hash;
} else {
say sprintf('Sorry, unable to find an URL with short = %s', $short);
......
......@@ -31,11 +31,13 @@ sub run {
);
}
if (scalar @remove) {
my @ips;
c(@remove)->each(
sub {
my ($e, $num) = @_;
my $u = get_short($c, $e);
if ($u) {
push @ips, $u->created_by if $u->created_by;
print_infos($u->to_hash);
my $confirm = ($yes) ? 'yes' : undef;
unless (defined $confirm) {
......@@ -55,28 +57,35 @@ sub run {
}
}
);
say sprintf("If you want to ban the uploaders' IPs, please do:\n carton exec script/lstu ban --ban %s", join(' ', @ips)) if @ips;
}
if ($search) {
my $u = Lstu::DB::URL->new(app => $c->app)->search_url($search);
my @shorts;
my @ips;
$u->each(sub {
my ($e, $num) = @_;
push @shorts, $e->{short};
push @ips, $e->{created_by} if $e->{created_by};
print_infos($e);
});
say sprintf('%d matching URLs', $u->size);
say sprintf("If you want to delete those URLs, please do:\n carton exec script/lstu url --remove %s", join(' ', @shorts)) if @shorts;
say sprintf("If you want to ban those IPs, please do:\n carton exec script/lstu ban --ban %s", join(' ', @ips)) if @ips;
}
if ($ip) {
my $u = Lstu::DB::URL->new(app => $c->app)->search_creator($ip);
my @shorts;
my @ips;
$u->each(sub {
my ($e, $num) = @_;
push @shorts, $e->{short};
push @ips, $e->{created_by} if $e->{created_by};
print_infos($e);
});
say sprintf('%d matching URLs', $u->size);
say sprintf("If you want to delete those URLs, please do:\n carton exec script/lstu url --remove %s", join(' ', @shorts)) if @shorts;
say sprintf("If you want to ban those IPs, please do:\n carton exec script/lstu ban --ban %s", join(' ', @ips)) if @ips;
}
}
......@@ -85,7 +94,7 @@ sub get_short {
my $short = shift;
my $u = Lstu::DB::URL->new(app => $c->app, short => $short);
if ($u->url) {
if ($u->url && !$u->disabled) {
return $u;
} else {
say sprintf('Sorry, unable to find an URL with short = %s', $short);
......
......@@ -184,10 +184,13 @@ sub get {
my $url;
if (scalar(@{$c->config('memcached_servers')})) {
$url = $c->chi('lstu_urls_cache')->compute($short, undef, sub {
return Lstu::DB::URL->new(app => $c, short => $short)->url;
my $db_url = Lstu::DB::URL->new(app => $c, short => $short);
return $db_url->url unless $db_url->disabled;
return undef;
});
} else {
$url = Lstu::DB::URL->new(app => $c, short => $short)->url;
my $db_url = Lstu::DB::URL->new(app => $c, short => $short);
$url = $db_url->url unless $db_url->disabled;
}
if ($url) {
......
......@@ -7,6 +7,7 @@ has 'url';
has 'counter' => 0;
has 'timestamp';
has 'created_by';
has 'disabled' => 0;
has 'record' => 0;
has 'app';
......@@ -35,6 +36,8 @@ Have a look at Lstu::DB::URL::SQLite's code: it's simple and may be more underst
=item B<created_by> : the IP address of the creator
=item B<disabled> : boolean (0 or 1), is the URL active?
=item B<app> : a mojolicious object
=back
......@@ -139,9 +142,9 @@ sub write {
my $c = shift;
if ($c->record) {
$c->app->dbi->db->query('UPDATE lstu SET url = ?, counter = ?, timestamp = ?, created_by = ? WHERE short = ?', $c->url, $c->counter, $c->timestamp, $c->created_by, $c->short);
$c->app->dbi->db->query('UPDATE lstu SET url = ?, counter = ?, timestamp = ?, created_by = ?, disabled = ? WHERE short = ?', $c->url, $c->counter, $c->timestamp, $c->created_by, $c->disabled, $c->short);
} else {
$c->app->dbi->db->query('INSERT INTO lstu (short, url, counter, timestamp, created_by) VALUES (?, ?, ?, ?, ?)', $c->short, $c->url, $c->counter, $c->timestamp, $c->created_by);
$c->app->dbi->db->query('INSERT INTO lstu (short, url, counter, timestamp, created_by, disabled) VALUES (?, ?, ?, ?, ?, ?)', $c->short, $c->url, $c->counter, $c->timestamp, $c->created_by, $c->disabled);
$c->record(1);
}
......@@ -167,20 +170,15 @@ sub write {
sub remove {
my $c = shift;
$c->app->dbi->db->query('DELETE FROM lstu WHERE short = ?', $c->short);
my $h = $c->app->dbi->db->query('SELECT * FROM lstu WHERE short = ?', $c->short)->hashes;
if ($h->size) {
# We found the URL, it hasn't been removed
return 0;
} else {
$c->app->dbi->db->query('UPDATE lstu SET disabled = 1 WHERE short = ?', $c->short);
my $disabled = $c->app->dbi->db->query('SELECT disabled FROM lstu WHERE short = ?', $c->short)->hashes->first->{disabled};
if ($disabled) {
if (scalar(@{$c->app->config('memcached_servers')})) {
$c->app->chi('lstu_urls_cache')->remove($c->short);
}
$c = Lstu::DB::URL->new(app => $c->app);
# We didn't found the URL, it has been removed
return 1;
}
return $disabled;
}
=head2 exist
......@@ -494,6 +492,7 @@ sub _slurp {
$c->counter($h->first->{counter});
$c->timestamp($h->first->{timestamp});
$c->created_by($h->first->{created_by});
$c->disabled($h->first->{disabled});
$c->record(1);
}
......
......@@ -24,18 +24,15 @@ sub increment_counter {
sub remove {
my $c = shift;
my $h = $c->app->dbi->db->query('DELETE FROM lstu WHERE short = ? RETURNING *', $c->short)->hashes;
# $h->size is the number of deleted rows
# 0 means failure
# 1 means success
if ($h->size) {
my $disabled = $c->app->dbi->db->query('UPDATE lstu SET disabled = 1 WHERE short = ? RETURNING disabled', $c->short)->hashes->first->{disabled};
if ($disabled) {
if (scalar(@{$c->app->config('memcached_servers')})) {
$c->app->chi('lstu_urls_cache')->remove($c->short);
}
$c = Lstu::DB::URL->new(app => $c->app);
}
return $h->size;
return $disabled;
}
1;
......@@ -25,9 +25,9 @@ sub register {
my $sql = Mojo::SQLite->new('sqlite:'.$app->config('db_path'));
my $migrations = $sql->migrations;
if ($app->mode eq 'development' && $ENV{LSTU_DEBUG}) {
$migrations->from_file('utilities/migrations/sqlite.sql')->migrate(0)->migrate(2);
$migrations->from_file('utilities/migrations/sqlite.sql')->migrate(0)->migrate(3);
} else {
$migrations->from_file('utilities/migrations/sqlite.sql')->migrate(2);
$migrations->from_file('utilities/migrations/sqlite.sql')->migrate(3);
}
} elsif ($app->config('dbtype') eq 'postgresql') {
require Mojo::Pg;
......@@ -36,9 +36,9 @@ sub register {
# Database migration
my $migrations = Mojo::Pg::Migrations->new(pg => $app->dbi);
if ($app->mode eq 'development' && $ENV{LSTU_DEBUG}) {
$migrations->from_file('utilities/migrations/postgresql.sql')->migrate(0)->migrate(3);
$migrations->from_file('utilities/migrations/postgresql.sql')->migrate(0)->migrate(4);
} else {
$migrations->from_file('utilities/migrations/postgresql.sql')->migrate(3);
$migrations->from_file('utilities/migrations/postgresql.sql')->migrate(4);
}
} elsif ($app->config('dbtype') eq 'mysql') {
require Mojo::mysql;
......@@ -47,9 +47,9 @@ sub register {
# Database migration
my $migrations = Mojo::mysql::Migrations->new(mysql => $app->dbi);
if ($app->mode eq 'development' && $ENV{LSTU_DEBUG}) {
$migrations->from_file('utilities/migrations/mysql.sql')->migrate(0)->migrate(2);
$migrations->from_file('utilities/migrations/mysql.sql')->migrate(0)->migrate(3);
} else {
$migrations->from_file('utilities/migrations/mysql.sql')->migrate(2);
$migrations->from_file('utilities/migrations/mysql.sql')->migrate(3);
}
}
......
......@@ -231,7 +231,7 @@ msgid "The URL you want to shorten comes from a domain (%1) that is blacklisted
msgstr ""
#. ($c->url_for('/')
#: lib/Lstu/Controller/Admin.pm:131 lib/Lstu/Controller/Stats.pm:184 lib/Lstu/Controller/URL.pm:223
#: lib/Lstu/Controller/Admin.pm:131 lib/Lstu/Controller/Stats.pm:184 lib/Lstu/Controller/URL.pm:226
msgid "The shortened URL %1 doesn't exist."
msgstr ""
......
......@@ -22,3 +22,7 @@ DROP TABLE lstu;
ALTER TABLE lstu ADD created_by text;
-- 2 down
ALTER TABLE lstu DROP COLUMN created_by;
-- 3 up
ALTER TABLE lstu ADD disabled integer DEFAULT 0;
-- 3 down
ALTER TABLE lstu DROP COLUMN disabled;
......@@ -26,3 +26,7 @@ DROP INDEX empty_short_idx;
ALTER TABLE lstu ADD COLUMN created_by text;
-- 3 down
ALTER TABLE lstu DROP COLUMN created_by;
-- 4 up
ALTER TABLE lstu ADD disabled integer default 0;
-- 4 down
ALTER TABLE lstu DROP COLUMN disabled;
......@@ -34,3 +34,18 @@ BEGIN TRANSACTION;
DROP TABLE lstu;
ALTER TABLE lstu_backup RENAME TO lstu;
COMMIT;
-- 3 up
ALTER TABLE lstu ADD COLUMN disabled INTEGER default 0;
-- 3 down
BEGIN TRANSACTION;
CREATE TABLE lstu_backup(
short TEXT PRIMARY KEY,
url TEXT,
counter INTEGER,
timestamp INTEGER,
created_by TEXT
);
INSERT INTO lstu_backup SELECT short, url, counter, timestamp, created_by FROM lstu;
DROP TABLE lstu;
ALTER TABLE lstu_backup RENAME TO lstu;
COMMIT;
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment