serve hashedfavicon path as png

This commit is contained in:
Nick Krecklow 2020-05-08 05:05:46 -05:00
parent 82e8db0128
commit 15814cf86b
No known key found for this signature in database
GPG Key ID: 5F149FDE156FFA94
2 changed files with 10 additions and 3 deletions

@ -6,6 +6,9 @@ const serveStatic = require('serve-static')
const logger = require('./logger') const logger = require('./logger')
const HASHED_FAVICON_URL = '/hashedfavicon_'
const HASHED_FAVICON_EXTENSION = '.png'
function getRemoteAddr (req) { function getRemoteAddr (req) {
return req.headers['cf-connecting-ip'] || req.headers['x-forwarded-for'] || req.connection.remoteAddress return req.headers['cf-connecting-ip'] || req.headers['x-forwarded-for'] || req.connection.remoteAddress
} }
@ -25,7 +28,7 @@ class Server {
this._http = http.createServer((req, res) => { this._http = http.createServer((req, res) => {
logger.log('info', '%s requested: %s', getRemoteAddr(req), req.url) logger.log('info', '%s requested: %s', getRemoteAddr(req), req.url)
if (req.url.startsWith('/hashedfavicon?')) { if (req.url.startsWith(HASHED_FAVICON_URL) && req.url.endsWith(HASHED_FAVICON_EXTENSION)) {
this.handleFaviconRequest(req, res) this.handleFaviconRequest(req, res)
} else { } else {
// Attempt to handle req using distServeStatic, otherwise fail over to faviconServeStatic // Attempt to handle req using distServeStatic, otherwise fail over to faviconServeStatic
@ -38,7 +41,8 @@ class Server {
} }
handleFaviconRequest = (req, res) => { handleFaviconRequest = (req, res) => {
const hash = req.url.split('?')[1] let hash = req.url.substring(HASHED_FAVICON_URL.length)
hash = hash.substring(0, hash.length - HASHED_FAVICON_EXTENSION.length)
for (const serverRegistration of this._app.serverRegistrations) { for (const serverRegistration of this._app.serverRegistrations) {
if (serverRegistration.faviconHash && serverRegistration.faviconHash === hash) { if (serverRegistration.faviconHash && serverRegistration.faviconHash === hash) {

@ -5,6 +5,9 @@ const TimeTracker = require('./time')
const config = require('../config') const config = require('../config')
const minecraftVersions = require('../minecraft_versions') const minecraftVersions = require('../minecraft_versions')
const HASHED_FAVICON_URL = '/hashedfavicon_'
const HASHED_FAVICON_EXTENSION = '.png'
class ServerRegistration { class ServerRegistration {
serverId serverId
lastFavicon lastFavicon
@ -239,7 +242,7 @@ class ServerRegistration {
getFaviconUrl () { getFaviconUrl () {
if (this.faviconHash) { if (this.faviconHash) {
return '/hashedfavicon?' + this.faviconHash return HASHED_FAVICON_URL + this.faviconHash + HASHED_FAVICON_EXTENSION
} else if (this.data.favicon) { } else if (this.data.favicon) {
return this.data.favicon return this.data.favicon
} }