Last active
May 28, 2025 19:22
-
-
Save ChiChou/0fefb4e33537fe732269d01ec58c146d to your computer and use it in GitHub Desktop.
CloudFlare worker to query permesso di soggiorno status
This file contains hidden or 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
/** | |
* Welcome to Cloudflare Workers! This is your first worker. | |
* | |
* - Run "npm run dev" in your terminal to start a development server | |
* - Open a browser tab at http://localhost:8787/ to see your worker in action | |
* - Run "npm run deploy" to publish your worker | |
* | |
* Learn more at https://developers.cloudflare.com/workers/ | |
*/ | |
async function get(pass) { | |
const OPEN_TAG = "<![CDATA["; | |
const CLOSE_TAG = "]]>"; | |
const url = `https://questure.poliziadistato.it/servizio/stranieri?lang=english&pratica=${pass}&invia=Submit&mime=4`; | |
const response = await fetch(url); | |
if (!response.ok) { | |
throw new Error(`${response.status} ${response.statusText}`); | |
} | |
const text = await response.text(); | |
const start = text.indexOf(OPEN_TAG); | |
const end = text.indexOf(CLOSE_TAG, start); | |
if (start !== -1 && end !== -1) | |
return text.substring(start + OPEN_TAG.length, end); | |
throw new Error("Invalid response format"); | |
} | |
export default { | |
async fetch(request, env, ctx) { | |
const url = new URL(request.url); | |
if (url.pathname !== "/proxy") | |
return new Response("Not Found", { status: 404 }); | |
const pass = url.searchParams.get("pass"); | |
if (!pass) | |
return new Response("Password can not be empty", { status: 400 }); | |
try { | |
const msg = await get(pass); | |
return new Response(msg, { | |
headers: { "Content-Type": "text/plain" }, | |
}); | |
} catch (error) { | |
return new Response(`Error: ${error.message}`, { status: 500 }); | |
} | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment