Node.php 4.9 KB

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