Skip to content

Commit 12fc779

Browse files
committed
Updated postip.pl
1 parent a5fead5 commit 12fc779

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

BaseInstall.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ Basic Setup
194194
5. Alternative: You can use `postip.pl` from this repository in the `crontab` to send an
195195
HTTP request instead of a UDP packet; remember to use the full pathname to the script
196196
and adjust the variables at the top of the script as necessary.
197-
TODO: The HMAC-SHA256 signature provided by the script can be used by the server to verify the authenticity.
197+
The HMAC-SHA256 signature provided by the script can be used by the server to verify the authenticity.
198198

199199
6. **Mail**: Configure Postfix either as "Local only" or "Internet Site" as appropriate in the following steps:
200200

postip.pl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
#!/usr/bin/perl
22
use warnings;
3-
use strict;
3+
use 5.014; # strict, HTTP::Tiny, JSON::PP
44
# should run on vanilla Perl, use core modules only
55
use Sys::Hostname 'hostname';
66
use Digest::SHA 'hmac_sha256_base64';
7+
use JSON::PP ();
8+
use HTTP::Tiny ();
79

8-
my $SECRET = 'password'; # for signature, so server can verify authenticity
9-
my $BASEURL = 'https://example.com/hellorpi/'; # end with slash!
10+
my $SECRET = 'secret'; # FIXME: CHANGE THIS - for signature, so server can verify authenticity
11+
my $URL = 'https://example.com/hellorpi';
1012

1113
# IPv4 only at the moment:
12-
my @ips = grep { /\A\d+(?:\.\d+){3}\z/ } split ' ', `/usr/bin/hostname -I`;
13-
die "hostname -I failed with \$?=$?\n" if $?;
14+
my @ips = sort grep { /\A\d+(?:\.\d+){3}\z/ } split ' ', `/usr/bin/hostname -I`;
15+
die "`hostname -I` failed with \$?=$?\n" if $?;
1416
exit unless @ips;
1517

1618
my $host = hostname;
1719
die "unexpected hostname '$host'\n" unless $host=~/\A[A-Za-z0-9\.\-\_]+\z/;
1820

19-
my $url = join '/', $host, @ips;
21+
my $sig = hmac_sha256_base64(join("\0", $host, @ips), $SECRET);
22+
$sig .= '=' while length($sig) % 4; # pad
2023

21-
my $sig = hmac_sha256_base64($url, $SECRET);
22-
$sig =~ tr#+/#-_#; # like Python's base64.urlsafe_b64encode
23-
#$sig =~ s/=+$//g; # not actually needed, docs say there won't be padding
24+
my $resp = HTTP::Tiny->new->request('POST', $URL, { content=>
25+
JSON::PP->new->ascii->canonical->pretty->encode(
26+
{ host => $host, ips => \@ips, sig => $sig }) });
27+
die "POST $URL => $resp->{status} $resp->{reason}\n" unless $resp->{success};
2428

25-
my @cmd = ('curl','--silent','--max-time','5',
26-
'--fail','--fail-early','--show-error',
27-
'--header','Content-Type: application/octet-stream',
28-
'--data-raw',$sig,"$BASEURL$url",'--output','/dev/null');
29-
system(@cmd) and die "@cmd\nFAILED with \$?=$?, \$!=$!\n";
29+
# spell: ignore hmac

0 commit comments

Comments
 (0)