spf-dkim-dmarc-demo/console/web-api/lib/Email/SpoofingDemo/Web.pm

128 lines
3.4 KiB
Perl
Raw Normal View History

2023-10-25 15:50:25 +02:00
package Email::SpoofingDemo::Web;
use Dancer2;
use JSON;
use REST::Client;
our $VERSION = '0.1';
sub call_api {
my ($method, $target, $url, $body_parameters) = @_;
my $host = config->{'api'}{$target};
die "Invalid target: $target" unless defined $host;
my $client = REST::Client->new();
$client->setHost($host);
$client->setTimeout(5);
$client->addHeader('Accept' => 'application/json');
$client->addHeader('Content-Type' => 'application/json');
my $body_data;
$body_data = encode_json($body_parameters) if defined $body_parameters;
$client->request($method, $url, $body_data);
my $status = $client->responseCode();
if ($status =~ /^2\d\d$/) {
my $response;
if ($client->responseContent() ne '') {
$response = decode_json($client->responseContent());
}
return ($response, $status);
}
else {
warn "API request returned $status";
return ($client->responseContent(), $status);
}
}
2023-10-25 15:50:25 +02:00
get '/' => sub {
template 'index' => { 'title' => 'Accueil' };
};
get '/dns/zone-edit/:zone' => sub {
my $zone = route_parameters->get('zone');
if (defined $zone and not (grep { $_ eq $zone } @{config->{'editable_zones'}})) {
pass;
}
my $zone_contents;
if (defined $zone) {
my $client = REST::Client->new();
$client->setHost(config->{'api'}{'dns'});
$client->GET("/zone/${zone}/file");
my $response = from_json($client->responseContent());
$zone_contents = $response->{'contents'};
}
template 'dns/zone-edit' => {
'title' => 'Éditeur de zone DNS',
'zone_to_edit' => $zone // '',
'zone_contents' => $zone_contents // '',
};
};
post '/dns/zone-edit/:zone' => sub {
my $zone = route_parameters->{'zone'};
unless (grep { $_ eq $zone } @{config->{'editable_zones'}}) {
pass;
}
my $contents = body_parameters->{'zone-contents'};
my $client = REST::Client->new();
$client->setHost(config->{'api'}{'dns'});
$client->PUT("/zone/${zone}/file",
encode_json({ contents => $contents }),
{
"Content-Type" => "application/json",
"Accept" => "application/json"
});
my $success;
if ($client->responseCode() eq '200') {
$success = 'success';
} else {
warn "Got " . $client->responseCode() . " from upstream: " . $client->responseContent();
$success = 'failure'
}
redirect "/dns/zone-edit/$zone?success=$success", 303;
get '/recipient/settings' => sub {
my ($system_status, $http_code) = call_api(GET => 'recipient', '/status');
die if $http_code ne '200';
template 'recipient/settings' => {
title => 'Paramètres du système destinataire',
system_status => $system_status
};
};
post '/recipient/settings' => sub {
my %api_params = map {
$_ => body_parameters->{"$_-status"} ? 'enabled' : 'disabled'
} qw(spf dkim dmarc);
my (undef, $status) = call_api(PUT => 'recipient', '/status', \%api_params);
my $success = ($status eq 200) ? 'success' : 'failure';
redirect "/recipient/settings?success=$success", 303;
};
2023-10-25 15:50:25 +02:00
};
get '/recipient/webmail' => sub {
template 'recipient/webmail' => {
title => 'Courriels'
};
};
2023-10-25 15:50:25 +02:00
any qr{.*} => sub {
template '404';
};
dance;
true;