Skip to content

Commit f6114b1

Browse files
authored
Merge pull request #1005 from mikaelpopowicz/feature/unsecure-proxy
Add unsecure proxy
2 parents d312a58 + 00707ce commit f6114b1

File tree

8 files changed

+136
-26
lines changed

8 files changed

+136
-26
lines changed

cli/Valet/Site.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -710,9 +710,10 @@ function unsecureAll()
710710
*
711711
* @param string $url The domain name to serve
712712
* @param string $host The URL to proxy to, eg: http://127.0.0.1:8080
713+
* @param bool $secure
713714
* @return string
714715
*/
715-
function proxyCreate($url, $host)
716+
function proxyCreate($url, $host, $secure = false)
716717
{
717718
if (!preg_match('~^https?://.*$~', $host)) {
718719
throw new \InvalidArgumentException(sprintf('"%s" is not a valid URL', $host));
@@ -724,7 +725,9 @@ function proxyCreate($url, $host)
724725
}
725726

726727
$siteConf = $this->replaceOldLoopbackWithNew(
727-
$this->files->get(__DIR__.'/../stubs/proxy.valet.conf'),
728+
$this->files->get(
729+
$secure ? __DIR__.'/../stubs/secure.proxy.valet.conf' : __DIR__.'/../stubs/proxy.valet.conf'
730+
),
728731
'VALET_LOOPBACK',
729732
$this->valetLoopback()
730733
);
@@ -735,9 +738,15 @@ function proxyCreate($url, $host)
735738
$siteConf
736739
);
737740

738-
$this->secure($url, $siteConf);
741+
if ($secure) {
742+
$this->secure($url, $siteConf);
743+
} else {
744+
$this->put($url, $siteConf);
745+
}
746+
747+
$protocol = $secure ? 'https' : 'http';
739748

740-
info('Valet will now proxy [https://'.$url.'] traffic to ['.$host.'].');
749+
info('Valet will now proxy ['.$protocol.'://'.$url.'] traffic to ['.$host.'].');
741750
}
742751

