forked from xicilion/gptproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgptproxy.js
72 lines (59 loc) · 2.24 KB
/
gptproxy.js
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
const http = require('http');
const ssl = require('ssl');
const io = require('io');
const path = require('path');
const crypto = require('crypto');
const util = require('util');
const OPENAI_API_KEY = 'sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
const OPENAI_API_ENTRY = 'api.openai.com';
const GITLAB_API_ENTRY = 'git.corp.com';
const hc = new http.Client();
const cache = new util.LruCache(100, 60000);
const timeout = 90000;
const svr = new ssl.Server(
crypto.loadCert(path.join(__dirname, 'cert.pem')), crypto.loadPKey(path.join(__dirname, 'key.pem')),
443, conn => {
if (conn.stream)
conn.stream.timeout = timeout;
else
conn.timeout = timeout;
var upconn;
const bs = new io.BufferedStream(conn);
bs.EOL = '\r\n';
try {
while (true) {
const req = new http.Request();
req.readFrom(bs);
const auth = req.headers["Authorization"];
if (auth && auth.substr(0, 7) === 'Bearer ') {
const token = auth.substr(7);
cache.get(token, token => {
console.log(token);
var res = hc.get(`https://${GITLAB_API_ENTRY}/api/v4/version`, {
headers: {
'PRIVATE-TOKEN': token
}
});
if (res.statusCode > 300)
throw new Error(res.json().message);
return true;
});
req.setHeader('Authorization', `Bearer ${OPENAI_API_KEY}`);
}
if (!upconn) {
upconn = ssl.connect(`ssl://${OPENAI_API_ENTRY}:443`);
upconn.stream.timeout = timeout;
upconn.copyTo(conn, (err, bytes) => {
conn.close(() => { });
upconn.close(() => { });
});
}
req.setHeader('Host', OPENAI_API_ENTRY);
req.sendTo(upconn);
}
} finally {
conn.close(() => { });
upconn.close(() => { });
}
});
svr.start();