Node.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace App\Models;
  3. use App\Components\IP;
  4. use Arr;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  8. use Illuminate\Database\Eloquent\Relations\HasMany;
  9. use Illuminate\Database\Eloquent\Relations\HasOne;
  10. use Log;
  11. /**
  12. * 节点配置信息.
  13. */
  14. class Node extends Model
  15. {
  16. protected $table = 'node';
  17. protected $guarded = [];
  18. protected $casts = [
  19. 'profile' => 'array',
  20. ];
  21. public function labels()
  22. {
  23. return $this->belongsToMany(Label::class);
  24. }
  25. public function heartbeats(): HasMany
  26. {
  27. return $this->hasMany(NodeHeartbeat::class);
  28. }
  29. public function onlineIps(): HasMany
  30. {
  31. return $this->hasMany(NodeOnlineIp::class);
  32. }
  33. public function onlineLogs(): HasMany
  34. {
  35. return $this->hasMany(NodeOnlineLog::class);
  36. }
  37. public function userDataFlowLogs(): HasMany
  38. {
  39. return $this->hasMany(UserDataFlowLog::class);
  40. }
  41. public function ruleLogs(): HasMany
  42. {
  43. return $this->hasMany(RuleLog::class);
  44. }
  45. public function dailyDataFlows(): HasMany
  46. {
  47. return $this->hasMany(NodeDailyDataFlow::class);
  48. }
  49. public function hourlyDataFlows(): HasMany
  50. {
  51. return $this->hasMany(NodeHourlyDataFlow::class);
  52. }
  53. public function ruleGroup(): BelongsTo
  54. {
  55. return $this->belongsTo(RuleGroup::class);
  56. }
  57. public function relayNode(): BelongsTo
  58. {
  59. return $this->belongsTo(Node::class);
  60. }
  61. public function childNodes(): hasMany
  62. {
  63. return $this->hasMany(Node::class, 'relay_node_id', 'id');
  64. }
  65. public function userGroups(): BelongsToMany
  66. {
  67. return $this->belongsToMany(UserGroup::class);
  68. }
  69. public function auth(): HasOne
  70. {
  71. return $this->hasOne(NodeAuth::class);
  72. }
  73. public function level_table(): HasOne
  74. {
  75. return $this->hasOne(Level::class, 'level', 'level');
  76. }
  77. public function users()
  78. {
  79. return User::activeUser()
  80. ->where('level', '>=', $this->attributes['level'])
  81. ->where(function ($query) {
  82. $query->whereIn('user_group_id', $this->userGroups->pluck('id'))->orWhereNull('user_group_id');
  83. })
  84. ->get();
  85. }
  86. public function refresh_geo(): bool
  87. {
  88. $ip = $this->ips();
  89. if ($ip !== []) {
  90. $data = IP::IPSB($ip[0]); // 复数IP都以第一个为准
  91. if ($data) {
  92. self::withoutEvents(function () use ($data) {
  93. $this->update(['geo' => ($data['latitude'] ?? null).','.($data['longitude'] ?? null)]);
  94. });
  95. return true;
  96. }
  97. }
  98. return false;
  99. }
  100. public function ips(int $type = 4): array
  101. {
  102. // 使用DDNS的node先通过gethostbyname获取ip地址
  103. if ($this->attributes['is_ddns']) { // When ddns is enable, only domain can be used to check the ip
  104. $ip = gethostbyname($this->attributes['server']);
  105. if (strcmp($ip, $this->attributes['server']) === 0) {
  106. Log::warning('获取 【'.$this->attributes['server'].'】 IP失败'.$ip);
  107. $ip = '';
  108. }
  109. } else {
  110. $ip = $type === 4 ? $this->attributes['ip'] : $this->attributes['ipv6']; // check the multiple existing of ip
  111. }
  112. return array_map('trim', explode(',', $ip));
  113. }
  114. public function getConfig(User $user)
  115. {
  116. $config = [
  117. 'id' => $this->id,
  118. 'name' => $this->name,
  119. 'host' => $this->host,
  120. 'group' => sysConfig('website_name'),
  121. 'udp' => $this->is_udp,
  122. ];
  123. if ($this->relay_node_id) {
  124. $parentConfig = $this->relayNode->getConfig($user);
  125. $config = array_merge($config, Arr::except($parentConfig, ['id', 'name', 'host', 'group', 'udp']));
  126. if ($parentConfig['type'] === 'trojan') {
  127. $config['sni'] = $parentConfig['host'];
  128. }
  129. $config['port'] = $this->port;
  130. } else {
  131. switch ($this->type) {
  132. case 0:
  133. $config = array_merge($config, [
  134. 'type' => 'shadowsocks',
  135. 'passwd' => $user->passwd,
  136. ], $this->profile);
  137. if ($this->port) {
  138. $config['port'] = $this->port;
  139. } else {
  140. $config['port'] = $user->port;
  141. }
  142. break;
  143. case 2:
  144. $config = array_merge($config, [
  145. 'type' => 'v2ray',
  146. 'port' => $this->port,
  147. 'uuid' => $user->vmess_id,
  148. ], $this->profile);
  149. break;
  150. case 3:
  151. $config = array_merge($config, [
  152. 'type' => 'trojan',
  153. 'port' => $this->port,
  154. 'passwd' => $user->passwd,
  155. 'sni' => '',
  156. ], $this->profile);
  157. break;
  158. case 1:
  159. case 4:
  160. $config = array_merge($config, [
  161. 'type' => 'shadowsocksr',
  162. ], $this->profile);
  163. if ($this->profile['passwd'] && $this->port) {
  164. //单端口使用中转的端口
  165. $config['port'] = $this->port;
  166. $config['protocol_param'] = $user->port.':'.$user->passwd;
  167. } else {
  168. $config['port'] = $user->port;
  169. $config['passwd'] = $user->passwd;
  170. if ($this->type === 1) {
  171. $config['method'] = $user->method;
  172. $config['protocol'] = $user->protocol;
  173. $config['obfs'] = $user->obfs;
  174. }
  175. }
  176. break;
  177. }
  178. }
  179. return $config;
  180. }
  181. public function getSpeedLimitAttribute($value)
  182. {
  183. return $value / Mbps;
  184. }
  185. public function setSpeedLimitAttribute($value)
  186. {
  187. return $this->attributes['speed_limit'] = $value * Mbps;
  188. }
  189. public function getTypeLabelAttribute(): string
  190. {
  191. return [
  192. 0 => 'Shadowsocks',
  193. 1 => 'ShadowsocksR',
  194. 2 => 'V2Ray',
  195. 3 => 'Trojan',
  196. 4 => 'VNet',
  197. ][$this->attributes['type']] ?? 'UnKnown';
  198. }
  199. public function getHostAttribute(): string
  200. {
  201. return $this->server ?? $this->ip ?? $this->ipv6;
  202. }
  203. }