-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
231 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import type { | ||
Server as HTTPServer, | ||
IncomingMessage, | ||
ServerResponse, | ||
} from 'http'; | ||
import { request } from 'http'; | ||
import type { Socket } from 'net'; | ||
import { connect } from 'net'; | ||
|
||
export interface ProxyHandlersResult { | ||
connectRequests: IncomingMessage[]; | ||
httpForwardRequests: IncomingMessage[]; | ||
connections: Socket[]; | ||
} | ||
|
||
export function setupProxyServer(server: HTTPServer): ProxyHandlersResult { | ||
const connectRequests: IncomingMessage[] = []; | ||
const httpForwardRequests: IncomingMessage[] = []; | ||
const connections: Socket[] = []; | ||
|
||
server.on('connect', onconnect); | ||
server.on('request', onrequest); | ||
function onconnect( | ||
this: HTTPServer, | ||
req: IncomingMessage, | ||
socket: Socket, | ||
head: Buffer | ||
): void { | ||
(req as any).server = this; | ||
let host: string; | ||
let port: string; | ||
if (req.url?.includes(']:')) { | ||
[host, port] = req.url.slice(1).split(']:'); | ||
} else { | ||
[host, port] = (req.url ?? '').split(':'); | ||
} | ||
if (host === 'compass.mongodb.com' || host === 'downloads.mongodb.com') { | ||
// The snippet loader and update notifier can reach out to thes endpoints, | ||
// but we usually do not actually wait for this to happen or not in CI, | ||
// so we're just ignoring these requests here to avoid flaky behavior. | ||
socket.end(); | ||
return; | ||
} | ||
connectRequests.push(req); | ||
socket.unshift(head); | ||
socket.write('HTTP/1.0 200 OK\r\n\r\n'); | ||
const outbound = connect(+port, host); | ||
socket.pipe(outbound).pipe(socket); | ||
// socket.on('data', chk => console.log('[from client] ' + chk.toString())); | ||
// outbound.on('data', chk => console.log('[from server] ' + chk.toString())); | ||
const cleanup = () => { | ||
outbound.destroy(); | ||
socket.destroy(); | ||
}; | ||
outbound.on('error', cleanup); | ||
socket.on('error', cleanup); | ||
connections.push(socket, outbound); | ||
} | ||
function onrequest(req: IncomingMessage, res: ServerResponse) { | ||
httpForwardRequests.push(req); | ||
const proxyReq = request( | ||
req.url!, | ||
{ method: req.method, headers: req.headers }, | ||
(proxyRes) => proxyRes.pipe(res) | ||
); | ||
if (req.method === 'GET') proxyReq.end(); | ||
else req.pipe(proxyReq); | ||
} | ||
|
||
return { connections, connectRequests, httpForwardRequests }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters