kamranbiglari/laravel-ssh-tunnel

轻松创建和维护Laravel的SSH隧道

dev-master 2024-06-26 13:54 UTC

This package is auto-updated.

Last update: 2024-09-26 14:28:05 UTC


README

我们从Signature Tech Studio接管了该包(原来是stechstudio/laravel-ssh-tunnel)的维护工作。衷心感谢@bubba-h57以及所有贡献者使这个包变得如此出色。

Laravel SSH隧道

通过SSH隧道访问远程主机上的服务!例如,人们多年来一直在询问如何在PHP中通过SSH连接到MySQL服务器。

我们遇到了类似的挑战,特别是访问通过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);
});

它如何工作?

它首先通过exec使用netcat(nc)检查本地端口以查看隧道是否打开。如果端口存在,则不执行其他操作。

如果端口不存在,它将创建SSH隧道连接命令,并通过exec执行它。执行后,我们等待定义的TUNNELER_CONN_WAIT时间,然后再次运行netcat以验证连接是否就绪。

就是这样。隧道将一直保持到超时,如果超时,则根据您选择的确保在需要时隧道可用和可用的策略,它将简单地按需重新创建。