-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Jon Breen <[email protected]>
- Loading branch information
1 parent
88a0214
commit 7692625
Showing
1 changed file
with
74 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,12 +51,19 @@ export default { | |
.json() | ||
.then(async (data: any) => { | ||
let dataPoint: AnalyticsEngineDataPoint = { indexes: [], blobs: [] }; | ||
let ipAddress: string =""; | ||
let asn: string =""; | ||
let city: string =""; | ||
if (request.cf) { | ||
dataPoint.blobs!.push(request.cf.clientTrustScoretr as string); | ||
dataPoint.blobs!.push(request.cf.asn as string); | ||
dataPoint.blobs!.push(request.headers.get('cf-connecting-ip') as string); | ||
asn = request.cf.asn as string; | ||
dataPoint.blobs!.push(asn); | ||
ipAddress = request.headers.get('cf-connecting-ip') as string | ||
dataPoint.blobs!.push(ipAddress); | ||
dataPoint.blobs!.push(request.headers.get('User-Agent') as string); | ||
if (request.cf.city) { | ||
dataPoint.blobs!.push(request.cf.city as string); | ||
city = request.cf.city as string; | ||
dataPoint.blobs!.push(city); | ||
} | ||
if (request.cf.country) { | ||
dataPoint.blobs!.push(request.cf.country as string); | ||
|
@@ -103,38 +110,75 @@ export default { | |
dataPoint.blobs?.push(data.skip); | ||
dataPoint.blobs?.push(data.orderby); | ||
} | ||
let response = await fetch(url, { | ||
cf: { | ||
cacheEverything: true, | ||
cacheTtl: 600 | ||
}, | ||
headers: { | ||
"api-key": env.apikey, | ||
"content-type": "application/json;charset=UTF-8", | ||
}, | ||
body: requestBody, | ||
method: "POST" | ||
}); | ||
if (dataPoint) { | ||
dataPoint.blobs?.push(response.status.toString()); | ||
env.Analytics.writeDataPoint(dataPoint); | ||
|
||
let isLeech: boolean = false; | ||
if (city == "Wimbledon") { | ||
isLeech = true; | ||
} | ||
|
||
const headers = new Headers(); | ||
headers.set("Cache-Control", "max-age=600"); | ||
headers.append("Content-Type", "application/json"); | ||
headers.append("Access-Control-Allow-Origin", "*"); | ||
headers.append("Access-Control-Allow-Methods", "POST,GET,OPTIONS"); | ||
if (!isLeech) { | ||
let response = await fetch(url, { | ||
cf: { | ||
cacheEverything: true, | ||
cacheTtl: 600 | ||
}, | ||
headers: { | ||
"api-key": env.apikey, | ||
"content-type": "application/json;charset=UTF-8", | ||
}, | ||
body: requestBody, | ||
method: "POST" | ||
}); | ||
if (dataPoint) { | ||
dataPoint.blobs?.push(response.status.toString()); | ||
env.Analytics.writeDataPoint(dataPoint); | ||
} | ||
|
||
if (response.status != 200) { | ||
return new Response(response.body, { headers, status: response.status }) | ||
} | ||
const headers = new Headers(); | ||
headers.set("Cache-Control", "max-age=600"); | ||
headers.append("Content-Type", "application/json"); | ||
headers.append("Access-Control-Allow-Origin", "*"); | ||
headers.append("Access-Control-Allow-Methods", "POST,GET,OPTIONS"); | ||
|
||
if (response.status != 200) { | ||
return new Response(response.body, { headers, status: response.status }) | ||
} | ||
|
||
let body: any = await response.json(); | ||
body["@odata.context"] = null; | ||
let bodyJson = JSON.stringify(body); | ||
|
||
let body: any = await response.json(); | ||
body["@odata.context"] = null; | ||
let bodyJson = JSON.stringify(body); | ||
return new Response(bodyJson, { headers }) | ||
} else { | ||
const leechResponse = { | ||
"@odata.context": null, | ||
"@odata.count": 1, | ||
"@search.facets": { | ||
"subjects": [], | ||
"podcastName": [] | ||
}, | ||
"value": [{ | ||
"@search.score": 1.0, | ||
"id": "00000000-0000-0000-0000-000000000000", | ||
"episodeTitle": "Leech Detected", | ||
"podcastName": "Leech Detected", | ||
"episodeDescription": "Contact [email protected].", | ||
"release": "1970-01-01T00:00:00Z", | ||
"duration": "01:00:00.0000000", | ||
"explicit": false, | ||
"spotify": null, | ||
"apple": , | ||
"youtube": "https://www.youtube.com/watch?v=dQw4w9WgXcQ", | ||
"subjects": [] | ||
} | ||
] | ||
} | ||
; | ||
dataPoint.blobs!.push("Leech"); | ||
env.Analytics.writeDataPoint(dataPoint); | ||
return new Response(JSON.stringify(leechResponse); | ||
} | ||
|
||
return new Response(bodyJson, { headers }) | ||
}); | ||
} | ||
|
||
|