Skip to content

Commit

Permalink
Add —not-redirected argument to secure command
Browse files Browse the repository at this point in the history
This makes it possible to still access the site via http if it is secure

This touches laravel#382, laravel#148, laravel#156, laravel#504
  • Loading branch information
sjelfull committed Mar 10, 2018
1 parent 567d38e commit b86812c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
23 changes: 16 additions & 7 deletions cli/Valet/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,12 @@ function secured()
/**
* Secure the given host with TLS.
*
* @param string $url
* @param string $url
* @param bool $notRedirected
*
* @return void
*/
function secure($url)
function secure($url, $notRedirected = false)
{
$this->unsecure($url);

Expand All @@ -188,7 +190,7 @@ function secure($url)
$this->createCertificate($url);

$this->files->putAsUser(
VALET_HOME_PATH.'/Nginx/'.$url, $this->buildSecureNginxServer($url)
VALET_HOME_PATH.'/Nginx/'.$url, $this->buildSecureNginxServer($url, $notRedirected)
);
}

Expand Down Expand Up @@ -270,16 +272,23 @@ function buildCertificateConf($path, $url)
/**
* Build the TLS secured Nginx server for the given URL.
*
* @param string $url
* @param string $url
* @param bool $notRedirected
*
* @return string
*/
function buildSecureNginxServer($url)
function buildSecureNginxServer($url, $notRedirected = false)
{
$path = $this->certificatesPath();
$redirectStub = !$notRedirected ? str_replace(
['VALET_SITE'],
[$url],
$this->files->get(__DIR__.'/../stubs/redirect-unsecure.valet.conf')
) : '';

return str_replace(
['VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX', 'VALET_SITE', 'VALET_CERT', 'VALET_KEY'],
[VALET_HOME_PATH, VALET_SERVER_PATH, VALET_STATIC_PREFIX, $url, $path.'/'.$url.'.crt', $path.'/'.$url.'.key'],
['VALET_REDIRECT_STUB', 'VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX', 'VALET_SITE', 'VALET_CERT', 'VALET_KEY'],
[$redirectStub, VALET_HOME_PATH, VALET_SERVER_PATH, VALET_STATIC_PREFIX, $url, $path.'/'.$url.'.crt', $path.'/'.$url.'.key'],
$this->files->get(__DIR__.'/../stubs/secure.valet.conf')
);
}
Expand Down
5 changes: 5 additions & 0 deletions cli/stubs/redirect-unsecure.valet.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
server {
listen 80;
server_name VALET_SITE www.VALET_SITE *.VALET_SITE;
return 301 https://$host$request_uri;
}
6 changes: 1 addition & 5 deletions cli/stubs/secure.valet.conf
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
server {
listen 80;
server_name VALET_SITE www.VALET_SITE *.VALET_SITE;
return 301 https://$host$request_uri;
}
VALET_REDIRECT_STUB

server {
listen 443 ssl http2;
Expand Down
14 changes: 9 additions & 5 deletions cli/valet.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,17 @@
/**
* Register a symbolic link with Valet.
*/
$app->command('link [name] [--secure]', function ($name, $secure) {
$app->command('link [name] [--secure] [--not-redirected]', function ($name, $secure, $notRedirected) {
$linkPath = Site::link(getcwd(), $name = $name ?: basename(getcwd()));

info('A ['.$name.'] symbolic link has been created in ['.$linkPath.'].');

if ($secure) {
$this->runCommand('secure '.$name);
}
})->descriptions('Link the current working directory to Valet');
})->descriptions('Link the current working directory to Valet' [
'--not-redirected' => 'Make the site accessible via port 80'
]);

/**
* Display all of the registered symbolic links.
Expand All @@ -124,17 +126,19 @@
/**
* Secure the given domain with a trusted TLS certificate.
*/
$app->command('secure [domain]', function ($domain = null) {
$app->command('secure [domain] [--not-redirected]', function ($domain = null, $notRedirected = null) {
$url = ($domain ?: Site::host(getcwd())).'.'.Configuration::read()['domain'];

Site::secure($url);
Site::secure($url, $notRedirected);

PhpFpm::restart();

Nginx::restart();

info('The ['.$url.'] site has been secured with a fresh TLS certificate.');
})->descriptions('Secure the given domain with a trusted TLS certificate');
})->descriptions('Secure the given domain with a trusted TLS certificate', [
'--not-redirected' => 'Make the site accessible via port 80'
]);

/**
* Stop serving the given domain over HTTPS and remove the trusted TLS certificate.
Expand Down

0 comments on commit b86812c

Please sign in to comment.