Add API to script updating individual servers user-side

This commit is contained in:
Christopher Cookman 2025-10-03 22:04:13 -06:00
parent a4b846f379
commit b4bd93c57d
2 changed files with 48 additions and 0 deletions

View file

@ -590,6 +590,32 @@ app.put('/api/v1/user/route', (req, res) => { // Update route
}); });
}); });
app.patch('/api/v1/user/update', async (req, res) => { // Update users server, port, auth, or secret via API key instead of session. Used for automated scripts
const apiKey = req.headers['authorization'];
if (!apiKey) {
res.status(401).json({ error: 'Unauthorized' });
return;
}
const oldData = await pool.query("SELECT * FROM routes WHERE apiKey = ?", [apiKey]);
if (!oldData || oldData.length === 0) {
res.status(401).json({ error: 'Unauthorized' });
return;
}
const row = oldData[0];
const server = req.body.server || row.server;
const port = req.body.port || row.port;
const auth = req.body.auth || row.auth;
const secret = req.body.secret || row.secret;
const updateData = await pool.query('UPDATE routes SET server = ?, port = ?, auth = ?, secret = ? WHERE apiKey = ?',
[server, port, auth, secret, apiKey]);
if (updateData.affectedRows === 1) {
res.json({ message: 'Updated' });
} else {
res.status(500).json({ error: 'Internal server error' });
}
});
app.get('/api/v1/user/directory', (req, res) => { // Get directory entries created by user app.get('/api/v1/user/directory', (req, res) => { // Get directory entries created by user
if (!req.session.userAuthenticated) { if (!req.session.userAuthenticated) {
res.status(401).json({ error: 'Unauthorized' }); res.status(401).json({ error: 'Unauthorized' });

View file

@ -0,0 +1,22 @@
#!/bin/bash
# AstroCom Dynamic IP Update Script
# Gets current public IP from https://myip.wtf/text and posts it to the AstroCom API
# Requires: curl
# Configuration
API_KEY="your_api_key_here" # Replace with your AstroCom API Key!
# Get current IP
CURRENT_IP=$(curl -s https://myip.wtf/text)
if [[ -z "$CURRENT_IP" ]]; then
echo "Failed to retrieve current IP address."
exit 1
fi
echo "Current IP: $CURRENT_IP"
# Update IP via AstroCom API PATCH https://astrocom.tel/api/v1/user/update; JSON body: {"server": "current_ip"}
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -X PATCH https://astrocom.tel/api/v1/user/update \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d "{\"server\": \"$CURRENT_IP\"}"