Server.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. $str=[];
  34. foreach ($data as $val) {
  35. $ip = $val['ip'];
  36. $port = $val['port'];
  37. $status = $this->check($ip, $port);
  38. if ($status == 1) {
  39. $str[]=date("Y-m-d H:i:s")."\t\t".$ip . "\t可用\r\n";
  40. }
  41. // if ($val['status'] != $status) {
  42. ServerTable::update(['status' => $status, "last_check" => date("Y-m-d H:i:s")], ['id' => $val['id']]);
  43. if ($status==1){
  44. if (!$recode_data[$val['domain']]){
  45. $recode=$dns->list_records($zone_identifier,null,$val['domain']);
  46. $recode=json_decode(json_encode($recode),true);
  47. if ($recode['result']){
  48. $result=$recode['result'][0];
  49. $recode_data[$val['domain']]=$result;
  50. }
  51. }
  52. $result=$recode_data[$val['domain']];
  53. // print_r($result);
  54. // die();
  55. if (!$result){
  56. $str[]= date("Y-m-d H:i:s")."\t\t"."域名记录【".$val['domain']."】不存在,正在建立\r\n";
  57. $recode=$dns->create($zone_identifier, "A", $val['domain'], $val['ip']);
  58. $recode=json_decode(json_encode($recode),true);
  59. if ($recode['result']){
  60. $result=$recode['result'][0];
  61. $recode_data[$val['domain']]=$result;
  62. $str[]= date("Y-m-d H:i:s")."\t\t"."域名记录【".$val['domain']."】新建成功\r\n";
  63. }
  64. }
  65. if ($result['content']!=$val['ip']){
  66. $str[]= date("Y-m-d H:i:s")."\t\t"."域名记录【".$val['domain']."】与数据库不一致,正在更新\r\n";
  67. $dns->update($zone_identifier, $result['id'],"A",$val['domain'],$val['ip']);
  68. }
  69. }
  70. // }
  71. }
  72. $str[]= "第" . $i . "次执行完毕\r\n\r\n";
  73. file_put_contents("/home/wwwroot/phplife.net/update.txt", implode("\r\n", $str),FILE_APPEND);
  74. file_put_contents("/home/wwwroot/phplife.net/dns.txt", var_export($recode_data));
  75. $i++;
  76. sleep(60);
  77. }
  78. }
  79. /**
  80. * @param string $ip ip
  81. * @param int $port 端口
  82. * @return int 返回状态
  83. */
  84. public
  85. function check($ip, $port)
  86. {
  87. $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  88. socket_set_nonblock($sock);
  89. socket_connect($sock, $ip, $port);
  90. socket_set_block($sock);
  91. $status = socket_select($r = array($sock), $w = array($sock), $f = array($sock), 3);
  92. return $status;
  93. }
  94. }