Badman
commited on
Commit
·
fd4e061
1
Parent(s):
8bcd306
Upload 2 files
Browse files- index.js +97 -0
- package.json +17 -0
index.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
console.log('Debug init started');
|
| 2 |
+
|
| 3 |
+
const express = require('express');
|
| 4 |
+
const { createProxyMiddleware } = require('http-proxy-middleware');
|
| 5 |
+
|
| 6 |
+
// Create Express Server
|
| 7 |
+
const app = express();
|
| 8 |
+
|
| 9 |
+
function GetEnv(name)
|
| 10 |
+
{
|
| 11 |
+
const value = process.env[name];
|
| 12 |
+
if (value === undefined) {
|
| 13 |
+
return 'bad_value';
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
return value;
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
// Configuration
|
| 20 |
+
const PORT = 7860;
|
| 21 |
+
const API_SERVICE_URL = GetEnv('SECRET_PROXYSITE_URL');
|
| 22 |
+
const NKEY = GetEnv('SECRET_PROXY_KEY');
|
| 23 |
+
|
| 24 |
+
// Info GET endpoint
|
| 25 |
+
app.get('/info', (req, res, next) => {
|
| 26 |
+
res.send('This is a proxy service which proxies to Billing and Account APIs.');
|
| 27 |
+
});
|
| 28 |
+
|
| 29 |
+
// Info GET endpoint
|
| 30 |
+
app.get('/sink/v1/chat/completions', (req, res, next) => {
|
| 31 |
+
res.send('Sink test.');
|
| 32 |
+
|
| 33 |
+
console.log(req);
|
| 34 |
+
});
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
function rewriteRequest(proxyReq, req, res)
|
| 39 |
+
{
|
| 40 |
+
proxyReq.setHeader("origin", "");
|
| 41 |
+
proxyReq.setHeader("referer", "");
|
| 42 |
+
|
| 43 |
+
proxyReq.setHeader('x-forwarded-for', '');
|
| 44 |
+
proxyReq.setHeader('x-forwarded-proto', '');
|
| 45 |
+
proxyReq.setHeader('x-forwarded-port', '');
|
| 46 |
+
proxyReq.setHeader('x-amzn-trace-id', '');
|
| 47 |
+
proxyReq.setHeader('x-request-id', '');
|
| 48 |
+
|
| 49 |
+
//console.log(req);
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
// Proxy endpoints
|
| 54 |
+
app.use(`/${NKEY}`, createProxyMiddleware({
|
| 55 |
+
target: API_SERVICE_URL,
|
| 56 |
+
|
| 57 |
+
on: {
|
| 58 |
+
proxyReq: rewriteRequest
|
| 59 |
+
},
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
changeOrigin: true,
|
| 63 |
+
pathRewrite: {
|
| 64 |
+
[`^/${NKEY}`]: '',
|
| 65 |
+
},
|
| 66 |
+
}));
|
| 67 |
+
|
| 68 |
+
// Proxy endpoints
|
| 69 |
+
app.use(`/redirect_test/`, createProxyMiddleware({
|
| 70 |
+
target: "https://iamthebadman-proxyone.hf.space/sink/",
|
| 71 |
+
|
| 72 |
+
on: {
|
| 73 |
+
proxyReq: rewriteRequest
|
| 74 |
+
},
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
changeOrigin: true,
|
| 78 |
+
pathRewrite: {
|
| 79 |
+
[`^/redirect_test`]: '',
|
| 80 |
+
},
|
| 81 |
+
}));
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
async function start() {
|
| 85 |
+
console.log("Start A");
|
| 86 |
+
|
| 87 |
+
app.listen(PORT, async () => {
|
| 88 |
+
console.log("Listening now!");
|
| 89 |
+
});
|
| 90 |
+
|
| 91 |
+
console.log("Start B");
|
| 92 |
+
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
start();
|
| 96 |
+
|
| 97 |
+
console.log('Debug ok started');
|
package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "my-proxy",
|
| 3 |
+
"version": "1.0.0",
|
| 4 |
+
"description": "Reverse proxy",
|
| 5 |
+
"scripts": {
|
| 6 |
+
"start": "node index.js"
|
| 7 |
+
},
|
| 8 |
+
"engines": {
|
| 9 |
+
"node": ">=18.0.0"
|
| 10 |
+
},
|
| 11 |
+
"author": "",
|
| 12 |
+
"license": "MIT",
|
| 13 |
+
"dependencies": {
|
| 14 |
+
"express": "^4.18.2",
|
| 15 |
+
"http-proxy-middleware": "^3.0.0-beta.1"
|
| 16 |
+
}
|
| 17 |
+
}
|