This commit is contained in:
Marcel Pociot
2020-06-02 16:51:36 +02:00
parent f058ce8c5c
commit 6a47264be9
34 changed files with 661 additions and 9 deletions

View File

@@ -0,0 +1,27 @@
<?php
namespace App\Client\Support;
use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
class TokenNodeVisitor extends NodeVisitorAbstract
{
/** @var string */
protected $token;
public function __construct(string $token)
{
$this->token = $token;
}
public function enterNode(Node $node)
{
if ($node instanceof Node\Expr\ArrayItem && $node->key->value === 'auth_token') {
$node->value->value = $this->token;
return $node;
}
return null;
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Commands;
use Illuminate\Console\Command;
class PublishCommand extends Command
{
protected $signature = 'publish';
protected $description = 'Publish the expose configuration file';
public function handle()
{
$configFile = implode(DIRECTORY_SEPARATOR, [
$_SERVER['HOME'],
'.expose',
'config.php'
]);
if (file_exists($configFile)) {
$this->error('Expose configuration file already exists at '.$configFile);
return;
}
file_put_contents($configFile, file_get_contents(base_path('config/expose.php')));
$this->info('Published expose configuration file to: ' . $configFile);
}
}

View File

@@ -8,7 +8,7 @@ use React\EventLoop\LoopInterface;
class ServeCommand extends Command
{
protected $signature = 'serve {host=0.0.0.0} {hostname=localhost} {--validateAuthTokens}';
protected $signature = 'serve {hostname=localhost} {host=0.0.0.0} {--validateAuthTokens}';
protected $description = 'Start the shaft server';

View File

@@ -28,6 +28,6 @@ class ShareCurrentWorkingDirectoryCommand extends ShareCommand
return $valetConfig->tld;
}
return 'test';
return config('expose.default_tld', 'test');
}
}

View File

@@ -2,7 +2,14 @@
namespace App\Commands;
use App\Client\Support\TokenNodeVisitor;
use Illuminate\Console\Command;
use PhpParser\Lexer\Emulative;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\CloningVisitor;
use PhpParser\Parser\Php7;
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter\Standard;
class StoreAuthenticationTokenCommand extends Command
{
@@ -17,17 +24,20 @@ class StoreAuthenticationTokenCommand extends Command
if (!is_null($this->argument('token'))) {
$this->info('Setting the expose authentication token to "' . $this->argument('token') . '"');
$config['auth_token'] = $this->argument('token');
$configFile = implode(DIRECTORY_SEPARATOR, [
$_SERVER['HOME'],
'.expose',
'config.php'
]);
@mkdir(dirname($configFile), 0777, true);
if (! file_exists($configFile)) {
@mkdir(dirname($configFile), 0777, true);
$updatedConfigFile = $this->modifyConfigurationFile(base_path('config/expose.php'), $this->argument('token'));
} else {
$updatedConfigFile = $this->modifyConfigurationFile($configFile, $this->argument('token'));
}
file_put_contents($configFile, '<?php return ' . var_export($config, true) . ';');
file_put_contents($configFile, $updatedConfigFile);
return;
}
@@ -37,4 +47,32 @@ class StoreAuthenticationTokenCommand extends Command
$this->info('Current authentication token: ' . $token);
}
}
protected function modifyConfigurationFile(string $configFile, string $token)
{
$lexer = new Emulative([
'usedAttributes' => [
'comments',
'startLine', 'endLine',
'startTokenPos', 'endTokenPos',
],
]);
$parser = new Php7($lexer);
$oldStmts = $parser->parse(file_get_contents($configFile));
$oldTokens = $lexer->getTokens();
$nodeTraverser = new NodeTraverser;
$nodeTraverser->addVisitor(new CloningVisitor());
$newStmts = $nodeTraverser->traverse($oldStmts);
$nodeTraverser = new NodeTraverser;
$nodeTraverser->addVisitor(new TokenNodeVisitor($token));
$newStmts = $nodeTraverser->traverse($newStmts);
$prettyPrinter = new Standard();
return $prettyPrinter->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
}
}

