Node.php 6.4 KB

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