Last active
November 20, 2024 19:29
-
-
Save robere2/e3be025f47a747c49df3358606ca3e73 to your computer and use it in GitHub Desktop.
Example of a simple Cloudflare Worker script which proxies the Hypixel API
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
/** | |
* Copyright 2024 https://ecr.dev/ | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | |
* documentation files (the “Software”), to deal in the Software without restriction, including without limitation | |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and | |
* to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of | |
* the Software. | |
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO | |
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
*/ | |
export default { | |
/* | |
* Required environment variables: | |
* - API_KEY: Your Hypixel API key | |
* - PASSWORD: A secret password which is required to use this API proxy | |
*/ | |
CACHE_TTL: 30, | |
notAllowed(request) { | |
return new Response(JSON.stringify({success: false, cause: `Proxy method ${request.method} not allowed`}), { | |
status: 405, | |
headers: { | |
"Content-Type": "application/json", | |
Allow: "GET", | |
}, | |
}); | |
}, | |
notFound(url) { | |
return new Response(JSON.stringify({success: false, cause: `Path ${url.pathname} not found or not supported`}), { | |
status: 404, | |
headers: { | |
"Content-Type": "application/json" | |
}, | |
}); | |
}, | |
unauthorized() { | |
return new Response(JSON.stringify({success: false, cause: "Incorrect proxy password"}), { | |
status: 401, | |
headers: { | |
"Content-Type": "application/json" | |
}, | |
}) | |
}, | |
async feedThroughCache(request, ctx) { | |
const cachedResponse = await caches.default.match(request); | |
if(cachedResponse) { | |
return cachedResponse; | |
} | |
const hypixelResponse = await fetch(request); | |
// Remove Hypixel API response headers (Cf headers, API rate limits, etc.) | |
const response = new Response(hypixelResponse.body, { | |
headers: { | |
"Content-Type": "application/json", | |
"Cache-Control": `s-maxage=${this.CACHE_TTL}` | |
} | |
}); | |
ctx.waitUntil(caches.default.put(request, response.clone())); | |
return response; | |
}, | |
getPlayer(uuid, apiKey, ctx) { | |
const hypixelRequest = new Request(`https://api.hypixel.net/v2/player?uuid=${uuid}`, { | |
headers: { | |
"API-Key": apiKey | |
} | |
}); | |
return this.feedThroughCache(hypixelRequest, ctx) | |
}, | |
getCounts(apiKey, ctx) { | |
const hypixelRequest = new Request(`https://api.hypixel.net/v2/counts`, { | |
headers: { | |
"API-Key": apiKey | |
} | |
}); | |
return this.feedThroughCache(hypixelRequest, ctx) | |
}, | |
async fetch(request, env, ctx) { | |
if(request.method !== "GET") return this.notAllowed(request); | |
if(request.headers.get("Authorization") !== `Bearer ${env.PASSWORD}`) return this.unauthorized() | |
const url = new URL(request.url) | |
if(url.pathname === "/player") { | |
return this.getPlayer(url.searchParams.get("uuid"), env.API_KEY, ctx) | |
} else if(url.pathname === "/counts") { | |
return this.getCounts(env.API_KEY, ctx) | |
} else { | |
return this.notFound(url) | |
} | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment