diff --git a/src/Console/TunnelerReset.php b/src/Console/TunnelerReset.php new file mode 100644 index 0000000..b587862 --- /dev/null +++ b/src/Console/TunnelerReset.php @@ -0,0 +1,45 @@ +destoryTunnel(); + + \Artisan::call('tunneler:activate'); + } +} diff --git a/src/Jobs/CreateTunnel.php b/src/Jobs/CreateTunnel.php index e715b31..714c731 100644 --- a/src/Jobs/CreateTunnel.php +++ b/src/Jobs/CreateTunnel.php @@ -58,13 +58,13 @@ public function handle(): int } $this->createTunnel(); - + $tries = config('tunneler.tries'); for ($i = 0; $i < $tries; $i++) { if ($this->verifyTunnel()) { return 2; } - + // Wait a bit until next iteration usleep(config('tunneler.wait')); } @@ -101,6 +101,15 @@ protected function verifyTunnel() return $this->runCommand($this->ncCommand); } + /* + * Use pkill to kill the SSH tunnel + */ + + public function destoryTunnel(){ + $ssh_command = preg_replace('/[\s]{2}[\s]*/',' ',$this->sshCommand); + return $this->runCommand('pkill -f "'.$ssh_command.'"'); + } + /** * Runs a command and converts the exit code to a boolean * @param $command diff --git a/src/TunnelerServiceProvider.php b/src/TunnelerServiceProvider.php index af7db78..522e146 100644 --- a/src/TunnelerServiceProvider.php +++ b/src/TunnelerServiceProvider.php @@ -2,6 +2,7 @@ use Illuminate\Support\ServiceProvider; use STS\Tunneler\Console\TunnelerCommand; +use STS\Tunneler\Console\TunnelerReset; use STS\Tunneler\Jobs\CreateTunnel; @@ -50,6 +51,14 @@ function ($app) { ); $this->commands('command.tunneler.activate'); + + $this->app->singleton('command.tunneler.reset', + function ($app) { + return new TunnelerReset(); + } + ); + + $this->commands('command.tunneler.reset'); } /**