Skip to content

Commit 2aca468

Browse files
committed
Merge branch 'release/1.0.0'
2 parents f281a05 + d049c70 commit 2aca468

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

README.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Laravel SSH Tunnel
2+
Access a service on a remote host, via an SSH Tunnel! For example, people have been asking how to connect to a MySQL server over SSH in PHP for years.
3+
4+
- [Connect to a MySQL server over SSH in PHP](http://stackoverflow.com/questions/464317/connect-to-a-mysql-server-over-ssh-in-php)
5+
- [Connect to a MySQL server over SSH in PHP](http://stackoverflow.com/questions/309615/connect-to-a-mysql-server-over-ssh-in-php)
6+
- [Connect to a mysql database via SSH through PHP](http://stackoverflow.com/questions/18069658/connect-to-a-mysql-database-via-ssh-through-php)
7+
- [Connect to remote MySQL database with PHP using SSH](http://stackoverflow.com/questions/4927056/connect-to-remote-mysql-database-with-php-using-ssh)
8+
- [Laravel MySql DB Connection with SSH](http://stackoverflow.com/questions/25495364/laravel-mysql-db-connection-with-ssh)
9+
10+
We had a similar challenge, specifically accessing a MySQL database over an SSH Tunnel and all of the Questions and Answers were helpful in finding a solution. However, we wanted something that would just plug and play with our Laravel applications and Lumen Services.
11+
12+
So we wrote this package. We hope you enjoy it!
13+
14+
## Installation
15+
```
16+
composer require stechstudio/laravel-ssh-tunnel
17+
```
18+
Then register the provider
19+
```php
20+
$app->register(stechstudio\Tunneler\TunnelerServiceProvider::class);
21+
```
22+
in your `bootstrap/app.php` for Lumen services or add it to your `providers` array in `config/app.php` for Laravel applications.
23+
24+
## Configuration
25+
All configuration can and should be done in your `.env` file.
26+
```ini
27+
; Path to the nc executable
28+
TUNNELER_NC_PATH=/usr/bin/nc
29+
; Path to the ssh executable
30+
TUNNELER_SSH_PATH=/usr/bin/ssh
31+
; Path to the nohup executable
32+
TUNNELER_NOHUP_PATH=/usr/bin/nohup
33+
34+
; The identity file you want to use for ssh auth
35+
TUNNELER_IDENTITY_FILE=/home/user/.ssh/id_rsa
36+
37+
; The local address and port for the tunnel
38+
TUNNELER_LOCAL_PORT=13306
39+
TUNNELER_LOCAL_ADDRESS=127.0.0.1
40+
41+
; The remote address and port for the tunnel
42+
TUNNELER_BIND_PORT=3306
43+
TUNNELER_BIND_ADDRESS=127.0.0.1
44+
45+
; The ssh connection: sshuser@sshhost:sshport
46+
TUNNELER_USER=sshuser
47+
TUNNELER_HOSTNAME=sshhost
48+
TUNNELER_PORT=sshport
49+
50+
; How long to wait, in microseconds, before testing to see if the tunnel is created.
51+
; Depending on your network speeds you will want to modify the default of .5 seconds
52+
TUNNELER_CONN_WAIT=500000
53+
54+
; Do you want to ensure you have the Tunnel in place for each bootstrap of the framework?
55+
TUNNELER_ON_BOOT=false
56+
```
57+
58+
## Quickstart
59+
The simplest way to use the Tunneler is to set `TUNNELER_ON_BOOT=true` in your `.env` file. This will ensure the tunnel is in place everytime the framework bootstraps.
60+
61+
However, there is minimal performance impact because the tunnel will get reused. You only have to bear the connection costs when the tunnel has been disconnected for some reason.
62+
63+
Then you can just configure your service, we will demonstrate a database connection.
64+
65+
```php
66+
'mysql_tunnel' => [
67+
'driver' => 'mysql',
68+
'host' => env('TUNNELER_LOCAL_ADDRESS'),
69+
'port' => env('TUNNELER_LOCAL_PORT'),
70+
'database' => env('DB_DATABASE'),
71+
'username' => env('DB_USERNAME'),
72+
'password' => env('DB_PASSWORD'),
73+
'charset' => env('DB_CHARSET', 'utf8'),
74+
'collation' => env('DB_COLLATION', 'utf8_unicode_ci'),
75+
'prefix' => env('DB_PREFIX', ''),
76+
'timezone' => env('DB_TIMEZONE', '+00:00'),
77+
'strict' => env('DB_STRICT_MODE', false),
78+
],
79+
```
80+
And there you have it. Go set up your Eloquent models now.
81+
82+
## Artisan Command
83+
```
84+
php artisan tunneler:activate
85+
```
86+
87+
This artisan command will either verify the connection is up, or will create the connection. This probably isn't of great benefit for running manually, apart for testing your configuration.
88+
89+
However, if you would like to ensure that the tunnel is available all the time, and not do the work on bootstrap, you can use the [Laravel Scheduler](https://laravel.com/docs/5.3/scheduling) to schedule the artisan command to run at whatever interval you think is best to maintain your connection. In your `App\Console\Kernel` for example:
90+
91+
```php
92+
protected function schedule(Schedule $schedule)
93+
{
94+
$schedule->command('tunneler:activate')->everyFiveMinutes();
95+
}
96+
```
97+
98+
Then, assuming you have properly set up the Scheduler in cron, the artisan command will check the tunnel every five minutes and restart it if it isn't up.
99+
100+
## Dispatch It
101+
Perhaps your application rarely needs to do this, but when it does, you'd like to have an easy way to ensure the tunnel is in place before the connection attempt.
102+
103+
```php
104+
$app->get('/mysql_tunnel', function () use ($app) {
105+
dispatch(new stechstudio\Tunneler\Jobs\CreateTunnel());
106+
107+
$users = DB::connection('mysql_tunnel')
108+
->table('users')
109+
->get();
110+
111+
dd($users);
112+
});
113+
114+
```
115+
116+
## How Does it Work?
117+
It first uses netcat (`nc`) via `exec` to check the local port to see if the tunnel is open. If the port is there, it does nothing else.
118+
119+
If the port isn't there, it then creates the ssh tunnel connection command and executes that via `exec` after execution we wait the defined `TUNNELER_CONN_WAIT` time before running netcat again to verify that the connection is in place.
120+
121+
That's it. The tunnel will stay up until it times out, if it times out, and depending on the strategy you have chosen to ensure it is up and available when you need it, it will simply be recreated on demand.
122+

0 commit comments

Comments
 (0)