Node.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Models;
  4. /**
  5. * Node Model
  6. *
  7. * @property-read int $id id
  8. *
  9. * @property string $name Display name
  10. * @property bool $type If node display @todo Correct column name and type
  11. * @property string $server Domain
  12. * @property string $method Crypt method @deprecated
  13. * @property string $info Infomation
  14. * @property string $status Status description
  15. * @property int $sort Node type @todo Correct column name to `type`
  16. * @property int $custom_method Customs node crypt @deprecated
  17. * @property float $traffic_rate Node traffic rate
  18. *
  19. * @todo More property
  20. *
  21. * @property bool $online If node is online
  22. * @property bool $gfw_block If node is blocked by GFW
  23. */
  24. final class Node extends Model
  25. {
  26. protected $connection = 'default';
  27. protected $table = 'node';
  28. protected $casts = [
  29. 'traffic_rate' => 'float',
  30. 'mu_only' => 'int',
  31. 'node_heartbeat' => 'int',
  32. ];
  33. /**
  34. * 节点是否显示和隐藏
  35. */
  36. public function type(): string
  37. {
  38. return $this->type ? '显示' : '隐藏';
  39. }
  40. /**
  41. * 节点类型
  42. */
  43. public function sort(): string
  44. {
  45. switch ($this->sort) {
  46. case 0:
  47. $sort = 'Shadowsocks';
  48. break;
  49. case 1:
  50. $sort = 'ShadowsocksR';
  51. break;
  52. case 11:
  53. $sort = 'V2Ray 节点';
  54. break;
  55. case 14:
  56. $sort = 'Trojan';
  57. break;
  58. default:
  59. $sort = '系统保留';
  60. }
  61. return $sort;
  62. }
  63. /**
  64. * 单端口多用户启用类型
  65. */
  66. public function muOnly(): string
  67. {
  68. switch ($this->mu_only) {
  69. case -1:
  70. $mu_only = '只启用普通端口';
  71. break;
  72. case 0:
  73. $mu_only = '单端口多用户与普通端口并存';
  74. break;
  75. case 1:
  76. $mu_only = '只启用单端口多用户';
  77. break;
  78. default:
  79. $mu_only = '错误类型';
  80. }
  81. return $mu_only;
  82. }
  83. /**
  84. * 节点最后活跃时间
  85. */
  86. public function nodeHeartbeat(): string
  87. {
  88. return date('Y-m-d H:i:s', $this->node_heartbeat);
  89. }
  90. /**
  91. * 获取节点在线状态
  92. *
  93. * @return int 0 = new node OR -1 = offline OR 1 = online
  94. */
  95. public function getNodeOnlineStatus(): int
  96. {
  97. // 类型 9 或者心跳为 0
  98. if ($this->node_heartbeat === 0 || \in_array($this->sort, [9])) {
  99. return 0;
  100. }
  101. return $this->node_heartbeat + 300 > \time() ? 1 : -1;
  102. }
  103. /**
  104. * 获取节点速率文本信息
  105. */
  106. public function getNodeSpeedlimit(): string
  107. {
  108. if ($this->node_speedlimit === 0.0) {
  109. return '0';
  110. }
  111. if ($this->node_speedlimit >= 1024.00) {
  112. return round($this->node_speedlimit / 1024.00, 1) . 'Gbps';
  113. }
  114. return $this->node_speedlimit . 'Mbps';
  115. }
  116. /**
  117. * 节点是在线的
  118. */
  119. public function isNodeOnline(): ?bool
  120. {
  121. if ($this->node_heartbeat === 0) {
  122. return false;
  123. }
  124. return $this->node_heartbeat > \time() - 300;
  125. }
  126. /**
  127. * 节点流量已耗尽
  128. */
  129. public function isNodeTrafficOut(): bool
  130. {
  131. return ! ($this->node_bandwidth_limit === 0 || $this->node_bandwidth < $this->node_bandwidth_limit);
  132. }
  133. /**
  134. * 节点是可用的,即流量未耗尽并且在线
  135. */
  136. public function isNodeAccessable(): bool
  137. {
  138. return $this->isNodeTrafficOut() === false && $this->isNodeOnline() === true;
  139. }
  140. /**
  141. * 更新节点 IP
  142. */
  143. public function changeNodeIp(string $server_name): bool
  144. {
  145. $result = dns_get_record($server_name, DNS_A + DNS_AAAA);
  146. $dns = [];
  147. if (count($result) > 0) {
  148. $dns = $result[0];
  149. }
  150. if (array_key_exists('ip', $dns)) {
  151. $ip = $dns['ip'];
  152. } elseif (array_key_exists('ipv6', $dns)) {
  153. $ip = $dns['ipv6'];
  154. } else {
  155. $ip = $server_name;
  156. }
  157. $this->node_ip = $ip;
  158. return true;
  159. }
  160. /**
  161. * 获取节点 IP
  162. */
  163. public function getNodeIp(): string
  164. {
  165. $node_ip_str = $this->node_ip;
  166. $node_ip_array = explode(',', $node_ip_str);
  167. return $node_ip_array[0];
  168. }
  169. }