Skip to content

Commit 4818143

Browse files
authored
feat: tunnel-node service + CodeFull.gs (#93)
Standalone Rust/axum HTTP server + Apps Script-side CodeFull.gs for users who want to deploy a remote tunnel node. All new files; no changes to the main Rust crate. This is part 1 of 3 of the full-tunnel feature — it adds scaffolding that users can opt into once the Rust-side Mode::Full lands in #94.
1 parent a286b7b commit 4818143

8 files changed

Lines changed: 1913 additions & 0 deletions

File tree

‎assets/apps_script/CodeFull.gs‎

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
/**
2+
* DomainFront Relay + Full Tunnel — Google Apps Script
3+
*
4+
* FOUR modes:
5+
* 1. Single relay: POST { k, m, u, h, b, ct, r } → { s, h, b }
6+
* 2. Batch relay: POST { k, q: [{m,u,h,b,ct,r}, ...] } → { q: [{s,h,b}, ...] }
7+
* 3. Tunnel: POST { k, t, h, p, sid, d } → { sid, d, eof }
8+
* 4. Tunnel batch: POST { k, t:"batch", ops:[...] } → { r: [...] }
9+
*
10+
* CHANGE THESE TO YOUR OWN VALUES!
11+
*/
12+
13+
const AUTH_KEY = "CHANGE_ME_TO_A_STRONG_SECRET";
14+
const TUNNEL_SERVER_URL = "https://YOUR_TUNNEL_NODE_URL";
15+
const TUNNEL_AUTH_KEY = "YOUR_TUNNEL_AUTH_KEY";
16+
17+
const SKIP_HEADERS = {
18+
host: 1, connection: 1, "content-length": 1,
19+
"transfer-encoding": 1, "proxy-connection": 1, "proxy-authorization": 1,
20+
"priority": 1, te: 1,
21+
};
22+
23+
// ========================== Entry point ==========================
24+
25+
function doPost(e) {
26+
try {
27+
var req = JSON.parse(e.postData.contents);
28+
if (req.k !== AUTH_KEY) return _json({ e: "unauthorized" });
29+
30+
// Tunnel mode
31+
if (req.t) return _doTunnel(req);
32+
33+
// Batch relay mode
34+
if (Array.isArray(req.q)) return _doBatch(req.q);
35+
36+
// Single relay mode
37+
return _doSingle(req);
38+
} catch (err) {
39+
return _json({ e: String(err) });
40+
}
41+
}
42+
43+
// ========================== Tunnel mode ==========================
44+
45+
function _doTunnel(req) {
46+
// Batch tunnel: { k, t:"batch", ops:[...] }
47+
if (req.t === "batch") {
48+
return _doTunnelBatch(req);
49+
}
50+
51+
// Single tunnel op
52+
var payload = { k: TUNNEL_AUTH_KEY };
53+
switch (req.t) {
54+
case "connect":
55+
payload.op = "connect";
56+
payload.host = req.h;
57+
payload.port = req.p;
58+
break;
59+
case "data":
60+
payload.op = "data";
61+
payload.sid = req.sid;
62+
if (req.d) payload.data = req.d;
63+
break;
64+
case "close":
65+
payload.op = "close";
66+
payload.sid = req.sid;
67+
break;
68+
default:
69+
return _json({ e: "unknown tunnel op: " + req.t });
70+
}
71+
72+
var resp = UrlFetchApp.fetch(TUNNEL_SERVER_URL + "/tunnel", {
73+
method: "post",
74+
contentType: "application/json",
75+
payload: JSON.stringify(payload),
76+
muteHttpExceptions: true,
77+
followRedirects: true,
78+
});
79+
80+
if (resp.getResponseCode() !== 200) {
81+
return _json({ e: "tunnel node HTTP " + resp.getResponseCode() });
82+
}
83+
84+
return ContentService.createTextOutput(resp.getContentText())
85+
.setMimeType(ContentService.MimeType.JSON);
86+
}
87+
88+
// Batch tunnel: forward all ops in one request to /tunnel/batch
89+
function _doTunnelBatch(req) {
90+
var payload = {
91+
k: TUNNEL_AUTH_KEY,
92+
ops: req.ops || [],
93+
};
94+
95+
var resp = UrlFetchApp.fetch(TUNNEL_SERVER_URL + "/tunnel/batch", {
96+
method: "post",
97+
contentType: "application/json",
98+
payload: JSON.stringify(payload),
99+
muteHttpExceptions: true,
100+
followRedirects: true,
101+
});
102+
103+
if (resp.getResponseCode() !== 200) {
104+
return _json({ e: "tunnel batch HTTP " + resp.getResponseCode() });
105+
}
106+
107+
return ContentService.createTextOutput(resp.getContentText())
108+
.setMimeType(ContentService.MimeType.JSON);
109+
}
110+
111+
// ========================== HTTP relay mode ==========================
112+
113+
function _doSingle(req) {
114+
if (!req.u || typeof req.u !== "string" || !req.u.match(/^https?:\/\//i)) {
115+
return _json({ e: "bad url" });
116+
}
117+
var opts = _buildOpts(req);
118+
var resp = UrlFetchApp.fetch(req.u, opts);
119+
return _json({
120+
s: resp.getResponseCode(),
121+
h: _respHeaders(resp),
122+
b: Utilities.base64Encode(resp.getContent()),
123+
});
124+
}
125+
126+
function _doBatch(items) {
127+
var fetchArgs = [];
128+
var errorMap = {};
129+
for (var i = 0; i < items.length; i++) {
130+
var item = items[i];
131+
if (!item.u || typeof item.u !== "string" || !item.u.match(/^https?:\/\//i)) {
132+
errorMap[i] = "bad url";
133+
continue;
134+
}
135+
var opts = _buildOpts(item);
136+
opts.url = item.u;
137+
fetchArgs.push({ _i: i, _o: opts });
138+
}
139+
var responses = [];
140+
if (fetchArgs.length > 0) {
141+
responses = UrlFetchApp.fetchAll(fetchArgs.map(function(x) { return x._o; }));
142+
}
143+
var results = [];
144+
var rIdx = 0;
145+
for (var i = 0; i < items.length; i++) {
146+
if (errorMap.hasOwnProperty(i)) {
147+
results.push({ e: errorMap[i] });
148+
} else {
149+
var resp = responses[rIdx++];
150+
results.push({
151+
s: resp.getResponseCode(),
152+
h: _respHeaders(resp),
153+
b: Utilities.base64Encode(resp.getContent()),
154+
});
155+
}
156+
}
157+
return _json({ q: results });
158+
}
159+
160+
// ========================== Helpers ==========================
161+
162+
function _buildOpts(req) {
163+
var opts = {
164+
method: (req.m || "GET").toLowerCase(),
165+
muteHttpExceptions: true,
166+
followRedirects: req.r !== false,
167+
validateHttpsCertificates: true,
168+
escaping: false,
169+
};
170+
if (req.h && typeof req.h === "object") {
171+
var headers = {};
172+
for (var k in req.h) {
173+
if (req.h.hasOwnProperty(k) && !SKIP_HEADERS[k.toLowerCase()]) {
174+
headers[k] = req.h[k];
175+
}
176+
}
177+
opts.headers = headers;
178+
}
179+
if (req.b) {
180+
opts.payload = Utilities.base64Decode(req.b);
181+
if (req.ct) opts.contentType = req.ct;
182+
}
183+
return opts;
184+
}
185+
186+
function _respHeaders(resp) {
187+
try {
188+
if (typeof resp.getAllHeaders === "function") {
189+
return resp.getAllHeaders();
190+
}
191+
} catch (err) {}
192+
return resp.getHeaders();
193+
}
194+
195+
function doGet(e) {
196+
return HtmlService.createHtmlOutput(
197+
"<!DOCTYPE html><html><head><title>My App</title></head>" +
198+
'<body style="font-family:sans-serif;max-width:600px;margin:40px auto">' +
199+
"<h1>Welcome</h1><p>This application is running normally.</p>" +
200+
"</body></html>"
201+
);
202+
}
203+
204+
function _json(obj) {
205+
return ContentService.createTextOutput(JSON.stringify(obj)).setMimeType(
206+
ContentService.MimeType.JSON
207+
);
208+
}

‎config.full.example.json‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"mode": "full",
3+
"google_ip": "216.239.38.120",
4+
"front_domain": "www.google.com",
5+
"script_id": "YOUR_APPS_SCRIPT_DEPLOYMENT_ID",
6+
"auth_key": "CHANGE_ME_TO_A_STRONG_SECRET",
7+
"listen_host": "127.0.0.1",
8+
"listen_port": 8085,
9+
"socks5_port": 8086,
10+
"log_level": "info",
11+
"verify_ssl": true
12+
}

‎tunnel-node/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

0 commit comments

Comments
 (0)