update: the domain service3
This commit is contained in:
@@ -10,6 +10,12 @@ export class CreateDnsRecordDto {
|
|||||||
@ApiProperty({ description: "The name of the DNS record", example: "mail" })
|
@ApiProperty({ description: "The name of the DNS record", example: "mail" })
|
||||||
name!: string;
|
name!: string;
|
||||||
|
|
||||||
|
@IsNotEmpty()
|
||||||
|
@IsString()
|
||||||
|
@MaxLength(255)
|
||||||
|
@ApiProperty({ description: "The name of the DNS record", example: "mail" })
|
||||||
|
fullName: string;
|
||||||
|
|
||||||
@IsEnum(DNSRecordType)
|
@IsEnum(DNSRecordType)
|
||||||
@ApiProperty({ description: "The type of the DNS record", example: "MX" })
|
@ApiProperty({ description: "The type of the DNS record", example: "MX" })
|
||||||
type!: DNSRecordType;
|
type!: DNSRecordType;
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ export class DnsRecord extends BaseEntity {
|
|||||||
@Property({ type: "varchar", length: 255, nullable: false })
|
@Property({ type: "varchar", length: 255, nullable: false })
|
||||||
name!: string;
|
name!: string;
|
||||||
|
|
||||||
|
@Property({ type: "varchar", length: 255, nullable: false })
|
||||||
|
fullName!: string;
|
||||||
|
|
||||||
@Enum({ items: () => DNSRecordType, nativeEnumName: "dns_record_type_enum" })
|
@Enum({ items: () => DNSRecordType, nativeEnumName: "dns_record_type_enum" })
|
||||||
type!: DNSRecordType;
|
type!: DNSRecordType;
|
||||||
|
|
||||||
|
|||||||
@@ -90,8 +90,8 @@ export class DnsService {
|
|||||||
|
|
||||||
// MX Record
|
// MX Record
|
||||||
requiredRecords.push({
|
requiredRecords.push({
|
||||||
// name: domain.name,
|
|
||||||
name: "@",
|
name: "@",
|
||||||
|
fullName: domain.name,
|
||||||
type: DNSRecordType.MX,
|
type: DNSRecordType.MX,
|
||||||
value: this.mailServerDomain,
|
value: this.mailServerDomain,
|
||||||
priority: 10,
|
priority: 10,
|
||||||
@@ -101,8 +101,8 @@ export class DnsService {
|
|||||||
|
|
||||||
// SPF Record
|
// SPF Record
|
||||||
requiredRecords.push({
|
requiredRecords.push({
|
||||||
// name: domain.name,
|
|
||||||
name: "@",
|
name: "@",
|
||||||
|
fullName: domain.name,
|
||||||
type: DNSRecordType.TXT,
|
type: DNSRecordType.TXT,
|
||||||
value: this.spfRecord,
|
value: this.spfRecord,
|
||||||
isRequired: true,
|
isRequired: true,
|
||||||
@@ -112,8 +112,8 @@ export class DnsService {
|
|||||||
// DMARC Record
|
// DMARC Record
|
||||||
const dmarcRecord = `v=DMARC1; p=quarantine; rua=mailto:dmarc@${domain.name}; ruf=mailto:dmarc@${domain.name}`;
|
const dmarcRecord = `v=DMARC1; p=quarantine; rua=mailto:dmarc@${domain.name}; ruf=mailto:dmarc@${domain.name}`;
|
||||||
requiredRecords.push({
|
requiredRecords.push({
|
||||||
// name: `_dmarc.${domain.name}`,
|
|
||||||
name: `_dmarc`,
|
name: `_dmarc`,
|
||||||
|
fullName: `_dmarc.${domain.name}`,
|
||||||
type: DNSRecordType.TXT,
|
type: DNSRecordType.TXT,
|
||||||
value: dmarcRecord,
|
value: dmarcRecord,
|
||||||
isRequired: true,
|
isRequired: true,
|
||||||
@@ -125,6 +125,7 @@ export class DnsService {
|
|||||||
for (const recordData of requiredRecords) {
|
for (const recordData of requiredRecords) {
|
||||||
const dnsRecord = this.dnsRecordRepository.create({
|
const dnsRecord = this.dnsRecordRepository.create({
|
||||||
name: recordData.name!,
|
name: recordData.name!,
|
||||||
|
fullName: recordData.fullName!,
|
||||||
type: recordData.type!,
|
type: recordData.type!,
|
||||||
value: recordData.value!,
|
value: recordData.value!,
|
||||||
domain,
|
domain,
|
||||||
@@ -145,9 +146,10 @@ export class DnsService {
|
|||||||
/**
|
/**
|
||||||
* Create DKIM record
|
* Create DKIM record
|
||||||
*/
|
*/
|
||||||
async createDKIMRecord(domain: Domain, selector: string, dkimName: string, dkimValue: string, wildduckId: string) {
|
async createDKIMRecord(domain: Domain, selector: string, dkimName: string, dkimFullName: string, dkimValue: string, wildduckId: string) {
|
||||||
const dkimRecord = this.dnsRecordRepository.create({
|
const dkimRecord = this.dnsRecordRepository.create({
|
||||||
name: dkimName,
|
name: dkimName,
|
||||||
|
fullName: dkimFullName,
|
||||||
type: DNSRecordType.TXT,
|
type: DNSRecordType.TXT,
|
||||||
value: dkimValue,
|
value: dkimValue,
|
||||||
domain,
|
domain,
|
||||||
@@ -261,7 +263,7 @@ export class DnsService {
|
|||||||
dnsRecord.errorMessage = error instanceof Error ? error.message : "Unknown error";
|
dnsRecord.errorMessage = error instanceof Error ? error.message : "Unknown error";
|
||||||
await this.em.persistAndFlush(dnsRecord);
|
await this.em.persistAndFlush(dnsRecord);
|
||||||
|
|
||||||
this.logger.error(`DNS verification failed for ${dnsRecord.name}:`, error);
|
this.logger.error(`DNS verification failed for ${dnsRecord.fullName}:`, error);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -271,7 +273,7 @@ export class DnsService {
|
|||||||
*/
|
*/
|
||||||
private async verifyTxtRecord(dnsRecord: DnsRecord) {
|
private async verifyTxtRecord(dnsRecord: DnsRecord) {
|
||||||
try {
|
try {
|
||||||
const txtRecords = await this.resolveTxt(dnsRecord.name);
|
const txtRecords = await this.resolveTxt(dnsRecord.fullName);
|
||||||
const flatRecords = txtRecords.flat();
|
const flatRecords = txtRecords.flat();
|
||||||
|
|
||||||
// Join all TXT record parts together (handles multi-part TXT records like DKIM)
|
// Join all TXT record parts together (handles multi-part TXT records like DKIM)
|
||||||
@@ -290,7 +292,7 @@ export class DnsService {
|
|||||||
*/
|
*/
|
||||||
private async verifyMxRecord(dnsRecord: DnsRecord) {
|
private async verifyMxRecord(dnsRecord: DnsRecord) {
|
||||||
try {
|
try {
|
||||||
const mxRecords = await this.resolveMx(dnsRecord.name);
|
const mxRecords = await this.resolveMx(dnsRecord.fullName);
|
||||||
|
|
||||||
return mxRecords.some((mx) => mx.exchange === dnsRecord.value && mx.priority === (dnsRecord.priority || 10));
|
return mxRecords.some((mx) => mx.exchange === dnsRecord.value && mx.priority === (dnsRecord.priority || 10));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -304,7 +306,7 @@ export class DnsService {
|
|||||||
*/
|
*/
|
||||||
private async verifyCnameRecord(dnsRecord: DnsRecord) {
|
private async verifyCnameRecord(dnsRecord: DnsRecord) {
|
||||||
try {
|
try {
|
||||||
const cnameRecords = await this.resolveCname(dnsRecord.name);
|
const cnameRecords = await this.resolveCname(dnsRecord.fullName);
|
||||||
|
|
||||||
return cnameRecords.includes(dnsRecord.value);
|
return cnameRecords.includes(dnsRecord.value);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -144,7 +144,14 @@ export class DomainsService {
|
|||||||
// const dkimSelector = dkimNameParts.slice(0, 2).join("."); // Takes first two parts: "default._domainkey"
|
// const dkimSelector = dkimNameParts.slice(0, 2).join("."); // Takes first two parts: "default._domainkey"
|
||||||
|
|
||||||
// Create DKIM DNS record
|
// Create DKIM DNS record
|
||||||
await this.dnsService.createDKIMRecord(domain, selector, this.dkimSelectorName, dkimKeys.dnsTxt.value, dkimKeys.wildduckId);
|
await this.dnsService.createDKIMRecord(
|
||||||
|
domain,
|
||||||
|
selector,
|
||||||
|
this.dkimSelectorName,
|
||||||
|
dkimKeys.dnsTxt.name,
|
||||||
|
dkimKeys.dnsTxt.value,
|
||||||
|
dkimKeys.wildduckId,
|
||||||
|
);
|
||||||
|
|
||||||
this.logger.log(`DKIM automatically enabled for domain ${domain.name}`);
|
this.logger.log(`DKIM automatically enabled for domain ${domain.name}`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user