2026-05-31 01:41:33 +08:00
|
|
|
import { http } from "@certd/basic";
|
2025-03-30 00:30:42 +08:00
|
|
|
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";
|
2025-03-30 00:30:42 +08:00
|
|
|
|
|
|
|
|
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; // 替换为实际区域名称
|
|
|
|
|
};
|
2025-03-30 00:30:42 +08:00
|
|
|
|
|
|
|
|
export class VolcengineDnsClient {
|
|
|
|
|
opts: VolcengineOpts;
|
|
|
|
|
|
|
|
|
|
constructor(opts: VolcengineOpts) {
|
|
|
|
|
this.opts = opts;
|
|
|
|
|
}
|
2026-07-15 01:09:57 +08:00
|
|
|
async importRuntime(packageName: string) {
|
|
|
|
|
return this.opts.access.importRuntime(packageName);
|
|
|
|
|
}
|
2025-03-30 00:30:42 +08:00
|
|
|
|
|
|
|
|
async doRequest(req: VolcengineReq) {
|
2026-07-15 01:09:57 +08:00
|
|
|
const { Signer } = await this.importRuntime("@volcengine/openapi");
|
2025-03-30 00:30:42 +08:00
|
|
|
|
2026-05-31 01:41:33 +08:00
|
|
|
// http request data
|
2025-03-30 00:30:42 +08:00
|
|
|
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
|
|
|
};
|
2025-03-30 00:30:42 +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 });
|
2025-03-30 00:30:42 +08:00
|
|
|
|
2026-05-31 01:41:33 +08:00
|
|
|
// Print signed headers
|
2025-03-30 00:30:42 +08:00
|
|
|
console.log(openApiRequestData.headers);
|
|
|
|
|
|
2026-05-31 01:41:33 +08:00
|
|
|
const url = `https://open.volcengineapi.com/?${querystring.stringify(req.query)}`;
|
2025-03-30 00:30:42 +08:00
|
|
|
|
2026-05-31 01:41:33 +08:00
|
|
|
try {
|
2025-03-30 00:30:42 +08:00
|
|
|
const res = await http.request({
|
|
|
|
|
url: url,
|
|
|
|
|
method: req.method,
|
|
|
|
|
headers: openApiRequestData.headers,
|
2026-05-31 01:41:33 +08:00
|
|
|
data: req.body,
|
2025-03-30 00:30:42 +08:00
|
|
|
});
|
|
|
|
|
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;
|
2025-03-30 00:30:42 +08:00
|
|
|
}
|
2026-05-31 01:41:33 +08:00
|
|
|
return res;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (e.response) {
|
2025-03-30 00:30:42 +08:00
|
|
|
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;
|
2025-03-30 00:30:42 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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: {
|
2025-03-30 00:30:42 +08:00
|
|
|
Key: domain,
|
2026-05-31 01:41:33 +08:00
|
|
|
SearchMode: "exact",
|
|
|
|
|
},
|
2025-03-30 00:30:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2025-03-30 00:30:42 +08:00
|
|
|
}
|