]>
Commit | Line | Data |
---|---|---|
5460050d WL |
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 | ||
435e1726 WL |
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 | ||
16925001 WL |
36 | sub get_subplugins { |
37 | return []; | |
38 | } | |
39 | ||
5460050d | 40 | sub setup { |
d18383f0 | 41 | my ($class, $data) = @_; |
5460050d | 42 | |
d18383f0 | 43 | print "Setting up webserver\n"; |
5460050d | 44 | |
d18383f0 | 45 | my $key_auth = $data->{key_authorization}; |
5460050d WL |
46 | |
47 | my $server = HTTP::Daemon->new( | |
48 | LocalPort => 80, | |
49 | ReuseAddr => 1, | |
d18383f0 | 50 | ) or die "Failed to initialize HTTP daemon\n"; |
5460050d WL |
51 | my $pid = fork() // die "Failed to fork HTTP daemon - $!\n"; |
52 | if ($pid) { | |
d18383f0 WL |
53 | $data->{server} = $server; |
54 | $data->{pid} = $pid; | |
5460050d WL |
55 | } else { |
56 | while (my $c = $server->accept()) { | |
57 | while (my $r = $c->get_request()) { | |
d18383f0 WL |
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); | |
5460050d WL |
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 { | |
d18383f0 | 74 | my ($self, $data) = @_; |
5460050d | 75 | |
d18383f0 WL |
76 | eval { $data->{server}->close() }; |
77 | kill('KILL', $data->{pid}); | |
78 | waitpid($data->{pid}, 0); | |
5460050d WL |
79 | } |
80 | ||
81 | 1; |