From 224bbaded6be94d7f4640f64249f8233fa32413e Mon Sep 17 00:00:00 2001 From: dangered wolf Date: Sun, 28 Aug 2022 17:17:55 -0400 Subject: [PATCH] Add version endpoint --- src/server.ts | 23 ++++++++++++++++ src/strings.ts | 71 ++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 89 insertions(+), 5 deletions(-) diff --git a/src/server.ts b/src/server.ts index 85d936b..f731762 100644 --- a/src/server.ts +++ b/src/server.ts @@ -6,6 +6,7 @@ import { handleStatus } from './status'; import { Strings } from './strings'; import motd from '../motd.json'; +import { sanitizeText } from './helpers/utils'; const router = Router(); @@ -146,6 +147,27 @@ const profileRequest = async (request: Request) => { } }; +const versionRequest = async (request: Request) => { + return new Response(Strings.VERSION_HTML.format({ + rtt: request.cf?.clientTcpRtt ? `🏓 ${request.cf.clientTcpRtt} ms RTT` : '', + colo: request.cf?.colo || '??', + httpversion: request.cf?.httpProtocol || 'Unknown HTTP Version', + tlsversion: request.cf?.tlsVersion || 'Unknown TLS Version', + ip: request.headers.get('x-real-ip') || request.headers.get('cf-connecting-ip') || 'Unknown IP', + city: request.cf?.city || 'Unknown City', + region: request.cf?.region || request.cf?.country || 'Unknown Region', + country: request.cf?.country || 'Unknown Country', + asn: `AS${request.cf?.asn || '??'} (${request.cf?.asOrganization || 'Unknown ASN'})`, + ua: sanitizeText(request.headers.get('user-agent') || 'Unknown User Agent'), + }), { + headers: { + ...Constants.RESPONSE_HEADERS, + 'cache-control': 'max-age=1' + }, + status: 200 + }); +} + /* TODO: is there any way to consolidate these stupid routes for itty-router? I couldn't find documentation allowing for regex matching */ router.get('/:prefix?/:handle/status/:id', statusRequest); @@ -160,6 +182,7 @@ router.get('/:prefix?/:handle/status/:id/:language', statusRequest); router.get('/:prefix?/:handle/statuses/:id/:language', statusRequest); router.get('/status/:id', statusRequest); router.get('/status/:id/:language', statusRequest); +router.get('/version', versionRequest); /* Oembeds (used by Discord to enhance responses) diff --git a/src/strings.ts b/src/strings.ts index 6a3638b..b8aa33c 100644 --- a/src/strings.ts +++ b/src/strings.ts @@ -37,14 +37,14 @@ export const Strings = { - + :( @@ -63,7 +63,68 @@ This is caused by Twitter API downtime or a new bug. Try again in a little while

You hit a snag that broke ${BRANDING_NAME}. It's not your fault though—This is usually caused by a Twitter outage or a new bug.

${RELEASE_NAME}

- `.replace(/( {2}|\n)/g, ''), + `.replace(/( {2})/g, '').replace(/>[\s|\n]+<'), + VERSION_HTML: ` + + + + + + + + + + + ${BRANDING_NAME} + + + +

${BRANDING_NAME}

+

A better Tweet embedding service, by @dangeredwolf, et al.

+

Worker release: ${RELEASE_NAME}

+
+

Stats for nerds:

+

Edge: + {rtt} {colo}

+

Connection: + {httpversion} - {tlsversion}

+

User Agent: + {ua}

+ + `.replace(/( {2})/g, '').replace(/>[\s|\n]+<'), DEFAULT_AUTHOR_TEXT: 'Twitter', QUOTE_TEXT: `═ ↘️ Quoting {name} (@{screen_name}) ═════`,