]> git.proxmox.com Git - proxmox-acme.git/blame - src/PVE/ACME/StandAlone.pm
plugins: remove get_subplugins
[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 {
122626b3 12 return ['http-01'];
5460050d
WL
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
5460050d 30sub setup {
f00829fd 31 my ($self, $acme, $auth, $data) = @_;
5460050d 32
d18383f0 33 print "Setting up webserver\n";
5460050d 34
f00829fd
FG
35 my $challenge = $self->extract_challenge($auth->{challenges});
36 my $key_auth = $acme->key_authorization($challenge->{token});
5460050d
WL
37
38 my $server = HTTP::Daemon->new(
39 LocalPort => 80,
40 ReuseAddr => 1,
d18383f0 41 ) or die "Failed to initialize HTTP daemon\n";
5460050d
WL
42 my $pid = fork() // die "Failed to fork HTTP daemon - $!\n";
43 if ($pid) {
d18383f0
WL
44 $data->{server} = $server;
45 $data->{pid} = $pid;
f00829fd 46 $data->{url} = $challenge->{url};
5460050d
WL
47 } else {
48 while (my $c = $server->accept()) {
49 while (my $r = $c->get_request()) {
d18383f0 50 if ($r->method() eq 'GET' and
f00829fd 51 $r->uri->path eq "/.well-known/acme-challenge/$challenge->{token}") {
d18383f0 52 my $resp = HTTP::Response->new(200, 'OK', undef, $key_auth);
5460050d
WL
53 $resp->request($r);
54 $c->send_response($resp);
55 } else {
56 $c->send_error(404, 'Not found.')
57 }
58 }
59 $c->close();
60 $c = undef;
61 }
62 }
63}
64
65sub teardown {
f00829fd 66 my ($self, $acme, $auth, $data) = @_;
5460050d 67
d18383f0
WL
68 eval { $data->{server}->close() };
69 kill('KILL', $data->{pid});
70 waitpid($data->{pid}, 0);
5460050d
WL
71}
72
731;