2
2
3
3
namespace Valet ;
4
4
5
- use DomainException ;
6
-
7
5
class Site
8
6
{
9
7
public $ config ;
@@ -89,7 +87,7 @@ public function getCertificates($path)
89
87
return collect ($ this ->files ->scanDir ($ path ))->filter (function ($ value , $ key ) {
90
88
return ends_with ($ value , '.crt ' );
91
89
})->map (function ($ cert ) {
92
- return substr ($ cert , 0 , -8 );
90
+ return substr ($ cert , 0 , -9 );
93
91
})->flip ();
94
92
}
95
93
@@ -104,16 +102,44 @@ public function getLinks($path, $certs)
104
102
{
105
103
$ config = $ this ->config ->read ();
106
104
105
+ $ httpPort = $ this ->httpSuffix ();
106
+ $ httpsPort = $ this ->httpsSuffix ();
107
+
107
108
return collect ($ this ->files ->scanDir ($ path ))->mapWithKeys (function ($ site ) use ($ path ) {
108
109
return [$ site => $ this ->files ->readLink ($ path . '/ ' . $ site )];
109
- })->map (function ($ path , $ site ) use ($ certs , $ config ) {
110
+ })->map (function ($ path , $ site ) use ($ certs , $ config, $ httpPort , $ httpsPort ) {
110
111
$ secured = $ certs ->has ($ site );
111
- $ url = ($ secured ? 'https ' : 'http ' ) . ':// ' . $ site . '. ' . $ config ['domain ' ];
112
+
113
+ $ url = ($ secured ? 'https ' : 'http ' ) . ':// ' . $ site . '. ' . $ config ['domain ' ] . ($ secured ? $ httpsPort : $ httpPort );
112
114
113
115
return [$ site , $ secured ? ' X ' : '' , $ url , $ path ];
114
116
});
115
117
}
116
118
119
+ /**
120
+ * Return http port suffix
121
+ *
122
+ * @return string
123
+ */
124
+ public function httpSuffix ()
125
+ {
126
+ $ port = $ this ->config ->get ('port ' , 80 );
127
+
128
+ return ($ port == 80 ) ? '' : ': ' . $ port ;
129
+ }
130
+
131
+ /**
132
+ * Return https port suffix
133
+ *
134
+ * @return string
135
+ */
136
+ public function httpsSuffix ()
137
+ {
138
+ $ port = $ this ->config ->get ('https_port ' , 443 );
139
+
140
+ return ($ port == 443 ) ? '' : ': ' . $ port ;
141
+ }
142
+
117
143
/**
118
144
* Unlink the given symbolic link.
119
145
*
@@ -166,14 +192,14 @@ public function resecureForNewDomain($oldDomain, $domain)
166
192
/**
167
193
* Get all of the URLs that are currently secured.
168
194
*
169
- * @return array
195
+ * @return \Illuminate\Support\Collection
170
196
*/
171
197
public function secured ()
172
198
{
173
199
return collect ($ this ->files ->scandir ($ this ->certificatesPath ()))
174
200
->map (function ($ file ) {
175
201
return str_replace (['.key ' , '.csr ' , '.crt ' , '.conf ' ], '' , $ file );
176
- })->unique ()->values ()-> all () ;
202
+ })->unique ()->values ();
177
203
}
178
204
179
205
/**
@@ -190,10 +216,7 @@ public function secure($url)
190
216
191
217
$ this ->createCertificate ($ url );
192
218
193
- $ this ->files ->putAsUser (
194
- VALET_HOME_PATH . '/Nginx/ ' . $ url ,
195
- $ this ->buildSecureNginxServer ($ url )
196
- );
219
+ $ this ->createSecureNginxServer ($ url );
197
220
}
198
221
199
222
/**
@@ -275,6 +298,17 @@ public function trustCertificate($crtPath, $url)
275
298
));
276
299
}
277
300
301
+ /**
302
+ * @param $url
303
+ */
304
+ public function createSecureNginxServer ($ url )
305
+ {
306
+ $ this ->files ->putAsUser (
307
+ VALET_HOME_PATH . '/Nginx/ ' . $ url ,
308
+ $ this ->buildSecureNginxServer ($ url )
309
+ );
310
+ }
311
+
278
312
/**
279
313
* Build the TLS secured Nginx server for the given URL.
280
314
*
@@ -285,9 +319,17 @@ public function buildSecureNginxServer($url)
285
319
{
286
320
$ path = $ this ->certificatesPath ();
287
321
288
- return str_replace (
289
- ['VALET_HOME_PATH ' , 'VALET_SERVER_PATH ' , 'VALET_SITE ' , 'VALET_CERT ' , 'VALET_KEY ' ],
290
- [VALET_HOME_PATH , VALET_SERVER_PATH , $ url , $ path . '/ ' . $ url . '.crt ' , $ path . '/ ' . $ url . '.key ' ],
322
+ return str_array_replace (
323
+ [
324
+ 'VALET_HOME_PATH ' => VALET_HOME_PATH ,
325
+ 'VALET_SERVER_PATH ' => VALET_SERVER_PATH ,
326
+ 'VALET_SITE ' => $ url ,
327
+ 'VALET_CERT ' => $ path . '/ ' . $ url . '.crt ' ,
328
+ 'VALET_KEY ' => $ path . '/ ' . $ url . '.key ' ,
329
+ 'VALET_HTTP_PORT ' => $ this ->config ->get ('port ' , 80 ),
330
+ 'VALET_HTTPS_PORT ' => $ this ->config ->get ('https_port ' , 443 ),
331
+ 'VALET_REDIRECT_PORT ' => $ this ->httpsSuffix (),
332
+ ],
291
333
$ this ->files ->get (__DIR__ . '/../stubs/secure.valet.conf ' )
292
334
);
293
335
}
@@ -313,6 +355,18 @@ public function unsecure($url)
313
355
}
314
356
}
315
357
358
+ /**
359
+ * Regenerate all secured file configurations
360
+ *
361
+ * @return void
362
+ */
363
+ public function regenerateSecuredSitesConfig ()
364
+ {
365
+ $ this ->secured ()->each (function ($ url ) {
366
+ $ this ->createSecureNginxServer ($ url );
367
+ });
368
+ }
369
+
316
370
/**
317
371
* Get the path to the linked Valet sites.
318
372
*
0 commit comments