create a simple http reverse-proxy server
This commit is contained in:
parent
4339149483
commit
7d710fc43a
7
config.json
Normal file
7
config.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"net": {
|
||||
"address": "127.0.0.1",
|
||||
"port": 9856
|
||||
},
|
||||
"target": "https://example.org"
|
||||
}
|
1207
package-lock.json
generated
Normal file
1207
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
31
package.json
Normal file
31
package.json
Normal file
@ -0,0 +1,31 @@
|
||||
{
|
||||
"name": "ai-proxy-server",
|
||||
"version": "0.0.1",
|
||||
"description": "",
|
||||
"main": "server.js",
|
||||
"scripts": {
|
||||
"test": "nodemon server.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "http://git.fullgream.online/fullgream/ai-proxy-server.git"
|
||||
},
|
||||
"keywords": [
|
||||
"AI",
|
||||
"koboldcpp",
|
||||
"openai",
|
||||
"gpt",
|
||||
"chat-gpt"
|
||||
],
|
||||
"author": "FullGreaM",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"body": "^5.1.0",
|
||||
"body-parser": "^1.20.3",
|
||||
"express": "^4.21.2",
|
||||
"node-fetch": "^2.6.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "^3.1.9"
|
||||
}
|
||||
}
|
68
server.js
Normal file
68
server.js
Normal file
@ -0,0 +1,68 @@
|
||||
'use strict';
|
||||
|
||||
const express = require("express");
|
||||
const bodyParser = require("body-parser");
|
||||
const bodyHand = require('body');
|
||||
const url = require('url');
|
||||
const path = require('path');
|
||||
const fetch = require('node-fetch');
|
||||
global.config = require("./config.json");
|
||||
|
||||
const app = express();
|
||||
|
||||
const address = global.config.net.address ?? "127.0.0.1";
|
||||
const reverseTo = global.config.target ?? "https://example.org";
|
||||
|
||||
function logger (...p) {
|
||||
return;
|
||||
console.log("[LOGGER]:", ...p);
|
||||
}
|
||||
|
||||
app.use('/*', async (req, res, next) => { // Myself body parser :)
|
||||
bodyHand(req, res, {
|
||||
limit: 9999999999,
|
||||
cache: false,
|
||||
encoding: 'base64'
|
||||
}, (err, body) => {
|
||||
if (!err) {
|
||||
req.body = Buffer.from(body, 'base64');
|
||||
} else {
|
||||
req.body = undefined;
|
||||
}
|
||||
next();
|
||||
});
|
||||
});
|
||||
|
||||
app.use("*", async (req, res) => {
|
||||
logger("REQ HEADERS:>", req.headers);
|
||||
logger("originalUrl:>", req.originalUrl);
|
||||
const params = { method: req.method.toUpperCase(), headers: req.headers, redirect: "follow", follow: 20 };
|
||||
delete params.headers.host;
|
||||
delete params.headers.referer;
|
||||
if (!["GET", "HEAD"].includes(params.method))
|
||||
params.body = req.body;
|
||||
const reqAddress = url.resolve(reverseTo, path.join("/", req.originalUrl));
|
||||
fetch(reverseTo, params)
|
||||
.then(async result => {
|
||||
const content = await result.text();
|
||||
logger("result:", result);
|
||||
logger("result.text():", content);
|
||||
Object.entries(result?.headers ? result.headers : new Object()).forEach(([header, value]) => {
|
||||
res.set(header, value);
|
||||
});
|
||||
res.status(result.status).send(content);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
res.status(500).send("<head><title>Error</title></head><body><h1>Error:</h1><hr/><p>Error catched. Check logs</p></body>");
|
||||
});
|
||||
});
|
||||
|
||||
app.listen(global.config.net.port, address, (err) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
console.log(`Koboldcpp reverse proxy server listened at ${
|
||||
address
|
||||
}:${global.config.net.port}`);
|
||||
})
|
Loading…
Reference in New Issue
Block a user