ServerService.php 8.5 KB

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