-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocessDns.js
104 lines (87 loc) · 3.56 KB
/
processDns.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const fs = require('fs');
const path = require('path');
const axios = require('axios');
const CLOUDFLARE_API_URL = `https://api.cloudflare.com/client/v4/zones/${process.env.CLOUDFLARE_ZONE_ID}/dns_records`;
// Read and process all DNS records from files
async function processPullRequest() {
const recordsDir = path.join(__dirname, 'records');
const files = fs.readdirSync(recordsDir);
for (const file of files) {
const filePath = path.join(recordsDir, file);
const content = fs.readFileSync(filePath, 'utf-8').trim();
if (content) {
const records = parseDNSRecords(content);
for (const record of records) {
console.log(`Processing record: ${JSON.stringify(record)}`);
await addDNSRecord(file.replace('.txt', ''), record);
}
}
}
}
// Parse DNS records from file content
function parseDNSRecords(content) {
return content.split('\n').map(line => {
const parts = line.split(' ').filter(part => part);
const type = parts[0];
const value = parts.slice(1, -1).join(' '); // Join all but the last part for the value
const priority = (parts.length > 2 && type === 'MX') ? parseInt(parts[parts.length - 1]) : null;
return { type, value, priority };
});
}
// Add a DNS record to Cloudflare
async function addDNSRecord(subdomain, record) {
// Skip if the value is empty for A, CNAME, or AAAA records
if ((record.type === 'A' || record.type === 'CNAME' || record.type === 'AAAA') && !record.value) {
console.error(`Invalid DNS record in ${subdomain}.txt: ${JSON.stringify(record)}`);
return;
}
const data = {
type: record.type,
name: `${subdomain}.is-cod.in`,
ttl: 1,
proxied: false,
content: record.value
};
if (record.type === 'MX' && record.priority !== null) {
data.priority = record.priority;
}
console.log(`Checking for existing records for: ${data.name} (${record.type})`);
try {
const existingRecordsResponse = await axios.get(CLOUDFLARE_API_URL, {
headers: {
'Authorization': `Bearer ${process.env.CLOUDFLARE_API_TOKEN}`,
'Content-Type': 'application/json'
},
params: {
name: data.name,
type: record.type
}
});
const existingRecords = existingRecordsResponse.data.result;
if (existingRecords.length > 0) {
console.log(`Record already exists: ${JSON.stringify(existingRecords)}`);
return; // Skip adding if it already exists
}
} catch (error) {
console.error(`Error checking existing records: ${error.response ? JSON.stringify(error.response.data) : error.message}`);
return;
}
console.log(`Sending to Cloudflare: ${JSON.stringify(data)}`);
try {
const response = await axios.post(CLOUDFLARE_API_URL, data, {
headers: {
'Authorization': `Bearer ${process.env.CLOUDFLARE_API_TOKEN}`,
'Content-Type': 'application/json'
}
});
if (response.data.success) {
console.log(`Successfully added DNS record: ${JSON.stringify(response.data.result)}`);
} else {
console.error(`Error adding DNS record: ${JSON.stringify(response.data.errors)}`);
}
} catch (error) {
console.error(`Error adding DNS record: ${error.response ? JSON.stringify(error.response.data) : error.message}`);
}
}
// Start the process
processPullRequest();