Node.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. * @property int $id
  13. * @property int $type 服务类型:1-Shadowsocks(R)、2-V2ray、3-Trojan、4-VNet
  14. * @property string $name 名称
  15. * @property string $country_code 国家代码
  16. * @property string|null $server 服务器域名地址
  17. * @property string|null $ip 服务器IPV4地址
  18. * @property string|null $ipv6 服务器IPV6地址
  19. * @property int $level 等级:0-无等级,全部可见
  20. * @property int|null $rule_group_id 从属规则分组ID
  21. * @property int $speed_limit 节点限速,为0表示不限速,单位Byte
  22. * @property int $client_limit 设备数限制
  23. * @property string|null $relay_server 中转地址
  24. * @property int|null $relay_port 中转端口
  25. * @property string|null $description 节点简单描述
  26. * @property string|null $geo 节点地理位置
  27. * @property string $method 加密方式
  28. * @property string $protocol 协议
  29. * @property string|null $protocol_param 协议参数
  30. * @property string $obfs 混淆
  31. * @property string|null $obfs_param 混淆参数
  32. * @property float $traffic_rate 流量比率
  33. * @property int $is_subscribe 是否允许用户订阅该节点:0-否、1-是
  34. * @property int $is_ddns 是否使用DDNS:0-否、1-是
  35. * @property int $is_relay 是否中转节点:0-否、1-是
  36. * @property int $is_udp 是否启用UDP:0-不启用、1-启用
  37. * @property int $push_port 消息推送端口
  38. * @property int $detection_type 节点检测: 0-关闭、1-只检测TCP、2-只检测ICMP、3-检测全部
  39. * @property int $compatible 兼容SS
  40. * @property int $single 启用单端口功能:0-否、1-是
  41. * @property int|null $port 单端口的端口号或连接端口号
  42. * @property string|null $passwd 单端口的连接密码
  43. * @property int $sort 排序值,值越大越靠前显示
  44. * @property int $status 状态:0-维护、1-正常
  45. * @property int $v2_alter_id V2Ray额外ID
  46. * @property int $v2_port V2Ray服务端口
  47. * @property string $v2_method V2Ray加密方式
  48. * @property string $v2_net V2Ray传输协议
  49. * @property string $v2_type V2Ray伪装类型
  50. * @property string|null $v2_host V2Ray伪装的域名
  51. * @property string|null $v2_path V2Ray的WS/H2路径
  52. * @property int $v2_tls V2Ray连接TLS:0-未开启、1-开启
  53. * @property string|null $tls_provider V2Ray节点的TLS提供商授权信息
  54. * @property \Illuminate\Support\Carbon $created_at 创建时间
  55. * @property \Illuminate\Support\Carbon $updated_at 最后更新时间
  56. * @property string|null $v2_sni
  57. * @property int|null $vless
  58. * @property string|null $vless_privateKey 密钥key
  59. * @property string|null $vless_pulkey 公钥
  60. * @property string|null $shortIds
  61. * @property-read \App\Models\NodeAuth|null $auth
  62. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\NodeDailyDataFlow[] $dailyDataFlows
  63. * @property-read int|null $daily_data_flows_count
  64. * @property-read string $type_label
  65. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\NodeHeartbeat[] $heartbeats
  66. * @property-read int|null $heartbeats_count
  67. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\NodeHourlyDataFlow[] $hourlyDataFlows
  68. * @property-read int|null $hourly_data_flows_count
  69. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Label[] $labels
  70. * @property-read int|null $labels_count
  71. * @property-read \App\Models\Level|null $level_table
  72. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\NodeOnlineIp[] $onlineIps
  73. * @property-read int|null $online_ips_count
  74. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\NodeOnlineLog[] $onlineLogs
  75. * @property-read int|null $online_logs_count
  76. * @property-read \App\Models\RuleGroup|null $ruleGroup
  77. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\RuleLog[] $ruleLogs
  78. * @property-read int|null $rule_logs_count
  79. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserDataFlowLog[] $userDataFlowLogs
  80. * @property-read int|null $user_data_flow_logs_count
  81. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserGroup[] $userGroups
  82. * @property-read int|null $user_groups_count
  83. * @method static \Illuminate\Database\Eloquent\Builder|Node newModelQuery()
  84. * @method static \Illuminate\Database\Eloquent\Builder|Node newQuery()
  85. * @method static \Illuminate\Database\Eloquent\Builder|Node query()
  86. * @method static \Illuminate\Database\Eloquent\Builder|Node whereClientLimit($value)
  87. * @method static \Illuminate\Database\Eloquent\Builder|Node whereCompatible($value)
  88. * @method static \Illuminate\Database\Eloquent\Builder|Node whereCountryCode($value)
  89. * @method static \Illuminate\Database\Eloquent\Builder|Node whereCreatedAt($value)
  90. * @method static \Illuminate\Database\Eloquent\Builder|Node whereDescription($value)
  91. * @method static \Illuminate\Database\Eloquent\Builder|Node whereDetectionType($value)
  92. * @method static \Illuminate\Database\Eloquent\Builder|Node whereGeo($value)
  93. * @method static \Illuminate\Database\Eloquent\Builder|Node whereId($value)
  94. * @method static \Illuminate\Database\Eloquent\Builder|Node whereIp($value)
  95. * @method static \Illuminate\Database\Eloquent\Builder|Node whereIpv6($value)
  96. * @method static \Illuminate\Database\Eloquent\Builder|Node whereIsDdns($value)
  97. * @method static \Illuminate\Database\Eloquent\Builder|Node whereIsRelay($value)
  98. * @method static \Illuminate\Database\Eloquent\Builder|Node whereIsSubscribe($value)
  99. * @method static \Illuminate\Database\Eloquent\Builder|Node whereIsUdp($value)
  100. * @method static \Illuminate\Database\Eloquent\Builder|Node whereLevel($value)
  101. * @method static \Illuminate\Database\Eloquent\Builder|Node whereMethod($value)
  102. * @method static \Illuminate\Database\Eloquent\Builder|Node whereName($value)
  103. * @method static \Illuminate\Database\Eloquent\Builder|Node whereObfs($value)
  104. * @method static \Illuminate\Database\Eloquent\Builder|Node whereObfsParam($value)
  105. * @method static \Illuminate\Database\Eloquent\Builder|Node wherePasswd($value)
  106. * @method static \Illuminate\Database\Eloquent\Builder|Node wherePort($value)
  107. * @method static \Illuminate\Database\Eloquent\Builder|Node whereProtocol($value)
  108. * @method static \Illuminate\Database\Eloquent\Builder|Node whereProtocolParam($value)
  109. * @method static \Illuminate\Database\Eloquent\Builder|Node wherePushPort($value)
  110. * @method static \Illuminate\Database\Eloquent\Builder|Node whereRelayPort($value)
  111. * @method static \Illuminate\Database\Eloquent\Builder|Node whereRelayServer($value)
  112. * @method static \Illuminate\Database\Eloquent\Builder|Node whereRuleGroupId($value)
  113. * @method static \Illuminate\Database\Eloquent\Builder|Node whereServer($value)
  114. * @method static \Illuminate\Database\Eloquent\Builder|Node whereShortIds($value)
  115. * @method static \Illuminate\Database\Eloquent\Builder|Node whereSingle($value)
  116. * @method static \Illuminate\Database\Eloquent\Builder|Node whereSort($value)
  117. * @method static \Illuminate\Database\Eloquent\Builder|Node whereSpeedLimit($value)
  118. * @method static \Illuminate\Database\Eloquent\Builder|Node whereStatus($value)
  119. * @method static \Illuminate\Database\Eloquent\Builder|Node whereTlsProvider($value)
  120. * @method static \Illuminate\Database\Eloquent\Builder|Node whereTrafficRate($value)
  121. * @method static \Illuminate\Database\Eloquent\Builder|Node whereType($value)
  122. * @method static \Illuminate\Database\Eloquent\Builder|Node whereUpdatedAt($value)
  123. * @method static \Illuminate\Database\Eloquent\Builder|Node whereV2AlterId($value)
  124. * @method static \Illuminate\Database\Eloquent\Builder|Node whereV2Host($value)
  125. * @method static \Illuminate\Database\Eloquent\Builder|Node whereV2Method($value)
  126. * @method static \Illuminate\Database\Eloquent\Builder|Node whereV2Net($value)
  127. * @method static \Illuminate\Database\Eloquent\Builder|Node whereV2Path($value)
  128. * @method static \Illuminate\Database\Eloquent\Builder|Node whereV2Port($value)
  129. * @method static \Illuminate\Database\Eloquent\Builder|Node whereV2Sni($value)
  130. * @method static \Illuminate\Database\Eloquent\Builder|Node whereV2Tls($value)
  131. * @method static \Illuminate\Database\Eloquent\Builder|Node whereV2Type($value)
  132. * @method static \Illuminate\Database\Eloquent\Builder|Node whereVless($value)
  133. * @method static \Illuminate\Database\Eloquent\Builder|Node whereVlessPrivateKey($value)
  134. * @method static \Illuminate\Database\Eloquent\Builder|Node whereVlessPulkey($value)
  135. * @mixin \Eloquent
  136. */
  137. class Node extends Model
  138. {
  139. protected $table = 'node';
  140. protected $guarded = [];
  141. public function labels()
  142. {
  143. return $this->belongsToMany(Label::class);
  144. }
  145. public function heartbeats(): HasMany
  146. {
  147. return $this->hasMany(NodeHeartbeat::class);
  148. }
  149. public function onlineIps(): HasMany
  150. {
  151. return $this->hasMany(NodeOnlineIp::class);
  152. }
  153. public function onlineLogs(): HasMany
  154. {
  155. return $this->hasMany(NodeOnlineLog::class);
  156. }
  157. public function userDataFlowLogs(): HasMany
  158. {
  159. return $this->hasMany(UserDataFlowLog::class);
  160. }
  161. public function ruleLogs(): HasMany
  162. {
  163. return $this->hasMany(RuleLog::class);
  164. }
  165. public function dailyDataFlows(): HasMany
  166. {
  167. return $this->hasMany(NodeDailyDataFlow::class);
  168. }
  169. public function hourlyDataFlows(): HasMany
  170. {
  171. return $this->hasMany(NodeHourlyDataFlow::class);
  172. }
  173. public function ruleGroup(): BelongsTo
  174. {
  175. return $this->belongsTo(RuleGroup::class);
  176. }
  177. public function userGroups(): BelongsToMany
  178. {
  179. return $this->belongsToMany(UserGroup::class);
  180. }
  181. public function auth(): HasOne
  182. {
  183. return $this->hasOne(NodeAuth::class);
  184. }
  185. public function level_table(): HasOne
  186. {
  187. return $this->hasOne(Level::class, 'level', 'level');
  188. }
  189. public function users()
  190. {
  191. return User::activeUser()
  192. ->where('level', '>=', $this->attributes['level'])
  193. ->whereNull('user_group_id')
  194. ->orwhereIn('user_group_id', $this->userGroups->pluck('id')->toArray())
  195. ->get();
  196. }
  197. public function refresh_geo()
  198. {
  199. $data = IP::IPSB($this->is_ddns ? gethostbyname($this->server) : $this->ip);
  200. if ($data) {
  201. self::withoutEvents(function () use ($data) {
  202. $this->update(['geo' => $data['latitude'].','.$data['longitude']]);
  203. });
  204. return 1;
  205. }
  206. return 0;
  207. }
  208. public function config(User $user)
  209. {
  210. $config = [
  211. 'id' => $this->id,
  212. 'name' => $this->name,
  213. 'host' => $this->is_relay ? $this->relay_server : ($this->server ?: $this->ip),
  214. 'group' => sysConfig('website_name'),
  215. ];
  216. switch ($this->type) {
  217. case 0:
  218. $config = array_merge($config, [
  219. 'type' => 'shadowsocks',
  220. 'method' => $this->method,
  221. 'udp' => $this->is_udp,
  222. 'port' => $this->is_relay ? $this->relay_port : $this->port,
  223. 'passwd' => $user->passwd
  224. ]);
  225. break;
  226. case 2:
  227. $config = array_merge($config, [
  228. 'type' => 'v2ray',
  229. 'port' => $this->is_relay ? $this->relay_port : $this->port,
  230. 'uuid' => $user->vmess_id,
  231. 'method' => $this->v2_method,
  232. 'v2_alter_id' => $this->v2_alter_id,
  233. 'v2_net' => $this->v2_net,
  234. 'v2_type' => $this->vless === 1 ? 'vless' : $this->v2_type,
  235. 'v2_host' => $this->v2_host == null ? '':$this->v2_host,
  236. 'v2_path' => $this->v2_path == null ? '':$this->v2_path,
  237. 'v2_tls' => $this->v2_tls ? 'tls' : '',
  238. 'v2_sni' => $this->v2_sni == null ? '':$this->v2_sni,
  239. 'udp' => $this->is_udp,
  240. 'vless' => $this->vless,
  241. 'vless_pulkey'=> $this->vless_pulkey,
  242. ]);
  243. break;
  244. case 3:
  245. $config = array_merge($config, [
  246. 'type' => 'trojan',
  247. 'port' => $this->is_relay ? $this->relay_port : 443,
  248. 'passwd' => $user->passwd,
  249. 'sni' => $this->is_relay ? $this->server : '',
  250. 'udp' => $this->is_udp,
  251. ]);
  252. break;
  253. case 1:
  254. case 4:
  255. $config = array_merge($config, [
  256. 'type' => $this->compatible ? 'shadowsocks' : 'shadowsocksr',
  257. 'method' => $this->method,
  258. 'protocol' => $this->protocol,
  259. 'obfs' => $this->obfs,
  260. 'obfs_param' => $this->obfs_param,
  261. 'udp' => $this->is_udp,
  262. ]);
  263. if ($this->single) {
  264. //单端口使用中转的端口
  265. $config['port'] = $this->is_relay ? $this->relay_port : $this->port;
  266. $config['passwd'] = $this->passwd;
  267. $config['protocol_param'] = $user->port.':'.$user->passwd;
  268. } else {
  269. $config['port'] = $user->port;
  270. $config['passwd'] = $user->passwd;
  271. $config['protocol_param'] = $this->protocol_param;
  272. if ($this->type === 1) {
  273. $config['method'] = $user->method;
  274. $config['protocol'] = $user->protocol;
  275. $config['obfs'] = $user->obfs;
  276. }
  277. }
  278. break;
  279. }
  280. return $config;
  281. }
  282. public function getSpeedLimitAttribute($value)
  283. {
  284. return $value / Mbps;
  285. }
  286. public function setSpeedLimitAttribute($value)
  287. {
  288. return $this->attributes['speed_limit'] = $value * Mbps;
  289. }
  290. public function getTypeLabelAttribute(): string
  291. {
  292. switch ($this->attributes['type']) {
  293. case 0:
  294. $type_label = 'shadowsocks';
  295. break;
  296. case 1:
  297. $type_label = 'ShadowsocksR';
  298. break;
  299. case 2:
  300. $type_label = 'V2Ray';
  301. break;
  302. case 3:
  303. $type_label = 'Trojan';
  304. break;
  305. case 4:
  306. $type_label = 'VNet';
  307. break;
  308. default:
  309. $type_label = 'UnKnown';
  310. }
  311. return $type_label;
  312. }
  313. }