]>
Commit | Line | Data |
---|---|---|
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 %sockopts = ( | |
39 | LocalPort => 80, | |
40 | ReuseAddr => 1, | |
41 | ); | |
42 | my $server = HTTP::Daemon->new( LocalHost => '::', V6Only => 0, %sockopts) // | |
43 | HTTP::Daemon->new( LocalHost => '0.0.0.0', %sockopts) | |
44 | or die "Failed to initialize HTTP daemon\n"; | |
45 | ||
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; |