Node.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 pingLogs(): HasMany
  40. {
  41. return $this->hasMany(NodePing::class);
  42. }
  43. public function dailyDataFlows(): HasMany
  44. {
  45. return $this->hasMany(NodeDailyDataFlow::class);
  46. }
  47. public function hourlyDataFlows(): HasMany
  48. {
  49. return $this->hasMany(NodeHourlyDataFlow::class);
  50. }
  51. public function ruleGroup(): BelongsTo
  52. {
  53. return $this->belongsTo(RuleGroup::class);
  54. }
  55. public function userGroups(): BelongsToMany
  56. {
  57. return $this->belongsToMany(UserGroup::class);
  58. }
  59. public function auth(): HasOne
  60. {
  61. return $this->hasOne(NodeAuth::class);
  62. }
  63. public function level_table(): HasOne
  64. {
  65. return $this->hasOne(Level::class, 'level', 'level');
  66. }
  67. public function users()
  68. {
  69. return User::activeUser()->whereIn('user_group_id', $this->userGroups->pluck('id')->toArray())->orwhereNull('user_group_id')->where('level', '>=',
  70. $this->attributes['level'])->get();
  71. }
  72. public function config($user)
  73. {
  74. $config = [
  75. 'id' => $this->id,
  76. 'name' => $this->name,
  77. 'host' => $this->is_relay ? $this->relay_server : ($this->server ?: $this->ip),
  78. 'group' => sysConfig('website_name'),
  79. ];
  80. switch ($this->type) {
  81. case 2:
  82. $config = array_merge($config, [
  83. 'type' => 'v2ray',
  84. 'port' => $this->is_relay ? $this->relay_port : $this->v2_port,
  85. 'uuid' => $user->vmess_id,
  86. 'method' => $this->v2_method,
  87. 'v2_alter_id' => $this->v2_alter_id,
  88. 'v2_net' => $this->v2_net,
  89. 'v2_type' => $this->v2_type,
  90. 'v2_host' => $this->v2_host,
  91. 'v2_path' => $this->v2_path,
  92. 'v2_tls' => $this->v2_tls ? 'tls' : '',
  93. 'udp' => $this->is_udp,
  94. ]);
  95. break;
  96. case 3:
  97. $config = array_merge($config, [
  98. 'type' => 'trojan',
  99. 'port' => $this->is_relay ? $this->relay_port : $this->v2_port,
  100. 'passwd' => $user->passwd,
  101. 'sni' => $this->is_relay ? $this->server : '',
  102. 'udp' => $this->is_udp,
  103. ]);
  104. break;
  105. case 1:
  106. case 4:
  107. $config = array_merge($config, [
  108. 'type' => $this->compatible ? 'shadowsocks' : 'shadowsocksr',
  109. 'method' => $this->method,
  110. 'protocol' => $this->protocol,
  111. 'obfs' => $this->obfs,
  112. 'obfs_param' => $this->obfs_param,
  113. 'udp' => $this->is_udp,
  114. ]);
  115. if ($this->single) {
  116. //单端口使用中转的端口
  117. $config['port'] = $this->is_relay ? $this->relay_port : $this->port;
  118. $config['passwd'] = $this->passwd;
  119. $config['protocol_param'] = $user->port.':'.$user->passwd;
  120. } else {
  121. $config['port'] = $user->port;
  122. $config['passwd'] = $user->passwd;
  123. $config['protocol_param'] = $this->protocol_param;
  124. if ($this->type === 1) {
  125. $config['method'] = $user->method;
  126. $config['protocol'] = $user->protocol;
  127. $config['obfs'] = $user->obfs;
  128. }
  129. }
  130. break;
  131. }
  132. return $config;
  133. }
  134. public function getSpeedLimitAttribute($value)
  135. {
  136. return $value / Mbps;
  137. }
  138. public function setSpeedLimitAttribute($value)
  139. {
  140. return $this->attributes['speed_limit'] = $value * Mbps;
  141. }
  142. public function getTypeLabelAttribute(): string
  143. {
  144. switch ($this->attributes['type']) {
  145. case 1:
  146. $type_label = 'ShadowsocksR';
  147. break;
  148. case 2:
  149. $type_label = 'V2Ray';
  150. break;
  151. case 3:
  152. $type_label = 'Trojan';
  153. break;
  154. case 4:
  155. $type_label = 'VNet';
  156. break;
  157. default:
  158. $type_label = 'UnKnown';
  159. }
  160. return $type_label;
  161. }
  162. }