]> git.proxmox.com Git - proxmox-acme.git/blob - src/PVE/ACME/StandAlone.pm
plugins: remove get_subplugins
[proxmox-acme.git] / src / PVE / ACME / StandAlone.pm
1 package PVE::ACME::StandAlone;
2
3 use strict;
4 use warnings;
5
6 use HTTP::Daemon;
7 use HTTP::Response;
8
9 use base qw(PVE::ACME::Challenge);
10
11 sub supported_challenge_types {
12 return ['http-01'];
13 }
14
15 sub type {
16 return 'standalone';
17 }
18
19 sub properties {
20 return {};
21 }
22
23 sub options {
24 return {
25 nodes => { optional => 1 },
26 disable => { optional => 1 },
27 };
28 }
29
30 sub setup {
31 my ($self, $acme, $auth, $data) = @_;
32
33 print "Setting up webserver\n";
34
35 my $challenge = $self->extract_challenge($auth->{challenges});
36 my $key_auth = $acme->key_authorization($challenge->{token});
37
38 my $server = HTTP::Daemon->new(
39 LocalPort => 80,
40 ReuseAddr => 1,
41 ) or die "Failed to initialize HTTP daemon\n";
42 my $pid = fork() // die "Failed to fork HTTP daemon - $!\n";
43 if ($pid) {
44 $data->{server} = $server;
45 $data->{pid} = $pid;
46 $data->{url} = $challenge->{url};
47 } else {
48 while (my $c = $server->accept()) {
49 while (my $r = $c->get_request()) {
50 if ($r->method() eq 'GET' and
51 $r->uri->path eq "/.well-known/acme-challenge/$challenge->{token}") {
52 my $resp = HTTP::Response->new(200, 'OK', undef, $key_auth);
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
65 sub teardown {
66 my ($self, $acme, $auth, $data) = @_;
67
68 eval { $data->{server}->close() };
69 kill('KILL', $data->{pid});
70 waitpid($data->{pid}, 0);
71 }
72
73 1;