<?php

namespace app\controller;
use support\Request;
use Webman\Captcha\CaptchaBuilder;
use support\View;
use Respect\Validation\Validator as v;
use support\Redis;
use GeoIp2\Database\Reader;
use yzh52521\EasyHttp\Http;
use yzh52521\EasyHttp\Response;
use yzh52521\EasyHttp\RequestException;


class api
{
    public function search(Request $request)
    {
        $content=$request->input('content','0');
        $contenttype=v::ip()->validate($content);
        if(!$contenttype){
            $contenttype=v::domain(false)->validate($content);
            if(!$contenttype){
                return '请输入合法的IP或域名';
            }else{
                $contenttype='domain';
                $msg['DNS']=dns_get_record("$content",DNS_ANY);
                $whois = Http::get('https://api.devopsclub.cn/api/whoisquery?type=json&standard=true', ['domain' => "$content"])->body();
                $whois=json_decode($whois);
                if($whois->code !=0){
                    $msg['Whois']='Whois查询失败';
                };
                if($whois->data->status !=0){
                    $msg['Whois']='Whois查询出错,请稍后重试';
                };
                if($whois->data->status ==2 || $data->status ==3 ){
                    $msg['Whois']='该域名未注册或该后缀暂不支持查询';
                };
                if($whois->code == 0 && $whois->data->status == 0){
                $msg['Whois']=$whois->data->data;
                }
                $safe = Http::get('https://api.devopsclub.cn/api/dcheck', ['url' => "$content"])->body();
                $safe=json_decode($safe);
                if($safe->code == 0){
                $msg['Safe']='说明:Wx-Safe和QQ-Safe为该域名的微信/QQ红白状态 danger为红 unknown未知 safe安全 仅供参考';
                $msg['Wx-Safe']=$safe->data->wx;
                $msg['QQ-Safe']=$safe->data->qq;
                }
            }
        }else{
            $contenttype='ip';
            $msg['Host']=gethostbyaddr("$content");
            $reader = new Reader(base_path().'/geoip/GeoLite2-City/GeoLite2-City.mmdb');
            $locate=$reader->city($content);
            $country=$locate->raw['country']['names']['zh-CN'];
            if(array_key_exists("city",$locate->raw)){
                $city=$locate->raw['city']['names']['zh-CN'];
            }else{
                $city=$country;
            }
            $msg['Location']="$city/$country";
            $msg['Network']=$locate->traits->network;
            $asnreader= new Reader(base_path().'/geoip/GeoLite2-ASN/GeoLite2-ASN.mmdb');
            $asn=$asnreader->Asn("$content");
            $asnnumber=$asn->autonomousSystemNumber;
            $msg['ASN']="<a href='//whois.ipip.net/AS$asnnumber' target='_ipip'>AS:".$asnnumber.'/'.$asn->autonomousSystemOrganization.'</a>';
        }

        View::assign('msg', $msg);
        return view('print');
    }

}