为宝塔面板 SSL(Let’s Encrypt) 添加 Hostker API

⚠️ 当前适用版本:7.2.0,其他版本未测试

1. 修改 DNS API 配置文件:/www/server/panel/config/dns_api.json

[{"ps": "\u4f7f\u7528CloudFlare\u7684API\u63a5\u53e3\u81ea\u52a8\u89e3\u6790\u7533\u8bf7SSL", "data": [{"key": "SAVED_CF_MAIL", "value": "", "name": "E-Mail"}, {"key": "SAVED_CF_KEY", "value": "", "name": "API Key"}], "help": "CloudFlare\u540e\u53f0\u83b7\u53d6Global API Key", "name": "CloudFlareDns", "title": "CloudFlare"}, {"ps": "\u4f7f\u7528 Hostker \u7684 API \u63a5\u53e3\u81ea\u52a8\u89e3\u6790\u7533\u8bf7 SSL", "data": [{"key": "SAVED_KER_MAIL", "value": "", "name": "E-Mail"}, {"key": "SAVED_KER_KEY", "value": "", "name": "API Token"}], "help": "\u957f\u5ea6\u4e3a 64 \u4f4d\u7684 API \u4ee4\u724c\uff0c\u767b\u5f55\u5ba2\u6237\u4e2d\u5fc3\u300c\u8d26\u53f7\u8d44\u6599\u300d\u4e2d\u53ef\u4ee5\u83b7\u53d6", "name": "HostkerDns", "title": "Hostker"}, {"ps": "\u4f7f\u7528\u5b9d\u5854DNS\u4e91\u89e3\u6790\u63d2\u4ef6\u81ea\u52a8\u89e3\u6790\u7533\u8bf7SSL", "title": "\u5b9d\u5854DNS\u4e91\u89e3\u6790", "data": false, "help": "", "name": "Dns_com"}, {"ps": "\u4f7f\u7528DnsPod\u7684API\u63a5\u53e3\u81ea\u52a8\u89e3\u6790\u7533\u8bf7SSL", "title": "DnsPod", "data": [{"value": "", "key": "SAVED_DP_Id", "name": "ID"}, {"value": "", "key": "SAVED_DP_Key", "name": "Token"}], "help": "DnsPod\u540e\u53f0\u300b\u7528\u6237\u4e2d\u5fc3\u300b\u5b89\u5168\u8bbe\u7f6e\uff0c\u5f00\u542fAPI Token", "name": "DNSPodDns"}, {"ps": "\u4f7f\u7528\u963f\u91cc\u4e91API\u63a5\u53e3\u81ea\u52a8\u89e3\u6790\u7533\u8bf7SSL", "title": "\u963f\u91cc\u4e91DNS", "data": [{"value": "", "key": "SAVED_Ali_Key", "name": "AccessKey"}, {"value": "", "key": "SAVED_Ali_Secret", "name": "SecretKey"}], "help": "\u963f\u91cc\u4e91\u63a7\u5236\u53f0\u300b\u7528\u6237\u5934\u50cf\u300baccesskeys\u6309\u6307\u5f15\u83b7\u53d6AccessKey/SecretKey", "name": "AliyunDns"}, {"ps": "\u4f7f\u7528cloudxns\u7684API\u63a5\u53e3\u81ea\u52a8\u89e3\u6790\u7533\u8bf7SSL", "title": "CloudXns", "data": [{"value": "", "key": "SAVED_CX_Key", "name": "AccessKey"}, {"value": "", "key": "SAVED_CX_Secret", "name": "SecretKey"}], "help": "cloudxns\u540e\u53f0\u300b\u7528\u6237\u4e2d\u5fc3\u300bAPI\u7ba1\u7406\uff0c\u6309\u6307\u5f15\u83b7\u53d6AccessKey/SecretKey", "name": "CloudxnsDns"}, {"ps": "\u8fd4\u56dehost\u548ctxt\u503c,\u7531\u7528\u6237\u624b\u52a8\u89e3\u6790", "data": false, "name": "dns", "title": "\u624b\u52a8\u89e3\u6790"}]

* 代码较长,复制时注意复制完整(在代码上双击可选择全部)

2. 编辑 /www/server/panel/class/panelDnsapi.py 在文件末尾加入以下代码

class HostkerDns(BaseDns):
    dns_provider_name = "hostker"

    def __init__(self, HOSTKER_EMAIL, HOSTKER_TOKEN, HOSTKER_API_BASE_URL="https://i.hostker.com/api/"):
        self.HOSTKER_EMAIL = HOSTKER_EMAIL
        self.HOSTKER_TOKEN = HOSTKER_TOKEN
        self.HOSTKER_API_BASE_URL = HOSTKER_API_BASE_URL
        self.HTTP_TIMEOUT = 65  # seconds

        if HOSTKER_API_BASE_URL[-1] != "/":
            self.HOSTKER_API_BASE_URL = HOSTKER_API_BASE_URL + "/"
        else:
            self.HOSTKER_API_BASE_URL = HOSTKER_API_BASE_URL
        super(HostkerDns, self).__init__()

    def create_dns_record(self, domain_name, domain_dns_value):
        domain_name,_,subd = extract_zone(domain_name)
        self.add_record(domain_name,subd,domain_dns_value,'TXT')

       

    def add_record(self,domain_name,subd,domain_dns_value,s_type):
        url = urljoin(self.HOSTKER_API_BASE_URL, "dnsAddRecord")
        body = {
            "type": s_type,
            "domain": domain_name,
            "header": subd,
            "data": domain_dns_value,
            "ttl": "120",
            "email": self.HOSTKER_EMAIL,
            "token": self.HOSTKER_TOKEN,
        }

        create_hostker_dns_record_response = requests.post(
            url, data=body, timeout=self.HTTP_TIMEOUT
        ).json()

        if create_hostker_dns_record_response["success"] != 1:
            raise ValueError(
                "Error creating hostker dns record: status_code={status_code} response={response}".format(
                    status_code=create_hostker_dns_record_response["success"],
                    response=create_hostker_dns_record_response["errorMessage"],
                )
            )


    def remove_record(self,domain_name,subd,s_type="TXT"):
        url = urljoin(self.HOSTKER_API_BASE_URL, "dnsGetRecords")
        rootdomain = domain_name
        body = {
            "domain": domain_name,
            "email": self.HOSTKER_EMAIL,
            "token": self.HOSTKER_TOKEN,
        }

        list_dns_response = requests.post(url, data=body, timeout=self.HTTP_TIMEOUT).json()
        for i in range(0, len(list_dns_response["records"])):
            if list_dns_response["records"][i]['header'] != subd:
                continue
            rid = list_dns_response["records"][i]["id"]
            urlr = urljoin(self.HOSTKER_API_BASE_URL, "dnsDeleteRecord")
            bodyr = {
                "id": rid,
                "email": self.HOSTKER_EMAIL,
                "token": self.HOSTKER_TOKEN,
            }
            requests.post(
                urlr, data=bodyr, timeout=self.HTTP_TIMEOUT
            ).json()

    def delete_dns_record(self, domain_name, domain_dns_value):
        try:
            domain_name,_,subd = extract_zone(domain_name)
            self.remove_record(domain_name,subd,'TXT')
        except:
            pass
# end HostkerDns

3. 重启宝塔面板即可

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据