Server.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: chendeben
  5. * Date: 2018/8/31
  6. * Time: 下午9:35
  7. */
  8. namespace PhpLife\App\Script\Controller;
  9. use Cloudflare\Api;
  10. use Cloudflare\Zone\Dns;
  11. use PhpLife\App\Script\Controller;
  12. use PhpLife\App\Api\Package\Database\Server as ServerTable;
  13. use PhpLife\Library\Curl;
  14. class Server extends Controller
  15. {
  16. /**
  17. * 执行逻辑
  18. */
  19. protected function main()
  20. {
  21. $recode_data = [];
  22. $i = 0;
  23. $key = "093fcd440ed453c2689c42150bdb7d5dbc4fb";
  24. $zone_identifier = "89a0fe0d09762c557fd0c7799efb1c9a";
  25. $client = new Api('chendeben@qq.com', $key);
  26. $dns = new Dns($client);
  27. // $id=$dns->create($zone_identifier, "A", "test.97admin.com", "127.0.0.1");
  28. // $id=$dns->update($zone_identifier, "23f0bb6a9eede8750153b1983af5dacc","A","test.97admin.com","127.0.0.2");
  29. // print_r($id);
  30. // $dns->update($zone_identifier, $identifier)
  31. // die();
  32. while (true) {
  33. $data = ServerTable::select([],[],"",null,0,['sort'=>false]);
  34. $str = [];
  35. $ips = [];
  36. foreach ($data as $val) {
  37. $ip = $val['ip'];
  38. $port = $val['port'];
  39. $status = $this->check($ip, $port);
  40. if ($status == 1) {
  41. $ips[] = date("Y-m-d H:i:s")."\t\t【" . $val['domain'] . "】" . $ip . "可用";
  42. $str[] = date("Y-m-d H:i:s") . "\t\t" . $ip . "\t可用\r\n";
  43. }
  44. // if ($val['status'] != $status) {
  45. ServerTable::update(['status' => $status, "last_check" => date("Y-m-d H:i:s")], ['id' => $val['id']]);
  46. if ($status == 1) {
  47. if (!$recode_data[$val['domain']]) {
  48. $recode = $dns->list_records($zone_identifier, null, $val['domain']);
  49. $recode = json_decode(json_encode($recode), true);
  50. if ($recode['result']) {
  51. $result = $recode['result'][0];
  52. $recode_data[$val['domain']] = $result;
  53. }
  54. }
  55. $result = $recode_data[$val['domain']];
  56. // print_r($result);
  57. // die();
  58. if (!$result) {
  59. $str[] = date("Y-m-d H:i:s") . "\t\t" . "域名记录【" . $val['domain'] . "】不存在,正在建立\r\n";
  60. $recode = $dns->create($zone_identifier, "A", $val['domain'], $val['ip']);
  61. $recode = json_decode(json_encode($recode), true);
  62. if ($recode['result']) {
  63. $result = $recode['result'][0];
  64. $recode_data[$val['domain']] = $result;
  65. $str[] = date("Y-m-d H:i:s") . "\t\t" . "域名记录【" . $val['domain'] . "】新建成功\r\n";
  66. }
  67. }
  68. if ($result['content'] != $val['ip']) {
  69. $str[] = date("Y-m-d H:i:s") . "\t\t" . "域名记录【" . $val['domain'] . "】与数据库不一致,正在更新\r\n";
  70. $dns->update($zone_identifier, $result['id'], "A", $val['domain'], $val['ip']);
  71. }
  72. }
  73. // }
  74. }
  75. $str[] = "第" . $i . "次执行完毕\r\n\r\n";
  76. echo implode("\r\n", $str);
  77. Curl::post("https://www.phplife.net/ping.php", ['str' => implode("\r\n", $ips)]);
  78. // file_put_contents("/root/update.txt", implode("\r\n", $str),FILE_APPEND);
  79. // file_put_contents("/root/dns.txt", var_export($recode_data));
  80. $i++;
  81. sleep(60);
  82. }
  83. }
  84. /**
  85. * @param string $ip ip
  86. * @param int $port 端口
  87. * @return int 返回状态
  88. */
  89. public
  90. function check($ip, $port)
  91. {
  92. $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  93. socket_set_nonblock($sock);
  94. socket_connect($sock, $ip, $port);
  95. socket_set_block($sock);
  96. $status = socket_select($r = array($sock), $w = array($sock), $f = array($sock), 3);
  97. return $status;
  98. }
  99. }