-
Notifications
You must be signed in to change notification settings - Fork 2
/
qrserve
executable file
·48 lines (36 loc) · 1.1 KB
/
qrserve
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/perl
use strict;
use warnings;
use 5.10.0;
# serves up a QR code as a PNG in response to
# http://127.0.0.1:3536/qr/BASE32SECRET. Note that the IP should not be
# changed, for security reasons, although the port is fine.
{
package MyWebServer;
use HTTP::Server::Simple::CGI;
use base qw(HTTP::Server::Simple::CGI);
sub handle_request {
my $self = shift;
my $cgi = shift;
$|++;
if ( $cgi->path_info() =~ m(^/qr/([A-Z2-7]+)$)i ) {
print "HTTP/1.0 200 OK\r\n";
qrcode($1);
} else {
print "HTTP/1.0 404 Not found\r\n";
print $cgi->header, $cgi->start_html('Bad Base32 string'), $cgi->h1('Bad Base32 string'), $cgi->end_html;
}
}
sub qrcode {
my $secret = shift;
my $hn = `hostname -s`; chomp($hn);
print "Content-Type: image/png\r\n";
print "\r\n";
# print $cgi->header('image/png');
print `qrencode -s16 -m1 -o- otpauth://totp/$hn?secret=$secret`;
}
}
my $x = MyWebServer->new;
$x->host('127.0.0.1');
$x->port(3536);
$x->background();