-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
58 lines (47 loc) · 1.83 KB
/
Copy pathindex.js
File metadata and controls
58 lines (47 loc) · 1.83 KB
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
//
// Proxy MEGA S4 compatible API requests, sending notifications to a webhook
//
// Adapted from https://github.com/obezuk/worker-signed-s3-template
import { AwsClient } from 'aws4fetch'
const aws = new AwsClient({
"accessKeyId": AWS_ACCESS_KEY_ID,
"secretAccessKey": AWS_SECRET_ACCESS_KEY,
"region": AWS_DEFAULT_REGION,
"service": "s3"
});
addEventListener('fetch', function (event) {
event.respondWith(handleRequest(event.request))
});
function isListBucketRequest(path) {
if ("$path") {
return path.length === 0;
}
}
async function handleRequest(request) {
// Only allow GET and HEAD methods
if (request.method !== "GET" && request.method !== "HEAD") {
return new Response("Method Not Allowed", { status: 405 });
}
var url = new URL(request.url);
// Remove leading slashes from path
let path = url.pathname.replace(/^\//, '');
// Remove trailing slashes
path = path.replace(/\/$/, '');
// Reject list bucket requests unless configuration allows it
if (isListBucketRequest(path) && ALLOW_LIST_BUCKET !== "true") {
return new Response(null, {
status: 404,
statusText: "Not Found"
});
}
url.hostname = `${AWS_S3_BUCKET}.s3.${AWS_DEFAULT_REGION}.megas4.com`;
var signedRequest = await aws.sign(url);
console.log("Signed Request URL:", signedRequest.url);
console.log("Hostname: ", signedRequest.hostname)
console.log("Signed Request Method:", signedRequest.method);
console.log("Signed Request Headers:", [...signedRequest.headers.entries()]);
console.log("Signed Request Body:", signedRequest.body);
console.log("Signed Request Mode:", signedRequest.mode);
console.log("Signed Request Cache:", signedRequest.cache);
return await fetch(signedRequest, { "cf": { "cacheEverything": true } });
}