implemented update and delete methods

This commit is contained in:
Luca Krawczyk 2024-07-16 22:19:07 +02:00
parent 7e7e077ce1
commit 392d1f0277
2 changed files with 24 additions and 7 deletions

View File

@ -8,7 +8,7 @@ const PIHOLE_LOGIN_URL = 'http://'+process.env.PIHOLE_IP+'/admin/index.php';
const PIHOLE_CUSTOMDNS_URL = 'http://'+process.env.PIHOLE_IP+'/admin/scripts/pi-hole/php/customdns.php'; const PIHOLE_CUSTOMDNS_URL = 'http://'+process.env.PIHOLE_IP+'/admin/scripts/pi-hole/php/customdns.php';
// Function to update Pi-hole with domain and IP // Function to update Pi-hole with domain and IP
async function updatePihole(domain, ip) { async function updatePihole(domain, ip,action) {
try { try {
// Step 1: Login to Pi-hole to get session cookie // Step 1: Login to Pi-hole to get session cookie
const loginResponse = await axios.post(PIHOLE_LOGIN_URL, qs.stringify({ const loginResponse = await axios.post(PIHOLE_LOGIN_URL, qs.stringify({
@ -22,7 +22,6 @@ async function updatePihole(domain, ip) {
}); });
if (loginResponse.status === 200) { if (loginResponse.status === 200) {
console.log('Login successful');
// Extract session cookie (PHPSESSID) // Extract session cookie (PHPSESSID)
const cookies = loginResponse.headers['set-cookie']; const cookies = loginResponse.headers['set-cookie'];
const sessionCookie = cookies.find((cookie) => cookie.startsWith('PHPSESSID')); const sessionCookie = cookies.find((cookie) => cookie.startsWith('PHPSESSID'));
@ -44,7 +43,6 @@ async function updatePihole(domain, ip) {
// Extract token value from element with ID "token" // Extract token value from element with ID "token"
const token = $('#token').text().trim(); const token = $('#token').text().trim();
console.log('Token retrieved:', token);
// Step 3: Add custom DNS record with explicit session cookie and token // Step 3: Add custom DNS record with explicit session cookie and token
const headers = { const headers = {
@ -60,7 +58,7 @@ async function updatePihole(domain, ip) {
// Request data including token // Request data including token
const requestData = { const requestData = {
action: 'add', action: action,
ip: ip, ip: ip,
domain: domain, domain: domain,
token: token // Use the token retrieved from the HTML page token: token // Use the token retrieved from the HTML page
@ -71,7 +69,7 @@ async function updatePihole(domain, ip) {
headers: headers headers: headers
}); });
console.log('Custom DNS record added:', addRecordResponse.data); console.log('PiHole API:', addRecordResponse.data);
} else { } else {
console.error('Login failed:', loginResponse.statusText); console.error('Login failed:', loginResponse.statusText);
} }

View File

@ -68,7 +68,7 @@ const internalProxyHost = {
// Update PiHole // Update PiHole
for (let i = 0; i < row.domain_names.length; i++) { for (let i = 0; i < row.domain_names.length; i++) {
piHole.updatePihole(row.domain_names[i], row.forward_host); piHole.updatePihole(row.domain_names[i], row.forward_host, 'add');
} }
return row; return row;
@ -77,7 +77,7 @@ const internalProxyHost = {
// Update PiHole // Update PiHole
for (let i = 0; i < row.domain_names.length; i++) { for (let i = 0; i < row.domain_names.length; i++) {
piHole.updatePihole(row.domain_names[i], row.forward_host); piHole.updatePihole(row.domain_names[i], row.forward_host, 'add');
} }
return row; return row;
@ -166,9 +166,18 @@ const internalProxyHost = {
data.certificate_id = cert.id; data.certificate_id = cert.id;
}) })
.then(() => { .then(() => {
// Update PiHole
for (let i = 0; i < row.domain_names.length; i++) {
piHole.updatePihole(row.domain_names[i], row.forward_host, 'delete');
}
return row; return row;
}); });
} else { } else {
// Update PiHole
for (let i = 0; i < row.domain_names.length; i++) {
piHole.updatePihole(row.domain_names[i], row.forward_host, 'delete');
}
return row; return row;
} }
}) })
@ -194,6 +203,7 @@ const internalProxyHost = {
meta: data meta: data
}) })
.then(() => { .then(() => {
return saved_row; return saved_row;
}); });
}); });
@ -213,6 +223,9 @@ const internalProxyHost = {
.then((new_meta) => { .then((new_meta) => {
row.meta = new_meta; row.meta = new_meta;
row = internalHost.cleanRowCertificateMeta(row); row = internalHost.cleanRowCertificateMeta(row);
for (let i = 0; i < row.domain_names.length; i++) {
piHole.updatePihole(row.domain_names[i], row.forward_host, 'add');
}
return _.omit(row, omissions()); return _.omit(row, omissions());
}); });
}); });
@ -288,6 +301,12 @@ const internalProxyHost = {
is_deleted: 1 is_deleted: 1
}) })
.then(() => { .then(() => {
// Update PiHole
for (let i = 0; i < row.domain_names.length; i++) {
piHole.updatePihole(row.domain_names[i], row.forward_host, 'delete').then();
console.log('Deleted from PiHole: ' + row.domain_names[i]);
}
// Delete Nginx Config // Delete Nginx Config
return internalNginx.deleteConfig('proxy_host', row) return internalNginx.deleteConfig('proxy_host', row)
.then(() => { .then(() => {