-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
129 lines (117 loc) · 4.34 KB
/
index.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
async function handleRequest(event) {
const request = event.request
const cacheUrl = new URL(request.url)
// Construct the cache key from the cache URL
const cacheKey = new Request(cacheUrl, request)
const cache = caches.default
// Check whether the value is already available in the cache
// if not, you will need to fetch it from origin, and store it in the cache
// for future access
let response = await cache.match(cacheKey)
if (!response) {
// If not in cache, get it from origin
const init = {
headers: {
"content-type": "application/dns-json",
},
cf: {
cacheEverything: true,
cacheTtl: 10
}
};
let facebookRequest = null;
let isStatusCorrect = false;
let dnsRequest = await fetch("https://dns.google/resolve?name=facebook.com", init);
let dnsResponse = await dnsRequest.text();
var isUp = JSON.parse(dnsResponse).Status === 0;
if (isUp) {
facebookRequest = await fetch("https://www.facebook.com", init);
let _text = await facebookRequest.text();
isStatusCorrect = facebookRequest.status == 200;
}
var color = isUp ? (isStatusCorrect ? "green" : "yellow") : "red";
var text = isUp ? (isStatusCorrect ? "YES" : "KIND OF. ERROR PAGE.") : "NO";
let randomUUID = crypto.randomUUID();
let cssUUID = crypto.randomUUID();
response = new Response(`<!doctype html>
<html lang="en">
<head>
<!--
I added these for debugging purposes:
${facebookRequest ? facebookRequest.headers.get("date") : ""}
${dnsRequest.headers.get("date")}
${dnsResponse}
-->
<link rel="preconnect dns-prefetch" href="https://www.google-analytics.com">
<link rel="preconnect dns-prefetch" href="https://www.googletagmanager.com">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<!-- Google Tag Manager -->
<script nonce='${randomUUID}'>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;var n=d.querySelector('[nonce]');
n&&j.setAttribute('nonce',n.nonce||n.getAttribute('nonce'));f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','G-2T3FL0Y975');</script>
<!-- End Google Tag Manager -->
<meta name="Description" content="${text}">
<meta charset="utf-8">
<meta property=”og:description” content=”Is Facebook up yet?” />
<meta property=”og:type content=”Website” />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Is Facebook up yet?</title>
<style nonce='${cssUUID}'>
h1 {
font-size: 180px;
font-style: normal;
font-variant: normal;
font-weight: 700;
line-height: 26.4px;
color: ${color};
text-align: center;
}
h2 {
font-size: 16px;
font-style: normal;
font-variant: normal;
font-weight: 380;
line-height: 26.4px;
color: white;
text-align: center;
}
a {
color: green;
}
body {
background: black }
section {
position: absolute;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%) }
</style>
<section>
<h1>${text}</h1>
<br>
<br>
<h2>quickly imitated from <a href="https://isitdns.com/">https://isitdns.com/</a> with CF workers by <a href="https://rod.codes">rod.codes</a>. Code at <a href="https://github.com/rod-dot-codes/isfacebookupyet.com">https://github.com/rod-dot-codes/isfacebookupyet.com</a>.</h2>`
, {
headers: {
"content-type": "text/html;charset=UTF-8",
"cache-control": "no-cache",
"etag": `w/"${dnsRequest.headers.get("date")}"`,
"content-security-policy": `default-src 'self' www.google-analytics.com ssl.google-analytics.com;style-src 'nonce-${cssUUID}';script-src 'nonce-${randomUUID}' www.google-analytics.com ssl.google-analytics.com www.googletagmanager.com www.google-analytics.com;`,
},
});
// Store the fetched response as cacheKey
// Use waitUntil so you can return the response without blocking on
// writing to cache
event.waitUntil(cache.put(cacheKey, response.clone()))
}
return response
}
addEventListener("fetch", event => {
return event.respondWith(handleRequest(event))
})