DelUser.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Jobs\Hysteria2;
  3. use App\Models\Node;
  4. use Exception;
  5. use Http;
  6. use Illuminate\Bus\Queueable;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. use Illuminate\Database\Eloquent\Collection;
  9. use Illuminate\Foundation\Bus\Dispatchable;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use Illuminate\Queue\SerializesModels;
  12. use Log;
  13. use Throwable;
  14. class DelUser implements ShouldQueue
  15. {
  16. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  17. public function __construct(private array|int $uids, private Collection|Node $nodes)
  18. {
  19. if (! $nodes instanceof Collection) {
  20. $this->nodes = new Collection([$nodes]);
  21. }
  22. if (! is_array($this->uids)) {
  23. $this->uids = [$this->uids];
  24. }
  25. }
  26. public function handle(): void
  27. {
  28. foreach ($this->nodes as $node) {
  29. if ($node->is_ddns) {
  30. $this->send($node->server.':'.$node->push_port, $node->auth->secret, $this->uids);
  31. } else { // 多IP支持
  32. foreach ($node->ips() as $ip) {
  33. $this->send($ip.':'.$node->push_port, $node->auth->secret, $this->uids);
  34. }
  35. }
  36. }
  37. }
  38. private function send(string $host, string $secret, array $uids): void
  39. {
  40. try {
  41. $request = Http::baseUrl($host)->timeout(15)->withHeader('Authorization', $secret)->asJson();
  42. $response = $request->post('/kick', $uids);
  43. if (! $response->ok()) {
  44. Log::alert("【删除用户】推送失败(推送地址:{$host})", $uids);
  45. } else {
  46. Log::info("【删除用户】推送成功(推送地址:{$host})", ['uids' => $uids, 'response' => $response->json()]);
  47. }
  48. } catch (Exception $exception) {
  49. Log::alert('【删除用户】推送异常:'.$exception->getMessage(), ['uids' => $uids]);
  50. }
  51. }
  52. // 队列失败处理
  53. public function failed(Throwable $exception): void
  54. {
  55. Log::alert('【删除用户】推送异常:'.$exception->getMessage());
  56. }
  57. }