743752
/**
@@ -759,6 +768,24 @@ function proxyDelete($url)
759768
info('Valet will no longer proxy [https://'.$url.'].');
760769
}
761770

771+
/**
772+
* Create the given nginx host.
773+
*
774+
* @param string $url
775+
* @param string $siteConf pregenerated Nginx config file contents
776+
* @return void
777+
*/
778+
function put($url, $siteConf)
779+
{
780+
$this->unsecure($url);
781+
782+
$this->files->ensureDirExists($this->nginxPath(), user());
783+
784+
$this->files->putAsUser(
785+
$this->nginxPath($url), $siteConf
786+
);
787+
}
788+
762789
/**
763790
* Remove old loopback interface alias and add a new one if necessary.
764791
*

cli/stubs/proxy.valet.conf

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,16 @@ server {
44
listen 127.0.0.1:80;
55
#listen VALET_LOOPBACK:80; # valet loopback
66
server_name VALET_SITE www.VALET_SITE *.VALET_SITE;
7-
return 301 https://$host$request_uri;
8-
}
9-
10-
server {
11-
listen 127.0.0.1:443 ssl http2;
12-
#listen VALET_LOOPBACK:443 ssl http2; # valet loopback
13-
server_name VALET_SITE www.VALET_SITE *.VALET_SITE;
147
root /;
158
charset utf-8;
169
client_max_body_size 128M;
17-
http2_push_preload on;
1810

1911
location /VALET_STATIC_PREFIX/ {
2012
internal;
2113
alias /;
2214
try_files $uri $uri/;
2315
}
2416

25-
ssl_certificate "VALET_CERT";
26-
ssl_certificate_key "VALET_KEY";
27-
2817
access_log off;
2918
error_log "VALET_HOME_PATH/Log/VALET_SITE-error.log";
3019

cli/stubs/secure.proxy.valet.conf

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# valet stub: secure.proxy.valet.conf
2+
3+
server {
4+
listen 127.0.0.1:80;
5+
#listen VALET_LOOPBACK:80; # valet loopback
6+
server_name VALET_SITE www.VALET_SITE *.VALET_SITE;
7+
return 301 https://$host$request_uri;
8+
}
9+
10+
server {
11+
listen 127.0.0.1:443 ssl http2;
12+
#listen VALET_LOOPBACK:443 ssl http2; # valet loopback
13+
server_name VALET_SITE www.VALET_SITE *.VALET_SITE;
14+
root /;
15+
charset utf-8;
16+
client_max_body_size 128M;
17+
http2_push_preload on;
18+
19+
location /VALET_STATIC_PREFIX/ {
20+
internal;
21+
alias /;
22+
try_files $uri $uri/;
23+
}
24+
25+
ssl_certificate "VALET_CERT";
26+
ssl_certificate_key "VALET_KEY";
27+
28+
access_log off;
29+
error_log "VALET_HOME_PATH/Log/VALET_SITE-error.log";
30+
31+
error_page 404 "VALET_SERVER_PATH";
32+
33+
location / {
34+
proxy_pass VALET_PROXY_HOST;
35+
proxy_set_header Host $host;
36+
proxy_set_header X-Real-IP $remote_addr;
37+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
38+
proxy_set_header X-Forwarded-Proto $scheme;
39+
proxy_set_header X-Client-Verify SUCCESS;
40+
proxy_set_header X-Client-DN $ssl_client_s_dn;
41+
proxy_set_header X-SSL-Subject $ssl_client_s_dn;
42+
proxy_set_header X-SSL-Issuer $ssl_client_i_dn;
43+
proxy_set_header X-NginX-Proxy true;
44+
proxy_set_header Upgrade $http_upgrade;
45+
proxy_set_header Connection "upgrade";
46+
proxy_http_version 1.1;
47+
proxy_read_timeout 1800;
48+
proxy_connect_timeout 1800;
49+
chunked_transfer_encoding on;
50+
proxy_redirect off;
51+
proxy_buffering off;
52+
}
53+
54+
location ~ /\.ht {
55+
deny all;
56+
}
57+
}

cli/valet.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,14 @@
210210
/**
211211
* Create an Nginx proxy config for the specified domain
212212
*/
213-
$app->command('proxy domain host', function ($domain, $host) {
213+
$app->command('proxy domain host [--secure]', function ($domain, $host, $secure) {
214214

215-
Site::proxyCreate($domain, $host);
215+
Site::proxyCreate($domain, $host, $secure);
216216
Nginx::restart();
217217

218-
})->descriptions('Create an Nginx proxy site for the specified host. Useful for docker, mailhog etc.');
218+
})->descriptions('Create an Nginx proxy site for the specified host. Useful for docker, mailhog etc.', [
219+
'--secure' => 'Create a proxy with a trusted TLS certificate'
220+
]);
219221

