Skip to content

Commit

Permalink
Merge pull request #3 from jijoel/master
Browse files Browse the repository at this point in the history
enable logging ssh messages
  • Loading branch information
bubba-h57 committed Apr 10, 2017
2 parents 946ad5b + c4e4988 commit 0d52ffc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ Access a service on a remote host, via an SSH Tunnel! For example, people have b
- [Connect to a mysql database via SSH through PHP](http://stackoverflow.com/questions/18069658/connect-to-a-mysql-database-via-ssh-through-php)
- [Connect to remote MySQL database with PHP using SSH](http://stackoverflow.com/questions/4927056/connect-to-remote-mysql-database-with-php-using-ssh)
- [Laravel MySql DB Connection with SSH](http://stackoverflow.com/questions/25495364/laravel-mysql-db-connection-with-ssh)

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.

So we wrote this package. We hope you enjoy it!

## Requirements
This package has been tested against Laravel/Lumen versions 5.2. 5.3, and 5.4.
This package has been tested against Laravel/Lumen versions 5.2. 5.3, and 5.4.

We do not support version <=5.1.

Expand All @@ -37,6 +37,10 @@ TUNNELER_SSH_PATH=/usr/bin/ssh
; Path to the nohup executable
TUNNELER_NOHUP_PATH=/usr/bin/nohup

; Log messages for troubleshooting
SSH_VERBOSITY=
NOHUP_LOG=/dev/null

; The identity file you want to use for ssh auth
TUNNELER_IDENTITY_FILE=/home/user/.ssh/id_rsa

Expand All @@ -62,7 +66,7 @@ TUNNELER_ON_BOOT=false
```

## Quickstart
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.
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.

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.

Expand Down Expand Up @@ -90,7 +94,7 @@ And there you have it. Go set up your Eloquent models now.
php artisan tunneler:activate
```

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.
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.

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:

Expand All @@ -109,11 +113,11 @@ Perhaps your application rarely needs to do this, but when it does, you'd like t
```php
$app->get('/mysql_tunnel', function () use ($app) {
dispatch(new STS\Tunneler\Jobs\CreateTunnel());

$users = DB::connection('mysql_tunnel')
->table('users')
->get();

dd($users);
});

Expand Down
6 changes: 4 additions & 2 deletions config/tunneler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@
'port' => env('TUNNELER_PORT'),
'wait' => env('TUNNELER_CONN_WAIT', '500000'),

'on_boot' => filter_var(env('TUNNELER_ON_BOOT', false), FILTER_VALIDATE_BOOLEAN)
];
'on_boot' => filter_var(env('TUNNELER_ON_BOOT', false), FILTER_VALIDATE_BOOLEAN),
'ssh_verbosity' => env('SSH_VERBOSITY',''),
'nohup_log' => env('NOHUP_LOG', '/dev/null'),
];
11 changes: 8 additions & 3 deletions src/Jobs/CreateTunnel.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ public function __construct()
config('tunneler.local_port')
);

$this->sshCommand = sprintf('%s -N -i %s -L %d:%s:%d -p %d %s@%s',
$this->sshCommand = sprintf('%s %s -N -i %s -L %d:%s:%d -p %d %s@%s',
config('tunneler.ssh_path'),
config('tunneler.ssh_verbosity'),
config('tunneler.identity_file'),
config('tunneler.local_port'),
config('tunneler.bind_address'),
Expand Down Expand Up @@ -64,7 +65,11 @@ public function handle(): int
*/
protected function createTunnel()
{
$this->runCommand(sprintf('%s %s > /dev/null &', config('tunneler.nohup_path'), $this->sshCommand));
$this->runCommand(sprintf('%s %s >> %s 2>&1 &',
config('tunneler.nohup_path'),
$this->sshCommand,
config('tunneler.nohup_log')
));
// Ensure we wait long enough for it to actually connect.
usleep(config('tunneler.wait'));
}
Expand All @@ -91,4 +96,4 @@ protected function runCommand($command)
}


}
}

0 comments on commit 0d52ffc

Please sign in to comment.