Spaces:
Runtime error
Runtime error
Update server.js
Browse files
server.js
CHANGED
|
@@ -2,7 +2,6 @@ const express = require('express');
|
|
| 2 |
const path = require('path');
|
| 3 |
const fs = require('fs');
|
| 4 |
const crypto = require('crypto');
|
| 5 |
-
const fetch = require('node-fetch');
|
| 6 |
|
| 7 |
console.log(">>> [MODULE] server.js: Initializing...");
|
| 8 |
const app = express();
|
|
@@ -76,17 +75,8 @@ function hasDashboardPermissions(permissionBits) {
|
|
| 76 |
}
|
| 77 |
|
| 78 |
// --- Helper: Get redirect URI ---
|
| 79 |
-
function getRedirectUri(
|
| 80 |
if (REDIRECT_URI) return REDIRECT_URI;
|
| 81 |
-
|
| 82 |
-
// Attempt to detect from request (X-Forwarded-Host is common behind proxies like HF)
|
| 83 |
-
if (req) {
|
| 84 |
-
const protocol = req.headers['x-forwarded-proto'] || req.protocol || 'http';
|
| 85 |
-
const host = req.headers['x-forwarded-host'] || req.get('host');
|
| 86 |
-
if (host) return `${protocol}://${host}/callback`;
|
| 87 |
-
}
|
| 88 |
-
|
| 89 |
-
// Fallback to environment variables
|
| 90 |
const spaceId = process.env.SPACE_ID;
|
| 91 |
if (spaceId) return `https://${spaceId.replace('/', '-').toLowerCase()}.hf.space/callback`;
|
| 92 |
return `http://localhost:${port}/callback`;
|
|
@@ -148,22 +138,18 @@ app.get('/healthz', (req, res) => {
|
|
| 148 |
// --- Discord OAuth2: Step 1 — Redirect to Discord ---
|
| 149 |
app.get('/auth/login', (req, res) => {
|
| 150 |
const clientId = getClientId();
|
| 151 |
-
const redirect = encodeURIComponent(getRedirectUri(
|
| 152 |
const url = `https://discord.com/api/oauth2/authorize?client_id=${clientId}&redirect_uri=${redirect}&response_type=code&scope=identify%20guilds`;
|
| 153 |
res.redirect(url);
|
| 154 |
});
|
| 155 |
|
| 156 |
async function fetchWithRetry(url, options, retries = 2) {
|
| 157 |
-
const https = require('https');
|
| 158 |
-
const agent = new https.Agent({ rejectUnauthorized: false });
|
| 159 |
-
|
| 160 |
for (let i = 0; i <= retries; i++) {
|
| 161 |
try {
|
| 162 |
let targetUrl = url;
|
| 163 |
// Use direct connection to Discord API
|
| 164 |
if (url.includes('discord.com/api')) {
|
| 165 |
-
targetUrl = url;
|
| 166 |
-
options.agent = agent; // Apply the agent here
|
| 167 |
if (i > 0) console.log(`[Dashboard] Retrying Discord API call (${i + 1}/${retries})...`);
|
| 168 |
}
|
| 169 |
|
|
@@ -201,7 +187,7 @@ async function handleOAuthCallback(req, res) {
|
|
| 201 |
client_secret: CLIENT_SECRET,
|
| 202 |
grant_type: 'authorization_code',
|
| 203 |
code,
|
| 204 |
-
redirect_uri: getRedirectUri(
|
| 205 |
})
|
| 206 |
});
|
| 207 |
const tokenData = await tokenRes.json();
|
|
@@ -517,7 +503,7 @@ function startServerInternal(client, cmdsData) {
|
|
| 517 |
|
| 518 |
app.listen(port, '0.0.0.0', () => {
|
| 519 |
console.log(`[*] Web Server running on port ${port}`);
|
| 520 |
-
console.log(`[*] Dashboard: ${getRedirectUri().replace('/callback', '')}`);
|
| 521 |
});
|
| 522 |
}
|
| 523 |
|
|
|
|
| 2 |
const path = require('path');
|
| 3 |
const fs = require('fs');
|
| 4 |
const crypto = require('crypto');
|
|
|
|
| 5 |
|
| 6 |
console.log(">>> [MODULE] server.js: Initializing...");
|
| 7 |
const app = express();
|
|
|
|
| 75 |
}
|
| 76 |
|
| 77 |
// --- Helper: Get redirect URI ---
|
| 78 |
+
function getRedirectUri() {
|
| 79 |
if (REDIRECT_URI) return REDIRECT_URI;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
const spaceId = process.env.SPACE_ID;
|
| 81 |
if (spaceId) return `https://${spaceId.replace('/', '-').toLowerCase()}.hf.space/callback`;
|
| 82 |
return `http://localhost:${port}/callback`;
|
|
|
|
| 138 |
// --- Discord OAuth2: Step 1 — Redirect to Discord ---
|
| 139 |
app.get('/auth/login', (req, res) => {
|
| 140 |
const clientId = getClientId();
|
| 141 |
+
const redirect = encodeURIComponent(getRedirectUri());
|
| 142 |
const url = `https://discord.com/api/oauth2/authorize?client_id=${clientId}&redirect_uri=${redirect}&response_type=code&scope=identify%20guilds`;
|
| 143 |
res.redirect(url);
|
| 144 |
});
|
| 145 |
|
| 146 |
async function fetchWithRetry(url, options, retries = 2) {
|
|
|
|
|
|
|
|
|
|
| 147 |
for (let i = 0; i <= retries; i++) {
|
| 148 |
try {
|
| 149 |
let targetUrl = url;
|
| 150 |
// Use direct connection to Discord API
|
| 151 |
if (url.includes('discord.com/api')) {
|
| 152 |
+
targetUrl = url; // Keep direct connection - no endpoint rotation
|
|
|
|
| 153 |
if (i > 0) console.log(`[Dashboard] Retrying Discord API call (${i + 1}/${retries})...`);
|
| 154 |
}
|
| 155 |
|
|
|
|
| 187 |
client_secret: CLIENT_SECRET,
|
| 188 |
grant_type: 'authorization_code',
|
| 189 |
code,
|
| 190 |
+
redirect_uri: getRedirectUri()
|
| 191 |
})
|
| 192 |
});
|
| 193 |
const tokenData = await tokenRes.json();
|
|
|
|
| 503 |
|
| 504 |
app.listen(port, '0.0.0.0', () => {
|
| 505 |
console.log(`[*] Web Server running on port ${port}`);
|
| 506 |
+
console.log(`[*] Dashboard: ${getRedirectUri().replace('/auth/callback', '')}`);
|
| 507 |
});
|
| 508 |
}
|
| 509 |
|