Node.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 = [
  18. 'profile' => 'array',
  19. ];
  20. public function labels()
  21. {
  22. return $this->belongsToMany(Label::class);
  23. }
  24. public function heartbeats(): HasMany
  25. {
  26. return $this->hasMany(NodeHeartbeat::class);
  27. }
  28. public function onlineIps(): HasMany
  29. {
  30. return $this->hasMany(NodeOnlineIp::class);
  31. }
  32. public function onlineLogs(): HasMany
  33. {
  34. return $this->hasMany(NodeOnlineLog::class);
  35. }
  36. public function userDataFlowLogs(): HasMany
  37. {
  38. return $this->hasMany(UserDataFlowLog::class);
  39. }
  40. public function ruleLogs(): HasMany
  41. {
  42. return $this->hasMany(RuleLog::class);
  43. }
  44. public function dailyDataFlows(): HasMany
  45. {
  46. return $this->hasMany(NodeDailyDataFlow::class);
  47. }
  48. public function hourlyDataFlows(): HasMany
  49. {
  50. return $this->hasMany(NodeHourlyDataFlow::class);
  51. }
  52. public function ruleGroup(): BelongsTo
  53. {
  54. return $this->belongsTo(RuleGroup::class);
  55. }
  56. public function relayNode(): BelongsTo
  57. {
  58. return $this->belongsTo(Node::class);
  59. }
  60. public function userGroups(): BelongsToMany
  61. {
  62. return $this->belongsToMany(UserGroup::class);
  63. }
  64. public function auth(): HasOne
  65. {
  66. return $this->hasOne(NodeAuth::class);
  67. }
  68. public function level_table(): HasOne
  69. {
  70. return $this->hasOne(Level::class, 'level', 'level');
  71. }
  72. public function users()
  73. {
  74. return User::activeUser()
  75. ->where('level', '>=', $this->attributes['level'])
  76. ->where(function ($query) {
  77. $query->whereIn('user_group_id', $this->userGroups->pluck('id'))->orWhereNull('user_group_id');
  78. })
  79. ->get();
  80. }
  81. public function refresh_geo(): bool
  82. {
  83. $ip = $this->ips();
  84. if ($ip !== []) {
  85. $data = IP::IPSB($ip[0]);
  86. if ($data) {
  87. self::withoutEvents(function () use ($data) {
  88. $this->update(['geo' => $data['latitude'].','.$data['longitude']]);
  89. });
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. public function ips(int $type = 4): array
  96. {
  97. // 使用DDNS的node先通过gethostbyname获取ip地址
  98. if ($this->attributes['is_ddns']) { // When ddns is enable, only domain can be used to check the ip
  99. $ip = gethostbyname($this->attributes['server']);
  100. if (strcmp($ip, $this->attributes['server']) === 0) {
  101. Log::warning('获取 【'.$this->attributes['server'].'】 IP失败'.$ip);
  102. $ip = '';
  103. }
  104. } else {
  105. $ip = $type === 4 ? $this->attributes['ip'] : $this->attributes['ipv6']; // check the multiple existing of ip
  106. }
  107. return array_map('trim', explode(',', $ip));
  108. }
  109. public function getConfig(User $user)
  110. {
  111. $config = [
  112. 'id' => $this->id,
  113. 'name' => $this->name,
  114. 'host' => $this->host,
  115. 'group' => sysConfig('website_name'),
  116. 'udp' => $this->is_udp,
  117. ];
  118. switch ($this->type) {
  119. case 0:
  120. $config = array_merge($config, [
  121. 'type' => 'shadowsocks',
  122. 'passwd' => $user->passwd,
  123. ], $this->profile);
  124. if ($this->port) {
  125. $config['port'] = $this->port;
  126. } else {
  127. $config['port'] = $user->port;
  128. }
  129. break;
  130. case 2:
  131. $config = array_merge($config, [
  132. 'type' => 'v2ray',
  133. 'port' => $this->port,
  134. 'uuid' => $user->vmess_id,
  135. ], $this->profile);
  136. break;
  137. case 3:
  138. $config = array_merge($config, [
  139. 'type' => 'trojan',
  140. 'port' => $this->port,
  141. 'passwd' => $user->passwd,
  142. 'sni' => $this->relay_node_id ? $this->server : '',
  143. ], $this->profile);
  144. break;
  145. case 1:
  146. case 4:
  147. $config = array_merge($config, [
  148. 'type' => 'shadowsocksr',
  149. ], $this->profile);
  150. if ($this->profile['passwd'] && $this->port) {
  151. //单端口使用中转的端口
  152. $config['port'] = $this->port;
  153. $config['protocol_param'] = $user->port.':'.$user->passwd;
  154. } else {
  155. $config['port'] = $user->port;
  156. $config['passwd'] = $user->passwd;
  157. if ($this->type === 1) {
  158. $config['method'] = $user->method;
  159. $config['protocol'] = $user->protocol;
  160. $config['obfs'] = $user->obfs;
  161. }
  162. }
  163. break;
  164. }
  165. return $config;
  166. }
  167. public function getSpeedLimitAttribute($value)
  168. {
  169. return $value / Mbps;
  170. }
  171. public function setSpeedLimitAttribute($value)
  172. {
  173. return $this->attributes['speed_limit'] = $value * Mbps;
  174. }
  175. public function getTypeLabelAttribute(): string
  176. {
  177. return [
  178. 1 => 'ShadowsocksR',
  179. 2 => 'V2Ray',
  180. 3 => 'Trojan',
  181. 4 => 'VNet',
  182. ][$this->attributes['type']] ?? 'UnKnown';
  183. }
  184. public function getHostAttribute(): string
  185. {
  186. return $this->server ?? $this->ip ?? $this->ipv6;
  187. }
  188. }