leopersan / laravel-ssh-tunnel
为 Laravel 容易创建和维护 SSH 隧道
Requires
- illuminate/support: ^5.5|^6.0|^7.0|^8.0|^9.0|^10.0
This package is auto-updated.
Last update: 2024-09-30 01:17:48 UTC
README
我们从 Signature Tech Studio 接管了此包(以前是 stechstudio/laravel-ssh-tunnel)的维护工作。非常感谢 @bubba-h57 及其他贡献者为此包做出杰出贡献。
Laravel SSH 隧道
通过 SSH 隧道访问远程主机上的服务!例如,多年来人们一直在询问如何在 PHP 中通过 SSH 连接到 MySQL 服务器。
- 通过 SSH 在 PHP 中连接到 MySQL 服务器
- 通过 SSH 在 PHP 中连接到 MySQL 服务器
- 通过 SSH 通过 PHP 连接到 mysql 数据库
- 使用 SSH 通过 PHP 连接到远程 MySQL 数据库
- Laravel MySql DB 连接通过 SSH
我们面临了类似的挑战,特别是通过 SSH 隧道访问 MySQL 数据库,所有问题和答案都在寻找解决方案时非常有帮助。然而,我们希望有一个可以直接插入我们 Laravel 应用程序和 Lumen 服务中使用的解决方案。
因此,我们编写了这个包。希望您喜欢它!
安装
composer require prodigyphp/laravel-ssh-tunnel
配置
所有配置都可以且应该在您的 .env
文件中完成。
# Process used to verify connection # Use bash if your distro uses nmap-ncat (RHEL/CentOS 7.x) TUNNELER_VERIFY_PROCESS=nc # Path to the nc executable TUNNELER_NC_PATH=/usr/bin/nc # Path to the bash executable TUNNELER_BASH_PATH=/usr/bin/bash # Path to the ssh executable 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 # The local address and port for the tunnel TUNNELER_LOCAL_PORT=13306 TUNNELER_LOCAL_ADDRESS=127.0.0.1 # The remote address and port for the tunnel TUNNELER_BIND_PORT=3306 TUNNELER_BIND_ADDRESS=127.0.0.1 # The ssh connection: sshuser@sshhost:sshport TUNNELER_USER=sshuser TUNNELER_HOSTNAME=sshhost TUNNELER_PORT=sshport # How long to wait, in microseconds, before testing to see if the tunnel is created. # Depending on your network speeds you will want to modify the default of 1 seconds TUNNELER_CONN_WAIT=1000000 # How often it is checked if the tunnel is created. Useful if the tunnel creation is sometimes slow, # and you want to minimize waiting times TUNNELER_CONN_TRIES=1 # Do you want to ensure you have the Tunnel in place for each bootstrap of the framework? TUNNELER_ON_BOOT=false # Do you want to use additional SSH options when the tunnel is created? TUNNELER_SSH_OPTIONS="-o StrictHostKeyChecking=no"
快速入门
使用 Tunneler 的最简单方法是设置 TUNNELER_ON_BOOT=true
在您的 .env
文件中。这将确保每次框架引导时隧道都处于就绪状态。
然而,由于隧道将得到重用,所以性能影响最小。您只需要在隧道因某些原因断开连接时承担连接成本。
然后您可以配置您的服务,我们将以数据库连接为例进行演示。在您的 config/database.php
文件中的 'connections'
下添加以下内容
'mysql_tunnel' => [ 'driver' => 'mysql', 'host' => env('TUNNELER_LOCAL_ADDRESS'), 'port' => env('TUNNELER_LOCAL_PORT'), 'database' => env('DB_DATABASE'), 'username' => env('DB_USERNAME'), 'password' => env('DB_PASSWORD'), 'charset' => env('DB_CHARSET', 'utf8'), 'collation' => env('DB_COLLATION', 'utf8_unicode_ci'), 'prefix' => env('DB_PREFIX', ''), 'timezone' => env('DB_TIMEZONE', '+00:00'), 'strict' => env('DB_STRICT_MODE', false), ],
然后您就可以设置您的 Eloquent 模型了。
Artisan 命令
php artisan tunneler:activate
此 artisan 命令将验证连接是否已建立,或者将创建连接。这可能对手动运行没有太大帮助,除了测试您的配置。
但是,如果您想确保隧道始终可用,并且不在引导时进行操作,您可以使用 Laravel 调度器 来安排 artisan 命令在您认为最适合维护连接的任何间隔运行。例如,在您的 App\Console\Kernel
中
protected function schedule(Schedule $schedule) { $schedule->command('tunneler:activate')->everyFiveMinutes(); }
然后,假设您已正确设置 cron 调度器,artisan 命令将每五分钟检查一次隧道,并在隧道未运行时重新启动它。
派发
也许您的应用程序很少需要这样做,但每次需要时,您都希望有一种简单的方法来确保在尝试连接之前隧道已就绪。
$app->get('/mysql_tunnel', function () use ($app) { dispatch(new STS\Tunneler\Jobs\CreateTunnel()); $users = DB::connection('mysql_tunnel') ->table('users') ->get(); dd($users); });
它是如何工作的?
它首先通过 exec
使用 netcat(《nc》)检查本地端口以查看隧道是否已打开。如果端口存在,则不再执行其他操作。
如果端口不存在,它将创建 SSH 隧道连接命令并通过 exec
执行该命令。执行后,我们等待定义的 TUNNELER_CONN_WAIT
时间,然后再次运行 netcat 以验证连接是否就绪。
就是这样。隧道将保持开启状态,直到超时。如果超时,则根据您选择的确保在需要时隧道处于就绪状态和可用的策略,它将简单地根据需要重新创建。