-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathworker.js
95 lines (84 loc) · 3.31 KB
/
worker.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// project url: https://github.com/yinm0591/cf-openai-with-sub-account-proxy
// item format: My_API_Sub_Keys : [OpenAI_API_Key, OpenAI_Organization_ID]
// set OpenAI_Organization_ID to null if it does not exist
const subKey = {
'security-key-1': ['sk-Cf***', 'YOUR_ORG_ID'],
'security-key-2': ['sk-Cf***', null]
};
const validPaths = [
'/v1/models',
'/v1/chat/completions',
'/dashboard/billing/usage',
'/dashboard/billing/subscription',
];
const enable_forwarding_OpenAI_Key = false;
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleOptions(request) {
let headers = request.headers
if (
headers.get("Origin") !== null &&
headers.get("Access-Control-Request-Method") !== null &&
headers.get("Access-Control-Request-Headers") !== null
) {
let respHeaders = {
"Access-Control-Allow-Origin": headers.get("Origin"),
"Access-Control-Allow-Methods": "GET, OPTIONS, POST",
"Access-Control-Max-Age": "86400",
"Access-Control-Allow-Headers": headers.get("Access-Control-Request-Headers"),
}
return new Response(null, {
headers: respHeaders,
})
}
else {
return new Response(null, {
headers: {
Allow: "GET, HEAD, POST, OPTIONS",
},
});
}
}
async function handleRequest(request) {
if (request.method === "OPTIONS") {
return handleOptions(request);
}
var url = new URL(request.url);
url.protocol = 'https';
url.hostname = 'api.openai.com';
var newRequest = new Request(url, {
headers: request.headers,
method: request.method,
body: request.body,
redirect: request.redirect,
})
let chkVaildPath = true;
console.log('url:', url.pathname);
const authHeader = newRequest.headers.get('Authorization');
if (enable_forwarding_OpenAI_Key && authHeader && authHeader.startsWith('Bearer sk-')) {
// Forwarding Openai API Key
chkVaildPath = false;
}
else if (authHeader && authHeader.startsWith('Bearer ')) {
const authKey = authHeader.replace('Bearer ', '');
if (!(authKey in subKey)) {
return new Response('{"error": {"message": "Incorrect API key","type": "invalid_request_error"}}\n', { status: 401 });
}
newRequest.headers.set("Authorization", "Bearer " + subKey[authKey][0]);
if (subKey[authKey][1] !== null) {
newRequest.headers.set("OpenAI-Organization", subKey[authKey][1]);
}
} else {
return new Response('{"error": {"message": "Missing API key","type": "invalid_request_error"}}\n', { status: 401 });
}
if (chkVaildPath && !validPaths.some(prefix => url.pathname.startsWith(prefix))) {
return new Response('{"error": {"message": "API Error","type": "invalid_request_error"}}\n', { status: 401 });
}
const response = await fetch(newRequest, { cf: { stream: true } });
const newResponse = new Response(response.body, response);
newResponse.headers.set('Content-Length', response.headers.get('Content-Length'));
newResponse.headers.set('Access-Control-Allow-Origin', "*");
newResponse.headers.set('Access-Control-Request-Method', 'GET, OPTIONS, POST');
return newResponse;
}