Last active
November 24, 2024 00:40
-
-
Save mrfarhadir/78216dc946f5b032adb134d6a6814f80 to your computer and use it in GitHub Desktop.
Proxy middleware for Nuxt framework. (usage in the comment)
This file contains hidden or 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
import crypto from 'crypto'; | |
export function randomHex(length) { | |
return crypto.randomBytes(length / 2).toString('hex'); | |
} | |
export function createHash(url, secretKey) { | |
const randomHexStr = randomHex(64); | |
const timestamp = Date.now(); | |
const hmacPayload = `${randomHexStr}:${url}:${timestamp}`; | |
const hmac = crypto.createHmac('sha256', secretKey).update(hmacPayload).digest('hex'); | |
return `${randomHexStr}:${timestamp}:${hmac}`; | |
} | |
export function verifyHash(hash, url, secretKey) { | |
const [randomHexStr, timestamp, hmac] = hash.split(':'); | |
if (!randomHexStr || !timestamp || !hmac) { | |
console.error('Invalid hash structure'); | |
return false; | |
} | |
// Validate timestamp (e.g., within 5 minutes) | |
const timeDifference = Date.now() - parseInt(timestamp, 10); | |
const maxAllowedTime = 5 * 60 * 1000; // 5 minutes in milliseconds | |
if (timeDifference > maxAllowedTime) { | |
console.error('Hash expired'); | |
return false; | |
} | |
// Recompute the HMAC | |
const hmacPayload = `${randomHexStr}:${url}:${timestamp}`; | |
const expectedHmac = crypto.createHmac('sha256', secretKey).update(hmacPayload).digest('hex'); | |
// Validate HMAC | |
if (expectedHmac !== hmac) { | |
console.error('HMAC mismatch'); | |
return false; | |
} | |
return true; | |
} |
This file contains hidden or 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
import HttpProxy from 'http-proxy'; | |
import { createHash } from '~/utils/hash'; | |
const proxy = new HttpProxy(); | |
export default fromNodeMiddleware((req, res, next) => { | |
const { apiTarget: API_TARGET, secretKey: SECRET_KEY, useVerifyHash } = useRuntimeConfig(); | |
if (req.url.startsWith('/api')) { | |
proxy.web( | |
req, | |
res, | |
{ | |
target: API_TARGET, | |
}, | |
next, | |
); | |
proxy.on('proxyReq', (proxyReq, req, res) => { | |
if (useVerifyHash) { | |
const hash = createHash(req.url, SECRET_KEY); | |
proxyReq.setHeader('x-verify-hash', hash) | |
} | |
}) | |
} | |
else next() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install
npm install http-proxy
Usage
Create the following files in your project:
middleware/proxy.js
utils/hash.js
Add the following config to your
nuxt.config
Middleware config
apiTarget
: Your main endpointuseVerifyHash
: If set to true, proxy will add a hash to the header of each proxy request then in the target server you can check if the verifyHash value In the header, is correct or not.secretKey
: a key is used for hashingIf you use the verifyHash, REMOVE the verifyHash function from your front-end project