Skip to content

Commit 3a2e7c6

Browse files
committed
check if redirect to stop loops
1 parent 540ed96 commit 3a2e7c6

3 files changed

Lines changed: 15 additions & 13 deletions

File tree

src/app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ app.set("view engine", "ejs")
1919
app.set("trust proxy", true)
2020

2121
app.use((req, res, next) => {
22+
res.setHeader("X-Redirect-Service", "1")
23+
2224
log.logger.info(`${req.method} ${req.path} ${req.query}`)
2325
next()
2426
})

src/wol.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,22 +158,22 @@ async function trySendWakeupPackets(hosts, wolUrl) {
158158
return { output, err }
159159
}
160160

161-
async function waitForHostUp(
162-
url,
163-
{ interval = 3000, timeout = 60000, okOnly = true } = {}
164-
) {
161+
async function isRealDevice(res) {
162+
return !res.headers.get("X-Redirect-Service")
163+
}
164+
165+
async function waitForHostUp(url, options = {}) {
166+
const { interval = 3000, timeout = 60000 } = options
165167
const start = Date.now()
166168

167169
while (Date.now() - start < timeout) {
168170
try {
169-
const res = await fetch(url, { method: "GET" })
171+
const res = await fetch(url)
170172

171-
if (okOnly) {
172-
if (res.ok) return true
173-
} else {
173+
if (res.ok && (await isRealDevice(res))) {
174174
return true
175175
}
176-
} catch (_) {}
176+
} catch {}
177177

178178
await new Promise((r) => setTimeout(r, interval))
179179
}
@@ -270,7 +270,7 @@ async function startProcessing(req, res) {
270270
const dockerRes = await post(ENV.woldURL, { query })
271271

272272
if (dockerRes?.output) {
273-
if (ws && ws.readyState === WebSocket.OPEN) {
273+
if (ws.readyState === WebSocket.OPEN) {
274274
ws.send(
275275
JSON.stringify({
276276
error: err,
@@ -290,7 +290,7 @@ async function startProcessing(req, res) {
290290
ws.send(
291291
JSON.stringify({
292292
error: err,
293-
message: "Timeout waiting for Service",
293+
message: "Timeout waiting for service",
294294
})
295295
)
296296
}

views/home.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,13 @@
302302
}
303303
304304
if (!msg.error && msg.url) {
305-
logEl.textContent += "Device is online! Redirecting...\n"
305+
logEl.textContent += "Service is online! Redirecting...\n"
306306
ws.close()
307307
window.location.href = msg.url
308308
}
309309
310310
if (msg.error) {
311-
logEl.textContent += "Error occurred while starting device.\n"
311+
logEl.textContent += "Error occurred while starting service.\n"
312312
ws.close()
313313
handleError()
314314
}

0 commit comments

Comments
 (0)