View File

@@ -19,7 +19,8 @@
],
"require": {
"php": "^7.2.5",
"ext-json": "*"
"ext-json": "*",
"nikic/php-parser": "^4.4"
},
"require-dev": {
"cboden/ratchet": "^0.4.2",

57
composer.lock generated
View File

@@ -4,8 +4,61 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "710c897a2bbd37c60950d540b46ba1b5",
"packages": [],
"content-hash": "1e617bd6ba5f8d13959acf397882e120",
"packages": [
{
"name": "nikic/php-parser",
"version": "v4.4.0",
"source": {
"type": "git",
"url": "https://github.com/nikic/PHP-Parser.git",
"reference": "bd43ec7152eaaab3bd8c6d0aa95ceeb1df8ee120"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/bd43ec7152eaaab3bd8c6d0aa95ceeb1df8ee120",
"reference": "bd43ec7152eaaab3bd8c6d0aa95ceeb1df8ee120",
"shasum": ""
},
"require": {
"ext-tokenizer": "*",
"php": ">=7.0"
},
"require-dev": {
"ircmaxell/php-yacc": "0.0.5",
"phpunit/phpunit": "^6.5 || ^7.0 || ^8.0"
},
"bin": [
"bin/php-parse"
],
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "4.3-dev"
}
},
"autoload": {
"psr-4": {
"PhpParser\\": "lib/PhpParser"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Nikita Popov"
}
],
"description": "A PHP parser written in PHP",
"keywords": [
"parser",
"php"
],
"time": "2020-04-10T16:34:50+00:00"
}
],
"packages-dev": [
{
"name": "bfunky/http-parser",

View File

@@ -4,6 +4,7 @@ return [
'host' => 'expose.dev',
'port' => 443,
'auth_token' => '',
'default_tld' => 'test',
'admin' => [

4
docs/_index.md Normal file
View File

@@ -0,0 +1,4 @@
---
packageName: Expose
githubUrl: https://github.com/beyondcode/expose
---

4
docs/api/_index.md Normal file
View File

@@ -0,0 +1,4 @@
---
title: API
order: 30
---

View File

@@ -0,0 +1,8 @@
---
title: Basic Authentication
order: 8
---
# Sharing local sites
This page will be under the "Basic Usage" submenu.

View File

@@ -0,0 +1,8 @@
---
title: Configuration
order: 1
---
# Sharing local sites
This page will be under the "Basic Usage" submenu.

8
docs/api/dashboard.md Normal file
View File

@@ -0,0 +1,8 @@
---
title: Dashboard
order: 5
---
# Sharing local sites
This page will be under the "Basic Usage" submenu.

8
docs/api/extending.md Normal file
View File

@@ -0,0 +1,8 @@
---
title: Extending
order: 10
---
# Sharing local sites
This page will be under the "Basic Usage" submenu.

8
docs/api/sharing.md Normal file
View File

@@ -0,0 +1,8 @@
---
title: Sharing your local sites
order: 1
---
# Sharing local sites
This page will be under the "Basic Usage" submenu.

4
docs/client/_index.md Normal file
View File

@@ -0,0 +1,4 @@
---
title: Client
order: 2
---

View File

@@ -0,0 +1,32 @@
---
title: Basic Authentication
order: 2
---
# Sharing sites with basic authentication
Expose allows you to share your local sites with custom basic authentication credentials.
This can be useful, if you have a static subdomain that you share with someone else, for example a client, and you want to provide some additional security to it. Before someone can access your shared site, they need to provide the correct credentials.
> **Warning**: You can not add basic authentication to a website that already uses basic authentication.
To share your site with basic authentication, use:
```bash
expose --auth="admin:secret"
```
Or if you want to use the explicit sharing:
```bash
expose share my-site.test --auth="admin:secret"
```
This will share the local URL my-site.test with the username "admin" and the password "secret".
You can also use the basic authentication parameter in addition to a custom subdomain:
```bash
expose share my-site.test --subdomain=site --auth="admin:secret"
```

View File

@@ -0,0 +1,189 @@
---
title: Configuration
order: 3
---
# Configuration
To configure the Expose client, you first need to publish the configuration file.
This can be done using the publish command:
```bash
expose publish
```
The configuration file will be written to your home directory inside a `.expose` folder:
`~/.expose/config.php`
And the default content of the configuration file is this:
```php
return [
/*
|--------------------------------------------------------------------------
| Host
|--------------------------------------------------------------------------
|
| The expose server to connect to. By default, expose is using the free
| expose.dev server, offered by Beyond Code. You will need a free
| Beyond Code account in order to authenticate with the server.
| Feel free to host your own server and change this value.
|
*/
'host' => 'expose.dev',
/*
|--------------------------------------------------------------------------
| Port
|--------------------------------------------------------------------------
|
| The port that expose will try to connect to. If you want to bypass
| firewalls and have proper SSL encrypted tunnels, make sure to use
| port 443 and use a reverse proxy for Expose.
|
| The free default server is already running on port 443.
|
*/
'port' => 443,
/*
|--------------------------------------------------------------------------
| Auth Token
|--------------------------------------------------------------------------
|
| The global authentication token to use for the expose server that you
| are connecting to. You can let expose automatically update this value
| for you by running
|
| > expose token YOUR-AUTH-TOKEN
|
*/
'auth_token' => '',
/*
|--------------------------------------------------------------------------
| Default TLD
|--------------------------------------------------------------------------
|
| The default TLD to use when sharing your local sites. Expose will try
| to look up the TLD if you are using Laravel Valet automatically.
| Otherwise you can specify it here manually.
|
*/
'default_tld' => 'test',
'admin' => [
/*
|--------------------------------------------------------------------------
| Database
|--------------------------------------------------------------------------
|
| The SQLite database that your expose server should use. This datbaase
| will hold all users that are able to authenticate with your server,
| if you enable authentication token validation.
|
*/
'database' => base_path('database/expose.db'),
/*
|--------------------------------------------------------------------------
| Validate auth tokens
|--------------------------------------------------------------------------
|
| By default, once you start an expose server, anyone is able to connect to
| it, given that they know the server host. If you want to only allow the
| connection from users that have valid authentication tokens, set this
| setting to true. You can also modify this at runtime in the server
| admin interface.
|
*/
'validate_auth_tokens' => false,
/*
|--------------------------------------------------------------------------
| Maximum connection length
|--------------------------------------------------------------------------
|
| If you want to limit the amount of time that a single connection can
| stay connected to the expose server, you can specify the maximum
| connection length in minutes here. A maximum length of 0 means that
| clients can stay connected as long as they want.
|
*/
'maximum_connection_length' => 0,
/*
|--------------------------------------------------------------------------
| Subdomain
|--------------------------------------------------------------------------
|
| This is the subdomain that your expose admin dashboard will be available at.
| The given subdomain will be reserved, so no other tunnel connection can
| request this subdomain for their own connection.
|
*/
'subdomain' => 'expose',
/*
|--------------------------------------------------------------------------
| Subdomain Generator
|--------------------------------------------------------------------------
|
| This is the subdomain generator that will be used, when no specific
| subdomain was provided. The default implementation simply generates
| a random string for you. Feel free to change this.
|
*/
'subdomain_generator' => \App\Server\SubdomainGenerator\RandomSubdomainGenerator::class,
/*
|--------------------------------------------------------------------------
| Users
|--------------------------------------------------------------------------
|
| The admin dashboard of expose is protected via HTTP basic authentication
| Here you may add the user/password combinations that you want to
| accept as valid logins for the dashboard.
|
*/
'users' => [
'username' => 'password'
],
/*
|--------------------------------------------------------------------------
| User Repository
|--------------------------------------------------------------------------
|
| This is the user repository, which by default loads and saves all authorized
| users in a SQLite database. You can implement your own user repository
| if you want to store your users in a different store (Redis, MySQL, etc.)
|
*/
'user_repository' => \App\Server\UserRepository\DatabaseUserRepository::class,
/*
|--------------------------------------------------------------------------
| Messages
|--------------------------------------------------------------------------
|
| The default messages that the expose server will send the clients.
| These settings can also be changed at runtime in the expose admin
| interface.
|
*/
'messages' => [
'message_of_the_day' => 'Thank you for using expose.',
'invalid_auth_token' => 'Authentication failed. Please check your authentication token and try again.',
'subdomain_taken' => 'The chosen subdomain :subdomain is already taken. Please choose a different subdomain.',
]
]
];
```

11
docs/client/dashboard.md Normal file
View File

@@ -0,0 +1,11 @@
---
title: Dashboard
order: 5
---
# Dashboard
Once you share a local site, expose will show you all incoming HTTP requests along with their status code and duration in your terminal:
![](/img/expose_terminal.png)

8
docs/client/extending.md Normal file
View File

@@ -0,0 +1,8 @@
---
title: Extending
order: 100
---
# Sharing local sites
This page will be under the "Basic Usage" submenu.

57
docs/client/sharing.md Normal file
View File

@@ -0,0 +1,57 @@
---
title: Sharing your local sites
order: 1
---
# Sharing local sites
Expose allows you to share any kind of HTTP/HTTPS traffic for websites that you can reach on your own computer, with anyone on the internet.
There are multiple different ways on how you can initiate the site sharing with Expose.
## Sharing the current working directory
To share the current working directory with expose, all you need to do is go into the directory and call `expose`.
This makes the assumption that you have access to the current working directory name as a domain with the `.test` TLD.
If you are using Laravel Valet, the configured Valet subdomain will automatically be detected.
If you are using a different domain for your local sites, you can change the default TLD that expose uses in the [configuration file]().
For example:
```bash
# Will share a local site "my-site.test" as "my-site.EXPOSE-SERVER"
~/Sites/my-site/ expose
# Will share a local site "api.my-site.test" as "api-my-site.EXPOSE-SERVER"
~/Sites/api.my-site/ expose
```
## Sharing a local site explicitly
If you want to explicitly share a local URL, without going into a specific folder first, you can do this by using the `expose share` command.
> By default, this command will generate a unique subdomain for the shared URL.
```bash
# Will share access to http://192.168.2.100 using a randomly generated subdomain
expose share http://192.168.2.100
# Will share access to http://my-local-site.dev using a randomly generated subdomain
expose share my-local-site.dev
```
## Share a local site with a given subdomain
You can also share one of your local sites explicitly and specify which exact subdomain you want Expose to use when sharing the site.
This works similar to the paid offerings of Ngrok - but you can use it with your own custom server.
To specify the subdomain, pass the `--subdomain` option to expose:
```bash
expose share my-site.test --subdomain=my-site
```
If the chosen subdomain is already taken on the Expose server, you will see an error message and the connection to the Expose server gets closed.

View File

@@ -0,0 +1,4 @@
---
title: Getting Started
order: 2
---

View File

@@ -0,0 +1,23 @@
---
title: Installation
order: 1
---
# Installation
Expose can be installed using composer.
The easiest way to install expose is by making it a global composer dependency:
```bash
composer global require beyondcode/expose
```
Now you're ready to go and can [share your first site](/getting-started/share-your-first-site).
### Extending Expose
By default, expose comes as an executable PHAR file. This allows you to use all of the expose features, like sharing your local sites, out of the box - without any additional setup required.
If you want to modify expose, for example by adding custom request/response modifiers, you will need to clone the GitHub repository instead.
You can learn more about how to customize expose in the [extending Expose](/extending) documentation section.

View File

@@ -0,0 +1,10 @@
---
title: Questions & Issues
order: 3
---
# Questions and issues
Find yourself stuck using the package? Found a bug? Do you have general questions or suggestions for improving expose? Feel free to create an issue on [GitHub](https://github.com/beyondcode/expose/issues), we'll try to address it as soon as possible.
If you've found a bug regarding security please mail [marcel@beyondco.de](mailto:marcel@beyondco.de) instead of using the issue tracker.

View File

@@ -0,0 +1,40 @@
---
title: Share your first site
order: 2
---
# Share your first site
Once you have installed Expose, you are ready to go and share your local sites.
The easiest way to share your local sites is by going into the folder that you want to share and run `expose`:
```bash
cd ~/Sites/my-awesome-project/
expose
```
This will connect to the provided server at expose.dev and give you a tunnel that you can immediately start using.
To learn more about how you can share your local sites, check out the [sharing local sites](/docs/expose/client/sharing) documentation.
## Using the provided server at expose.dev
A big advantage of Expose over other alternatives such as ngrok, is the ability to host your own server. To make sharing your sites as easy as possible, we provide and host a custom expose server on our own - so getting started with expose is a breeze.
This server is available free of charge for everyone, but makes use of Expose's [authentication token]() authorization method.
Therefore, in order to share your sites for the first time, you will need an authorization token.
You can obtain such a token by singing in to your [Beyond Code account](/login). If you do not yet have an account, you can [sign up and create an account](/register) for free.
## Authenticating with expose.dev
To register and use the given credentials, just run the following command:
```bash
expose token [YOUR-AUTH-TOKEN]
```
This will register the token globally in your expose configuration file, and all following expose calls, will automatically use the token to authenticate with the server.

16
docs/introduction.md Normal file
View File

@@ -0,0 +1,16 @@
---
title: Introduction
order: 1
---
# Introduction
Expose is a beautiful, open source, tunnel application that allows you to share your local websites with others via the internet.
Since you can host the server yourself, you have full control over the domains that your shared sites will be available at.
You can extend expose with additional features and middleware classes on the server and client side, to make it suit your specific needs.
Expose comes with a beautiful CLI view and web-based dashboard that show you incoming HTTP requests on the client.
![](/img/expose_terminal.png)
![](/img/dashboard.png)

4
docs/server/_index.md Normal file
View File

@@ -0,0 +1,4 @@
---
title: Server
order: 4
---

View File

@@ -0,0 +1,8 @@
---
title: Administrators
order: 10
---
# Sharing local sites
This page will be under the "Basic Usage" submenu.

View File

@@ -0,0 +1,8 @@
---
title: Configuration
order: 1
---
# Sharing local sites
This page will be under the "Basic Usage" submenu.

View File

@@ -0,0 +1,8 @@
---
title: Maximum connection length
order: 10
---
# Sharing local sites
This page will be under the "Basic Usage" submenu.

8
docs/server/extending.md Normal file
View File

@@ -0,0 +1,8 @@
---
title: Extending
order: 10
---
# Sharing local sites
This page will be under the "Basic Usage" submenu.

8
docs/server/motd.md Normal file
View File

@@ -0,0 +1,8 @@
---
title: Message of the day
order: 10
---
# Sharing local sites
This page will be under the "Basic Usage" submenu.

View File

@@ -0,0 +1,8 @@
---
title: Subdomain Generator
order: 10
---
# Sharing local sites
This page will be under the "Basic Usage" submenu.

8
docs/server/users.md Normal file
View File

@@ -0,0 +1,8 @@
---
title: User Repository
order: 10
---
# Sharing local sites
This page will be under the "Basic Usage" submenu.