mirror of https://github.com/certd/certd
perf: 证书申请支持51dns
parent
96a0900edc
commit
8638fc91ff
|
@ -1,5 +1,5 @@
|
||||||
import { createAxiosService, HttpClient, ILogger } from "@certd/basic";
|
import {createAxiosService, HttpClient, ILogger} from "@certd/basic";
|
||||||
import { Dns51Access } from "./access.js";
|
import {Dns51Access} from "./access.js";
|
||||||
|
|
||||||
export class Dns51Client {
|
export class Dns51Client {
|
||||||
logger: ILogger;
|
logger: ILogger;
|
||||||
|
@ -8,6 +8,7 @@ export class Dns51Client {
|
||||||
cryptoJs: any;
|
cryptoJs: any;
|
||||||
isLogined = false;
|
isLogined = false;
|
||||||
_token = "";
|
_token = "";
|
||||||
|
_cookie = "";
|
||||||
|
|
||||||
constructor(options: {
|
constructor(options: {
|
||||||
logger: ILogger;
|
logger: ILogger;
|
||||||
|
@ -56,17 +57,27 @@ export class Dns51Client {
|
||||||
url: "https://www.51dns.com/login.html",
|
url: "https://www.51dns.com/login.html",
|
||||||
method: "get",
|
method: "get",
|
||||||
withCredentials: true,
|
withCredentials: true,
|
||||||
logRes:false,
|
logRes: false,
|
||||||
returnResponse:true
|
returnResponse: true,
|
||||||
|
headers: {
|
||||||
|
// 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Safari/537.36',
|
||||||
|
'Origin': 'https://www.51dns.com',
|
||||||
|
'Referer': 'https://www.51dns.com',
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
let setCookie = res.headers['set-cookie']
|
||||||
|
let cookie = setCookie.map((item: any) => {
|
||||||
|
return item.split(';')[0]
|
||||||
|
}).join(';')
|
||||||
|
|
||||||
|
|
||||||
//提取 var csrfToken = "ieOfM21eDd9nWJv3OZtMJF6ogDsnPKQHJ17dlMck";
|
//提取 var csrfToken = "ieOfM21eDd9nWJv3OZtMJF6ogDsnPKQHJ17dlMck";
|
||||||
const _token = res.data.match(/var csrfToken = "(.*?)"/)[1];
|
const _token = res.data.match(/var csrfToken = "(.*?)"/)[1];
|
||||||
this.logger.info("_token:", _token);
|
this.logger.info("_token:", _token);
|
||||||
this._token = _token;
|
this._token = _token;
|
||||||
var obj = {
|
var obj = {
|
||||||
"email_or_phone": this.aes("18603046467"),
|
"email_or_phone": this.aes(this.access.username),
|
||||||
"password": this.aes("JiDian1Zu"),
|
"password": this.aes(this.access.password),
|
||||||
"type": this.aes("account"),
|
"type": this.aes("account"),
|
||||||
"redirectTo": "https://www.51dns.com/domain",
|
"redirectTo": "https://www.51dns.com/domain",
|
||||||
"_token": _token
|
"_token": _token
|
||||||
|
@ -78,20 +89,50 @@ export class Dns51Client {
|
||||||
...obj
|
...obj
|
||||||
},
|
},
|
||||||
withCredentials: true,
|
withCredentials: true,
|
||||||
|
logRes: false,
|
||||||
|
returnResponse: true,
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/x-www-form-urlencoded"
|
'Origin': 'https://www.51dns.com',
|
||||||
},
|
'Referer': 'https://www.51dns.com',
|
||||||
logRes:false,
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
returnResponse:true,
|
'Cookie': cookie,
|
||||||
|
'X-Requested-With': 'XMLHttpRequest'
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
this.logger.info("return headers:", JSON.stringify(res2.headers))
|
||||||
|
if (res2.data.code == 0) {
|
||||||
|
setCookie = res2.headers['set-cookie']
|
||||||
|
this._cookie = setCookie.map((item: any) => {
|
||||||
|
return item.split(';')[0]
|
||||||
|
}).join(';')
|
||||||
|
this.logger.info("cookie:", this._cookie)
|
||||||
|
this.logger.info("登录成功")
|
||||||
|
} else {
|
||||||
|
throw new Error("登录失败:", res2.data)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// 提取 <span class="user_email">182****43522</span><br>
|
const res3 = await this.http.request({
|
||||||
// console.log(res2.headers)
|
url: 'https://www.51dns.com/domain',
|
||||||
// console.log(res2.data)
|
method: 'get',
|
||||||
const username = res2.data.match(/<span class="user_email">(.*?)<\/span>/)[1];
|
withCredentials: true,
|
||||||
|
logRes: false,
|
||||||
|
returnResponse: true,
|
||||||
|
headers: {
|
||||||
|
// 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Safari/537.36',
|
||||||
|
'Origin': 'https://www.51dns.com',
|
||||||
|
'Referer': 'https://www.51dns.com/login.html',
|
||||||
|
'Cookie': this._cookie,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const success2 = res3.data.includes('<span class="nav-title">DNS解析</span>')
|
||||||
|
|
||||||
|
if (!success2) {
|
||||||
|
throw new Error("检查登录失败")
|
||||||
|
}
|
||||||
|
this.logger.info("检查登录成功")
|
||||||
|
|
||||||
this.logger.info("登录成功:username:", username);
|
|
||||||
this.isLogined = true;
|
this.isLogined = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,63 +143,58 @@ export class Dns51Client {
|
||||||
url: `https://www.51dns.com/domain?domain=${domain}&status=`,
|
url: `https://www.51dns.com/domain?domain=${domain}&status=`,
|
||||||
method: "get",
|
method: "get",
|
||||||
withCredentials: true,
|
withCredentials: true,
|
||||||
logRes:false,
|
logRes: false,
|
||||||
returnResponse:true
|
returnResponse: true,
|
||||||
|
headers: this.getRequestHeaders()
|
||||||
});
|
});
|
||||||
|
|
||||||
// 提取 <a target="_blank" href="https://www.51dns.com/domain/record/193341603"
|
// 提取 <a target="_blank" href="https://www.51dns.com/domain/record/193341603"
|
||||||
// class="color47">certd.top</a>
|
// class="color47">certd.top</a>
|
||||||
const regex = new RegExp(`<a target="_blank" href="https://www.51dns.com/domain/record/(.*?)".*${domain}<\/a>`, "g");
|
const regExp = new RegExp(`<a target="_blank" href="https://www.51dns.com/domain/record/(\\d+)"[^>]*>${domain}<\\/a>`, "i");
|
||||||
const matched = res.data.match(regex);
|
const matched = res.data.match(regExp);
|
||||||
if (!matched || matched.length === 0) {
|
if (!matched || matched.length < 1) {
|
||||||
throw new Error(`域名${domain}不存在`);
|
throw new Error(`域名${domain}不存在`);
|
||||||
}
|
}
|
||||||
return matched[1];
|
const domainId = matched[1];
|
||||||
|
this.logger.info(`域名${domain}的id为${domainId}`)
|
||||||
|
return parseInt(domainId);
|
||||||
}
|
}
|
||||||
|
|
||||||
async createRecord(param: { domain: string, data: any; domainId: void; host: string; ttl: number; type: string }) {
|
private getRequestHeaders() {
|
||||||
const { domain, data, host, type } = param;
|
return {
|
||||||
|
'Origin': 'https://www.51dns.com',
|
||||||
|
'Referer': 'https://www.51dns.com',
|
||||||
|
'Cookie': this._cookie
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async createRecord(param: { domain: string, data: any; domainId: number; host: string; ttl: number; type: string }) {
|
||||||
|
const {domain, data, host, type} = param;
|
||||||
const domainId = await this.getDomainId(domain);
|
const domainId = await this.getDomainId(domain);
|
||||||
const url = "https://www.51dns.com/domain/storenNewRecord";
|
const url = "https://www.51dns.com/domain/storenNewRecord";
|
||||||
const req = {
|
const req = {
|
||||||
_token: this._token,
|
_token: this._token,
|
||||||
domain_id: parseInt(domainId),
|
domain_id: domainId,
|
||||||
record: host,
|
record: host,
|
||||||
type: type,
|
type: type,
|
||||||
value: data,
|
value: data,
|
||||||
ttl: 300,
|
ttl: 300,
|
||||||
|
mx:"",
|
||||||
view_id: 0
|
view_id: 0
|
||||||
};
|
};
|
||||||
|
this.logger.info("req:", JSON.stringify(req))
|
||||||
const res = await this.http.request({
|
const res = await this.http.request({
|
||||||
url,
|
url,
|
||||||
method: "post",
|
method: "post",
|
||||||
data: req,
|
data: req,
|
||||||
withCredentials: true,
|
withCredentials: true,
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/x-www-form-urlencoded"
|
...this.getRequestHeaders(),
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
/*
|
if (res.status !== 200) {
|
||||||
{
|
|
||||||
"status": 200,
|
|
||||||
"msg": "\u6b63\u786e",
|
|
||||||
"data": {
|
|
||||||
"record": "1111",
|
|
||||||
"type": "TXT",
|
|
||||||
"value": "2222",
|
|
||||||
"mx": "-",
|
|
||||||
"ttl": "300",
|
|
||||||
"view_id": "0",
|
|
||||||
"id": 601019779,
|
|
||||||
"domain_id": "193341603",
|
|
||||||
"trecord": "1111",
|
|
||||||
"view_name": "\u9ed8\u8ba4"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
if(res.status !== 200){
|
|
||||||
throw new Error(`创建域名解析失败:${res.msg}`);
|
throw new Error(`创建域名解析失败:${res.msg}`);
|
||||||
}
|
}
|
||||||
const id = res.data.id;
|
const id = res.data.id;
|
||||||
|
@ -170,13 +206,13 @@ export class Dns51Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteRecord(param: { domainId: number; id: number }) {
|
async deleteRecord(param: { domainId: number; id: number }) {
|
||||||
const url ="https://www.51dns.com/domain/operateRecord"
|
const url = "https://www.51dns.com/domain/operateRecord"
|
||||||
/*
|
/*
|
||||||
type: delete
|
type: delete
|
||||||
ids[0]: 601019779
|
ids[0]: 601019779
|
||||||
domain_id: 193341603
|
domain_id: 193341603
|
||||||
_token: ieOfM21eDd9nWJv3OZtMJF6ogDsnPKQHJ17dlMck
|
_token: ieOfM21eDd9nWJv3OZtMJF6ogDsnPKQHJ17dlMck
|
||||||
*/
|
*/
|
||||||
const body = {
|
const body = {
|
||||||
type: "delete",
|
type: "delete",
|
||||||
ids: [param.id],
|
ids: [param.id],
|
||||||
|
@ -189,10 +225,11 @@ _token: ieOfM21eDd9nWJv3OZtMJF6ogDsnPKQHJ17dlMck
|
||||||
data: body,
|
data: body,
|
||||||
withCredentials: true,
|
withCredentials: true,
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/x-www-form-urlencoded"
|
...this.getRequestHeaders(),
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if(res.status !== 200){
|
if (res.status !== 200) {
|
||||||
throw new Error(`删除域名解析失败:${res.msg}`);
|
throw new Error(`删除域名解析失败:${res.msg}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,6 @@ import { Dns51Client } from "./client.js";
|
||||||
export type Dns51Record = {
|
export type Dns51Record = {
|
||||||
id: number;
|
id: number;
|
||||||
domainId: number,
|
domainId: number,
|
||||||
client: Dns51Client,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// 这里通过IsDnsProvider注册一个dnsProvider
|
// 这里通过IsDnsProvider注册一个dnsProvider
|
||||||
|
@ -21,10 +20,16 @@ export type Dns51Record = {
|
||||||
export class Dns51DnsProvider extends AbstractDnsProvider<Dns51Record> {
|
export class Dns51DnsProvider extends AbstractDnsProvider<Dns51Record> {
|
||||||
// 通过Autowire传递context
|
// 通过Autowire传递context
|
||||||
access!: Dns51Access;
|
access!: Dns51Access;
|
||||||
|
|
||||||
|
client!:Dns51Client;
|
||||||
async onInstance() {
|
async onInstance() {
|
||||||
//一些初始化的操作
|
//一些初始化的操作
|
||||||
// 也可以通过ctx成员变量传递context, 与Autowire效果一样
|
// 也可以通过ctx成员变量传递context, 与Autowire效果一样
|
||||||
this.access = this.ctx.access as Dns51Access;
|
this.access = this.ctx.access as Dns51Access;
|
||||||
|
this.client = new Dns51Client({
|
||||||
|
logger: this.logger,
|
||||||
|
access: this.access,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -41,15 +46,12 @@ export class Dns51DnsProvider extends AbstractDnsProvider<Dns51Record> {
|
||||||
this.logger.info('添加域名解析:', fullRecord, value, type, domain);
|
this.logger.info('添加域名解析:', fullRecord, value, type, domain);
|
||||||
|
|
||||||
|
|
||||||
const dns51Client = new Dns51Client({
|
|
||||||
logger: this.logger,
|
|
||||||
access: this.access,
|
|
||||||
});
|
|
||||||
|
|
||||||
const domainId = await dns51Client.getDomainId(domain);
|
|
||||||
|
const domainId = await this.client.getDomainId(domain);
|
||||||
this.logger.info('获取domainId成功:', domainId);
|
this.logger.info('获取domainId成功:', domainId);
|
||||||
|
|
||||||
const res = await dns51Client.createRecord({
|
const res = await this.client.createRecord({
|
||||||
domain: domain,
|
domain: domain,
|
||||||
domainId: domainId,
|
domainId: domainId,
|
||||||
type: 'TXT',
|
type: 'TXT',
|
||||||
|
@ -60,7 +62,6 @@ export class Dns51DnsProvider extends AbstractDnsProvider<Dns51Record> {
|
||||||
return {
|
return {
|
||||||
id: res.id,
|
id: res.id,
|
||||||
domainId: domainId,
|
domainId: domainId,
|
||||||
client: dns51Client,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,8 +85,8 @@ export class Dns51DnsProvider extends AbstractDnsProvider<Dns51Record> {
|
||||||
* Authorization: Basic {token}
|
* Authorization: Basic {token}
|
||||||
* 请求参数
|
* 请求参数
|
||||||
*/
|
*/
|
||||||
const { client,id,domainId} = record
|
const {id,domainId} = record
|
||||||
await client.deleteRecord({
|
await this.client.deleteRecord({
|
||||||
id,
|
id,
|
||||||
domainId
|
domainId
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue