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