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