This is a simple URL proxy script that forwards requests to a target URL specified by the url
query parameter. If no URL is provided or if the URL is invalid, the script returns a 400 error.
- Proxy Requests: Forwards incoming HTTP requests to a target URL.
- Error Handling: Returns a 400 error if the URL parameter is missing or invalid.
- Flexible Request Handling: Supports GET, HEAD, and other HTTP methods.
- Response Header Customization: Easily adjust response headers (e.g., for CORS).
- Listening for Requests: The script listens for
fetch
events. - URL Parsing: It extracts the
url
query parameter from the incoming request. - Validation: Checks if the provided URL is valid. If not, it returns a 400 error.
- Proxying the Request: Creates a new request to the target URL, preserving the original HTTP method, headers, and body.
- Returning the Response: Fetches the resource from the target URL and returns the response to the client.
-
Install Wrangler CLI
Install the Wrangler CLI globally if you haven't already:
npm install -g wrangler
-
Login to Cloudflare
Authenticate with your Cloudflare account:
wrangler login
-
Generate a New Worker Project
Create a new Worker project (or use an existing one):
wrangler generate my-worker
Replace the contents of the generated
index.js
with the script provided above, or integrate it as needed. -
Configure the Project
Update your
wrangler.toml
configuration file. For example:name = "my-url-proxy" type = "javascript" account_id = "YOUR_ACCOUNT_ID" workers_dev = true route = "" zone_id = ""
Replace
YOUR_ACCOUNT_ID
with your actual Cloudflare account ID. If you're deploying to a specific route, provide theroute
andzone_id
. -
Publish Your Worker
Deploy your Worker by running:
wrangler publish
Once published, your Worker will be live at the URL provided by Cloudflare Workers.
-
Log in to Your Cloudflare Account
Navigate to Cloudflare Dashboard and log in with your credentials.
-
Access the Workers Section
From the dashboard, click on Workers & Pages in the sidebar, then select Workers.
-
Create a New Worker
Click on the Create a Service (or Create a Worker) button. This will open the online editor with a default script.
-
Replace the Default Script
In the online editor, remove the default code and paste in the provided proxy script (next section, scroll down).
-
Save and Deploy
Click the Save button and then Deploy (or simply Deploy to Workers) to publish your service. Once deployed, your Worker will be assigned a unique URL, typically in the format
https://<service-name>.<random-string>.workers.dev
.
To use the proxy, send an HTTP request with a url
query parameter. For example:
https://<your-worker-subdomain>.workers.dev/?url=https%3A%2F%2Fexample.com
The Worker will fetch the resource from https://example.com
and return the response.
Contributions are welcome! If you have suggestions or improvements, please open an issue or submit a pull request.
This project is licensed under the MIT License.
Special thanks to OpenAI o3-mini-high model because I would develop this shit times longer. With o3, I completed it in 4 hours even being ill.