Skip to content
This repository was archived by the owner on Dec 9, 2019. It is now read-only.

Commit 7492d52

Browse files
TheNodicpriego
authored andcommitted
HTTPS port (cpriego#109)
* Configure HTTPS port * Error message when tring to share a secured site * Regenerate HTTPS config when changing HTTP port * Fix certificates returning final dot * Valet links display port when needed * Omit 443 port on https redirect * Remove share test for https sites * String replace with associative arrays
1 parent 3d989bb commit 7492d52

File tree

9 files changed

+141
-47
lines changed

9 files changed

+141
-47
lines changed

cli/Valet/Configuration.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,20 @@ public function read()
197197
return json_decode($this->files->get($this->path()), true);
198198
}
199199

200+
/**
201+
* Get a configuration value.
202+
*
203+
* @param string $key
204+
* @param mixed $default
205+
* @return mixed
206+
*/
207+
public function get($key, $default = null)
208+
{
209+
$config = $this->read();
210+
211+
return array_key_exists($key, $config) ? $config[$key] : $default;
212+
}
213+
200214
/**
201215
* Update a specific key in the configuration file.
202216
*

cli/Valet/Nginx.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,12 @@ public function installConfiguration()
8080

8181
$this->files->putAsUser(
8282
$nginx,
83-
str_replace(['VALET_USER', 'VALET_GROUP', 'VALET_HOME_PATH', 'VALET_PID'], [user(), group(), VALET_HOME_PATH, $pid_string], $contents)
83+
str_array_replace([
84+
'VALET_USER' => user(),
85+
'VALET_GROUP' => group(),
86+
'VALET_HOME_PATH' => VALET_HOME_PATH,
87+
'VALET_PID' => $pid_string,
88+
], $contents)
8489
);
8590
}
8691

cli/Valet/PhpFpm.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ public function installConfiguration()
7575

7676
$this->files->putAsUser(
7777
$this->fpmConfigPath().'/valet.conf',
78-
str_replace(['VALET_USER', 'VALET_GROUP','VALET_HOME_PATH'], [user(), group(), VALET_HOME_PATH], $contents)
78+
str_array_replace([
79+
'VALET_USER' => user(),
80+
'VALET_GROUP' => group(),
81+
'VALET_HOME_PATH' => VALET_HOME_PATH,
82+
], $contents)
7983
);
8084
}
8185

cli/Valet/Site.php

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Valet;
44

5-
use DomainException;
6-
75
class Site
86
{
97
public $config;
@@ -89,7 +87,7 @@ public function getCertificates($path)
8987
return collect($this->files->scanDir($path))->filter(function ($value, $key) {
9088
return ends_with($value, '.crt');
9189
})->map(function ($cert) {
92-
return substr($cert, 0, -8);
90+
return substr($cert, 0, -9);
9391
})->flip();
9492
}
9593

@@ -104,16 +102,44 @@ public function getLinks($path, $certs)
104102
{
105103
$config = $this->config->read();
106104

105+
$httpPort = $this->httpSuffix();
106+
$httpsPort = $this->httpsSuffix();
107+
107108
return collect($this->files->scanDir($path))->mapWithKeys(function ($site) use ($path) {
108109
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) {
110111
$secured = $certs->has($site);
111-
$url = ($secured ? 'https' : 'http') . '://' . $site . '.' . $config['domain'];
112+
113+
$url = ($secured ? 'https' : 'http') . '://' . $site . '.' . $config['domain'] . ($secured ? $httpsPort : $httpPort);
112114

113115
return [$site, $secured ? ' X' : '', $url, $path];
114116
});
115117
}
116118

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+
117143
/**
118144
* Unlink the given symbolic link.
119145
*
@@ -166,14 +192,14 @@ public function resecureForNewDomain($oldDomain, $domain)
166192
/**
167193
* Get all of the URLs that are currently secured.
168194
*
169-
* @return array
195+
* @return \Illuminate\Support\Collection
170196
*/
171197
public function secured()
172198
{
173199
return collect($this->files->scandir($this->certificatesPath()))
174200
->map(function ($file) {
175201
return str_replace(['.key', '.csr', '.crt', '.conf'], '', $file);
176-
})->unique()->values()->all();
202+
})->unique()->values();
177203
}
178204

