Subscribe.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Services;
  4. use App\Models\Link;
  5. use App\Models\Node;
  6. use App\Services\Subscribe\Clash;
  7. use App\Services\Subscribe\Json;
  8. use App\Services\Subscribe\SingBox;
  9. use App\Services\Subscribe\SIP002;
  10. use App\Services\Subscribe\SIP008;
  11. use App\Services\Subscribe\SS;
  12. use App\Services\Subscribe\Trojan;
  13. use App\Services\Subscribe\V2Ray;
  14. use App\Services\Subscribe\V2RayJson;
  15. use App\Utils\Tools;
  16. use Illuminate\Support\Collection;
  17. final class Subscribe
  18. {
  19. public static function getUniversalSubLink($user): string
  20. {
  21. $userid = $user->id;
  22. $token = (new Link())->where('userid', $userid)->first();
  23. if ($token === null) {
  24. $token = new Link();
  25. $token->userid = $userid;
  26. $token->token = Tools::genSubToken();
  27. $token->save();
  28. }
  29. return $_ENV['subUrl'] . '/sub/' . $token->token;
  30. }
  31. /**
  32. * @param $user
  33. *
  34. * @return Collection
  35. */
  36. public static function getUserSubNodes($user): Collection
  37. {
  38. $query = Node::query();
  39. $query->where('type', 1)->where('node_class', '<=', $user->class);
  40. if (! $user->is_admin) {
  41. $group = ($user->node_group !== 0 ? [0, $user->node_group] : [0]);
  42. $query->whereIn('node_group', $group);
  43. }
  44. return $query->where(static function ($query): void {
  45. $query->where('node_bandwidth_limit', '=', 0)->orWhereRaw('node_bandwidth < node_bandwidth_limit');
  46. })->orderBy('node_class')
  47. ->orderBy('name')
  48. ->get();
  49. }
  50. public static function getContent($user, $type): string
  51. {
  52. return self::getClient($type)->getContent($user);
  53. }
  54. public static function getClient($type): Json|SS|SIP002|V2Ray|Trojan|Clash|SIP008|SingBox|V2RayJson
  55. {
  56. return match ($type) {
  57. 'ss' => new SS(),
  58. 'sip002' => new SIP002(),
  59. 'v2ray' => new V2Ray(),
  60. 'trojan' => new Trojan(),
  61. 'clash' => new Clash(),
  62. 'sip008' => new SIP008(),
  63. 'singbox' => new SingBox(),
  64. 'v2rayjson' => new V2RayJson(),
  65. default => new Json(),
  66. };
  67. }
  68. }