220222
/**
221223
* Delete an Nginx proxy config

tests/SiteTest.php

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ public function test_add_proxy()
353353
$site->assertCertificateNotExists('my-new-proxy.com.test');
354354
$site->assertNginxNotExists('my-new-proxy.com.test');
355355

356-
$site->proxyCreate('my-new-proxy.com', 'https://127.0.0.1:9443');
356+
$site->proxyCreate('my-new-proxy.com', 'https://127.0.0.1:9443', true);
357357

358358
$site->assertCertificateExistsWithCounterValue('my-new-proxy.com.test', 0);
359359
$site->assertNginxExists('my-new-proxy.com.test');
@@ -368,6 +368,41 @@ public function test_add_proxy()
368368
], $site->proxies()->all());
369369
}
370370

371+
372+
public function test_add_non_secure_proxy()
373+
{
374+
$config = Mockery::mock(Configuration::class);
375+
$config->shouldReceive('read')
376+
->andReturn(['tld' => 'test', 'loopback' => VALET_LOOPBACK]);
377+
378+
swap(Configuration::class, $config);
379+
380+
swap(CommandLine::class, resolve(CommandLineFake::class));
381+
382+
/** @var FixturesSiteFake $site */
383+
$site = resolve(FixturesSiteFake::class);
384+
385+
$site->useOutput();
386+
387+
$site->assertCertificateNotExists('my-new-proxy.com.test');
388+
$site->assertNginxNotExists('my-new-proxy.com.test');
389+
390+
$site->proxyCreate('my-new-proxy.com', 'http://127.0.0.1:9443', false);
391+
392+
$site->assertCertificateNotExists('my-new-proxy.com.test');
393+
$site->assertNginxExists('my-new-proxy.com.test');
394+
395+
$this->assertEquals([
396+
'my-new-proxy.com' => [
397+
'site' => 'my-new-proxy.com',
398+
'secured' => '',
399+
'url' => 'http://my-new-proxy.com.test',
400+
'path' => 'http://127.0.0.1:9443',
401+
],
402+
], $site->proxies()->all());
403+
}
404+
405+
371406
public function test_add_proxy_clears_previous_proxy_certificate()
372407
{
373408
$config = Mockery::mock(Configuration::class);
@@ -383,7 +418,7 @@ public function test_add_proxy_clears_previous_proxy_certificate()
383418

384419
$site->useOutput();
385420

386-
$site->proxyCreate('my-new-proxy.com', 'https://127.0.0.1:7443');
421+
$site->proxyCreate('my-new-proxy.com', 'https://127.0.0.1:7443', true);
387422

388423
$site->assertCertificateExistsWithCounterValue('my-new-proxy.com.test', 0);
389424

@@ -397,7 +432,7 @@ public function test_add_proxy_clears_previous_proxy_certificate()
397432
], $site->proxies()->all());
398433

399434
// Note: different proxy port
400-
$site->proxyCreate('my-new-proxy.com', 'https://127.0.0.1:9443');
435+
$site->proxyCreate('my-new-proxy.com', 'https://127.0.0.1:9443', true);
401436

402437
// This shows we created a new certificate.
403438
$site->assertCertificateExistsWithCounterValue('my-new-proxy.com.test', 1);
@@ -435,7 +470,7 @@ public function test_add_proxy_clears_previous_non_proxy_certificate()
435470
$site->assertCertificateExistsWithCounterValue('my-new-proxy.com.test', 0);
436471
$site->assertNginxNotExists('my-new-proxy.com.test');
437472

438-
$site->proxyCreate('my-new-proxy.com', 'https://127.0.0.1:9443');
473+
$site->proxyCreate('my-new-proxy.com', 'https://127.0.0.1:9443', true);
439474

440475
// This shows we created a new certificate.
441476
$site->assertCertificateExistsWithCounterValue('my-new-proxy.com.test', 1);
@@ -472,7 +507,7 @@ public function test_remove_proxy()
472507

473508
$this->assertEquals([], $site->proxies()->all());
474509

475-
$site->proxyCreate('my-new-proxy.com', 'https://127.0.0.1:9443');
510+
$site->proxyCreate('my-new-proxy.com', 'https://127.0.0.1:9443', true);
476511

477512
$this->assertEquals([
478513
'my-new-proxy.com' => [

tests/fixtures/Proxies/Nginx/not-a-proxy.com.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# valet stub: proxy.valet.conf
1+
# valet stub: secure.proxy.valet.conf
22

33
server {
44
listen 127.0.0.1:80;

tests/fixtures/Proxies/Nginx/some-other-proxy.com.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# valet stub: proxy.valet.conf
1+
# valet stub: secure.proxy.valet.conf
22

33
server {
44
listen 127.0.0.1:80;

tests/fixtures/Proxies/Nginx/some-proxy.com.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# valet stub: proxy.valet.conf
1+
# valet stub: secure.proxy.valet.conf
22

33
server {
44
listen 127.0.0.1:80;

0 commit comments

Comments
 (0)