Node.php 5.0 KB

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