Files
proxy/nodejs/app.js
T
wmantly a2d194f855 Add dynamic DNS: keep A records pointed at the current public IP
For deployments on WAN DHCP, operators can declare A records in the DNS section
that the app updates to this box's current public IP every 4 hours (and
immediately on create).

- utils/public_ip.js: getPublicIp() queries external echo services (ipify +
  fallbacks, configurable) with pure isIPv4/extractIp helpers.
- utils/dns_records.js: pure planARecordUpdate() reconciliation decision.
- models/dns_provider.js: Domain.upsertARecord(name, ip) — provider-agnostic
  upsert via getRecords + deleteRecordById + createRecord (createRecord alone is
  not a reliable cross-provider upsert). Apex ('@') handling added to each
  provider (CloudFlare uses the domain name, Porkbun an empty name, DigitalOcean
  '@') via a new DnsApi.apexName().
- models/dynamic_record.js: DynamicRecord model (deterministic id per host,
  apply()/refreshAll()), registered + ModelPs-wrapped for live UI updates.
- services/dynamic_dns.js + conf: 4h scheduler mirroring host_scheduler.
- routes/dns.js: /dynamic CRUD + /dynamic/ip, gated to domain managers/admins.
- views/dns.ejs: "Dynamic A Records (WAN IP)" card with add form + list.
- test/unit/dynamic_record.test.js: public-IP parsing + reconciliation logic.

Verified end-to-end against a live Porkbun domain (create, idempotent, IP-change,
cleanup) plus unit suite (111 pass).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-10 23:55:56 -04:00

96 lines
2.8 KiB
JavaScript
Executable File

'use strict';
const path = require('path');
const ejs = require('ejs')
const express = require('express');
// Set up the express app.
const app = express();
// The app always runs behind the OpenResty reverse proxy (a single hop) which
// sets X-Real-IP / X-Forwarded-For. Trust that one proxy so req.ip reflects the
// real client — needed for correct per-client rate limiting on /api/auth.
app.set('trust proxy', 1);
// Hold list of functions to run when the server is ready
app.onListen = [];
// Allow the express app to be exported into other files.
module.exports = app;
// Hold onto the auth middleware
const middleware = require('./middleware/auth');
// Grab the projects PubSub
app.contoller = require('./controller');
/**
* Start background services
* These services run independently of the HTTP server:
* - host_lookup: Unix socket server for OpenResty host lookups
* - host_scheduler: Scheduled tasks for wildcard cert renewal
*/
require('./services/host_lookup');
require('./services/host_scheduler');
require('./services/dynamic_dns');
// Push pubsub over the socket and back.
app.onListen.push(function(){
app.io.use(middleware.authIO);
app.contoller.ps.subscribe(/./g, function(data, topic){
app.io.emit('P2PSub', { topic, data });
});
app.io.on('connection', (socket) => {
// console.log('socket', socket)
var user = socket.user;
socket.on('P2PSub', (msg) => {
app.contoller.ps.publish(msg.topic, {...msg.data, __from:socket.user});
// socket.broadcast.emit('P2PSub', msg);
});
});
});
// load the JSON parser middleware. Express will parse JSON into native objects
// for any request that has JSON in its content type.
app.use(express.json());
// Set up the templating engine to build HTML for the front end.
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// Routes for front end content.
app.use('/', require('./routes/render'));
// Routes for API
app.use('/api', require('./routes/api'));
// Catch 404 and forward to error handler. If none of the above routes are
// used, this is what will be called.
app.use(async function(req, res, next) {
try{
var err = new Error('Not Found');
err.message = 'Page not found'
err.status = 404;
next(err);
}catch(error){
console.log('app 404 catch error', error)
}
});
// Error handler. This is where `next()` will go on error
app.use(async function(err, req, res, next) {
try{
console.error(err.status || res.status, err.name, req.method, req.url);
console.error(err.message);
console.error(err.stack);
console.error('=========================================');
res.status(err.status || 500);
res.json({name: err.name, message: err.message, keys: err.keys});
}catch(error){
console.log('error in the catch all error fn....', error);
}
});