Compare commits

..

No commits in common. "c0598c582740d103751b8b65854ff57311708143" and "7e7e077ce1bb9fc324c650d478b069099b13ccdb" have entirely different histories.

3 changed files with 11 additions and 36 deletions

View File

@ -2,17 +2,13 @@ const axios = require('axios');
const cheerio = require('cheerio'); const cheerio = require('cheerio');
const qs = require('querystring'); const qs = require('querystring');
const PIHOLE_PLUGIN_ENABLED = process.env.PIHOLE_PLUGIN_ENABLED === 'true';
const PIHOLE_PASSWORD = process.env.PIHOLE_PASSWORD; const PIHOLE_PASSWORD = process.env.PIHOLE_PASSWORD;
const PIHOLE_LOGIN_URL = 'http://'+process.env.PIHOLE_IP+'/admin/index.php'; 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, action) { async function updatePihole(domain, ip) {
// Check if the Pi-hole plugin is enabled
if (!PIHOLE_PLUGIN_ENABLED) {
return;
}
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({
@ -26,6 +22,7 @@ async function updatePihole(domain, ip, action) {
}); });
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'));
@ -47,6 +44,7 @@ async function updatePihole(domain, ip, action) {
// 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 = {
@ -62,7 +60,7 @@ async function updatePihole(domain, ip, action) {
// Request data including token // Request data including token
const requestData = { const requestData = {
action: action, action: 'add',
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
@ -73,7 +71,7 @@ async function updatePihole(domain, ip, action) {
headers: headers headers: headers
}); });
console.log('PiHole API:', addRecordResponse.data); console.log('Custom DNS record added:', 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, 'add').then(); piHole.updatePihole(row.domain_names[i], row.forward_host);
} }
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, 'add').then(); piHole.updatePihole(row.domain_names[i], row.forward_host);
} }
return row; return row;
@ -166,18 +166,9 @@ 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').then();
}
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').then();
}
return row; return row;
} }
}) })
@ -203,7 +194,6 @@ const internalProxyHost = {
meta: data meta: data
}) })
.then(() => { .then(() => {
return saved_row; return saved_row;
}); });
}); });
@ -223,9 +213,6 @@ 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').then();
}
return _.omit(row, omissions()); return _.omit(row, omissions());
}); });
}); });
@ -301,11 +288,6 @@ 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();
}
// Delete Nginx Config // Delete Nginx Config
return internalNginx.deleteConfig('proxy_host', row) return internalNginx.deleteConfig('proxy_host', row)
.then(() => { .then(() => {

View File

@ -29,11 +29,6 @@ services:
DB_MYSQL_NAME: 'npm' DB_MYSQL_NAME: 'npm'
# DB_SQLITE_FILE: "/data/database.sqlite" # DB_SQLITE_FILE: "/data/database.sqlite"
# DISABLE_IPV6: "true" # DISABLE_IPV6: "true"
# pihole:
# PIHOLE_PLUGIN_ENABLED: 'true'
# PIHOLE_PASSWORD: 'password'
# PIHOLE_IP: '192.168.10.2'
volumes: volumes:
- npm_data:/data - npm_data:/data
- le_data:/etc/letsencrypt - le_data:/etc/letsencrypt