-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c9a54c
commit 3a43201
Showing
7 changed files
with
166 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
import type { Application } from "express-ws"; | ||
import { GameServer } from ".."; | ||
import { SimpleEventBus } from "../TypeHelper"; | ||
import { ModifiableData } from "@board-bound/sdk"; | ||
import type { Application } from 'express-ws' | ||
import { GameServer } from '..' | ||
import { SimpleEventBus } from '../TypeHelper' | ||
import { ModifiableData } from '@board-bound/sdk' | ||
|
||
export function installStatusHandler(app: Application, server: GameServer, bus: SimpleEventBus) { | ||
app.get("/status", async (req, res) => { | ||
server.getLogger().info("Status check from " + req.ip); | ||
const response = new ModifiableData({ status: "ok" }); | ||
const success = await bus.emit("serverStatusRequest", server, { ip: req.ip, response }) | ||
if (!success) res.status(560).json({ error: "Request canceled" }); | ||
else res.json(response.get()); | ||
}); | ||
export function installStatusHandler( | ||
app: Application, | ||
server: GameServer, | ||
bus: SimpleEventBus | ||
) { | ||
app.get('/status', async (req, res) => { | ||
server.getLogger().info('Status check from ' + req.ip) | ||
const response = new ModifiableData({ status: 'ok' }) | ||
const success = await bus.emit('serverStatusRequest', server, { | ||
ip: req.ip, | ||
response, | ||
}) | ||
if (!success) res.status(560).json({ error: 'Request canceled' }) | ||
else res.json(response.get()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,51 @@ | ||
import os from 'os'; | ||
import axios from 'axios'; | ||
import os from 'os' | ||
import axios from 'axios' | ||
|
||
export function getAllLocalIPAddresses(): string[] { | ||
const interfaces = os.networkInterfaces(); | ||
const addresses: string[] = []; | ||
const interfaces = os.networkInterfaces() | ||
const addresses: string[] = [] | ||
|
||
for (const name in interfaces) { | ||
const iface = interfaces[name]; | ||
if (!iface) continue; | ||
const iface = interfaces[name] | ||
if (!iface) continue | ||
|
||
for (const alias of iface) { | ||
if (alias.family !== 'IPv4' || alias.internal) continue; | ||
addresses.push(alias.address); | ||
if (alias.family !== 'IPv4' || alias.internal) continue | ||
addresses.push(alias.address) | ||
} | ||
} | ||
|
||
return addresses; | ||
return addresses | ||
} | ||
|
||
export function getLocalIPAddress(): string | null { | ||
const interfaces = os.networkInterfaces(); | ||
const interfaces = os.networkInterfaces() | ||
|
||
for (const name in interfaces) { | ||
const iface = interfaces[name]; | ||
if (!iface) continue; | ||
const iface = interfaces[name] | ||
if (!iface) continue | ||
|
||
for (const alias of iface) { | ||
if (alias.family === 'IPv4' && !alias.internal) { | ||
return alias.address; | ||
return alias.address | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
return null | ||
} | ||
|
||
export async function getPublicIPAddress(): Promise<string> { | ||
try { | ||
const response = await axios.get('https://api.ipify.org', { | ||
params: { format: 'json' }, | ||
}); | ||
}) | ||
|
||
const ip = response.data?.ip; | ||
if (!ip) throw new Error('Invalid response from IP service.'); | ||
const ip = response.data?.ip | ||
if (!ip) throw new Error('Invalid response from IP service.') | ||
|
||
return ip; | ||
return ip | ||
} catch (error) { | ||
throw new Error(`Error fetching public IP address: ${error.message}`); | ||
throw new Error(`Error fetching public IP address: ${error.message}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.