Node.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Models;
  4. use function array_key_exists;
  5. use function count;
  6. use function time;
  7. final class Node extends Model
  8. {
  9. protected $connection = 'default';
  10. protected $table = 'node';
  11. protected $casts = [
  12. 'traffic_rate' => 'float',
  13. 'node_heartbeat' => 'int',
  14. ];
  15. /**
  16. * 节点是否显示和隐藏
  17. */
  18. public function type(): string
  19. {
  20. return $this->type ? '显示' : '隐藏';
  21. }
  22. /**
  23. * 节点类型
  24. */
  25. public function sort(): string
  26. {
  27. return match ($this->sort) {
  28. 0 => 'Shadowsocks',
  29. 11 => 'V2Ray',
  30. 14 => 'Trojan',
  31. default => '未知',
  32. };
  33. }
  34. /**
  35. * 节点最后活跃时间
  36. */
  37. public function nodeHeartbeat(): string
  38. {
  39. return date('Y-m-d H:i:s', $this->node_heartbeat);
  40. }
  41. /**
  42. * 获取节点在线状态
  43. *
  44. * @return int 0 = new node OR -1 = offline OR 1 = online
  45. */
  46. public function getNodeOnlineStatus(): int
  47. {
  48. // 类型 9 或者心跳为 0
  49. if ($this->node_heartbeat === 0) {
  50. return 0;
  51. }
  52. return $this->node_heartbeat + 300 > time() ? 1 : -1;
  53. }
  54. /**
  55. * 获取节点速率文本信息
  56. */
  57. public function getNodeSpeedlimit(): string
  58. {
  59. if ($this->node_speedlimit === 0.0) {
  60. return '0';
  61. }
  62. if ($this->node_speedlimit >= 1024.00) {
  63. return round($this->node_speedlimit / 1024.00, 1) . 'Gbps';
  64. }
  65. return $this->node_speedlimit . 'Mbps';
  66. }
  67. /**
  68. * 节点流量已耗尽
  69. */
  70. public function isNodeTrafficOut(): bool
  71. {
  72. return ! ($this->node_bandwidth_limit === 0 || $this->node_bandwidth < $this->node_bandwidth_limit);
  73. }
  74. /**
  75. * 更新节点 IP
  76. */
  77. public function changeNodeIp(string $server_name): void
  78. {
  79. $result = dns_get_record($server_name, DNS_A + DNS_AAAA);
  80. $dns = [];
  81. if (count($result) > 0) {
  82. $dns = $result[0];
  83. }
  84. if (array_key_exists('ip', $dns)) {
  85. $ip = $dns['ip'];
  86. } elseif (array_key_exists('ipv6', $dns)) {
  87. $ip = $dns['ipv6'];
  88. } else {
  89. $ip = $server_name;
  90. }
  91. $this->node_ip = $ip;
  92. }
  93. }