Node.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace App\Models;
  3. use App\Casts\data_rate;
  4. use App\Observers\NodeObserver;
  5. use App\Utils\IP;
  6. use Illuminate\Database\Eloquent\Attributes\ObservedBy;
  7. use Illuminate\Database\Eloquent\Casts\Attribute;
  8. use Illuminate\Database\Eloquent\Collection;
  9. use Illuminate\Database\Eloquent\Model;
  10. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  11. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  12. use Illuminate\Database\Eloquent\Relations\HasMany;
  13. use Illuminate\Database\Eloquent\Relations\HasOne;
  14. use Log;
  15. /**
  16. * 节点配置信息.
  17. */
  18. #[ObservedBy([NodeObserver::class])]
  19. class Node extends Model
  20. {
  21. protected $table = 'node';
  22. protected $guarded = [];
  23. protected $casts = ['speed_limit' => data_rate::class, 'profile' => 'array', 'details' => 'array'];
  24. public function labels(): BelongsToMany
  25. {
  26. return $this->belongsToMany(Label::class);
  27. }
  28. public function heartbeats(): HasMany
  29. {
  30. return $this->hasMany(NodeHeartbeat::class);
  31. }
  32. public function onlineIps(): HasMany
  33. {
  34. return $this->hasMany(NodeOnlineIp::class);
  35. }
  36. public function onlineLogs(): HasMany
  37. {
  38. return $this->hasMany(NodeOnlineLog::class);
  39. }
  40. public function userDataFlowLogs(): HasMany
  41. {
  42. return $this->hasMany(UserDataFlowLog::class);
  43. }
  44. public function ruleLogs(): HasMany
  45. {
  46. return $this->hasMany(RuleLog::class);
  47. }
  48. public function dailyDataFlows(): HasMany
  49. {
  50. return $this->hasMany(NodeDailyDataFlow::class);
  51. }
  52. public function hourlyDataFlows(): HasMany
  53. {
  54. return $this->hasMany(NodeHourlyDataFlow::class);
  55. }
  56. public function country(): HasOne
  57. {
  58. return $this->HasOne(Country::class, 'code', 'country_code');
  59. }
  60. public function ruleGroup(): BelongsTo
  61. {
  62. return $this->belongsTo(RuleGroup::class);
  63. }
  64. public function relayNode(): BelongsTo
  65. {
  66. return $this->belongsTo(__CLASS__);
  67. }
  68. public function childNodes(): HasMany
  69. {
  70. return $this->hasMany(__CLASS__, 'relay_node_id', 'id');
  71. }
  72. public function userGroups(): BelongsToMany
  73. {
  74. return $this->belongsToMany(UserGroup::class);
  75. }
  76. public function auth(): HasOne
  77. {
  78. return $this->hasOne(NodeAuth::class);
  79. }
  80. public function level_table(): HasOne
  81. {
  82. return $this->hasOne(Level::class, 'level', 'level');
  83. }
  84. public function users(): Collection
  85. {
  86. return User::activeUser()
  87. ->where('level', '>=', $this->level)
  88. ->where(function ($query) {
  89. $query->whereIn('user_group_id', $this->userGroups->pluck('id'))->orWhereNull('user_group_id');
  90. })
  91. ->get();
  92. }
  93. public function refresh_geo(): bool
  94. {
  95. $ip = $this->ips();
  96. if ($ip !== []) {
  97. $data = IP::getIPGeo($ip[0]); // 复数IP都以第一个为准
  98. if ($data) {
  99. self::withoutEvents(function () use ($data) {
  100. $this->update(['geo' => ($data['latitude'] ?? null).','.($data['longitude'] ?? null)]);
  101. });
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. public function ips(int $type = 4): array
  108. {
  109. // 使用DDNS的node先通过gethostbyname获取ip地址
  110. if ($this->is_ddns ?? 0) { // When ddns is enabled, only domain can be used to check the ip
  111. $ip = gethostbyname($this->server);
  112. if (strcmp($ip, $this->server) === 0) {
  113. Log::warning('获取 【'.$this->server.'】 IP失败'.$ip);
  114. $ip = '';
  115. }
  116. } else {
  117. $ip = $type === 4 ? $this->ip : $this->ipv6; // check the multiple existing of ip
  118. }
  119. return array_map('trim', explode(',', $ip));
  120. }
  121. public function getSSRConfig(): array
  122. {
  123. return [
  124. 'id' => $this->id,
  125. 'method' => $this->profile['method'] ?? '',
  126. 'protocol' => $this->profile['protocol'] ?? '',
  127. 'obfs' => $this->profile['obfs'] ?? '',
  128. 'obfs_param' => $this->profile['obfs_param'] ?? '',
  129. 'is_udp' => (int) $this->is_udp,
  130. 'speed_limit' => $this->getRawOriginal('speed_limit'),
  131. 'client_limit' => $this->client_limit,
  132. 'single' => isset($this->profile['passwd']) ? 1 : 0,
  133. 'port' => (string) $this->port,
  134. 'passwd' => $this->profile['passwd'] ?? '',
  135. 'push_port' => $this->push_port,
  136. 'secret' => $this->auth->secret,
  137. 'redirect_url' => sysConfig('redirect_url'),
  138. ];
  139. }
  140. protected function typeLabel(): Attribute
  141. {
  142. return Attribute::make(
  143. get: fn () => match ($this->type) {
  144. 0 => 'Shadowsocks',
  145. 1 => 'ShadowsocksR',
  146. 2 => 'V2Ray',
  147. 3 => 'Trojan',
  148. 4 => 'VNet',
  149. default => 'UnKnown',
  150. },
  151. );
  152. }
  153. protected function host(): Attribute
  154. {
  155. return Attribute::make(
  156. get: fn () => $this->server ?? $this->ip ?? $this->ipv6,
  157. );
  158. }
  159. }