send alerts

This commit is contained in:
Al Beano 2016-09-15 21:34:59 +01:00
parent d1c7095ba4
commit ba4133170a
7 changed files with 123 additions and 9 deletions

View File

@ -6,7 +6,7 @@ chown www:www /var/www/run
install -o www -g www -m 0400 httpd.conf /etc/
echo "permit nopass root as www" >> /etc/doas.conf
curl -L https://cpanmin.us | perl - App::cpanminus
cpanm -l /var/www/perl5 FCGI Switch Template::Simple File::Slurp Email::Valid DBD::SQLite Math::Random::Secure Email::Sender::Simple Email::Simple Email::Simple::Creator Email::Sender::Transport::SMTPS YAML::Tiny
cpanm -l /var/www/perl5 FCGI Switch Template::Simple File::Slurp Email::Valid DBD::SQLite Math::Random::Secure Email::Sender::Simple Email::Simple Email::Simple::Creator Email::Sender::Transport::SMTPS YAML::Tiny LWP::UserAgent JSON::Tiny LWP::Protocol::https
install -o www -g www -m 0500 tormon/* /var/www/tormon/
cat db.sql | sqlite3 /var/www/tormon.db
chown www:www /var/www/tormon.db
@ -18,7 +18,4 @@ echo 'echo "Starting tormon" && doas -u www /var/www/tormon/tormon.fcgi &' >> /e
sh /etc/rc.local # assuming tormon is the only thing in rc.local
rcctl enable httpd
rcctl start httpd
# updating
install -o www -g www -m 0500 tormon/* /var/www/tormon/
```

9
db.sql
View File

@ -3,5 +3,12 @@ create table users (
email text not null,
confirmed integer not null,
fp text not null,
secret text
secret text,
/*
status
0 - everything is fine
1 - the relay was down
2 - the relay has gone!
*/
status integer not null
);

View File

@ -5,3 +5,4 @@ mail:
port: 587
user: SMTP_Injection
password: yourapikey
from: "tormon@tor.uptime.party"

View File

@ -12,6 +12,8 @@
<input id=spam type=text name=spam>
<br />
<button action=submit>Submit</button>
<br />
<small><em>For bridges, you should use the hashed fingerprint.</em></small>
</form>
<hr>
<p id="footer">

107
tormon/notify.pl Executable file
View File

@ -0,0 +1,107 @@
#!/usr/bin/perl -I /var/www/perl5/lib/perl5
use 5.010;
use strict;
use warnings;
use LWP::UserAgent;
use JSON::Tiny qw(decode_json);
use YAML::Tiny;
use DBI;
use Email::Sender::Simple qw(sendmail);
use Email::Simple;
use Email::Simple::Creator;
use Email::Sender::Transport::SMTPS;
my $onionoo = "https://onionoo.torproject.org";
my $config = YAML::Tiny->read("/var/www/tormon.yml")->[0] or die $!;
# get data from onionoo
my $ua = new LWP::UserAgent (
timeout => 20,
max_size => 16 * 1024**2,
);
$ua->agent("tormon ($ua->_agent) | for info/contact please write to albino AT autistici DOT org");
my $resp = $ua->get("$onionoo/details?fields=running,fingerprint,hashed_fingerprint");
die unless $resp->is_success;
my $onions = decode_json($resp->decoded_content);
# TODO: check the last updated date and only proceed if it is a newer list
# init db
my $dbh = DBI->connect("dbi:SQLite:dbname=/var/www/tormon.db", "", "") or die $!;
# get rows
my $sth = $dbh->prepare("select * from users");
$sth->execute;
SUB: while (my $sub = $sth->fetchrow_hashref) {
next SUB unless $sub->{"confirmed"};
my $status;
RELAY: for my $relay (@{ $onions->{"relays"} }, @{ $onions->{"bridges"} }) {
# check whether it's a relay or a bridge
# for bridges, we need to read the hashed_fingerprint
my $fp;
if (defined $relay->{"fingerprint"}) {
$fp = "fingerprint";
} elsif (defined $relay->{"hashed_fingerprint"}) {
$fp = "hashed_fingerprint";
} else {
warn "Relay has neither a `fingerprint` nor a `hashed_fingerprint` attribute!";
next RELAY;
}
if ($sub->{"fp"} eq $relay->{$fp}) {
# we have a match
# is it up?
if ($relay->{"running"}) {
$status = 0;
} else {
$status = 1;
}
last RELAY;
}
}
$status = 2 if !defined $status;
if ($status > $sub->{"status"}) {
# send email
# TODO: async magic
my $email = Email::Simple->create(
header => [
To => $sub->{"email"},
From => '"Tor Relay Monitor" <' . $config->{"mail"}->{"from"} . '>',
Subject => "Your Tor node is down!"
],
body => "Hi,\n\nThe Tor node with the fingerprint '"
. $sub->{"fp"} . "' " .
($status == 1 ? "is down." : "has disappeared from the Tor network!"),
);
my $trans = new Email::Sender::Transport::SMTPS (
host => $config->{mail}->{host},
port => $config->{mail}->{port},
ssl => "starttls",
sasl_username => $config->{mail}->{user},
sasl_password => $config->{mail}->{password},
debug => 0,
);
sendmail($email, {
transport => $trans,
});
}
if ($status != $sub->{"status"}) {
# update status
my $q = $dbh->prepare("update users set status=? where id=?");
$q->bind_param(1, $status);
$q->bind_param(2, $sub->{"id"});
$q->execute;
$q->finish;
}
}
$sth->finish;

View File

@ -105,8 +105,8 @@ while ($request->Accept() <= 0) {
# Add the email to database
my $secret = rand_string();
$sth = $dbh->prepare("insert into users (email, confirmed, fp, secret)
values (?, 0, ?, ?);");
$sth = $dbh->prepare("insert into users (email, confirmed, fp, secret, status)
values (?, 0, ?, ?, 0);");
$sth->bind_param(1, $input{"email"});
$sth->bind_param(2, $input{"fp"});
$sth->bind_param(3, $secret);
@ -119,7 +119,7 @@ while ($request->Accept() <= 0) {
my $email = Email::Simple->create(
header => [
To => $input{"email"},
From => '"Tor Relay Monitor" <tormon@tor.uptime.party>',
From => '"Tor Relay Monitor" <' . $config->{"mail"}->{"from"} . '>',
Subject => "Confirm your email",
],
body => "Hi,\n\nSomebody entered your email into the Tor relay monitor. If this was you, please click the link below to activate notifications.\n\n$config->{baseurl}/confirm?id=$id&s=$secret\n\nIf this wasn't you, just delete this email. If you'd like to contact the administrator, please send an email to albino\@autistici.org.\n",

View File

@ -8,7 +8,7 @@
input{width: calc(30% - 20px);}
label{width: 70%;}
label, input{display: inline-block;}
form{margin-bottom: 30px}
form{margin-bottom: 15px}
#footer{margin-top: 0;color:#666;font-style:italic;font-size:12px;}
</style>
</head>