Skip to content

Commit 51fef3c

Browse files
authored
Merge pull request #3210 from akhilmhdh/fix/gateway-patch-up
Gateway patch up
2 parents f686022 + df9e7bf commit 51fef3c

File tree

7 files changed

+160
-139
lines changed

7 files changed

+160
-139
lines changed

backend/src/lib/gateway/index.ts

+52-38
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export const pingGatewayAndVerify = async ({
9696
error: err as Error
9797
});
9898
});
99+
99100
for (let attempt = 1; attempt <= maxRetries; attempt += 1) {
100101
try {
101102
const stream = quicClient.connection.newStream("bidi");
@@ -108,17 +109,13 @@ export const pingGatewayAndVerify = async ({
108109
const { value, done } = await reader.read();
109110

110111
if (done) {
111-
throw new BadRequestError({
112-
message: "Gateway closed before receiving PONG"
113-
});
112+
throw new Error("Gateway closed before receiving PONG");
114113
}
115114

116115
const response = Buffer.from(value).toString();
117116

118117
if (response !== "PONG\n" && response !== "PONG") {
119-
throw new BadRequestError({
120-
message: `Failed to Ping. Unexpected response: ${response}`
121-
});
118+
throw new Error(`Failed to Ping. Unexpected response: ${response}`);
122119
}
123120

124121
reader.releaseLock();
@@ -146,6 +143,7 @@ interface TProxyServer {
146143
server: net.Server;
147144
port: number;
148145
cleanup: () => Promise<void>;
146+
getProxyError: () => string;
149147
}
150148

151149
const setupProxyServer = async ({
@@ -170,6 +168,7 @@ const setupProxyServer = async ({
170168
error: err as Error
171169
});
172170
});
171+
const proxyErrorMsg = [""];
173172

174173
return new Promise((resolve, reject) => {
175174
const server = net.createServer();
@@ -185,31 +184,33 @@ const setupProxyServer = async ({
185184
const forwardWriter = stream.writable.getWriter();
186185
await forwardWriter.write(Buffer.from(`FORWARD-TCP ${targetHost}:${targetPort}\n`));
187186
forwardWriter.releaseLock();
188-
/* eslint-disable @typescript-eslint/no-misused-promises */
187+
189188
// Set up bidirectional copy
190-
const setupCopy = async () => {
189+
const setupCopy = () => {
191190
// Client to QUIC
192191
// eslint-disable-next-line
193192
(async () => {
194-
try {
195-
const writer = stream.writable.getWriter();
193+
const writer = stream.writable.getWriter();
196194

197-
// Create a handler for client data
198-
clientConn.on("data", async (chunk) => {
199-
await writer.write(chunk);
195+
// Create a handler for client data
196+
clientConn.on("data", (chunk) => {
197+
writer.write(chunk).catch((err) => {
198+
proxyErrorMsg.push((err as Error)?.message);
200199
});
200+
});
201201

202-
// Handle client connection close
203-
clientConn.on("end", async () => {
204-
await writer.close();
202+
// Handle client connection close
203+
clientConn.on("end", () => {
204+
writer.close().catch((err) => {
205+
logger.error(err);
205206
});
207+
});
206208

207-
clientConn.on("error", async (err) => {
208-
await writer.abort(err);
209+
clientConn.on("error", (clientConnErr) => {
210+
writer.abort(clientConnErr?.message).catch((err) => {
211+
proxyErrorMsg.push((err as Error)?.message);
209212
});
210-
} catch (err) {
211-
clientConn.destroy();
212-
}
213+
});
213214
})();
214215

215216
// QUIC to Client
@@ -238,29 +239,37 @@ const setupProxyServer = async ({
238239
}
239240
}
240241
} catch (err) {
242+
proxyErrorMsg.push((err as Error)?.message);
241243
clientConn.destroy();
242244
}
243245
})();
244246
};
245-
await setupCopy();
246-
//
247+
248+
setupCopy();
247249
// Handle connection closure
248-
clientConn.on("close", async () => {
249-
await stream.destroy();
250+
clientConn.on("close", () => {
251+
stream.destroy().catch((err) => {
252+
proxyErrorMsg.push((err as Error)?.message);
253+
});
250254
});
251255

252256
const cleanup = async () => {
253257
clientConn?.destroy();
254258
await stream.destroy();
255259
};
256260

257-
clientConn.on("error", (err) => {
258-
logger.error(err, "Client socket error");
259-
void cleanup();
260-
reject(err);
261+
clientConn.on("error", (clientConnErr) => {
262+
logger.error(clientConnErr, "Client socket error");
263+
cleanup().catch((err) => {
264+
logger.error(err, "Client conn cleanup");
265+
});
261266
});
262267

263-
clientConn.on("end", cleanup);
268+
clientConn.on("end", () => {
269+
cleanup().catch((err) => {
270+
logger.error(err, "Client conn end");
271+
});
272+
});
264273
} catch (err) {
265274
logger.error(err, "Failed to establish target connection:");
266275
clientConn.end();
@@ -272,12 +281,12 @@ const setupProxyServer = async ({
272281
reject(err);
273282
});
274283

275-
server.on("close", async () => {
276-
await quicClient?.destroy();
284+
server.on("close", () => {
285+
quicClient?.destroy().catch((err) => {
286+
logger.error(err, "Failed to destroy quic client");
287+
});
277288
});
278289

279-
/* eslint-enable */
280-
281290
server.listen(0, () => {
282291
const address = server.address();
283292
if (!address || typeof address === "string") {
@@ -293,7 +302,8 @@ const setupProxyServer = async ({
293302
cleanup: async () => {
294303
server.close();
295304
await quicClient?.destroy();
296-
}
305+
},
306+
getProxyError: () => proxyErrorMsg.join(",")
297307
});
298308
});
299309
});
@@ -316,7 +326,7 @@ export const withGatewayProxy = async (
316326
const { relayHost, relayPort, targetHost, targetPort, tlsOptions, identityId, orgId } = options;
317327

318328
// Setup the proxy server
319-
const { port, cleanup } = await setupProxyServer({
329+
const { port, cleanup, getProxyError } = await setupProxyServer({
320330
targetHost,
321331
targetPort,
322332
relayPort,
@@ -330,8 +340,12 @@ export const withGatewayProxy = async (
330340
// Execute the callback with the allocated port
331341
await callback(port);
332342
} catch (err) {
333-
logger.error(err, "Failed to proxy");
334-
throw new BadRequestError({ message: (err as Error)?.message });
343+
const proxyErrorMessage = getProxyError();
344+
if (proxyErrorMessage) {
345+
logger.error(new Error(proxyErrorMessage), "Failed to proxy");
346+
}
347+
logger.error(err, "Failed to do gateway");
348+
throw new BadRequestError({ message: proxyErrorMessage || (err as Error)?.message });
335349
} finally {
336350
// Ensure cleanup happens regardless of success or failure
337351
await cleanup();

backend/src/lib/turn/credentials.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import crypto from "node:crypto";
22

3-
const TURN_TOKEN_TTL = 60 * 60 * 1000; // 24 hours in milliseconds
3+
const TURN_TOKEN_TTL = 24 * 60 * 60 * 1000; // 24 hours in milliseconds
44
export const getTurnCredentials = (id: string, authSecret: string, ttl = TURN_TOKEN_TTL) => {
55
const timestamp = Math.floor((Date.now() + ttl) / 1000);
66
const username = `${timestamp}:${id}`;

cli/go.mod

+5-5
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ require (
2929
github.com/spf13/cobra v1.6.1
3030
github.com/spf13/viper v1.8.1
3131
github.com/stretchr/testify v1.10.0
32-
golang.org/x/crypto v0.35.0
33-
golang.org/x/sys v0.30.0
34-
golang.org/x/term v0.29.0
32+
golang.org/x/crypto v0.36.0
33+
golang.org/x/sys v0.31.0
34+
golang.org/x/term v0.30.0
3535
gopkg.in/yaml.v2 v2.4.0
3636
)
3737

@@ -115,8 +115,8 @@ require (
115115
golang.org/x/mod v0.23.0 // indirect
116116
golang.org/x/net v0.35.0 // indirect
117117
golang.org/x/oauth2 v0.21.0 // indirect
118-
golang.org/x/sync v0.11.0 // indirect
119-
golang.org/x/text v0.22.0 // indirect
118+
golang.org/x/sync v0.12.0 // indirect
119+
golang.org/x/text v0.23.0 // indirect
120120
golang.org/x/time v0.6.0 // indirect
121121
golang.org/x/tools v0.30.0 // indirect
122122
google.golang.org/api v0.188.0 // indirect

cli/go.sum

+10
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,8 @@ golang.org/x/crypto v0.0.0-20211215165025-cf75a172585e/go.mod h1:P+XmwS30IXTQdn5
486486
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
487487
golang.org/x/crypto v0.35.0 h1:b15kiHdrGCHrP6LvwaQ3c03kgNhhiMgvlhxHQhmg2Xs=
488488
golang.org/x/crypto v0.35.0/go.mod h1:dy7dXNW32cAb/6/PRuTNsix8T+vJAqvuIy5Bli/x0YQ=
489+
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
490+
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
489491
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
490492
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
491493
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
@@ -592,6 +594,8 @@ golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJ
592594
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
593595
golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=
594596
golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
597+
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
598+
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
595599
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
596600
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
597601
golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@@ -642,9 +646,13 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
642646
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
643647
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
644648
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
649+
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
650+
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
645651
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
646652
golang.org/x/term v0.29.0 h1:L6pJp37ocefwRRtYPKSWOWzOtWSxVajvz2ldH/xi3iU=
647653
golang.org/x/term v0.29.0/go.mod h1:6bl4lRlvVuDgSf3179VpIxBF0o10JUpXWOnI7nErv7s=
654+
golang.org/x/term v0.30.0 h1:PQ39fJZ+mfadBm0y5WlL4vlM7Sx1Hgf13sMIY2+QS9Y=
655+
golang.org/x/term v0.30.0/go.mod h1:NYYFdzHoI5wRh/h5tDMdMqCqPJZEuNqVR5xJLd/n67g=
648656
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
649657
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
650658
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@@ -656,6 +664,8 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
656664
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
657665
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
658666
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
667+
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
668+
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
659669
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
660670
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
661671
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=

0 commit comments

Comments
 (0)