|
|
const express = require('express'); |
|
|
const request = require('request'); |
|
|
const rateLimit = require('express-rate-limit'); |
|
|
const path = require('path'); |
|
|
const bodyParser = require('body-parser'); |
|
|
const multer = require('multer'); |
|
|
const fs=require('fs'); |
|
|
|
|
|
|
|
|
let limiter_max=50; |
|
|
|
|
|
const PORT = process.env.PORT || 7860; |
|
|
const app = express(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.use((req, res, next) => { |
|
|
res.header('Access-Control-Allow-Origin', '*'); |
|
|
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); |
|
|
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); |
|
|
|
|
|
next(); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
app.use(express.static(path.join(__dirname, 'public'))); |
|
|
|
|
|
|
|
|
|
|
|
const limiter = rateLimit({ |
|
|
windowMs: 1 * 60 * 1000, |
|
|
max: limiter_max, |
|
|
message: 'Too many requests from this IP, please try again after a minute' |
|
|
}); |
|
|
|
|
|
|
|
|
app.use(limiter); |
|
|
|
|
|
|
|
|
app.use('/openai', (req, res) => { |
|
|
|
|
|
let targetUrl = req.query.url; |
|
|
|
|
|
if (!targetUrl) { |
|
|
targetUrl = process.env.AI_SERVER_URL +'/openai/'+ req.path; |
|
|
} |
|
|
|
|
|
console.log(`app.use('/openai'): Proxying request to: ${targetUrl}`); |
|
|
|
|
|
try { |
|
|
|
|
|
const options = { |
|
|
url: targetUrl, |
|
|
headers: { |
|
|
'User-Agent': 'Node.js Request Proxy', |
|
|
'Authorization': `Bearer ${process.env.AI_API_KEY}` |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
Object.keys(req.headers).forEach(key => { |
|
|
if (!['host', 'cookie'].includes(key)) { |
|
|
options.headers[key] = req.headers[key]; |
|
|
} |
|
|
}); |
|
|
|
|
|
req.pipe(request(options)).pipe(res); |
|
|
} catch (error) { |
|
|
console.error(`Error: ${error.message}`); |
|
|
res.status(500).send('Error occurred while proxying'); |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const upload = multer(); |
|
|
|
|
|
|
|
|
const storeDir = path.join(__dirname, 'kvStore'); |
|
|
|
|
|
|
|
|
if (!fs.existsSync(storeDir)) { |
|
|
fs.mkdirSync(storeDir); |
|
|
} |
|
|
|
|
|
|
|
|
const getFilePath = (key) => path.join(storeDir, `${key}.json`); |
|
|
|
|
|
|
|
|
app.post('/kv/set', upload.none(), (req, res) => { |
|
|
const key = req.body.key; |
|
|
const value = req.body.value; |
|
|
|
|
|
if (!key || !value) { |
|
|
return res.status(400).json({ error: 'Missing key or value' }); |
|
|
} |
|
|
|
|
|
const filePath = getFilePath(key); |
|
|
fs.writeFile(filePath, value, (err) => { |
|
|
if (err) { |
|
|
return res.status(500).json({ error: 'Failed to save key-value pair' }); |
|
|
} |
|
|
res.status(200).json({ message: 'Key-value pair set successfully' }); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
app.get('/kv/get', (req, res) => { |
|
|
const key = req.query.key; |
|
|
|
|
|
if (!key) { |
|
|
return res.status(400).json({ error: 'Missing key' }); |
|
|
} |
|
|
|
|
|
const filePath = getFilePath(key); |
|
|
fs.readFile(filePath, 'utf8', (err, data) => { |
|
|
if (err) { |
|
|
if (err.code === 'ENOENT') { |
|
|
return res.status(404).json({ error: 'Key not found' }); |
|
|
} |
|
|
return res.status(500).json({ error: 'Failed to retrieve key-value pair' }); |
|
|
} |
|
|
res.status(200).json({ key: key, value: JSON.parse(data) }); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.listen(PORT, () => { |
|
|
console.log(`CORS Proxy running on http://localhost:${PORT}`); |
|
|
}); |
|
|
|