CI / Docker image (push) Successful in 12s
CI / Server (push) Successful in 25s
CI / CLI (push) Successful in 13s
Development image / Development image (push) Successful in 21s
CI / CLI (windows) (push) Successful in 57s
CI / CLI (linux-arm64, static) (push) Successful in 1m37s
Release / Prepare release (push) Successful in 2s
Release / Server (linux-x64) (push) Successful in 53s
Release / CLI (linux-arm64, static) (push) Successful in 1m32s
Release / Docker image (push) Successful in 2m48s
Release / Server (windows-x64) (push) Successful in 6m20s
Release / CLI (windows-x64) (push) Successful in 6m48s
Release / CLI (linux-x64) (push) Successful in 7m8s
Release / Publish release (push) Successful in 1s
Squash the tree into one commit, drop docs/architecture.html, reset the version line to 0.3.0, consolidate the changelog, enable Renovate automerge for non-major updates and add AGENTS.md.
82 lines
3.0 KiB
JavaScript
82 lines
3.0 KiB
JavaScript
const { PassThrough } = require("node:stream");
|
|
const logger = require("../utils/logger");
|
|
|
|
const ANNOUNCE_DELAY = 150;
|
|
|
|
const tap = observe => new PassThrough({
|
|
transform(chunk, _encoding, callback) {
|
|
observe(chunk);
|
|
callback(null, chunk);
|
|
},
|
|
});
|
|
|
|
const summary = entry => ({
|
|
key: entry.key,
|
|
protocol: entry.protocol,
|
|
ip: entry.ip,
|
|
country: entry.intel.country,
|
|
org: entry.intel.org,
|
|
client: entry.client,
|
|
bytesIn: entry.bytesIn,
|
|
bytesOut: entry.bytesOut,
|
|
duration: (entry.endedAt || Date.now()) - entry.startedAt,
|
|
reason: entry.reason,
|
|
detail: entry.detail,
|
|
});
|
|
|
|
const relayStream = (tunnel, session, joinerStream, meta, { connections, intel, visitors }) => {
|
|
if (!tunnel.online) {
|
|
joinerStream.destroy(new Error("owner offline"));
|
|
return;
|
|
}
|
|
const protocol = meta.protocol === "udp" ? "udp" : "tcp";
|
|
const ip = session.client.ip;
|
|
const details = intel.lookup(ip);
|
|
const entry = connections.open(tunnel.id, { protocol, ip, intel: details, client: meta.remote || "", joiner: session.client.id });
|
|
visitors.seen(tunnel.id, ip, details);
|
|
const ownerStream = tunnel.session.open({ protocol, remote: meta.remote || "" });
|
|
|
|
const announce = event => {
|
|
if (tunnel.session && !tunnel.session.closed) tunnel.session.sendControl({ type: "connection", event, ...summary(entry) });
|
|
};
|
|
let announced = false;
|
|
const opened = () => {
|
|
if (announced) return;
|
|
announced = true;
|
|
clearTimeout(timer);
|
|
announce("open");
|
|
};
|
|
const timer = setTimeout(opened, ANNOUNCE_DELAY);
|
|
const observe = direction => chunk => {
|
|
connections.data(entry, direction, chunk);
|
|
if (entry.detail) opened();
|
|
};
|
|
|
|
if (protocol === "udp") {
|
|
joinerStream.on("datagram", data => { observe("in")(data); ownerStream.sendDatagram(data); });
|
|
ownerStream.on("datagram", data => { observe("out")(data); joinerStream.sendDatagram(data); });
|
|
}
|
|
joinerStream.pipe(tap(observe("in"))).pipe(ownerStream);
|
|
ownerStream.pipe(tap(observe("out"))).pipe(joinerStream);
|
|
|
|
let reason = null;
|
|
const finish = cause => {
|
|
if (!reason) reason = cause;
|
|
if (entry.endedAt) return;
|
|
connections.close(entry, reason);
|
|
opened();
|
|
announce("close");
|
|
};
|
|
joinerStream.on("error", () => { finish(session.closed ? "client disconnected" : "error"); ownerStream.destroy(); });
|
|
ownerStream.on("error", () => { finish(tunnel.session.closed ? "tunnel closed" : "error"); joinerStream.destroy(); });
|
|
joinerStream.on("close", () => { finish("client closed"); ownerStream.destroy(); });
|
|
ownerStream.on("close", () => { finish("app closed"); joinerStream.destroy(); });
|
|
logger.debug(`relay ${tunnel.id}: ${protocol} stream ${joinerStream.id} -> ${ownerStream.id}`);
|
|
};
|
|
|
|
const attachJoiner = (tunnel, session, context) => {
|
|
session.on("stream", (stream, meta) => relayStream(tunnel, session, stream, meta, context));
|
|
};
|
|
|
|
module.exports = { attachJoiner };
|