From 566e0b0aa55559e808f3e59b454a0cffd9011445 Mon Sep 17 00:00:00 2001 From: lapp Date: Wed, 6 May 2026 15:34:36 -0700 Subject: [PATCH] Add deploy/gas/Code.gs --- deploy/gas/Code.gs | 131 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 deploy/gas/Code.gs diff --git a/deploy/gas/Code.gs b/deploy/gas/Code.gs new file mode 100644 index 0000000..96edc3c --- /dev/null +++ b/deploy/gas/Code.gs @@ -0,0 +1,131 @@ +// Google Apps Script + +const AUTH_KEY = "STRONG_SECRET_KEY"; +const WORKER_URL = "https://example.workers.dev"; + +const SKIP_HEADERS = { + host: 1, connection: 1, "content-length": 1, + "transfer-encoding": 1, "proxy-connection": 1, "proxy-authorization": 1, +}; + +function doPost(e) { + try { + var req = JSON.parse(e.postData.contents); + if (req.k !== AUTH_KEY) return _json({ e: "unauthorized" }); + + if (Array.isArray(req.q)) return _doBatch(req.q); + return _doSingle(req); + + } catch (err) { + return _json({ e: String(err) }); + } +} + +function _doSingle(req) { + if (!req.u || typeof req.u !== "string" || !req.u.match(/^https?:\/\//i)) { + return _json({ e: "bad url" }); + } + + var payload = _buildWorkerPayload(req); + + var resp = UrlFetchApp.fetch(WORKER_URL, { + method: "post", + contentType: "application/json", + payload: JSON.stringify(payload), + muteHttpExceptions: true, + followRedirects: true + }); + + try { + return _json(JSON.parse(resp.getContentText())); + } catch (e) { + return _json({ e: "invalid worker response", raw: resp.getContentText() }); + } +} + +function _doBatch(items) { + var fetchArgs = []; + var errorMap = {}; + + for (var i = 0; i < items.length; i++) { + var item = items[i]; + + if (!item.u || typeof item.u !== "string" || !item.u.match(/^https?:\/\//i)) { + errorMap[i] = "bad url"; + continue; + } + + var payload = _buildWorkerPayload(item); + + fetchArgs.push({ + _i: i, + _o: { + url: WORKER_URL, + method: "post", + contentType: "application/json", + payload: JSON.stringify(payload), + muteHttpExceptions: true, + followRedirects: true + } + }); + } + + var responses = []; + if (fetchArgs.length > 0) { + responses = UrlFetchApp.fetchAll(fetchArgs.map(function(x) { return x._o; })); + } + + var results = []; + var rIdx = 0; + + for (var i = 0; i < items.length; i++) { + if (errorMap.hasOwnProperty(i)) { + results.push({ e: errorMap[i] }); + } else { + var resp = responses[rIdx++]; + try { + results.push(JSON.parse(resp.getContentText())); + } catch (e) { + results.push({ e: "invalid worker response", raw: resp.getContentText() }); + } + } + } + + return _json({ q: results }); +} + +function _buildWorkerPayload(req) { + var headers = {}; + + if (req.h && typeof req.h === "object") { + for (var k in req.h) { + if (req.h.hasOwnProperty(k) && !SKIP_HEADERS[k.toLowerCase()]) { + headers[k] = req.h[k]; + } + } + } + + return { + u: req.u, + m: (req.m || "GET").toUpperCase(), + h: headers, + b: req.b || null, + ct: req.ct || null, + r: req.r !== false + }; +} + +function doGet(e) { + return HtmlService.createHtmlOutput( + "My App" + + '' + + "

Relay Active

Cloudflare Worker routing enabled.

" + + "" + ); +} + +function _json(obj) { + return ContentService + .createTextOutput(JSON.stringify(obj)) + .setMimeType(ContentService.MimeType.JSON); +} \ No newline at end of file