NodeCertificate.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Casts\Attribute;
  4. use Illuminate\Database\Eloquent\Model;
  5. /**
  6. * 伪装域名证书.
  7. */
  8. class NodeCertificate extends Model
  9. {
  10. protected $table = 'node_certificate';
  11. protected $guarded = [];
  12. protected $appends = ['issuer', 'from', 'to'];
  13. private $certInfo = null;
  14. protected function getCertInfo(): ?array
  15. {
  16. if ($this->certInfo === null && $this->pem) {
  17. $this->certInfo = openssl_x509_parse($this->pem) ?: false;
  18. }
  19. return $this->certInfo ?: null;
  20. }
  21. protected function issuer(): Attribute
  22. {
  23. return Attribute::make(
  24. get: function () {
  25. $certInfo = $this->getCertInfo();
  26. return $certInfo ? ($certInfo['issuer']['O'] ?? null) : null;
  27. }
  28. );
  29. }
  30. protected function from(): Attribute
  31. {
  32. return Attribute::make(
  33. get: function () {
  34. $certInfo = $this->getCertInfo();
  35. if ($certInfo && isset($certInfo['validFrom_time_t'])) {
  36. return date('Y-m-d', $certInfo['validFrom_time_t']);
  37. }
  38. return null;
  39. }
  40. );
  41. }
  42. protected function to(): Attribute
  43. {
  44. return Attribute::make(
  45. get: function () {
  46. $certInfo = $this->getCertInfo();
  47. if ($certInfo && isset($certInfo['validTo_time_t'])) {
  48. return date('Y-m-d', $certInfo['validTo_time_t']);
  49. }
  50. return null;
  51. }
  52. );
  53. }
  54. }