Skip to content

Commit

Permalink
for gziped content
Browse files Browse the repository at this point in the history
  • Loading branch information
DFanso committed Dec 8, 2024
1 parent 97032a4 commit 027da63
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions src/services/TunnelService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { logger } from '../utils/logger';
import { TunnelConfig } from '../types/tunnel';
import { IncomingMessage, ServerResponse } from 'http';
import { ProxyService } from './ProxyService';
import zlib from 'zlib';
import { promisify } from 'util';

interface Connection {
clientId: string;
Expand Down Expand Up @@ -241,24 +243,37 @@ export class TunnelService extends EventEmitter {

// Handle response
if (response.type === 'response') {
// Ensure CORS headers for Next.js
const responseHeaders = {
...response.headers,
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'X-Requested-With, Content-Type, Accept'
};

// Copy all headers from the proxied response
const responseHeaders = { ...response.headers };

logger.debug(`Sending response for ${subdomain}:`, {
statusCode: response.statusCode,
headers: responseHeaders
headers: responseHeaders,
contentEncoding: responseHeaders['content-encoding']
});

res.writeHead(response.statusCode, responseHeaders);
// Handle gzipped content
if (response.data) {
const buffer = Buffer.from(response.data, 'base64');
res.end(buffer);
try {
const buffer = Buffer.from(response.data, 'base64');

if (responseHeaders['content-encoding'] === 'gzip') {
// Keep the content-encoding header and send as-is
res.writeHead(response.statusCode, responseHeaders);
res.end(buffer);
} else {
// If it's not gzipped, send as normal
delete responseHeaders['content-encoding'];
res.writeHead(response.statusCode, responseHeaders);
res.end(buffer);
}
} catch (error) {
logger.error(`Error processing response data for ${subdomain}:`, error);
res.writeHead(502);
res.end(JSON.stringify({ error: 'Error processing response' }));
}
} else {
res.writeHead(response.statusCode, responseHeaders);
res.end();
}
} else if (response.type === 'error') {
Expand Down

0 comments on commit 027da63

Please sign in to comment.