Server.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. class Server extends Controller
  14. {
  15. /**
  16. * 执行逻辑
  17. */
  18. protected function main()
  19. {
  20. $recode_data=[];
  21. // $i = 0;
  22. $key="093fcd440ed453c2689c42150bdb7d5dbc4fb";
  23. $zone_identifier="89a0fe0d09762c557fd0c7799efb1c9a";
  24. $client = new Api('chendeben@qq.com', $key);
  25. $dns = new Dns($client);
  26. // $id=$dns->create($zone_identifier, "A", "test.97admin.com", "127.0.0.1");
  27. // $id=$dns->update($zone_identifier, "23f0bb6a9eede8750153b1983af5dacc","A","test.97admin.com","127.0.0.2");
  28. // print_r($id);
  29. // $dns->update($zone_identifier, $identifier)
  30. // die();
  31. while (true) {
  32. $data = ServerTable::select();
  33. foreach ($data as $val) {
  34. $ip = $val['ip'];
  35. $port = $val['port'];
  36. $status = $this->check($ip, $port);
  37. if ($status == 1) {
  38. echo date("Y-m-d H:i:s")."\t\t".$ip . "\t可用\r\n";
  39. }
  40. // if ($val['status'] != $status) {
  41. ServerTable::update(['status' => $status, "last_check" => date("Y-m-d H:i:s")], ['id' => $val['id']]);
  42. if ($status==1){
  43. if (!$recode_data[$val['domain']]){
  44. $recode=$dns->list_records($zone_identifier,null,$val['domain']);
  45. $recode=json_decode(json_encode($recode),true);
  46. if ($recode['result']){
  47. $result=$recode['result'][0];
  48. $recode_data[$val['domain']]=$result;
  49. }
  50. }
  51. $result=$recode_data[$val['domain']];
  52. // print_r($result);
  53. // die();
  54. if (!$result){
  55. echo date("Y-m-d H:i:s")."\t\t"."域名记录【".$val['domain']."】不存在,正在建立\r\n";
  56. $recode=$dns->create($zone_identifier, "A", $val['domain'], $val['ip']);
  57. $recode=json_decode(json_encode($recode),true);
  58. if ($recode['result']){
  59. $result=$recode['result'][0];
  60. $recode_data[$val['domain']]=$result;
  61. echo date("Y-m-d H:i:s")."\t\t"."域名记录【".$val['domain']."】新建成功\r\n";
  62. }
  63. }
  64. if ($result['content']!=$val['ip']){
  65. echo date("Y-m-d H:i:s")."\t\t"."域名记录【".$val['domain']."】与数据库不一致,正在更新\r\n";
  66. $dns->update($zone_identifier, $result['id'],"A",$val['domain'],$val['ip']);
  67. }
  68. }
  69. // }
  70. }
  71. // echo "第" . $i . "次执行完毕\r\n";
  72. // $i++;
  73. sleep(60);
  74. }
  75. }
  76. /**
  77. * @param string $ip ip
  78. * @param int $port 端口
  79. * @return int 返回状态
  80. */
  81. public
  82. function check($ip, $port)
  83. {
  84. $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  85. socket_set_nonblock($sock);
  86. socket_connect($sock, $ip, $port);
  87. socket_set_block($sock);
  88. $status = socket_select($r = array($sock), $w = array($sock), $f = array($sock), 3);
  89. return $status;
  90. }
  91. }