-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f281a05
Showing
6 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "stechstudio/laravel-ssh-tunnel", | ||
"description": "Easy creation & maintenance of an SSH Tunnel for Laravel/Lumen", | ||
"type": "library", | ||
"authors": [ | ||
{ | ||
"name": "Bubba Hines", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"minimum-stability": "dev", | ||
"require": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php namespace stechstudio\Tunneler\Console; | ||
|
||
use Illuminate\Console\Command; | ||
use stechstudio\Tunneler\Jobs\CreateTunnel; | ||
|
||
class TunnelerCommand extends Command { | ||
/** | ||
* The console command name. | ||
* | ||
* @var string | ||
*/ | ||
protected $name = 'tunneler:activate'; | ||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Creates and Maintains an SSH Tunnel'; | ||
|
||
public function handle(){ | ||
try { | ||
$result = dispatch(new CreateTunnel()); | ||
}catch (\ErrorException $e){ | ||
$this->error($e->getMessage()); | ||
return 1; | ||
} | ||
|
||
if ($result === 1 ){ | ||
$this->info('The Tunnel is already Activated.'); | ||
return 0; | ||
} | ||
|
||
if ($result === 2 ){ | ||
$this->info('The Tunnel has been Activated.'); | ||
return 0; | ||
} | ||
|
||
$this->warn('I have no idea how this happened. Let me know if you figure it out.'); | ||
return 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php namespace stechstudio\Tunneler\Jobs; | ||
|
||
class CreateTunnel | ||
{ | ||
|
||
/** | ||
* The Command for checking if the tunnel is open | ||
* @var string | ||
*/ | ||
protected $ncCommand; | ||
|
||
/** | ||
* The command for creating the tunnel | ||
* @var string | ||
*/ | ||
protected $sshCommand; | ||
|
||
/** | ||
* Simple place to keep all output. | ||
* @var array | ||
*/ | ||
protected $output = []; | ||
|
||
public function __construct() | ||
{ | ||
$this->ncCommand = sprintf('%s -z %s %d > /dev/null 2>&1', | ||
config('tunneler.nc_path'), | ||
config('tunneler.local_address'), | ||
config('tunneler.local_port') | ||
); | ||
|
||
$this->sshCommand = sprintf('%s -N -i %s -L %d:%s:%d -p %d %s@%s', | ||
config('tunneler.ssh_path'), | ||
config('tunneler.identity_file'), | ||
config('tunneler.local_port'), | ||
config('tunneler.bind_address'), | ||
config('tunneler.bind_port'), | ||
config('tunneler.port'), | ||
config('tunneler.user'), | ||
config('tunneler.hostname') | ||
); | ||
} | ||
|
||
|
||
public function handle(): int | ||
{ | ||
if ($this->verifyTunnel()){ | ||
return 1; | ||
} | ||
|
||
$this->createTunnel(); | ||
|
||
if ($this->verifyTunnel()){ | ||
return 2; | ||
} | ||
|
||
throw new \ErrorException(sprintf("Could Not Create SSH Tunnel with command:\n\t%s\nCheck your configuration.", | ||
$this->sshCommand)); | ||
} | ||
|
||
|
||
/** | ||
* Creates the SSH Tunnel for us. | ||
*/ | ||
protected function createTunnel() | ||
{ | ||
$this->runCommand(sprintf('%s %s > /dev/null &', config('tunneler.nohup_path'), $this->sshCommand)); | ||
// Ensure we wait long enough for it to actually connect. | ||
usleep(config('tunneler.wait')); | ||
} | ||
|
||
/** | ||
* Verifies whether the tunnel is active or not. | ||
* @return bool | ||
*/ | ||
protected function verifyTunnel(): bool | ||
{ | ||
return $this->runCommand($this->ncCommand); | ||
} | ||
|
||
/** | ||
* Runs a command and converts the exit code to a boolean | ||
* @param $command | ||
* @return bool | ||
*/ | ||
protected function runCommand($command): bool | ||
{ | ||
$return_var = 1; | ||
exec($command, $this->output, $return_var); | ||
return (bool)($return_var === 0); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php namespace stechstudio\Tunneler; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
use stechstudio\Tunneler\Console\TunnelerCommand; | ||
use stechstudio\Tunneler\Jobs\CreateTunnel; | ||
|
||
|
||
class TunnelerServiceProvider extends ServiceProvider{ | ||
/** | ||
* Indicates if loading of the provider is deferred. | ||
* | ||
* @var bool | ||
*/ | ||
protected $defer = false; | ||
|
||
/** | ||
* Default path to configuration | ||
* @var string | ||
*/ | ||
protected $configPath = __DIR__ . '/config/tunneler.php'; | ||
|
||
|
||
public function boot() | ||
{ | ||
// helps deal with Lumen vs Laravel differences | ||
if (function_exists('config_path')) { | ||
$publishPath = config_path('tunneler.php'); | ||
} else { | ||
$publishPath = base_path('config/tunneler.php'); | ||
} | ||
|
||
$this->publishes([$this->configPath => $publishPath], 'config'); | ||
|
||
if (config('tunneler.on_boot')){ | ||
dispatch(new CreateTunnel()); | ||
} | ||
} | ||
|
||
public function register() | ||
{ | ||
if ( is_a($this->app,'Laravel\Lumen\Application')){ | ||
$this->app->configure('tunneler'); | ||
} | ||
$this->mergeConfigFrom($this->configPath, 'tunneler'); | ||
|
||
$this->app['command.tunneler.activate'] = $this->app->share( | ||
function ($app) { | ||
return new TunnelerCommand(); | ||
} | ||
); | ||
|
||
$this->commands('command.tunneler.activate'); | ||
} | ||
|
||
/** | ||
* Get the services provided by the provider. | ||
* | ||
* @return array | ||
*/ | ||
public function provides() | ||
{ | ||
return array('command.tunneler.activate'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
return [ | ||
'nc_path' => env('TUNNELER_NC_PATH', 'nc'), | ||
'ssh_path' => env('TUNNELER_SSH_PATH', 'ssh'), | ||
'nohup_path' => env('TUNNELER_NOHUP_PATH', 'nohup'), | ||
|
||
'local_address' => env('TUNNELER_LOCAL_ADDRESS', '127.0.0.1'), | ||
'local_port' => env('TUNNELER_LOCAL_PORT'), | ||
'identity_file' => env('TUNNELER_IDENTITY_FILE'), | ||
|
||
'bind_address' => env('TUNNELER_BIND_ADDRESS', '127.0.0.1'), | ||
'bind_port' => env('TUNNELER_BIND_PORT'), | ||
|
||
'user' => env('TUNNELER_USER'), | ||
'hostname' => env('TUNNELER_HOSTNAME'), | ||
'port' => env('TUNNELER_PORT'), | ||
'wait' => env('TUNNELER_CONN_WAIT', '500000'), | ||
|
||
'on_boot' => filter_var(env('TUNNELER_ON_BOOT', false), FILTER_VALIDATE_BOOLEAN) | ||
]; |