-
Notifications
You must be signed in to change notification settings - Fork 0
/
runnode.js
84 lines (71 loc) · 3.01 KB
/
runnode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#! /usr/bin/env node
"use strict";
const fs = require('fs');
const axios = require('axios');
require('dotenv').config();
console.log("Current directory:", __dirname);
const getPublicIP = async () => {
console.debug("Attempting to fetch public IP...");
try {
const response = await axios.get('https://icanhazip.com');
console.debug("Public IP fetched successfully:", response.data.trim());
return response.data.trim(); // Remove any extra whitespace/newline
} catch (error) {
console.error("Error fetching public IP:", error.message);
return null; // Return null in case of error
}
};
const registerWebhook = async (subdomain, publicIp) => {
const webhookUrl = process.env.WEBHOOK_URL; // Get the webhook URL from environment variables
console.debug("Preparing to register webhook for subdomain:", subdomain.name);
// Prepare data for registration
const data = {
name: subdomain, // Subdomain name from the config
host: publicIp, // Use the public IP address
};
const token = process.env.BEARER_TOKEN; // Get Bearer token from environment variables
console.debug("Using webhook URL:", webhookUrl);
console.debug("Using Bearer token:", token ? "Provided" : "Not provided");
try {
const response = await axios.post(webhookUrl, data, {
headers: {
Authorization: `Bearer ${token}`, // Set the Authorization header
}
});
console.log(`Registration successful for ${subdomain.name}:`, response.data);
} catch (error) {
console.error(`Error during registration for ${subdomain.name}:`, error.response ? error.response.data : error.message);
}
};
const run = async () => {
console.debug("Loading hyperconfig from JSON file...");
const hyperconfig = JSON.parse(fs.readFileSync('./hyperconfig.json'));
console.debug("Hyperconfig loaded:", hyperconfig);
// Get the public IP address
const publicIp = await getPublicIP();
// If we couldn't get an IP, exit
if (!publicIp) {
console.error("Could not retrieve public IP address. Exiting...");
return;
}
// Register each configuration initially
for (const subdomain of hyperconfig) {
console.debug("Registering subdomain:", subdomain);
await registerWebhook(subdomain, publicIp);
}
// Periodically re-register each subdomain every minute
setInterval(async () => {
console.log("Re-registering...");
const newPublicIp = await getPublicIP(); // Fetch the public IP again
if (newPublicIp) {
console.debug("New public IP retrieved:", newPublicIp);
for (const subdomain of hyperconfig) {
console.debug("Re-registering subdomain:", subdomain.name);
await registerWebhook(subdomain, newPublicIp);
}
} else {
console.error("Could not retrieve public IP address during re-registration.");
}
}, 60 * 1000); // 60 * 1000 milliseconds = 1 minute
};
run();