Node.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 level_table(): HasOne
  62. {
  63. return $this->hasOne(Level::class, 'level', 'level');
  64. }
  65. public function users()
  66. {
  67. return User::activeUser()
  68. ->where('level', '>=', $this->attributes['level'])
  69. ->where(function ($query) {
  70. $query->whereIn('user_group_id', $this->userGroups->pluck('id'))->orWhereNull('user_group_id');
  71. })
  72. ->get();
  73. }
  74. public function refresh_geo(): bool
  75. {
  76. $ip = $this->ips();
  77. if ($ip !== []) {
  78. $data = IP::IPSB($ip[0]);
  79. if ($data) {
  80. self::withoutEvents(function () use ($data) {
  81. $this->update(['geo' => $data['latitude'].','.$data['longitude']]);
  82. });
  83. return true;
  84. }
  85. }
  86. return false;
  87. }
  88. public function ips(int $type = 4): array
  89. {
  90. // 使用DDNS的node先通过gethostbyname获取ip地址
  91. if ($this->attributes['is_ddns']) { // When ddns is enable, only domain can be used to check the ip
  92. $ip = gethostbyname($this->attributes['server']);
  93. if (strcmp($ip, $this->attributes['server']) === 0) {
  94. Log::warning('获取 【'.$this->attributes['server'].'】 IP失败'.$ip);
  95. $ip = '';
  96. }
  97. } else {
  98. $ip = $type === 4 ? $this->attributes['ip'] : $this->attributes['ipv6']; // check the multiple existing of ip
  99. }
  100. return array_map('trim', explode(',', $ip));
  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 0:
  112. $config = array_merge($config, [
  113. 'type' => 'shadowsocks',
  114. 'method' => $this->method,
  115. 'udp' => $this->is_udp,
  116. 'passwd' => $user->passwd,
  117. ]);
  118. if ($this->single) {
  119. $config['port'] = $this->is_relay ? $this->relay_port : $this->port;
  120. } else {
  121. $config['port'] = $user->port;
  122. }
  123. break;
  124. case 2:
  125. $config = array_merge($config, [
  126. 'type' => 'v2ray',
  127. 'port' => $this->is_relay ? $this->relay_port : $this->port,
  128. 'uuid' => $user->vmess_id,
  129. 'method' => $this->v2_method,
  130. 'v2_alter_id' => $this->v2_alter_id,
  131. 'v2_net' => $this->v2_net,
  132. 'v2_type' => $this->v2_type,
  133. 'v2_host' => $this->v2_host,
  134. 'v2_path' => $this->v2_path,
  135. 'v2_tls' => $this->v2_tls ? 'tls' : '',
  136. 'v2_sni' => $this->v2_sni,
  137. 'udp' => $this->is_udp,
  138. ]);
  139. break;
  140. case 3:
  141. $config = array_merge($config, [
  142. 'type' => 'trojan',
  143. 'port' => $this->is_relay ? $this->relay_port : $this->port,
  144. 'passwd' => $user->passwd,
  145. 'sni' => $this->is_relay ? $this->server : '',
  146. 'udp' => $this->is_udp,
  147. ]);
  148. break;
  149. case 1:
  150. case 4:
  151. $config = array_merge($config, [
  152. 'type' => $this->compatible ? 'shadowsocks' : 'shadowsocksr',
  153. 'method' => $this->method,
  154. 'protocol' => $this->protocol,
  155. 'obfs' => $this->obfs,
  156. 'obfs_param' => $this->obfs_param,
  157. 'udp' => $this->is_udp,
  158. ]);
  159. if ($this->single) {
  160. //单端口使用中转的端口
  161. $config['port'] = $this->is_relay ? $this->relay_port : $this->port;
  162. $config['passwd'] = $this->passwd;
  163. $config['protocol_param'] = $user->port.':'.$user->passwd;
  164. } else {
  165. $config['port'] = $user->port;
  166. $config['passwd'] = $user->passwd;
  167. $config['protocol_param'] = $this->protocol_param;
  168. if ($this->type === 1) {
  169. $config['method'] = $user->method;
  170. $config['protocol'] = $user->protocol;
  171. $config['obfs'] = $user->obfs;
  172. }
  173. }
  174. break;
  175. }
  176. return $config;
  177. }
  178. public function getSpeedLimitAttribute($value)
  179. {
  180. return $value / Mbps;
  181. }
  182. public function setSpeedLimitAttribute($value)
  183. {
  184. return $this->attributes['speed_limit'] = $value * Mbps;
  185. }
  186. public function getTypeLabelAttribute(): string
  187. {
  188. return [
  189. 1 => 'ShadowsocksR',
  190. 2 => 'V2Ray',
  191. 3 => 'Trojan',
  192. 4 => 'VNet',
  193. ][$this->attributes['type']] ?? 'UnKnown';
  194. }
  195. public function getHostAttribute(): string
  196. {
  197. if ($this->is_relay) {
  198. return $this->relay_server;
  199. }
  200. return $this->server ?: $this->ip;
  201. }
  202. }