reloadNode.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Jobs\VNet;
  3. use GuzzleHttp\Client;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use Log;
  10. class reloadNode implements ShouldQueue {
  11. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  12. private $nodes;
  13. public function __construct($nodes) {
  14. $this->nodes = $nodes;
  15. }
  16. public function handle(): bool {
  17. $allSuccess = true;
  18. foreach($this->nodes as $node){
  19. $ret = $this->send(($node->server?: $node->ip).':'.$node->push_port, $node->auth->secret, [
  20. 'id' => $node->id,
  21. 'port' => (string) $node->port,
  22. 'passwd' => $node->passwd?: '',
  23. 'method' => $node->method,
  24. 'protocol' => $node->protocol,
  25. 'obfs' => $node->obfs,
  26. 'protocol_param' => $node->protocol_param,
  27. 'obfs_param' => $node->obfs_param?: '',
  28. 'push_port' => $node->push_port,
  29. 'single' => $node->single,
  30. 'secret' => $node->auth->secret,
  31. // 'is_udp' => $node->is_udp,
  32. // 'speed_limit' => $node->speed_limit,
  33. // 'client_limit' => $node->client_limit,
  34. // 'redirect_url' => (string) sysConfig('redirect_url')
  35. ]);
  36. if(!$ret){
  37. $allSuccess = false;
  38. }
  39. }
  40. return $allSuccess;
  41. }
  42. public function send($host, $secret, $data): bool {
  43. $client = new Client([
  44. 'base_uri' => $host,
  45. 'timeout' => 15,
  46. 'headers' => ['secret' => $secret]
  47. ]);
  48. $ret = $client->post('api/v2/node/reload', ['json' => $data]);
  49. if($ret->getStatusCode() == 200){
  50. $message = json_decode($ret->getBody(), true);
  51. if(array_key_exists('success', $message) && array_key_exists('content', $message)){
  52. if($message['success']){
  53. return true;
  54. }
  55. Log::error('重载节点失败:'.$host.' 反馈:'.$message['content']);
  56. }
  57. }
  58. Log::error('重载节点失败url: '.$host);
  59. return false;
  60. }
  61. }