Add better logging/Other stuff #3

Merged
ChrisChrome merged 6 commits from dev into main 2026-02-18 13:46:00 -07:00
5 changed files with 86 additions and 3 deletions
Showing only changes of commit 86fd430d46 - Show all commits

View file

@ -1031,10 +1031,10 @@ app.get("/api/healthcheck", (req, res) => {
}); });
// logCall function (caller, callee) // logCall function (caller, callee)
const logCall = (caller, callee) => { const logCall = (caller, callee, srcIp) => {
pool.getConnection().then(conn => { pool.getConnection().then(conn => {
conn.query('INSERT INTO callLogs (caller, callee, timestamp) VALUES (?, ?, ?)', conn.query('INSERT INTO callLogs (caller, callee, timestamp, srcIp) VALUES (?, ?, ?, ?)',
[caller, callee, Math.floor(Date.now())]).catch(err => { [caller, callee, Math.floor(Date.now()), srcIp]).catch(err => {
console.error('Error logging call:', err); console.error('Error logging call:', err);
}).finally(() => { }).finally(() => {
conn.release(); conn.release();
@ -1043,6 +1043,7 @@ const logCall = (caller, callee) => {
} }
const genCall = (req, res, apiKey, ani, number) => { const genCall = (req, res, apiKey, ani, number) => {
const srcIp = process.env.PROXY_HEADER ? req.headers[process.env.PROXY_HEADER] : req.remoteAddress;
pool.getConnection().then(conn => { pool.getConnection().then(conn => {
//conn.query("SELECT * FROM routes WHERE apiKey = ? AND block_start <= ? AND block_start + block_length >= ?", [apiKey, ani, ani]).then((rows) => { //conn.query("SELECT * FROM routes WHERE apiKey = ? AND block_start <= ? AND block_start + block_length >= ?", [apiKey, ani, ani]).then((rows) => {
conn.query("SELECT * FROM routes WHERE apiKey = ?", [apiKey]).then((rows) => { // We'll try this Nick, if it doesn't work we'll go back to the original conn.query("SELECT * FROM routes WHERE apiKey = ?", [apiKey]).then((rows) => { // We'll try this Nick, if it doesn't work we'll go back to the original

View file

@ -0,0 +1 @@
ALTER TABLE callLogs ADD COLUMN srcIp VARCHAR(255) NOT NULL DEFAULT 'unknown';

View file

@ -0,0 +1,5 @@
[Unit]
Description=Rotate AstroCom Secret
[Service]
ExecStart=/usr/bin/astrocom_rotate.sh
Type=oneshot

View file

@ -0,0 +1,7 @@
[Unit]
Description=Rotate AstroCom Secret
[Timer]
# This will run daily at 0200 local time
OnCalendar=*-*-* 02:00:00
[Install]
WantedBy=timers.target

View file

@ -0,0 +1,69 @@
#!/bin/bash
# --- USER CONFIG ---
IAXBLOCK="from-astrocom"
CONF="/etc/asterisk/iax_custom.conf"
# Astrocom API endpoint info
ASTROCOM_URL="https://astrocom.tel/api/v1/user/update" # Shouldn't need to change this!
ASTROCOM_TOKEN=""
# -------------------
# Parse flags
DRYRUN=0
if [[ "$1" == "--dry-run" || "$1" == "-n" ]]; then
DRYRUN=1
fi
# Generate a 32-char alphanumeric secret
NEWSECRET=$(tr -dc 'A-Za-z0-9' </dev/urandom | head -c 32)
# Escape block name for sed search
ESC_BLOCK=$(printf '%s\n' "$IAXBLOCK" | sed 's/[]\/$*.^[]/\\&/g')
# Sed script to modify only the first secret= inside the block
SED_SCRIPT="
/^\[$ESC_BLOCK\]/,/^\[/{
/^\[/!{
s/^secret=.*/secret=$NEWSECRET/
t done
}
}
: done
"
if [[ $DRYRUN -eq 1 ]]; then
echo "=== DRY RUN: Showing updated output only ==="
sed "$SED_SCRIPT" "$CONF"
exit 0
fi
#
# REAL UPDATE EXECUTION
#
echo "Updating secret in file..."
sed -i "$SED_SCRIPT" "$CONF" || { echo "Error updating $CONF"; exit 1; }
echo "New secret: $NEWSECRET"
#
# ASTROCOM API REQUEST
#
echo "Sending updated secret to Astrocom..."
# ----- EDIT THIS TO MATCH YOUR REAL API FORMAT -----
curl -X PATCH "$ASTROCOM_URL" \
-H "Authorization: Bearer $ASTROCOM_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"secret\": \"$NEWSECRET\"}" \
|| echo "Warning: Astrocom API call failed"
# ---------------------------------------------------
#
# RELOAD ASTERISK
#
echo "Reloading Asterisk..."
asterisk -x "core reload"
echo "Update complete."