179205
/**
@@ -190,10 +216,7 @@ public function secure($url)
190216

191217
$this->createCertificate($url);
192218

193-
$this->files->putAsUser(
194-
VALET_HOME_PATH . '/Nginx/' . $url,
195-
$this->buildSecureNginxServer($url)
196-
);
219+
$this->createSecureNginxServer($url);
197220
}
198221

199222
/**
@@ -275,6 +298,17 @@ public function trustCertificate($crtPath, $url)
275298
));
276299
}
277300

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+
278312
/**
279313
* Build the TLS secured Nginx server for the given URL.
280314
*
@@ -285,9 +319,17 @@ public function buildSecureNginxServer($url)
285319
{
286320
$path = $this->certificatesPath();
287321

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+
],
291333
$this->files->get(__DIR__ . '/../stubs/secure.valet.conf')
292334
);
293335
}
@@ -313,6 +355,18 @@ public function unsecure($url)
313355
}
314356
}
315357

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+
316370
/**
317371
* Get the path to the linked Valet sites.
318372
*

cli/includes/helpers.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,15 @@ function group()
189189

190190
return exec('id -gn '.$_SERVER['SUDO_USER']);
191191
}
192+
193+
/**
194+
* Search and replace using associative array
195+
*
196+
* @param array $searchAndReplace
197+
* @param string $subject
198+
* @return string
199+
*/
200+
function str_array_replace($searchAndReplace, $subject)
201+
{
202+
return str_replace(array_keys($searchAndReplace), array_values($searchAndReplace), $subject);
203+
}

cli/stubs/secure.valet.conf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
server {
2-
listen 80;
2+
listen VALET_HTTP_PORT;
33
server_name VALET_SITE www.VALET_SITE *.VALET_SITE;
4-
return 301 https://$host$request_uri;
4+
return 301 https://$hostVALET_REDIRECT_PORT$request_uri;
55
}
66

77
server {
8-
listen 443 ssl http2;
8+
listen VALET_HTTPS_PORT ssl http2;
99
server_name VALET_SITE www.VALET_SITE *.VALET_SITE;
1010
root /;
1111
charset utf-8;

cli/valet.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,44 @@
7979
/**
8080
* Get or set the port number currently being used by Valet.
8181
*/
82-
$app->command('port [port]', function ($port = null) {
82+
$app->command('port [port] [--https]', function ($port = null, $https) {
8383
if ($port === null) {
84-
return info('Current Nginx port: ' . Configuration::read()['port']);
84+
info('Current Nginx port (HTTP): ' . Configuration::get('port', 80));
85+
info('Current Nginx port (HTTPS): ' . Configuration::get('https_port', 443));
86+
return;
8587
}
8688

8789
$port = trim($port);
88-
Nginx::updatePort($port);
8990

90-
Configuration::updateKey('port', $port);
91+
if ($https) {
92+
Configuration::updateKey('https_port', $port);
93+
}else{
94+
Nginx::updatePort($port);
95+
Configuration::updateKey('port', $port);
96+
}
97+
98+
Site::regenerateSecuredSitesConfig();
99+
91100
Nginx::restart();
92101
PhpFpm::restart();
93102

94-
info('Your Nginx port has been updated to ['.$port.'].');
103+
$protocol = $https ? 'HTTPS' : 'HTTP';
104+
info("Your Nginx {$protocol} port has been updated to [{$port}].");
95105
})->descriptions('Get or set the port number used for Valet sites');
96106

107+
/**
108+
* Determine if the site is secured or not
109+
*/
110+
$app->command('secured [site]', function ($site) {
111+
if (Site::secured()->contains($site)) {
112+
info("{$site} is secured.");
113+
return 1;
114+
}
115+
116+
info("{$site} is not secured.");
117+
return 0;
118+
})->descriptions('Determine if the site is secured or not');
119+
97120
/**
98121
* Add the current working directory to the paths configuration.
99122
*/

tests/Functional/ShareTest.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,4 @@ public function test_we_can_share_an_http_site()
3939

4040
$ngrok->stop();
4141
}
42-
43-
public function test_we_can_share_an_https_site()
44-
{
45-
// Secure site
46-
$this->valetCommand('secure', $_SERVER['HOME'] . '/valet-site');
47-
48-
// Start ngrok tunnel
49-
$ngrok = $this->background($this->valet().' share', $_SERVER['HOME'] . '/valet-site');
50-
51-
// Assert tunnel URL is reachable
52-
$tunnel = Ngrok::currentTunnelUrl();
53-
54-
// TODO: Refactor Ngrok class so we can retrieve https tunnel too
55-
$tunnel = str_replace('http://', 'https://', $tunnel);
56-
57-
$this->assertContains('ngrok.io', $tunnel);
58-
59-
$response = \Httpful\Request::get($tunnel)->send();
60-
$this->assertEquals(200, $response->code);
61-
$this->assertContains('Valet site', $response->body);
62-
63-
$ngrok->stop();
64-
}
6542
}

valet

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ then
8989
fi
9090
done
9191

92+
php "$DIR/cli/valet.php" secured "$HOST.$DOMAIN" &> /dev/null || (
93+
printf "\033[1;31mSecured sites can not be shared.\033[0m\n"
94+
exit 1
95+
)
96+
9297
# Fetch Ngrok URL In Background...
9398
bash "$DIR/cli/scripts/fetch-share-url.sh" &
9499
sudo -u $USER "$DIR/bin/ngrok" http "$HOST.$DOMAIN:$PORT" -host-header=rewrite ${*:2}

0 commit comments

Comments
 (0)