ServerService.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace App\Services;
  3. use App\Models\ServerLog;
  4. use App\Models\ServerShadowsocks;
  5. use App\Models\User;
  6. use App\Models\ServerV2ray;
  7. use App\Models\ServerTrojan;
  8. use App\Utils\CacheKey;
  9. use App\Utils\Helper;
  10. use Illuminate\Support\Facades\Cache;
  11. class ServerService
  12. {
  13. public function getV2ray(User $user, $all = false):array
  14. {
  15. $servers = [];
  16. $model = ServerV2ray::orderBy('sort', 'ASC');
  17. if (!$all) {
  18. $model->where('show', 1);
  19. }
  20. $v2ray = $model->get();
  21. for ($i = 0; $i < count($v2ray); $i++) {
  22. $v2ray[$i]['type'] = 'v2ray';
  23. $groupId = $v2ray[$i]['group_id'];
  24. if (!in_array($user->group_id, $groupId)) continue;
  25. if (strpos($v2ray[$i]['port'], '-') !== false) {
  26. $v2ray[$i]['port'] = Helper::randomPort($v2ray[$i]['port']);
  27. }
  28. if ($v2ray[$i]['parent_id']) {
  29. $v2ray[$i]['last_check_at'] = Cache::get(CacheKey::get('SERVER_V2RAY_LAST_CHECK_AT', $v2ray[$i]['parent_id']));
  30. } else {
  31. $v2ray[$i]['last_check_at'] = Cache::get(CacheKey::get('SERVER_V2RAY_LAST_CHECK_AT', $v2ray[$i]['id']));
  32. }
  33. array_push($servers, $v2ray[$i]->toArray());
  34. }
  35. return $servers;
  36. }
  37. public function getTrojan(User $user, $all = false):array
  38. {
  39. $servers = [];
  40. $model = ServerTrojan::orderBy('sort', 'ASC');
  41. if (!$all) {
  42. $model->where('show', 1);
  43. }
  44. $trojan = $model->get();
  45. for ($i = 0; $i < count($trojan); $i++) {
  46. $trojan[$i]['type'] = 'trojan';
  47. $groupId = $trojan[$i]['group_id'];
  48. if (!in_array($user->group_id, $groupId)) continue;
  49. if (strpos($trojan[$i]['port'], '-') !== false) {
  50. $trojan[$i]['port'] = Helper::randomPort($trojan[$i]['port']);
  51. }
  52. if ($trojan[$i]['parent_id']) {
  53. $trojan[$i]['last_check_at'] = Cache::get(CacheKey::get('SERVER_TROJAN_LAST_CHECK_AT', $trojan[$i]['parent_id']));
  54. } else {
  55. $trojan[$i]['last_check_at'] = Cache::get(CacheKey::get('SERVER_TROJAN_LAST_CHECK_AT', $trojan[$i]['id']));
  56. }
  57. array_push($servers, $trojan[$i]->toArray());
  58. }
  59. return $servers;
  60. }
  61. public function getShadowsocks(User $user, $all = false)
  62. {
  63. $servers = [];
  64. $model = ServerShadowsocks::orderBy('sort', 'ASC');
  65. if (!$all) {
  66. $model->where('show', 1);
  67. }
  68. $shadowsocks = $model->get();
  69. for ($i = 0; $i < count($shadowsocks); $i++) {
  70. $shadowsocks[$i]['type'] = 'shadowsocks';
  71. $groupId = $shadowsocks[$i]['group_id'];
  72. if (!in_array($user->group_id, $groupId)) continue;
  73. if (strpos($shadowsocks[$i]['port'], '-') !== false) {
  74. $shadowsocks[$i]['port'] = Helper::randomPort($shadowsocks[$i]['port']);
  75. }
  76. if ($shadowsocks[$i]['parent_id']) {
  77. $shadowsocks[$i]['last_check_at'] = Cache::get(CacheKey::get('SERVER_SHADOWSOCKS_LAST_CHECK_AT', $shadowsocks[$i]['parent_id']));
  78. } else {
  79. $shadowsocks[$i]['last_check_at'] = Cache::get(CacheKey::get('SERVER_SHADOWSOCKS_LAST_CHECK_AT', $shadowsocks[$i]['id']));
  80. }
  81. array_push($servers, $shadowsocks[$i]->toArray());
  82. }
  83. return $servers;
  84. }
  85. public function getAvailableServers(User $user, $all = false)
  86. {
  87. $servers = array_merge(
  88. $this->getShadowsocks($user, $all),
  89. $this->getV2ray($user, $all),
  90. $this->getTrojan($user, $all)
  91. );
  92. $tmp = array_column($servers, 'sort');
  93. array_multisort($tmp, SORT_ASC, $servers);
  94. return $servers;
  95. }
  96. public function getAvailableUsers($groupId)
  97. {
  98. return User::whereIn('group_id', $groupId)
  99. ->whereRaw('u + d < transfer_enable')
  100. ->where(function ($query) {
  101. $query->where('expired_at', '>=', time())
  102. ->orWhere('expired_at', NULL);
  103. })
  104. ->where('banned', 0)
  105. ->select([
  106. 'id',
  107. 'uuid'
  108. ])
  109. ->get();
  110. }
  111. public function log(int $userId, int $serverId, int $u, int $d, float $rate, string $method)
  112. {
  113. if (($u + $d) < 10240) return true;
  114. $timestamp = strtotime(date('Y-m-d'));
  115. $serverLog = ServerLog::where('log_at', '>=', $timestamp)
  116. ->where('log_at', '<', $timestamp + 3600)
  117. ->where('server_id', $serverId)
  118. ->where('user_id', $userId)
  119. ->where('rate', $rate)
  120. ->where('method', $method)
  121. ->first();
  122. if ($serverLog) {
  123. try {
  124. $serverLog->increment('u', $u);
  125. $serverLog->increment('d', $d);
  126. return true;
  127. } catch (\Exception $e) {
  128. return false;
  129. }
  130. } else {
  131. $serverLog = new ServerLog();
  132. $serverLog->user_id = $userId;
  133. $serverLog->server_id = $serverId;
  134. $serverLog->u = $u;
  135. $serverLog->d = $d;
  136. $serverLog->rate = $rate;
  137. $serverLog->log_at = $timestamp;
  138. $serverLog->method = $method;
  139. return $serverLog->save();
  140. }
  141. }
  142. public function getShadowsocksServers()
  143. {
  144. $server = ServerShadowsocks::orderBy('sort', 'ASC')->get();
  145. for ($i = 0; $i < count($server); $i++) {
  146. $server[$i]['type'] = 'shadowsocks';
  147. }
  148. return $server->toArray();
  149. }
  150. public function getV2rayServers()
  151. {
  152. $server = ServerV2ray::orderBy('sort', 'ASC')->get();
  153. for ($i = 0; $i < count($server); $i++) {
  154. $server[$i]['type'] = 'v2ray';
  155. }
  156. return $server->toArray();
  157. }
  158. public function getTrojanServers()
  159. {
  160. $server = ServerTrojan::orderBy('sort', 'ASC')->get();
  161. for ($i = 0; $i < count($server); $i++) {
  162. $server[$i]['type'] = 'trojan';
  163. }
  164. return $server->toArray();
  165. }
  166. public function mergeData(&$servers)
  167. {
  168. foreach ($servers as $k => $v) {
  169. $serverType = strtoupper($servers[$k]['type']);
  170. $servers[$k]['online'] = Cache::get(CacheKey::get("SERVER_{$serverType}_ONLINE_USER", $servers[$k]['parent_id'] ? $servers[$k]['parent_id'] : $servers[$k]['id']));
  171. if ($servers[$k]['parent_id']) {
  172. $servers[$k]['last_check_at'] = Cache::get(CacheKey::get("SERVER_{$serverType}_LAST_CHECK_AT", $servers[$k]['parent_id']));
  173. $servers[$k]['last_push_at'] = Cache::get(CacheKey::get("SERVER_{$serverType}_LAST_PUSH_AT", $servers[$k]['parent_id']));
  174. } else {
  175. $servers[$k]['last_check_at'] = Cache::get(CacheKey::get("SERVER_{$serverType}_LAST_CHECK_AT", $servers[$k]['id']));
  176. $servers[$k]['last_push_at'] = Cache::get(CacheKey::get("SERVER_{$serverType}_LAST_PUSH_AT", $servers[$k]['id']));
  177. }
  178. if ((time() - 300) >= $servers[$k]['last_check_at']) {
  179. $servers[$k]['available_status'] = 0;
  180. } else if ((time() - 300) >= $servers[$k]['last_push_at']) {
  181. $servers[$k]['available_status'] = 1;
  182. } else {
  183. $servers[$k]['available_status'] = 2;
  184. }
  185. }
  186. }
  187. public function getAllServers()
  188. {
  189. $servers = array_merge(
  190. $this->getShadowsocksServers(),
  191. $this->getV2rayServers(),
  192. $this->getTrojanServers()
  193. );
  194. $this->mergeData($servers);
  195. $tmp = array_column($servers, 'sort');
  196. array_multisort($tmp, SORT_ASC, $servers);
  197. return $servers;
  198. }
  199. }