feat: upgrade to universal outbound proxy with x-target-host header
Browse filesFiles changed (4)
8 unmodified lines910111213141516171819207 unmodified lines2829303132333435368 unmodified lines## Step-by-Step Guide### Step 1: Create a Cloudflare Worker1. Log into [Cloudflare](https://dash.cloudflare.com/) (create an account if you don't have one).2. On the left sidebar, go to **Workers & Pages**.3. Click **Create Worker** -> **Start with Hello World!**.4. Name it something memorable, like `discord-proxy`, and click **Deploy**.### Step 2: Add the Proxy Code1. Once deployed, click the **Edit Code** button.2. In the online code editor, delete the existing code and replace it entirely with this 6-line snippet:7 unmodified lines}```3. Click **Save and Deploy** in the top right corner. Cloudflare will generate a unique URL for you (e.g., `https://discord-proxy.yourname.workers.dev`).### Step 3: Use the Proxy in n8nBecause the native Discord Node hardcodes connections to `discord.com` when using Bot tokens, you must use the **Webhook** method.1. Add the standard **Discord** node to your n8n workflow.8 unmodified lines9101112131415161718192021227 unmodified lines303132333435363738398 unmodified lines## Step-by-Step Guide### Step 1: Create a Cloudflare Worker1. Log into [Cloudflare](https://dash.cloudflare.com/) (create an account if you don't have one).2. On the left sidebar, go to **Workers & Pages**.3. Click **Create Worker** -> **Start with Hello World!**.4. Name it something memorable, like `discord-proxy`, and click **Deploy**.### Step 2: Add the Proxy Code1. Once deployed, click the **Edit Code** button.2. In the online code editor, delete the existing code and replace it entirely with this 6-line snippet:7 unmodified lines}```1. Click **Save and Deploy** in the top right corner. Cloudflare will generate a unique URL for you (e.g., `https://discord-proxy.yourname.workers.dev`).### Step 3: Use the Proxy in n8nBecause the native Discord Node hardcodes connections to `discord.com` when using Bot tokens, you must use the **Webhook** method.1. Add the standard **Discord** node to your n8n workflow.
27 unmodified lines28293031323334353 unmodified lines394041424344454647484950515227 unmodified lines### 5. Transparent Application-Level Proxy (Implemented)- **Status**: ✅ **Partially Fixed** (Requires External Worker)- **Method**: Implemented `outbound-fix.js` which patches `https.request` to redirect Telegram and Discord traffic through a Cloudflare Worker.- **Why it works**: By changing the target hostname to a custom Cloudflare Worker, we change the SNI (Server Name Indication). Since Cloudflare is not blocked by HF, the connection succeeds. The Worker then forwards the request to the real destination.- **Recursion Guard**: Uses a private property `_proxied: true` on the options object to ensure requests aren't intercepted twice.## Final Conclusion & Recommendations3 unmodified lines2. **Network/SNI Layer**: **Addressed** via `outbound-fix.js` (Transparent Proxy).### Next Steps for User1. **Deploy Cloudflare Worker**: Use the code provided in `telegram-proxy-worker.js`.2. **Set Environment Variable**: In HF Space Settings, set `OUTBOUND_PROXY_URL` to your worker's URL (e.g., `https://my-proxy.somrat.workers.dev`).3. **Restart Space**: The `n8n` instance will now automatically route all Telegram and Discord requests through your proxy without needing to change any workflow nodes.## Current State of Repository- `dns-fix.js`: Robust DoH fallback with recursion guards.- `outbound-fix.js`: Transparent SNI-bypass proxy for Telegram/Discord.- `telegram-proxy-worker.js`: Cloudflare Worker code for the proxy.- `Dockerfile`: Configured to preload both fixes.- `access.md`: Contains test tokens and execution logs.27 unmodified lines2829303132333435363 unmodified lines404142434445464748495051525327 unmodified lines### 5. Transparent Application-Level Proxy (Implemented)- **Status**: ✅ **Partially Fixed** (Requires External Worker)- **Method**: Implemented `outbound-fix.js` which patches `https.request` to redirect blocked traffic through a single **Universal Cloudflare Worker**.- **Multi-Service Support**: The patch adds a `x-target-host` header to the request. The Worker reads this header to determine the final destination (Telegram, Discord, etc.), allowing one Worker to handle all blocked services.- **Why it works**: By changing the target hostname to your custom Cloudflare Worker, we change the SNI (Server Name Indication). Since Cloudflare is not blocked by HF, the connection succeeds.- **Recursion Guard**: Uses a private property `_proxied: true` on the options object to ensure requests aren't intercepted twice.## Final Conclusion & Recommendations3 unmodified lines2. **Network/SNI Layer**: **Addressed** via `outbound-fix.js` (Transparent Proxy).### Next Steps for User1. **Deploy Cloudflare Worker**: Use the code provided in `outbound-proxy-worker.js`.2. **Set Environment Variable**: In HF Space Settings, set `OUTBOUND_PROXY_URL` to your worker's URL (e.g., `https://my-proxy.somrat.workers.dev`).3. **Restart Space**: The `n8n` instance will now automatically route all Telegram and Discord requests through your proxy without needing to change any workflow nodes.## Current State of Repository- `dns-fix.js`: Robust DoH fallback with recursion guards.- `outbound-fix.js`: Transparent SNI-bypass proxy with `x-target-host` support.- `outbound-proxy-worker.js`: Universal Cloudflare Worker code for any blocked target.- `Dockerfile`: Configured to preload both fixes.- `access.md`: Contains test tokens and execution logs.
47 unmodified lines48495051525354555657585960616247 unmodified lines// Mark to prevent recursionif (typeof newOptions === "object") {newOptions._proxied = true;newOptions.hostname = proxy.hostname;newOptions.host = proxy.host;newOptions.port = proxy.port || 443;// Fix headersif (newOptions.headers) {// Cloudflare needs the correct Host header to route to the workernewOptions.headers = { ...newOptions.headers, host: proxy.host };}}// Force HTTPS if it was HTTP (unlikely for these domains but good for safety)47 unmodified lines484950515253545556575859606162636447 unmodified lines// Mark to prevent recursionif (typeof newOptions === "object") {newOptions._proxied = true;// Set headersnewOptions.headers = {...(newOptions.headers || {}),host: proxy.host,"x-target-host": hostname // Tell the worker where to go};newOptions.hostname = proxy.hostname;newOptions.host = proxy.host;newOptions.port = proxy.port || 443;}// Force HTTPS if it was HTTP (unlikely for these domains but good for safety)
12345678910111213141516171819202122232425262728293031323334353 unmodified lines39404142434445/*** Cloudflare Worker: Simple Telegram/Discord Proxy** Deployment:* 1. Go to dash.cloudflare.com -> Workers & Pages -> Create Worker.* 2. Paste this code and deploy.* 3. Use your worker URL (e.g., https://my-proxy.workers.dev) in Hugging8n.*/export default {async fetch(request, env, ctx) {const url = new URL(request.url);// Determine target based on path or header// Default to telegram if path matches telegram patternlet targetBase = "";if (url.pathname.startsWith("/bot")) {targetBase = "https://api.telegram.org";} else if (url.pathname.startsWith("/api/webhooks") || url.pathname.startsWith("/api/v")) {targetBase = "https://discord.com";} else {return new Response("Invalid request. Target not recognized.", { status: 400 });}const targetUrl = targetBase + url.pathname + url.search;// Copy headers and remove Cloudflare-specific onesconst headers = new Headers(request.headers);headers.delete("cf-connecting-ip");headers.delete("cf-ray");headers.delete("cf-visitor");headers.delete("x-real-ip");const modifiedRequest = new Request(targetUrl, {method: request.method,3 unmodified lines});try {return await fetch(modifiedRequest);} catch (e) {return new Response(`Proxy Error: ${e.message}`, { status: 502 });}12345678910111213141516171819202122232425262728293031323334353637383940414243443 unmodified lines484950515253545556575859/*** Cloudflare Worker: Universal Outbound Proxy** Deployment:* 1. Go to dash.cloudflare.com -> Workers & Pages -> Create Worker.* 2. Paste this code and deploy.* 3. Use your worker URL (e.g., https://my-proxy.workers.dev) as OUTBOUND_PROXY_URL.** This worker reads the 'x-target-host' header to determine where to forward the request.*/export default {async fetch(request, env, ctx) {const url = new URL(request.url);const targetHost = request.headers.get("x-target-host");let targetBase = "";if (targetHost) {// Use the host provided in the header (preferred)targetBase = `https://${targetHost}`;} else {// Fallback: Guess based on path (legacy support)if (url.pathname.startsWith("/bot")) {targetBase = "https://api.telegram.org";} else if (url.pathname.startsWith("/api/webhooks") || url.pathname.startsWith("/api/v")) {targetBase = "https://discord.com";} else {return new Response("Invalid request. 'x-target-host' header missing and target not recognized via path.", { status: 400 });}}const targetUrl = targetBase + url.pathname + url.search;// Copy headers and remove internal/Cloudflare-specific onesconst headers = new Headers(request.headers);headers.delete("cf-connecting-ip");headers.delete("cf-ray");headers.delete("cf-visitor");headers.delete("x-real-ip");headers.delete("x-target-host"); // Don't leak this to the targetconst modifiedRequest = new Request(targetUrl, {method: request.method,3 unmodified lines});try {const response = await fetch(modifiedRequest);// Special handling for Discord/Telegram which might return 403 on some CF IPs// If needed, you can add retry logic here.return response;} catch (e) {return new Response(`Proxy Error: ${e.message}`, { status: 502 });}
DISCORD_PROXY_GUIDE.md+4-1
TASK_SUMMARY.md+6-5
outbound-fix.js+8-6
telegram-proxy-worker.js → outbound-proxy-worker.jsRenamed+25-11