Move redirect to python

This commit is contained in:
Michaela Wheeler 2022-01-01 18:42:38 +11:00
parent 9d2ae9b7e1
commit 224ce5c792
4 changed files with 77 additions and 107 deletions

8
cdn.tf
View File

@ -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
}

View File

@ -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&currency_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

View File

@ -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))

View File

@ -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&currency_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);
};