summaryrefslogtreecommitdiff
path: root/src/Connection.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Connection.php')
-rw-r--r--src/Connection.php60
1 files changed, 43 insertions, 17 deletions
diff --git a/src/Connection.php b/src/Connection.php
index 2052cd1..2cddeb4 100644
--- a/src/Connection.php
+++ b/src/Connection.php
@@ -2,7 +2,6 @@
namespace PHPIAC;
-use PHPIAC\Support\SingletonTraitWithArguments;
use phpseclib3\Crypt\PublicKeyLoader;
use phpseclib3\Net\SFTP;
use phpseclib3\Net\SSH2;
@@ -12,40 +11,65 @@ use phpseclib3\Net\SSH2;
*
* @method static string exec(string $command, callback $callback = null)
* @method static string|bool|null read(string $expect = '', int $read = 1)
+ * @method static bool write(string $cmd)
* @method static enablePty()
* @method static disablePty()
*/
class Connection
{
- use SingletonTraitWithArguments;
+ private static SSH2 $ssh;
+ private static SFTP $sftp;
- private SSH2 $ssh;
- private SFTP $sftp;
+ private static string $host;
+ private static string $user;
+ private static mixed $key;
/**
* Connection constructor.
*
* @param string $host
* @param string $user
- * @param string $key
+ * @param string $keyFile
*
* @throws \Exception
*/
- public function __construct(string $host, string $user, string $key)
+ public static function initialize(string $host, string $user, string $keyFile)
{
- $this->ssh = new SSH2($host);
- $key = PublicKeyLoader::load(file_get_contents($key));
- if (! $this->ssh->login($user, $key)) {
+ self::$host = $host;
+ self::$user = $user;
+ self::$key = PublicKeyLoader::load(file_get_contents($keyFile));
+
+ self::connect();
+ }
+
+ /**
+ * @throws \Exception
+ */
+ private static function connect()
+ {
+ self::$ssh = new SSH2(self::$host);
+ if (! self::$ssh->login(self::$user, self::$key)) {
throw new \Exception('SSH Login failed');
}
- $this->sftp = new SFTP($host);
- if (! $this->sftp->login($user, $key)) {
+ self::$sftp = new SFTP(self::$host);
+ if (! self::$sftp->login(self::$user, self::$key)) {
throw new \Exception('SFTP Login failed');
}
}
/**
+ * @throws \Exception
+ */
+ private static function ensureConnection()
+ {
+ if (! self::$ssh->isConnected() ||
+ ! self::$sftp->isConnected()) {
+ self::connect();
+ }
+ }
+
+ /**
* Calls SSH2 methods statically
*
* @param string $name
@@ -55,13 +79,13 @@ class Connection
*/
public static function __callStatic(string $name, array $arguments): mixed
{
- $self = self::getInstance();
+ self::ensureConnection();
- if (! method_exists($self->ssh, $name)) {
- return $self->sftp->$name(...$arguments);
+ if (! method_exists(self::$ssh, $name)) {
+ return self::$sftp->$name(...$arguments);
}
- return $self->ssh->$name(...$arguments);
+ return self::$ssh->$name(...$arguments);
}
/**
@@ -69,10 +93,12 @@ class Connection
*/
public static function put($remote_file, $data, $mode = SFTP::SOURCE_STRING, $start = -1, $local_start = -1, $progressCallback = null): bool
{
+ self::ensureConnection();
+
$tmp = bin2hex(random_bytes(10)); # work around sftp sudo put restrictions
return
- self::getInstance()->sftp->put("/tmp/$tmp", $data, $mode, $start, $local_start, $progressCallback) &&
- self::getInstance()->ssh->exec("sudo mv /tmp/$tmp $remote_file");
+ self::$sftp->put("/tmp/$tmp", $data, $mode, $start, $local_start, $progressCallback) &&
+ self::$ssh->exec("sudo mv /tmp/$tmp $remote_file");
}
}