#!/usr/bin/env python

"""
Copyright (c) 2006-2024 sqlmap developers (https://sqlmap.org/)
See the file 'LICENSE' for copying permission
"""

# 导入Backend、randomInt、conf、kb、logger、FROM_DUMMY_TABLE、SqlmapNotVulnerableException、dnsUse模块
from lib.core.common import Backend
from lib.core.common import randomInt
from lib.core.data import conf
from lib.core.data import kb
from lib.core.data import logger
from lib.core.dicts import FROM_DUMMY_TABLE
from lib.core.exception import SqlmapNotVulnerableException
from lib.techniques.dns.use import dnsUse

# 定义dnsTest函数，用于测试通过DNS通道获取数据
    # 打印日志，表示正在测试通过DNS通道获取数据
    # 打印信息，表示正在测试通过DNS通道获取数据
def dnsTest(payload):
    logger.info("testing for data retrieval through DNS channel")
    # 生成一个随机整数

    # 将kb.dnsTest设置为通过dnsUse函数获取的数据是否等于randInt
    randInt = randomInt()
    kb.dnsTest = dnsUse(payload, "SELECT %d%s" % (randInt, FROM_DUMMY_TABLE.get(Backend.getIdentifiedDbms(), ""))) == str(randInt)
    # 如果kb.dnsTest为False，表示通过DNS通道获取数据失败

        # 构造错误信息
    if not kb.dnsTest:
        # 如果没有强制使用DNS通道
        errMsg = "data retrieval through DNS channel failed"
            # 将conf.dnsDomain设置为None
        if not conf.forceDns:
            # 构造错误信息
            conf.dnsDomain = None
            errMsg += ". Turning off DNS exfiltration support"
            logger.error(errMsg)
        # 如果强制使用DNS通道
        else:
            # 抛出SqlmapNotVulnerableException异常
            raise SqlmapNotVulnerableException(errMsg)
    else:
        infoMsg = "data retrieval through DNS channel was successful"
        logger.info(infoMsg)
