Files
certd/packages/ui/certd-server/src/plugins/plugin-volcengine/dns-client.ts
T

131 lines
3.2 KiB
TypeScript
Raw Normal View History

2026-05-31 01:41:33 +08:00
import { http } from "@certd/basic";
import querystring from "querystring";
2026-05-31 01:41:33 +08:00
import { VolcengineOpts } from "./ve-client.js";
2026-01-23 16:56:01 +08:00
import { Pager, PageSearch } from "@certd/pipeline";
export type VolcengineReq = {
method?: string;
path?: string;
headers?: any;
body?: any;
query?: any;
2026-05-31 01:41:33 +08:00
service?: string; // 替换为实际服务名称
region?: string; // 替换为实际区域名称
};
export class VolcengineDnsClient {
opts: VolcengineOpts;
constructor(opts: VolcengineOpts) {
this.opts = opts;
}
async doRequest(req: VolcengineReq) {
const importRuntime = this.opts.importRuntime || this.opts.access.importRuntime.bind(this.opts.access);
const { Signer } = await importRuntime("@volcengine/openapi");
2026-05-31 01:41:33 +08:00
// http request data
const openApiRequestData: any = {
region: req.region,
method: req.method,
// [optional] http request url query
params: {
...req.query,
},
// http request headers
headers: {
"Content-Type": "application/json",
},
// [optional] http request body
body: req.body,
2026-05-31 01:41:33 +08:00
};
const signer = new Signer(openApiRequestData, req.service);
2026-05-31 01:41:33 +08:00
// sign
signer.addAuthorization({ accessKeyId: this.opts.access.accessKeyId, secretKey: this.opts.access.secretAccessKey });
2026-05-31 01:41:33 +08:00
// Print signed headers
console.log(openApiRequestData.headers);
2026-05-31 01:41:33 +08:00
const url = `https://open.volcengineapi.com/?${querystring.stringify(req.query)}`;
2026-05-31 01:41:33 +08:00
try {
const res = await http.request({
url: url,
method: req.method,
headers: openApiRequestData.headers,
2026-05-31 01:41:33 +08:00
data: req.body,
});
if (res?.ResponseMetadata?.Error) {
const err = new Error(JSON.stringify(res.ResponseMetadata.Error));
// @ts-ignore
err.detail = res.ResponseMetadata.Error;
2026-05-31 01:41:33 +08:00
throw err;
}
2026-05-31 01:41:33 +08:00
return res;
} catch (e) {
if (e.response) {
const err = new Error(JSON.stringify(e.response.data.ResponseMetadata.Error));
// @ts-ignore
err.detail = e.response.data.ResponseMetadata.Error;
2026-05-31 01:41:33 +08:00
throw err;
}
}
}
async findDomain(domain: string) {
const req: VolcengineReq = {
method: "POST",
region: "cn-beijing",
service: "dns",
query: {
Action: "ListZones",
Version: "2018-08-01",
},
2026-05-31 01:41:33 +08:00
body: {
Key: domain,
2026-05-31 01:41:33 +08:00
SearchMode: "exact",
},
};
return this.doRequest(req);
}
2026-01-23 16:56:01 +08:00
2026-05-31 01:41:33 +08:00
async getDomainList(page: PageSearch) {
const pager = new Pager(page);
const body: any = {
SearchMode: "like",
PageNumber: pager.pageNo,
PageSize: pager.pageSize,
};
2026-01-23 16:56:01 +08:00
if (page.searchKey) {
2026-05-31 01:41:33 +08:00
body.Key = page.searchKey;
2026-01-23 16:56:01 +08:00
}
const req: VolcengineReq = {
method: "POST",
region: "cn-beijing",
service: "dns",
query: {
Action: "ListZones",
Version: "2018-08-01",
},
2026-05-31 01:41:33 +08:00
body: body,
2026-01-23 16:56:01 +08:00
};
const res = await this.doRequest(req);
2026-05-31 01:41:33 +08:00
let list = res.Result?.Zones || [];
list = list.map((item: any) => {
2026-01-23 16:56:01 +08:00
return {
id: item.ZID,
domain: item.ZoneName,
2026-05-31 01:41:33 +08:00
};
});
const total = res.Result?.Total || list.length;
2026-01-23 16:56:01 +08:00
return {
list,
2026-05-31 01:41:33 +08:00
total,
};
2026-01-23 16:56:01 +08:00
}
}