A Cloudflare Worker written in Rust that demonstrates using the worker-rs crate to build serverless functions. This worker returns the visitor's IP address by reading Cloudflare's CF-Connecting-IP header.
- π¦ Written in Rust - Compiled to WebAssembly for maximum performance
- π IP Detection - Returns the real client IP address from Cloudflare headers
- π Router-based - Uses
worker::Routerfor clean request handling - β‘ Edge Computing - Runs on Cloudflare's global edge network
This worker exposes a single endpoint that returns the visitor's IP address:
- GET / - Returns the client's IP address as plain text
- If the
CF-Connecting-IPheader is not available, it returns "unknown"
- Rust (latest stable version)
- wrangler CLI
- A Cloudflare account
- Clone this repository:
git clone <your-repo-url>
cd cf-rust-worker- Install dependencies:
cargo build- Configure your
wrangler.tomlwith your account details
Run the worker locally:
wrangler devVisit http://localhost:8787 to see your IP address.
Deploy to Cloudflare Workers:
wrangler deploycurl https://your-worker.your-subdomain.workers.dev/
# Output: 192.168.1.100fetch('https://your-worker.your-subdomain.workers.dev/')
.then(response => response.text())
.then(ip => console.log('Your IP:', ip));<!DOCTYPE html>
<html>
<head>
<title>IP Checker</title>
</head>
<body>
<h1>Your IP Address</h1>
<p id="ip">Loading...</p>
<script>
fetch('https://your-worker.your-subdomain.workers.dev/')
.then(response => response.text())
.then(ip => {
document.getElementById('ip').textContent = ip;
})
.catch(error => {
document.getElementById('ip').textContent = 'Error: ' + error.message;
});
</script>
</body>
</html>src/
βββ lib.rs # Main worker logic with router and IP handling
βββ new.rs # (if present) Alternative implementation
- Router Setup: Uses
worker::Routerfor handling HTTP requests - IP Extraction: Reads the
CF-Connecting-IPheader provided by Cloudflare - Error Handling: Gracefully handles cases where the IP header is not available
The worker is configured via wrangler.toml. Key settings:
name = "cf-rust-worker"
main = "build/worker/shim.mjs"
compatibility_date = "2023-05-18"
[build]
command = "cargo install -q worker-build && worker-build --release"- Request Handling: The
fetchfunction receives incoming HTTP requests - Router Processing: Requests are routed through
worker::Router - IP Extraction: The
handle_getfunction extracts the IP from Cloudflare headers - Response: Returns the IP address as plain text
This worker compiles Rust code to WebAssembly (wasm32-unknown-unknown target) for execution on Cloudflare's V8 isolates. All dependencies must be compatible with the WebAssembly target.
worker- Cloudflare Workers runtime for Rustconsole_error_panic_hook- Better error messages in development
- The worker relies on Cloudflare's
CF-Connecting-IPheader - Only works when deployed to Cloudflare Workers (not other platforms)
- Returns "unknown" if the header is not present
- Fork the repository
- Create a feature branch
- Make your changes
- Test locally with
wrangler dev - Submit a pull request
- Cloudflare Workers Rust Documentation
- workers-rs GitHub Repository
- Cloudflare Workers Examples
- Wrangler CLI Documentation
This project is open source and available under the MIT License.