package org.jeecg.modules.adweb.common.service;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;
import org.xbill.DNS.CNAMERecord;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;
import org.xbill.DNS.Type;

import java.net.InetAddress;
import java.util.List;

/**
 * @Author: 汤星鹏
 * @Description: 域名检测service
 * @Date: 2025/1/6 9:13
 */
@Service
@Slf4j
public class DomainService {

    /**
     * 核实域名是否指向规定cname
     *
     * @param cname 网站cname
     * @param domain 站点域名
     * @return
     * @author Cyan -- 2019/12/6 11:47 @Update chenpeiqing -- 2025/1/3 17:53
     */
    public Boolean checkDomainIsCname(String cname, String domain) {
        boolean flag = false;
        try {
            Record[] records = null;
            Lookup lookup = new Lookup(domain, Type.CNAME);
            lookup.run();
            // 连通并获取结果集
            if (lookup.getResult() == Lookup.SUCCESSFUL) {
                records = lookup.getAnswers();
            } else {
                // 未查到结果
                log.info("未查询到结果!");
                return false;
            }
            for (Record record : records) {
                CNAMERecord mx = (CNAMERecord) record;
                log.info("域名:{}  ", mx.getName());
                log.info("CNAME: {} ", mx.getTarget().toString());
                int code = mx.getTarget().toString().indexOf(cname);
                if (-1 != code) {
                    flag = true;
                }
            }
        } catch (Exception e) {
            log.error("核实域名是否指向规定cname: {}", e.getMessage());
            flag = false;
        }
        return flag;
    }

    /**
     * 核实域名是否指向规定cname
     *
     * @param ip 网站ip
     * @param domain 站点域名
     * @return true:域名指向了ip false：域名没有指向ip
     * @author Cyan -- 2019/12/6 11:47
     */
    public Boolean checkDomainIsIp(String domain, String ip) {
        Boolean flag = false;
        try {
            InetAddress[] addresses = InetAddress.getAllByName(domain);
            for (int i = 0; i < addresses.length; i++) {
                String hostAddress = addresses[i].getHostAddress();
                int code = hostAddress.indexOf(ip);
                if (-1 != code) {
                    flag = true;
                    break;
                }
            }
        } catch (Exception e) {
            log.error("核实域名是否指向规定cname: {}", e.getMessage());
            flag = false;
        }
        return flag;
    }

    /**
     * 核实域名是否指向规定cname或ip
     *
     * @param cname 网站cname
     * @param domain 站点域名
     * @param ip
     * @return
     * @author Cyan -- 2019/12/6 11:47
     */
    public Boolean checkDomainIsCnameOrIp(String cname, String domain, String ip) {
        Boolean flag = false;
        boolean toCname = checkDomainIsCname(cname, domain);
        boolean toIP = checkDomainIsIp(domain, ip);
        // cname或ip  二者有其一指向正确
        if (toCname || toIP) {
            flag = true;
        }
        return flag;
    }

    /**
     * 检验域名以什么开头
     *
     * @param domain 域名
     * @param headStrs 头字符串集合
     * @return true:是以存在集合中的元素开头 false：不是以存在集合中的元素开头
     * @author Cyan -- 2019/12/12 9:36
     */
    public Boolean checkDomainBegin(String domain, List<String> headStrs) {
        Boolean flag = false;
        for (String headStr : headStrs) {
            if (domain.startsWith(headStr)) {
                flag = true;
                break;
            }
        }
        return flag;
    }
}
