Skip to content

Commit 32f94e9

Browse files
authoredMar 25, 2023
Update README.md
1 parent 3f4957e commit 32f94e9

File tree

1 file changed

+2
-137
lines changed

1 file changed

+2
-137
lines changed
 

‎README.md

+2-137
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,2 @@
1-
# Deprecated
2-
It has been about four years since we have used this package internally and we are no longer in a position to continue supporting it. If anyone in the community would like to take over support of the package, please let us know and we can coordinate a transition.
3-
4-
# Laravel SSH Tunnel
5-
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.
6-
7-
- [Connect to a MySQL server over SSH in PHP](http://stackoverflow.com/questions/464317/connect-to-a-mysql-server-over-ssh-in-php)
8-
- [Connect to a MySQL server over SSH in PHP](http://stackoverflow.com/questions/309615/connect-to-a-mysql-server-over-ssh-in-php)
9-
- [Connect to a mysql database via SSH through PHP](http://stackoverflow.com/questions/18069658/connect-to-a-mysql-database-via-ssh-through-php)
10-
- [Connect to remote MySQL database with PHP using SSH](http://stackoverflow.com/questions/4927056/connect-to-remote-mysql-database-with-php-using-ssh)
11-
- [Laravel MySql DB Connection with SSH](http://stackoverflow.com/questions/25495364/laravel-mysql-db-connection-with-ssh)
12-
13-
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.
14-
15-
So we wrote this package. We hope you enjoy it!
16-
17-
## Installation
18-
19-
```
20-
composer require stechstudio/laravel-ssh-tunnel
21-
```
22-
## Configuration
23-
All configuration can and should be done in your `.env` file.
24-
```env
25-
# Process used to verify connection
26-
# Use bash if your distro uses nmap-ncat (RHEL/CentOS 7.x)
27-
TUNNELER_VERIFY_PROCESS=nc
28-
29-
# Path to the nc executable
30-
TUNNELER_NC_PATH=/usr/bin/nc
31-
# Path to the bash executable
32-
TUNNELER_BASH_PATH=/usr/bin/bash
33-
# Path to the ssh executable
34-
TUNNELER_SSH_PATH=/usr/bin/ssh
35-
# Path to the nohup executable
36-
TUNNELER_NOHUP_PATH=/usr/bin/nohup
37-
38-
# Log messages for troubleshooting
39-
SSH_VERBOSITY=
40-
NOHUP_LOG=/dev/null
41-
42-
# The identity file you want to use for ssh auth
43-
TUNNELER_IDENTITY_FILE=/home/user/.ssh/id_rsa
44-
45-
# The local address and port for the tunnel
46-
TUNNELER_LOCAL_PORT=13306
47-
TUNNELER_LOCAL_ADDRESS=127.0.0.1
48-
49-
# The remote address and port for the tunnel
50-
TUNNELER_BIND_PORT=3306
51-
TUNNELER_BIND_ADDRESS=127.0.0.1
52-
53-
# The ssh connection: sshuser@sshhost:sshport
54-
TUNNELER_USER=sshuser
55-
TUNNELER_HOSTNAME=sshhost
56-
TUNNELER_PORT=sshport
57-
58-
# How long to wait, in microseconds, before testing to see if the tunnel is created.
59-
# Depending on your network speeds you will want to modify the default of 1 seconds
60-
TUNNELER_CONN_WAIT=1000000
61-
62-
# How often it is checked if the tunnel is created. Useful if the tunnel creation is sometimes slow,
63-
# and you want to minimize waiting times
64-
TUNNELER_CONN_TRIES=1
65-
66-
# Do you want to ensure you have the Tunnel in place for each bootstrap of the framework?
67-
TUNNELER_ON_BOOT=false
68-
69-
# Do you want to use additional SSH options when the tunnel is created?
70-
TUNNELER_SSH_OPTIONS="-o StrictHostKeyChecking=no"
71-
```
72-
73-
## Quickstart
74-
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.
75-
76-
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.
77-
78-
Then you can just configure your service, which we will demonstrate using a database connection. Add this under `'connections'` in your `config/database.php` file
79-
80-
```php
81-
'mysql_tunnel' => [
82-
'driver' => 'mysql',
83-
'host' => env('TUNNELER_LOCAL_ADDRESS'),
84-
'port' => env('TUNNELER_LOCAL_PORT'),
85-
'database' => env('DB_DATABASE'),
86-
'username' => env('DB_USERNAME'),
87-
'password' => env('DB_PASSWORD'),
88-
'charset' => env('DB_CHARSET', 'utf8'),
89-
'collation' => env('DB_COLLATION', 'utf8_unicode_ci'),
90-
'prefix' => env('DB_PREFIX', ''),
91-
'timezone' => env('DB_TIMEZONE', '+00:00'),
92-
'strict' => env('DB_STRICT_MODE', false),
93-
],
94-
```
95-
And there you have it. Go set up your Eloquent models now.
96-
97-
## Artisan Command
98-
```
99-
php artisan tunneler:activate
100-
```
101-
102-
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.
103-
104-
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:
105-
106-
```php
107-
protected function schedule(Schedule $schedule)
108-
{
109-
$schedule->command('tunneler:activate')->everyFiveMinutes();
110-
}
111-
```
112-
113-
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.
114-
115-
## Dispatch It
116-
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.
117-
118-
```php
119-
$app->get('/mysql_tunnel', function () use ($app) {
120-
dispatch(new STS\Tunneler\Jobs\CreateTunnel());
121-
122-
$users = DB::connection('mysql_tunnel')
123-
->table('users')
124-
->get();
125-
126-
dd($users);
127-
});
128-
129-
```
130-
131-
## How Does it Work?
132-
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.
133-
134-
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.
135-
136-
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.
137-
1+
# Abandoned
2+
But you can find a new maintainer of the package over at [prodigyphp/laravel-ssh-tunnel](https://packagist.org/packages/prodigyphp/laravel-ssh-tunnel) where they are keeping the project alive.

0 commit comments

Comments
 (0)
Please sign in to comment.