-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
85 lines (79 loc) · 2.97 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
export default {
async fetch(request, env, ctx) {
try {
// Parse the request URL
const url = new URL(request.url);
// Get the path (e.g., '/username') and remove the leading slash
const key = url.pathname.substring(1);
// Get the host (e.g., 'meet.domain.com')
const host = url.hostname;
// If the key is empty, redirect to 'domain.com' (remove subdomain)
if (!key) {
// Split the hostname into parts
const domainParts = host.split('.');
// Check if there's a subdomain
if (domainParts.length > 2) {
// Remove the first element (subdomain)
domainParts.shift();
// Reconstruct the hostname without the subdomain
const newHost = domainParts.join('.');
// Redirect to the new URL without the subdomain
return Response.redirect(`${url.protocol}//${newHost}`, 302);
} else {
// Already at the main domain, no subdomain to remove
return Response.redirect(`${url.protocol}//${host}`, 302);
}
}
// Fetch the JSON data from the specified URL
const dataResponse = await fetch(
'https://mie.webchartnow.com/webchart.cgi?f=layoutnouser&&name=conferencelist&raw=1&json&secret=' + env.DMS_SECRET
);
// Check if the fetch was successful
if (!dataResponse.ok) {
// Return a 500 error if the data fetch failed
return new Response('Error fetching data', { status: 500 });
}
// Parse the fetched JSON data
let data;
try {
data = await dataResponse.json();
} catch (e) {
return new Response('Error parsing data', { status: 500 });
}
// Look up the key in the JSON data
if (data[key]) {
const targetUrl = data[key];
// Return a 302 redirect to the target URL
return Response.redirect(targetUrl, 302);
} else {
// Return a 404 error with HTML content
const notFoundHtml = `
<!DOCTYPE html>
<html>
<head>
<title>Meet Not Found</title>
<style>
body { font-family: Arial, sans-serif; text-align: center; padding: 50px; }
h1 { color: #333; }
p { font-size: 18px; }
a { color: #0066cc; text-decoration: none; }
a:hover { text-decoration: underline; }
</style>
</head>
<body>
<h1>Meet Not Found</h1>
<p>If you're an MIE employee trying to set up a meet URL, <a href="https://docs.google.com/document/d/1wKkzSFmSnH29boLEJsXgPf3QkOoqZqvwGDSIT9tUi8I/edit?usp=sharing">click here</a>.</p>
</body>
</html>
`;
return new Response(notFoundHtml, {
status: 404,
headers: { 'Content-Type': 'text/html' },
});
}
} catch (err) {
// Return a 500 error if an exception occurred
return new Response('Internal Server Error', { status: 500 });
}
},
};