prodigyphp / 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-08-25 21:08:32 UTC
README
我们从 Signature Tech Studio 接管了该软件包(原 stechstudio/laravel-ssh-tunnel)的维护工作。衷心感谢 @bubba-h57 和其他贡献者为此软件包做出的贡献。
Laravel SSH 隧道
通过 SSH 隧道访问远程主机上的服务!例如,多年来人们一直在询问如何通过 SSH 在 PHP 中连接到 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 的最简单方法是在 .env
文件中设置 TUNNELER_ON_BOOT=true
。这将确保每次框架引导时隧道都在。
但是,由于隧道将被重用,因此影响很小。只有在隧道由于某些原因断开连接时,您才需要承担连接费用。
然后您可以配置您的服务,我们将使用数据库连接进行演示。在您的 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 Scheduler 来安排 artisan 命令以您认为最适合维护连接的任何间隔运行。例如,在您的 App\Console\Kernel
中
protected function schedule(Schedule $schedule) { $schedule->command('tunneler:activate')->everyFiveMinutes(); }
然后,假设您已正确设置 cron 中的 Scheduler,artisan 命令将每五分钟检查一次隧道,如果它没有启动,则重新启动它。
调度
也许您的应用程序很少需要这样做,但需要这样做时,您希望有一种简单的方法来确保在尝试连接之前隧道已经就绪。
$app->get('/mysql_tunnel', function () use ($app) { dispatch(new STS\Tunneler\Jobs\CreateTunnel()); $users = DB::connection('mysql_tunnel') ->table('users') ->get(); dd($users); });
它是如何工作的?
它首先使用 netcat (nc
) 通过 exec
检查本地端口以查看隧道是否已打开。如果端口存在,则不再执行其他操作。
如果端口不存在,则创建 ssh 隧道连接命令并执行它,通过 exec
执行后,我们等待定义的 TUNNELER_CONN_WAIT
时间,然后再运行 netcat 以验证连接是否就绪。
就是这样。隧道将持续到超时,如果超时,则根据您选择的策略来确保它在上次需要时可用,它将简单地根据需要重新创建。