-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use HTTP/2 to make requests from proxies #1375
Open
antoniosarosi
wants to merge
4
commits into
canary
Choose a base branch
from
antonio/google-ai-proxy-http2
base: canary
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
95177ff
Use HTTP/2 to make requests from fiddle-proxy
antoniosarosi 9d6c1e5
Add `Access-Control-Allow-Origin` header to proxy response
antoniosarosi 08b6cc6
VSCode ext proxy http2
antoniosarosi 20cf0fc
Remove old proxy from vscode ext & fix destructuring in for loop
antoniosarosi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
const cors = require('cors') | ||
const { createProxyMiddleware } = require('http-proxy-middleware') | ||
const assert = require('assert') | ||
const app = require('express')() | ||
require('dotenv').config() | ||
const cors = require('cors'); | ||
const express = require('express'); | ||
const http2 = require('http2'); | ||
const { URL } = require('url'); | ||
require('dotenv').config(); | ||
|
||
app.use(cors()) | ||
const app = express(); | ||
app.use(cors()); | ||
|
||
// From https://nodejs.org/api/url.html#url-strings-and-url-objects: | ||
// ┌────────────────────────────────────────────────────────────────────────────────────────────────┐ | ||
|
@@ -37,9 +39,9 @@ app.use(cors()) | |
const API_KEY_INJECTION_ALLOWED = { | ||
'https://api.openai.com': { Authorization: `Bearer ${process.env.OPENAI_API_KEY}` }, | ||
'https://api.anthropic.com': { 'x-api-key': process.env.ANTHROPIC_API_KEY }, | ||
'https://generativelanguage.googleapis.com': { 'x-goog-api-key': process.env.GOOGLE_API_KEY }, | ||
'https://generativelanguage.googleapis.com': { Authorization: `Bearer ${process.env.GOOGLE_API_KEY}` }, | ||
'https://openrouter.ai': { Authorization: `Bearer ${process.env.OPENROUTER_API_KEY}` }, | ||
} | ||
}; | ||
|
||
// Consult sam@ before changing this. | ||
for (const url of Object.keys(API_KEY_INJECTION_ALLOWED)) { | ||
|
@@ -49,62 +51,83 @@ for (const url of Object.keys(API_KEY_INJECTION_ALLOWED)) { | |
) | ||
} | ||
|
||
app.use( | ||
createProxyMiddleware({ | ||
changeOrigin: true, | ||
pathRewrite: (path, req) => { | ||
// Ensure the URL does not end with a slash | ||
if (path.endsWith('/')) { | ||
return path.slice(0, -1) | ||
// Middleware to handle proxy requests. | ||
app.use(async (req, res) => { | ||
const originalUrl = req.headers['baml-original-url']; | ||
if (!originalUrl) { | ||
res.status(400).send('Missing baml-original-url header'); | ||
return; | ||
} | ||
|
||
try { | ||
// Parse the original URL and append the request path. | ||
const targetUrl = new URL(originalUrl); | ||
|
||
const removeTrailingSlash = req.path.endsWith('/') | ||
? req.path.slice(0, -1) // Remove trailing slash | ||
: req.path; | ||
|
||
targetUrl.pathname = `${targetUrl.pathname}${removeTrailingSlash}`; | ||
|
||
const proxyReqHeaders = { ...req.headers }; // Clone incoming headers | ||
delete proxyReqHeaders.host; // Remove host header for upstream requests | ||
delete proxyReqHeaders.origin; // Remove origin header for upstream requests | ||
|
||
// It is very important that we ONLY resolve against API_KEY_INJECTION_ALLOWED | ||
// by using the URL origin! (i.e. NOT using str.startsWith - the latter can still | ||
// leak API keys to malicious subdomains e.g. https://api.openai.com.evil.com) | ||
const allowedHeaders = API_KEY_INJECTION_ALLOWED[targetUrl.origin]; | ||
|
||
if (allowedHeaders) { | ||
// Override headers. | ||
for ([header, value] of Object.entries(allowedHeaders)) { | ||
proxyReqHeaders[header.toLowerCase()] = value; | ||
} | ||
return path | ||
}, | ||
router: (req) => { | ||
// Extract the original target URL from the custom header | ||
const originalUrl = req.headers['baml-original-url'] | ||
|
||
if (typeof originalUrl === 'string') { | ||
return originalUrl | ||
} else { | ||
throw new Error('baml-original-url header is missing or invalid') | ||
} | ||
|
||
// Establish HTTP/2 connection | ||
const client = http2.connect(targetUrl.origin); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding error handling for |
||
const proxyReq = client.request({ | ||
':method': req.method, | ||
':path': `${targetUrl.pathname}${targetUrl.search}`, | ||
...proxyReqHeaders, | ||
}); | ||
|
||
// Pipe the request body to the upstream server. | ||
req.pipe(proxyReq); | ||
|
||
// Handle the response from the upstream server. | ||
proxyReq.on('response', (headers) => { | ||
// Set response headers | ||
for (const [key, value] of Object.entries(headers)) { | ||
if (key.startsWith(':')) continue; // Skip pseudo-headers | ||
res.setHeader(key, value); | ||
} | ||
}, | ||
logger: console, | ||
on: { | ||
proxyReq: (proxyReq, req, res) => { | ||
try { | ||
const bamlOriginalUrl = req.headers['baml-original-url'] | ||
if (bamlOriginalUrl === undefined) { | ||
return | ||
} | ||
const proxyOrigin = new URL(bamlOriginalUrl).origin | ||
// It is very important that we ONLY resolve against API_KEY_INJECTION_ALLOWED | ||
// by using the URL origin! (i.e. NOT using str.startsWith - the latter can still | ||
// leak API keys to malicious subdomains e.g. https://api.openai.com.evil.com) | ||
const headers = API_KEY_INJECTION_ALLOWED[proxyOrigin] | ||
if (headers === undefined) { | ||
return | ||
} | ||
for (const [header, value] of Object.entries(headers)) { | ||
proxyReq.setHeader(header, value) | ||
} | ||
proxyReq.removeHeader('origin') | ||
} catch (err) { | ||
// This is not console.warn because it's not important | ||
console.log('baml-original-url is not parsable', err) | ||
} | ||
}, | ||
proxyRes: (proxyRes, req, res) => { | ||
proxyRes.headers['Access-Control-Allow-Origin'] = '*' | ||
}, | ||
error: (error) => { | ||
console.error('proxy error:', error) | ||
}, | ||
}, | ||
}), | ||
) | ||
|
||
// Start web server on port 3000 | ||
res.statusCode = headers[':status']; | ||
}); | ||
|
||
proxyReq.on('data', (chunk) => { | ||
res.write(chunk); // Forward the data to the client | ||
}); | ||
|
||
proxyReq.on('end', () => { | ||
res.end(); // End the response | ||
client.close(); // Close the HTTP/2 connection | ||
}); | ||
|
||
proxyReq.on('error', (err) => { | ||
console.error('Proxy request error:', err); | ||
res.status(500).send('Internal Server Error'); | ||
client.close(); | ||
}); | ||
} catch (err) { | ||
console.error('Proxy error:', err); | ||
res.status(500).send('Failed to process request'); | ||
} | ||
}); | ||
|
||
// Start the server | ||
app.listen(3000, () => { | ||
console.log('Server is listening on port 3000') | ||
}) | ||
console.log('Server is listening on port 3000'); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Destructuring in the for loop is incorrect. It should be
for (const [header, value] of Object.entries(allowedHeaders)) {
. This issue is also present in other parts of the code where destructuring is used in a for loop.