Node.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  6. use Illuminate\Database\Eloquent\Relations\HasMany;
  7. use Illuminate\Database\Eloquent\Relations\HasOne;
  8. /**
  9. * 节点配置信息.
  10. */
  11. class Node extends Model
  12. {
  13. protected $table = 'node';
  14. protected $guarded = [];
  15. public function labels()
  16. {
  17. return $this->belongsToMany(Label::class);
  18. }
  19. public function heartBeats(): HasMany
  20. {
  21. return $this->hasMany(NodeHeartBeat::class);
  22. }
  23. public function onlineIps(): HasMany
  24. {
  25. return $this->hasMany(NodeOnlineIp::class);
  26. }
  27. public function onlineLogs(): HasMany
  28. {
  29. return $this->hasMany(NodeOnlineLog::class);
  30. }
  31. public function userDataFlowLogs(): HasMany
  32. {
  33. return $this->hasMany(UserDataFlowLog::class);
  34. }
  35. public function ruleLogs(): HasMany
  36. {
  37. return $this->hasMany(RuleLog::class);
  38. }
  39. public function dailyDataFlows(): HasMany
  40. {
  41. return $this->hasMany(NodeDailyDataFlow::class);
  42. }
  43. public function hourlyDataFlows(): HasMany
  44. {
  45. return $this->hasMany(NodeHourlyDataFlow::class);
  46. }
  47. public function ruleGroup(): BelongsTo
  48. {
  49. return $this->belongsTo(RuleGroup::class);
  50. }
  51. public function userGroups(): BelongsToMany
  52. {
  53. return $this->belongsToMany(UserGroup::class);
  54. }
  55. public function auth(): HasOne
  56. {
  57. return $this->hasOne(NodeAuth::class);
  58. }
  59. public function level_table(): HasOne
  60. {
  61. return $this->hasOne(Level::class, 'level', 'level');
  62. }
  63. public function users()
  64. {
  65. return User::activeUser()
  66. ->where('level', '>=', $this->attributes['level'])
  67. ->whereNull('user_group_id')
  68. ->orwhereIn('user_group_id', $this->userGroups->pluck('id')->toArray())
  69. ->get();
  70. }
  71. public function config(User $user)
  72. {
  73. $config = [
  74. 'id' => $this->id,
  75. 'name' => $this->name,
  76. 'host' => $this->is_relay ? $this->relay_server : ($this->server ?: $this->ip),
  77. 'group' => sysConfig('website_name'),
  78. ];
  79. switch ($this->type) {
  80. case 2:
  81. $config = array_merge($config, [
  82. 'type' => 'v2ray',
  83. 'port' => $this->is_relay ? $this->relay_port : $this->v2_port,
  84. 'uuid' => $user->vmess_id,
  85. 'method' => $this->v2_method,
  86. 'v2_alter_id' => $this->v2_alter_id,
  87. 'v2_net' => $this->v2_net,
  88. 'v2_type' => $this->v2_type,
  89. 'v2_host' => $this->v2_host,
  90. 'v2_path' => $this->v2_path,
  91. 'v2_tls' => $this->v2_tls ? 'tls' : '',
  92. 'udp' => $this->is_udp,
  93. ]);
  94. break;
  95. case 3:
  96. $config = array_merge($config, [
  97. 'type' => 'trojan',
  98. 'port' => $this->is_relay ? $this->relay_port : $this->v2_port,
  99. 'passwd' => $user->passwd,
  100. 'sni' => $this->is_relay ? $this->server : '',
  101. 'udp' => $this->is_udp,
  102. ]);
  103. break;
  104. case 1:
  105. case 4:
  106. $config = array_merge($config, [
  107. 'type' => $this->compatible ? 'shadowsocks' : 'shadowsocksr',
  108. 'method' => $this->method,
  109. 'protocol' => $this->protocol,
  110. 'obfs' => $this->obfs,
  111. 'obfs_param' => $this->obfs_param,
  112. 'udp' => $this->is_udp,
  113. ]);
  114. if ($this->single) {
  115. //单端口使用中转的端口
  116. $config['port'] = $this->is_relay ? $this->relay_port : $this->port;
  117. $config['passwd'] = $this->passwd;
  118. $config['protocol_param'] = $user->port.':'.$user->passwd;
  119. } else {
  120. $config['port'] = $user->port;
  121. $config['passwd'] = $user->passwd;
  122. $config['protocol_param'] = $this->protocol_param;
  123. if ($this->type === 1) {
  124. $config['method'] = $user->method;
  125. $config['protocol'] = $user->protocol;
  126. $config['obfs'] = $user->obfs;
  127. }
  128. }
  129. break;
  130. }
  131. return $config;
  132. }
  133. public function getSpeedLimitAttribute($value)
  134. {
  135. return $value / Mbps;
  136. }
  137. public function setSpeedLimitAttribute($value)
  138. {
  139. return $this->attributes['speed_limit'] = $value * Mbps;
  140. }
  141. public function getTypeLabelAttribute(): string
  142. {
  143. switch ($this->attributes['type']) {
  144. case 1:
  145. $type_label = 'ShadowsocksR';
  146. break;
  147. case 2:
  148. $type_label = 'V2Ray';
  149. break;
  150. case 3:
  151. $type_label = 'Trojan';
  152. break;
  153. case 4:
  154. $type_label = 'VNet';
  155. break;
  156. default:
  157. $type_label = 'UnKnown';
  158. }
  159. return $type_label;
  160. }
  161. }