]> git.proxmox.com Git - proxmox-acme.git/blame - src/PVE/ACME/StandAlone.pm
Use the caller's data instead of extracting it yourself.
[proxmox-acme.git] / src / PVE / ACME / StandAlone.pm
CommitLineData
5460050d
WL
1package PVE::ACME::StandAlone;
2
3use strict;
4use warnings;
5
6use HTTP::Daemon;
7use HTTP::Response;
8
9use base qw(PVE::ACME::Challenge);
10
11sub supported_challenge_types {
12 return { 'http-01' => 1 };
13}
14
435e1726
WL
15sub type {
16 return 'standalone';
17}
18
19sub properties {
20 return {};
21}
22
23sub options {
24 return {
25 nodes => { optional => 1 },
26 disable => { optional => 1 },
27 };
28}
29
30sub extract_challenge {
31 my ($self, $challenge) = @_;
32
33 return PVE::ACME::Challenge->extract_challenge($challenge, 'http-01');
34}
35
5460050d 36sub setup {
d18383f0 37 my ($class, $data) = @_;
5460050d 38
d18383f0 39 print "Setting up webserver\n";
5460050d 40
d18383f0 41 my $key_auth = $data->{key_authorization};
5460050d
WL
42
43 my $server = HTTP::Daemon->new(
44 LocalPort => 80,
45 ReuseAddr => 1,
d18383f0 46 ) or die "Failed to initialize HTTP daemon\n";
5460050d
WL
47 my $pid = fork() // die "Failed to fork HTTP daemon - $!\n";
48 if ($pid) {
d18383f0
WL
49 $data->{server} = $server;
50 $data->{pid} = $pid;
5460050d
WL
51 } else {
52 while (my $c = $server->accept()) {
53 while (my $r = $c->get_request()) {
d18383f0
WL
54 if ($r->method() eq 'GET' and
55 $r->uri->path eq "/.well-known/acme-challenge/$data->{token}") {
56 my $resp = HTTP::Response->new(200, 'OK', undef, $key_auth);
5460050d
WL
57 $resp->request($r);
58 $c->send_response($resp);
59 } else {
60 $c->send_error(404, 'Not found.')
61 }
62 }
63 $c->close();
64 $c = undef;
65 }
66 }
67}
68
69sub teardown {
d18383f0 70 my ($self, $data) = @_;
5460050d 71
d18383f0
WL
72 eval { $data->{server}->close() };
73 kill('KILL', $data->{pid});
74 waitpid($data->{pid}, 0);
5460050d
WL
75}
76
771;