File size: 2,657 Bytes
2889205
 
 
7f84064
2889205
7f84064
2889205
177a238
2889205
177a238
 
7f84064
71c1ee6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f84064
d5b575b
 
 
 
71c1ee6
d5b575b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71c1ee6
 
 
 
 
 
 
 
d5b575b
 
71c1ee6
 
 
3e0d39c
177a238
d5b575b
 
 
3e0d39c
 
177a238
d5b575b
 
2889205
 
 
d5b575b
3e0d39c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const express = require('express');
const proxy = require('express-http-proxy');
const app = express();

const targetUrl = 'https://api.openai.com';
const openaiKey = process.env.OPENAI_KEY;
const port = 7860;
const baseUrl = getExternalUrl(process.env.SPACE_ID);

let requestDetails = [];
let ipRequestCounts = {};

// Declare the proxy as middleware which can be reused
const openAiProxy = proxy(targetUrl, {
    proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
        proxyReqOpts.headers['Authorization'] = 'Bearer ' + openaiKey;
        return proxyReqOpts;
    },
    userResDecorator: (proxyRes, proxyResData, userReq, userRes) => {
        const data = proxyResData.toString('utf8');
        if (proxyRes.statusCode === 429) {
            // Handling at userResDecorator might be skipped based on your situation, thus handling it later
            return data;
        }
        return data;  // Pass through all other non-429 responses
    }
});

// Apply rate limiting and API proxying
app.use('/api', (req, res, next) => {
    const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
    const currentCount = ipRequestCounts[ip] || 0;

    if (currentCount >= 1) {
        res.status(429).send('You have reached your request limit of 1 message. Please contact support for more details.');
        return;
    }

    ipRequestCounts[ip] = currentCount + 1;
    const timestamp = new Date().toISOString();
    const requestData = {
        requestNumber: ipRequestCounts[ip],
        ip: ip,
        timestamp: timestamp,
        text: req.method + ' ' + req.url
    };
    requestDetails.push(requestData);
    console.log(`Request ${requestData.requestNumber} from IP ${requestData.ip} on ${requestData.timestamp}`);

    next();
}, openAiProxy);

// Modify proxy responses after the proxy has finished
app.use('/api', function(req, res, next) {
    res.on('finish', function() {
        if (res.statusCode === 429 && !res.headersSent) {
            res.status(429).json({
                message: 'Custom message for 429 status.'
            });
        }
    });
    next();
});

app.get("/", (req, res) => {
    let requestsInfo = requestDetails.map(detail =>
        `Request ${detail.requestNumber} from IP ${detail.ip} on ${detail.timestamp}`).join('<br>');
    res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}.<br><br>${requestsInfo}`);
});

function getExternalUrl(spaceId) {
    const [username, spacename] = spaceId.split("/");
    return `https://${username}-${spacename.replace(/_/g, "-")}.hf.space/api/v1`;
}

app.listen(port, () => {
    console.log(`Reverse proxy server running on ${baseUrl}.`);
});