| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- /* * ********************************************************************
- * ProxmoxVPS product developed. (2016-12-12)
- * *
- *
- * CREATED BY MODULESGARDEN -> http://modulesgarden.com
- * CONTACT -> contact@modulesgarden.com
- *
- *
- * This software is furnished under a license and may be used and copied
- * only in accordance with the terms of such license and with the
- * inclusion of the above copyright notice. This software or any other
- * copies thereof may not be provided or otherwise made available to any
- * other person. No title to and ownership of the software is hereby
- * transferred.
- *
- *
- * ******************************************************************** */
- namespace ModulesGarden\ProxmoxAddon\App\Models;
- use ModulesGarden\ProxmoxAddon\Core\Models\ExtendedEloquentModel;
- /**
- * Description of KeyPair
- *
- * @author Pawel Kopec <pawelk@modulesgarden.com>
- * @version 1.0.0
- * @property int $hosting_id
- * @property int $vm_id
- * @method static $this ofHostingId($hostingId)
- * @method $this ofVmId($id)
- */
- class KeyPair extends ExtendedEloquentModel
- {
- /** @var string */
- protected $table = 'KeyPair';
- protected $fillable = ['id', 'vm_id', 'hosting_id', 'public', 'private'];
- public $timestamps = false;
- public function getId()
- {
- return $this->id;
- }
- public function getHostingId()
- {
- return $this->hosting_id;
- }
- public function getPublic()
- {
- return decrypt($this->public);
- }
- public function getPrivate()
- {
- return decrypt($this->private);
- }
- public function isPublic()
- {
- return decrypt($this->public) != null;
- }
- public function isPrivate()
- {
- return decrypt($this->private) != null;
- }
- public function setId($id)
- {
- $this->id = $id;
- return $this;
- }
- public function setHostingId($hostingId)
- {
- $this->hosting_id = $hostingId;
- return $this;
- }
- public function setPublic($public)
- {
- $this->public = encrypt($public);
- return $this;
- }
- public function setPrivate($private)
- {
- $this->private = encrypt($private);
- return $this;
- }
- /**
- * @return KeyPair
- */
- public static function make()
- {
- $config = ['private_key_type' => OPENSSL_KEYTYPE_RSA];
- $res = openssl_pkey_new($config);
- openssl_pkey_export($res, $privkey);
- $pubkey = openssl_pkey_get_details($res);
- $pubKey = self::encodePublicKey($res);
- $obj = new KeyPair();
- $obj->setPublic($pubKey)
- ->setPrivate($privkey);
- return $obj;
- }
- private static function encodePublicKey($privKey)
- {
- $keyInfo = openssl_pkey_get_details($privKey);
- $buffer = pack("N", 7) . "ssh-rsa" .
- self::encodeBuffer($keyInfo['rsa']['e']) .
- self::encodeBuffer($keyInfo['rsa']['n']);
- return "ssh-rsa " . base64_encode($buffer);
- }
- private static function encodeBuffer($buffer)
- {
- $len = strlen($buffer);
- if (ord($buffer[0]) & 0x80)
- {
- $len++;
- $buffer = "\x00" . $buffer;
- }
- return pack("Na*", $len, $buffer);
- }
- public function scopeOfHostingId($query, $hostingId)
- {
- return $query->where("hosting_id", $hostingId);
- }
- public function scopeOfVmId($query, $vmId)
- {
- return $query->where("vm_id", $vmId);
- }
- }
|