Node.php 6.4 KB

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