Node.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\Relations\HasMany;
  5. use Illuminate\Database\Eloquent\Relations\HasOne;
  6. /**
  7. * 节点配置信息.
  8. */
  9. class Node extends Model
  10. {
  11. protected $table = 'ss_node';
  12. protected $guarded = ['id', 'created_at'];
  13. public function labels(): HasMany
  14. {
  15. return $this->hasMany(NodeLabel::class);
  16. }
  17. public function heartBeats(): HasMany
  18. {
  19. return $this->hasMany(NodeHeartBeat::class);
  20. }
  21. public function onlineLogs(): HasMany
  22. {
  23. return $this->hasMany(NodeOnlineLog::class);
  24. }
  25. public function pingLogs(): HasMany
  26. {
  27. return $this->hasMany(NodePing::class);
  28. }
  29. public function dailyDataFlows(): HasMany
  30. {
  31. return $this->hasMany(NodeDailyDataFlow::class);
  32. }
  33. public function hourlyDataFlows(): HasMany
  34. {
  35. return $this->hasMany(NodeHourlyDataFlow::class);
  36. }
  37. public function rules(): hasMany
  38. {
  39. return $this->hasMany(NodeRule::class);
  40. }
  41. public function ruleGroup(): hasOne
  42. {
  43. return $this->hasOne(RuleGroupNode::class);
  44. }
  45. public function auth(): HasOne
  46. {
  47. return $this->hasOne(NodeAuth::class);
  48. }
  49. public function level_table(): HasOne
  50. {
  51. return $this->hasOne(Level::class, 'level', 'level');
  52. }
  53. public function scopeUserAllowNodes($query, $user_group_id, $user_level)
  54. {
  55. $userGroup = UserGroup::find($user_group_id);
  56. if ($userGroup) {
  57. $query->whereIn('id', $userGroup->nodes);
  58. }
  59. return $query->whereStatus(1)->where('level', '<=', $user_level ?: 0);
  60. }
  61. public function getSpeedLimitAttribute($value)
  62. {
  63. return $value / Mbps;
  64. }
  65. public function setSpeedLimitAttribute($value)
  66. {
  67. return $this->attributes['speed_limit'] = $value * Mbps;
  68. }
  69. public function getNodeAccessUsersAttribute()
  70. {
  71. return User::nodeAllowUsers($this->attributes['id'], $this->attributes['level'])->get();
  72. }
  73. public function getTypeLabelAttribute(): string
  74. {
  75. switch ($this->attributes['type']) {
  76. case 1:
  77. $type_label = 'ShadowsocksR';
  78. break;
  79. case 2:
  80. $type_label = 'V2Ray';
  81. break;
  82. case 3:
  83. $type_label = 'Trojan';
  84. break;
  85. case 4:
  86. $type_label = 'VNet';
  87. break;
  88. default:
  89. $type_label = 'UnKnown';
  90. }
  91. return $type_label;
  92. }
  93. }