From 224ce5c7921bcb3bd46736b0781e652ed1501a63 Mon Sep 17 00:00:00 2001 From: Michaela Wheeler Date: Sat, 1 Jan 2022 18:42:38 +1100 Subject: [PATCH] Move redirect to python --- cdn.tf | 8 +-- lambda/redirect/__init__.py | 34 ++++++++++++ lambda/redirect/__main__.py | 39 ++++++++++++++ redirect/index.js | 103 ------------------------------------ 4 files changed, 77 insertions(+), 107 deletions(-) create mode 100644 lambda/redirect/__init__.py create mode 100644 lambda/redirect/__main__.py delete mode 100644 redirect/index.js diff --git a/cdn.tf b/cdn.tf index a0b020c..29655db 100644 --- a/cdn.tf +++ b/cdn.tf @@ -10,13 +10,13 @@ data "archive_file" "redirect" { resource "aws_lambda_function" "redirect" { function_name = "sondehub-redirect" - handler = "index.handler" - filename = "${path.module}/build/redirect.zip" - source_code_hash = data.archive_file.redirect.output_base64sha256 + handler = "redirect.handler" + s3_bucket = aws_s3_bucket_object.lambda.bucket + s3_key = aws_s3_bucket_object.lambda.key publish = true memory_size = 128 role = aws_iam_role.basic_lambda_role.arn - runtime = "nodejs14.x" + runtime = "python3.9" timeout = 3 } diff --git a/lambda/redirect/__init__.py b/lambda/redirect/__init__.py new file mode 100644 index 0000000..fdfca7e --- /dev/null +++ b/lambda/redirect/__init__.py @@ -0,0 +1,34 @@ +import re +def redirect(location): + return { + "status": '302', + "statusDescription": 'Found', + "headers": { + "location": [{ + "key": 'Location', + "value": location + }], + }, + } + +def handler(event, context): + request = event["Records"][0]["cf"]["request"] + uri = request["uri"] + if uri.startswith('/aprs/'): + sonde = uri.replace("/aprs/","") + return redirect('https://aprs.fi/#!call=' + sonde + '&timerange=36000&tail=36000') + if uri.startswith('/site/'): + site = uri.replace("/site/", "") + return redirect('https://sondehub.org/#!site=' + site) + if uri.startswith('/go/donate'): + return redirect('https://www.paypal.com/donate?business=YK2WHT6RNSYH8&item_name=SondeHub+Database+funding¤cy_code=USD') + if uri.startswith('/go/status'): + return redirect("https://cloudwatch.amazonaws.com/dashboard.html?dashboard=SondeHub&context=eyJSIjoidXMtZWFzdC0xIiwiRCI6ImN3LWRiLTE0Mzg0MTk0MTc3MyIsIlUiOiJ1cy1lYXN0LTFfZ2NlT3hwUnp0IiwiQyI6IjNuOWV0Y2ZxZm9zdm11aTc0NTYwMWFzajVzIiwiSSI6InVzLWVhc3QtMTo0ODI5YmQ4MC0yZmYzLTQ0MDktYjI1ZS0yOTE4MTM5YTgwM2MiLCJNIjoiUHVibGljIn0%3D") + if uri.startswith('/go/'): + tinyurl = uri.replace("/go/", "") + return redirect('https://tinyurl.com/' + tinyurl) + if uri != '/': + uri = re.sub(r"^\/","", uri) + sonde = re.sub(r'^(DFM|M10|M20|IMET|IMET54|MRZ|LMS6)-',"", uri) + return redirect('https://sondehub.org/?sondehub=1#!f=' + sonde + '&mz=9&qm=All&q=' + sonde) + return request \ No newline at end of file diff --git a/lambda/redirect/__main__.py b/lambda/redirect/__main__.py new file mode 100644 index 0000000..26a97de --- /dev/null +++ b/lambda/redirect/__main__.py @@ -0,0 +1,39 @@ +from . import * + +print(handler({ + "Records": [ + { + "cf": { + "config": { + "distributionId": "EXAMPLE" + }, + "request": { + "uri": "/MRZ-S1234", + "querystring": "auth=test&foo=bar", + "method": "GET", + "clientIp": "2001:cdba::3257:9652", + "headers": { + "host": [ + { + "key": "Host", + "value": "d123.cf.net" + } + ], + "user-agent": [ + { + "key": "User-Agent", + "value": "Test Agent" + } + ], + "user-name": [ + { + "key": "User-Name", + "value": "aws-cloudfront" + } + ] + } + } + } + } + ] +}, None)) \ No newline at end of file diff --git a/redirect/index.js b/redirect/index.js deleted file mode 100644 index d4264b9..0000000 --- a/redirect/index.js +++ /dev/null @@ -1,103 +0,0 @@ -'use strict'; - -exports.handler = (event, context, callback) => { - /* - * Generate HTTP redirect response with 302 status code and Location header. - */ - const request = event.Records[0].cf.request; - if (request.uri.startsWith('/aprs/')) { - var sonde = request.uri.replace(/^\/aprs\//, ""); - var specific_response = { - status: '302', - statusDescription: 'Found', - headers: { - location: [{ - key: 'Location', - value: 'https://aprs.fi/#!call=' + sonde + '&timerange=36000&tail=36000' - }], - }, - }; - callback(null, specific_response); - return; - } - if (request.uri.startsWith('/site/')) { - var site = request.uri.replace(/^\/site\//, ""); - var specific_response = { - status: '302', - statusDescription: 'Found', - headers: { - location: [{ - key: 'Location', - value: 'https://sondehub.org/#!site=' + site - }], - }, - }; - callback(null, specific_response); - return; - } - if (request.uri.startsWith('/go/')) { - var name = request.uri.replace(/^\/go\//, ""); - if (name == "donate") { - var specific_response = { - status: '302', - statusDescription: 'Found', - headers: { - location: [{ - key: 'Location', - value: 'https://www.paypal.com/donate?business=YK2WHT6RNSYH8&item_name=SondeHub+Database+funding¤cy_code=USD' - }], - }, - }; - callback(null, specific_response); - return; - } - if (name == "status") { - var specific_response = { - status: '302', - statusDescription: 'Found', - headers: { - location: [{ - key: 'Location', - value: 'https://cloudwatch.amazonaws.com/dashboard.html?dashboard=SondeHub&context=eyJSIjoidXMtZWFzdC0xIiwiRCI6ImN3LWRiLTE0Mzg0MTk0MTc3MyIsIlUiOiJ1cy1lYXN0LTFfZ2NlT3hwUnp0IiwiQyI6IjNuOWV0Y2ZxZm9zdm11aTc0NTYwMWFzajVzIiwiSSI6InVzLWVhc3QtMTo0ODI5YmQ4MC0yZmYzLTQ0MDktYjI1ZS0yOTE4MTM5YTgwM2MiLCJNIjoiUHVibGljIn0%3D' - }], - }, - }; - callback(null, specific_response); - return; - } - var specific_response = { - status: '302', - statusDescription: 'Found', - headers: { - location: [{ - key: 'Location', - value: 'https://tinyurl.com/' + name - }], - }, - }; - callback(null, specific_response); - return; - } - if (request.uri !== '/') { - var sonde = request.uri.replace(/^\//, "").replace(/^(DFM|M10|M20|IMET|IMET54|MRZ|LMS6)-/, ""); - var specific_response = { - status: '302', - statusDescription: 'Found', - headers: { - location: [{ - key: 'Location', - value: 'https://sondehub.org/?sondehub=1#!f=' + sonde + '&mz=9&qm=All&q=' + sonde - }], - }, - }; - callback(null, specific_response); - return; - } - - if (request.querystring !== '' && request.querystring !== undefined) { - // do not process if this is not an A-B test request - callback(null, request); - return; - } - callback(null, request); -};