luan-nvg/laravel-ssh

轻松创建Laravel的SSH隧道

dev-master 2018-05-03 18:12 UTC

This package is not auto-updated.

Last update: 2024-09-21 10:35:59 UTC


README

我们遇到了类似的挑战,特别是通过SSH隧道访问MySQL数据库,所有问题和答案都有助于找到解决方案。然而,我们希望有一种东西可以直接与我们的Laravel应用程序和Lumen服务一起使用。

所以我们编写了这个包。希望你们喜欢它!

需求

此包已针对Laravel/Lumen版本5.2、5.3和5.4进行了测试。

我们不支持版本 <=5.1。

安装

composer require luan-nvg/laravel-ssh

注册提供者

对于Lumen服务,添加

$app->register(MNP\Tunneler\TunnelerServiceProvider::class);

bootstrap/app.php。对于Laravel应用程序,添加

MNP\Tunneler\TunnelerServiceProvider::class,

config/app.php中的providers数组。

配置

所有配置都可以并在你的.env文件中完成。

; 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 .5 seconds
TUNNELER_CONN_WAIT=500000

; 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"

;延迟编译以允许时间处理隧道,以免同时发生。TIMEMOUT_TUNNEL=5

快速入门

使用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),
],