@@ -12,11 +12,13 @@ class Lescript
12
12
13
13
private $ certificatesDir ;
14
14
private $ webRootDir ;
15
+
16
+ /** @var \Psr\Log\LoggerInterface */
15
17
private $ logger ;
16
18
private $ client ;
17
19
private $ accountKeyPath ;
18
20
19
- public function __construct ($ certificatesDir , $ webRootDir , $ logger )
21
+ public function __construct ($ certificatesDir , $ webRootDir , $ logger = null )
20
22
{
21
23
$ this ->certificatesDir = $ certificatesDir ;
22
24
$ this ->webRootDir = $ webRootDir ;
@@ -32,25 +34,23 @@ public function initAccount()
32
34
// generate and save new private key for account
33
35
// ---------------------------------------------
34
36
35
- $ this ->logger -> info ('Starting new account registration ' );
37
+ $ this ->log ('Starting new account registration ' );
36
38
$ this ->generateKey (dirname ($ this ->accountKeyPath ));
37
39
$ this ->postNewReg ();
38
- $ this ->logger -> info ('New account certificate registered ' );
40
+ $ this ->log ('New account certificate registered ' );
39
41
40
42
} else {
41
43
42
- $ this ->logger -> info ('Account already registered. Continuing. ' );
44
+ $ this ->log ('Account already registered. Continuing. ' );
43
45
44
46
}
45
47
}
46
48
47
49
public function signDomains (array $ domains )
48
50
{
49
- $ this ->logger -> info ('Starting certificate generation process for domains ' );
51
+ $ this ->log ('Starting certificate generation process for domains ' );
50
52
51
- if (($ privateAccountKey = openssl_pkey_get_private ('file:// ' .$ this ->accountKeyPath )) === FALSE ) {
52
- throw new \RuntimeException (openssl_error_string ());
53
- }
53
+ $ privateAccountKey = $ this ->readPrivateKey ($ this ->accountKeyPath );
54
54
$ accountKeyDetails = openssl_pkey_get_details ($ privateAccountKey );
55
55
56
56
// start domains authentication
@@ -61,7 +61,7 @@ public function signDomains(array $domains)
61
61
// 1. getting available authentication options
62
62
// -------------------------------------------
63
63
64
- $ this ->logger -> info ("Requesting challenge for $ domain " );
64
+ $ this ->log ("Requesting challenge for $ domain " );
65
65
66
66
$ response = $ this ->signedRequest (
67
67
"/acme/new-authz " ,
@@ -70,9 +70,9 @@ public function signDomains(array $domains)
70
70
71
71
// choose http-01 challange only
72
72
$ challenge = array_reduce ($ response ['challenges ' ], function ($ v , $ w ) { return $ v ? $ v : ($ w ['type ' ] == 'http-01 ' ? $ w : false ); });
73
- if (!$ challenge ) throw new \RuntimeException ("HTTP Challenge for $ domain is not available " );
73
+ if (!$ challenge ) throw new \RuntimeException ("HTTP Challenge for $ domain is not available. Whole response: " . json_encode ( $ response ) );
74
74
75
- $ this ->logger -> info ("Got challenge token for $ domain " );
75
+ $ this ->log ("Got challenge token for $ domain " );
76
76
$ location = $ this ->client ->getLastLocation ();
77
77
78
78
@@ -103,14 +103,14 @@ public function signDomains(array $domains)
103
103
104
104
$ uri = "http:// $ {domain}/.well-known/acme-challenge/ $ {challenge['token ' ]}" ;
105
105
106
- $ this ->logger -> info ("Token for $ domain saved at $ tokenPath and should be available at $ uri " );
106
+ $ this ->log ("Token for $ domain saved at $ tokenPath and should be available at $ uri " );
107
107
108
108
// simple self check
109
109
if ($ payload !== trim (@file_get_contents ($ uri ))) {
110
110
throw new \RuntimeException ("Please check $ uri - token not available " );
111
111
}
112
112
113
- $ this ->logger -> info ("Sending request to challenge " );
113
+ $ this ->log ("Sending request to challenge " );
114
114
115
115
// send request to challenge
116
116
$ result = $ this ->signedRequest (
@@ -131,15 +131,15 @@ public function signDomains(array $domains)
131
131
$ ended = !($ result ['status ' ] === "pending " );
132
132
133
133
if (!$ ended ) {
134
- $ this ->logger -> info ("Verification pending, sleeping 1s " );
134
+ $ this ->log ("Verification pending, sleeping 1s " );
135
135
sleep (1 );
136
136
}
137
137
138
138
$ result = $ this ->client ->get ($ location );
139
139
140
140
} while (!$ ended );
141
141
142
- $ this ->logger -> info ("Verification ended with status: $ {result['status ' ]}" );
142
+ $ this ->log ("Verification ended with status: $ {result['status ' ]}" );
143
143
@unlink ($ tokenPath );
144
144
}
145
145
@@ -153,9 +153,7 @@ public function signDomains(array $domains)
153
153
}
154
154
155
155
// load domain key
156
- if (($ privateDomainKey = openssl_pkey_get_private ('file:// ' .$ domainPath .'/private.pem ' )) === FALSE ) {
157
- throw new \RuntimeException (openssl_error_string ());
158
- }
156
+ $ privateDomainKey = $ this ->readPrivateKey ($ domainPath .'/private.pem ' );
159
157
160
158
$ this ->client ->getLastLinks ();
161
159
@@ -178,17 +176,17 @@ public function signDomains(array $domains)
178
176
179
177
if ($ this ->client ->getLastCode () == 202 ) {
180
178
181
- $ this ->logger -> info ("Certificate generation pending, sleeping 1s " );
179
+ $ this ->log ("Certificate generation pending, sleeping 1s " );
182
180
sleep (1 );
183
181
184
182
} else if ($ this ->client ->getLastCode () == 200 ) {
185
183
186
- $ this ->logger -> info ("Got certificate! YAY! " );
184
+ $ this ->log ("Got certificate! YAY! " );
187
185
$ certificates [] = $ this ->parsePemFromBody ($ result );
188
186
189
187
190
188
foreach ($ this ->client ->getLastLinks () as $ link ) {
191
- $ this ->logger -> info ("Requesting chained cert at $ link " );
189
+ $ this ->log ("Requesting chained cert at $ link " );
192
190
$ result = $ this ->client ->get ($ link );
193
191
$ certificates [] = $ this ->parsePemFromBody ($ result );
194
192
}
@@ -203,16 +201,25 @@ public function signDomains(array $domains)
203
201
204
202
if (empty ($ certificates )) throw new \RuntimeException ('No certificates generated ' );
205
203
206
- $ this ->logger -> info ("Saving fullchain.pem " );
204
+ $ this ->log ("Saving fullchain.pem " );
207
205
file_put_contents ($ domainPath .'/fullchain.pem ' , implode ("\n" , $ certificates ));
208
206
209
- $ this ->logger -> info ("Saving cert.pem " );
207
+ $ this ->log ("Saving cert.pem " );
210
208
file_put_contents ($ domainPath .'/cert.pem ' , array_shift ($ certificates ));
211
209
212
- $ this ->logger -> info ("Saving chain.pem " );
210
+ $ this ->log ("Saving chain.pem " );
213
211
file_put_contents ($ domainPath ."/chain.pem " , implode ("\n" , $ certificates ));
214
212
215
- $ this ->logger ->info ("Done !!§§! " );
213
+ $ this ->log ("Done !!§§! " );
214
+ }
215
+
216
+ private function readPrivateKey ($ path )
217
+ {
218
+ if (($ key = openssl_pkey_get_private ('file:// ' .$ path )) === FALSE ) {
219
+ throw new \RuntimeException (openssl_error_string ());
220
+ }
221
+
222
+ return $ key ;
216
223
}
217
224
218
225
private function parsePemFromBody ($ body )
@@ -228,7 +235,7 @@ private function getDomainPath($domain)
228
235
229
236
private function postNewReg ()
230
237
{
231
- $ this ->logger -> info ('Sending registration to letsencrypt server ' );
238
+ $ this ->log ('Sending registration to letsencrypt server ' );
232
239
233
240
return $ this ->signedRequest (
234
241
'/acme/new-reg ' ,
@@ -305,12 +312,9 @@ private function generateKey($outputDirectory)
305
312
file_put_contents ($ outputDirectory .'/public.pem ' , $ details ['key ' ]);
306
313
}
307
314
308
- private function signedRequest ($ uri , array $ payload ) {
309
-
310
- if (($ privateKey = openssl_pkey_get_private ('file:// ' .$ this ->accountKeyPath )) === FALSE ) {
311
- throw new \RuntimeException (openssl_error_string ());
312
- }
313
-
315
+ private function signedRequest ($ uri , array $ payload )
316
+ {
317
+ $ privateKey = $ this ->readPrivateKey ($ this ->accountKeyPath );
314
318
$ details = openssl_pkey_get_details ($ privateKey );
315
319
316
320
$ header = array (
@@ -340,10 +344,19 @@ private function signedRequest($uri, array $payload) {
340
344
'signature ' => $ signed64
341
345
);
342
346
343
- $ this ->logger -> info ("Sending signed request to $ uri " );
347
+ $ this ->log ("Sending signed request to $ uri " );
344
348
345
349
return $ this ->client ->post ($ uri , json_encode ($ data ));
346
350
}
351
+
352
+ protected function log ($ message )
353
+ {
354
+ if ($ this ->logger ) {
355
+ $ this ->logger ->info ($ message );
356
+ } else {
357
+ echo $ message ."\n" ;
358
+ }
359
+ }
347
360
}
348
361
349
362
class Client
0 commit comments