| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- /**
- * Created by PhpStorm.
- * User: chendeben
- * Date: 2018/8/31
- * Time: 下午9:35
- */
- namespace PhpLife\App\Script\Controller;
- use Cloudflare\Api;
- use Cloudflare\Zone\Dns;
- use PhpLife\App\Script\Controller;
- use PhpLife\App\Api\Package\Database\Server as ServerTable;
- use PhpLife\Library\Curl;
- class Server extends Controller
- {
- /**
- * 执行逻辑
- */
- protected function main()
- {
- $recode_data = [];
- $i = 0;
- $key = "093fcd440ed453c2689c42150bdb7d5dbc4fb";
- $zone_identifier = "89a0fe0d09762c557fd0c7799efb1c9a";
- $client = new Api('chendeben@qq.com', $key);
- $dns = new Dns($client);
- // $id=$dns->create($zone_identifier, "A", "test.97admin.com", "127.0.0.1");
- // $id=$dns->update($zone_identifier, "23f0bb6a9eede8750153b1983af5dacc","A","test.97admin.com","127.0.0.2");
- // print_r($id);
- // $dns->update($zone_identifier, $identifier)
- // die();
- while (true) {
- $data = ServerTable::select([],[],"",null,0,['sort'=>false]);
- $str = [];
- $ips = [];
- foreach ($data as $val) {
- $ip = $val['ip'];
- $port = $val['port'];
- $status = $this->check($ip, $port);
- if ($status == 1) {
- $ips[] = date("Y-m-d H:i:s")."\t\t【" . $val['domain'] . "】" . $ip . "可用";
- $str[] = date("Y-m-d H:i:s") . "\t\t" . $ip . "\t可用\r\n";
- }
- // if ($val['status'] != $status) {
- ServerTable::update(['status' => $status, "last_check" => date("Y-m-d H:i:s")], ['id' => $val['id']]);
- if ($status == 1) {
- if (!$recode_data[$val['domain']]) {
- $recode = $dns->list_records($zone_identifier, null, $val['domain']);
- $recode = json_decode(json_encode($recode), true);
- if ($recode['result']) {
- $result = $recode['result'][0];
- $recode_data[$val['domain']] = $result;
- }
- }
- $result = $recode_data[$val['domain']];
- // print_r($result);
- // die();
- if (!$result) {
- $str[] = date("Y-m-d H:i:s") . "\t\t" . "域名记录【" . $val['domain'] . "】不存在,正在建立\r\n";
- $recode = $dns->create($zone_identifier, "A", $val['domain'], $val['ip']);
- $recode = json_decode(json_encode($recode), true);
- if ($recode['result']) {
- $result = $recode['result'][0];
- $recode_data[$val['domain']] = $result;
- $str[] = date("Y-m-d H:i:s") . "\t\t" . "域名记录【" . $val['domain'] . "】新建成功\r\n";
- }
- }
- if ($result['content'] != $val['ip']) {
- $str[] = date("Y-m-d H:i:s") . "\t\t" . "域名记录【" . $val['domain'] . "】与数据库不一致,正在更新\r\n";
- $dns->update($zone_identifier, $result['id'], "A", $val['domain'], $val['ip']);
- }
- }
- // }
- }
- $str[] = "第" . $i . "次执行完毕\r\n\r\n";
- echo implode("\r\n", $str);
- Curl::post("https://www.phplife.net/ping.php", ['str' => implode("\r\n", $ips)]);
- // file_put_contents("/root/update.txt", implode("\r\n", $str),FILE_APPEND);
- // file_put_contents("/root/dns.txt", var_export($recode_data));
- $i++;
- sleep(60);
- }
- }
- /**
- * @param string $ip ip
- * @param int $port 端口
- * @return int 返回状态
- */
- public
- function check($ip, $port)
- {
- $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
- socket_set_nonblock($sock);
- socket_connect($sock, $ip, $port);
- socket_set_block($sock);
- $status = socket_select($r = array($sock), $w = array($sock), $f = array($sock), 3);
- return $status;
- }
- }
|