2021-05-23 13:14:15 +00:00
|
|
|
import sys
|
2021-01-30 11:07:34 +00:00
|
|
|
import json
|
|
|
|
import boto3
|
|
|
|
import zlib
|
|
|
|
import base64
|
|
|
|
import datetime
|
|
|
|
import functools
|
2021-01-31 00:47:56 +00:00
|
|
|
from email.utils import parsedate
|
2021-02-01 06:08:59 +00:00
|
|
|
import os
|
2021-01-30 11:07:34 +00:00
|
|
|
|
2021-05-24 06:54:41 +00:00
|
|
|
|
2021-01-30 11:07:34 +00:00
|
|
|
# todo
|
|
|
|
# we should add some value checking
|
|
|
|
# we typically perform version banning here based on user agent
|
|
|
|
# error handling - at the moment we bail on a single failure
|
|
|
|
# report to the user what's happened
|
|
|
|
|
|
|
|
|
2021-05-23 13:14:15 +00:00
|
|
|
def set_connection_header(request, operation_name, **kwargs):
|
|
|
|
request.headers['Connection'] = 'keep-alive'
|
2021-01-30 11:07:34 +00:00
|
|
|
|
2021-05-24 06:54:41 +00:00
|
|
|
sns = boto3.client("sns",region_name="us-east-1")
|
2021-06-05 04:20:31 +00:00
|
|
|
sns.meta.events.register('request-created.sns', set_connection_header)
|
2021-02-01 04:10:25 +00:00
|
|
|
|
2021-05-24 06:54:41 +00:00
|
|
|
def post(payload):
|
|
|
|
sns.publish(
|
|
|
|
TopicArn=os.getenv("SNS_TOPIC"),
|
|
|
|
Message=json.dumps(payload)
|
|
|
|
)
|
2021-01-30 11:07:34 +00:00
|
|
|
|
2021-06-05 04:20:31 +00:00
|
|
|
def upload(event, context):
|
2021-05-24 10:44:29 +00:00
|
|
|
if "isBase64Encoded" in event and event["isBase64Encoded"] == True:
|
|
|
|
event["body"] = base64.b64decode(event["body"])
|
|
|
|
if (
|
|
|
|
"content-encoding" in event["headers"]
|
|
|
|
and event["headers"]["content-encoding"] == "gzip"
|
|
|
|
):
|
|
|
|
event["body"] = zlib.decompress(event["body"], 16 + zlib.MAX_WBITS)
|
|
|
|
time_delta = None
|
|
|
|
if "date" in event["headers"]:
|
2021-01-31 00:47:56 +00:00
|
|
|
try:
|
2021-05-24 10:44:29 +00:00
|
|
|
time_delta_header = event["headers"]["date"]
|
|
|
|
time_delta = (
|
|
|
|
datetime.datetime(*parsedate(time_delta_header)[:7])
|
|
|
|
- datetime.datetime.utcnow()
|
|
|
|
).total_seconds()
|
2021-01-31 00:47:56 +00:00
|
|
|
except:
|
2021-05-24 10:44:29 +00:00
|
|
|
pass
|
|
|
|
payloads = json.loads(event["body"])
|
|
|
|
to_sns = []
|
|
|
|
first = False
|
|
|
|
for payload in payloads:
|
|
|
|
if "user-agent" in event["headers"]:
|
|
|
|
event["time_server"] = datetime.datetime.now().isoformat()
|
|
|
|
payload["user-agent"] = event["headers"]["user-agent"]
|
|
|
|
payload["position"] = f'{payload["lat"]},{payload["lon"]}'
|
2021-05-28 12:21:53 +00:00
|
|
|
current_year = datetime.datetime.now().year
|
|
|
|
if payload["datetime"].split("-")[0] not in [str(current_year), str(current_year-1), str(current_year+1)]:
|
|
|
|
# skip any telm that's not last year, this year or next year
|
|
|
|
print(payload)
|
|
|
|
print("Not within time range - not suitable to add to queues")
|
|
|
|
continue
|
2021-05-24 10:44:29 +00:00
|
|
|
if time_delta:
|
|
|
|
payload["upload_time_delta"] = time_delta
|
|
|
|
if "uploader_position" in payload:
|
|
|
|
if not payload["uploader_position"]:
|
|
|
|
payload.pop("uploader_position")
|
|
|
|
else:
|
|
|
|
(payload["uploader_alt"], payload["uploader_position"]) = (
|
|
|
|
payload["uploader_position"][2],
|
|
|
|
f"{payload['uploader_position'][0]},{payload['uploader_position'][1]}",
|
|
|
|
)
|
|
|
|
to_sns.append(payload)
|
|
|
|
post(to_sns)
|
2021-05-23 13:14:15 +00:00
|
|
|
def lambda_handler(event, context):
|
2021-06-05 04:20:31 +00:00
|
|
|
upload(event, context)
|
2021-02-01 06:08:59 +00:00
|
|
|
return {"statusCode": 200, "body": "^v^ telm logged"}
|
|
|
|
|
2021-01-31 00:47:56 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
payload = {
|
|
|
|
"version": "2.0",
|
|
|
|
"routeKey": "PUT /sondes/telemetry",
|
|
|
|
"rawPath": "/sondes/telemetry",
|
|
|
|
"rawQueryString": "",
|
|
|
|
"headers": {
|
|
|
|
"accept": "*/*",
|
|
|
|
"accept-encoding": "gzip, deflate",
|
|
|
|
"content-encoding": "gzip",
|
|
|
|
"content-length": "2135",
|
|
|
|
"content-type": "application/json",
|
|
|
|
"host": "api.v2.sondehub.org",
|
|
|
|
"user-agent": "autorx-1.4.1-beta4",
|
|
|
|
"x-amzn-trace-id": "Root=1-6015f571-6aef2e73165042d53fcc317a",
|
|
|
|
"x-forwarded-for": "103.107.130.22",
|
|
|
|
"x-forwarded-port": "443",
|
|
|
|
"x-forwarded-proto": "https",
|
2021-02-01 06:08:59 +00:00
|
|
|
"date": "Sun, 31 Jan 2021 00:21:45 GMT",
|
2021-01-31 00:47:56 +00:00
|
|
|
},
|
|
|
|
"requestContext": {
|
|
|
|
"accountId": "143841941773",
|
|
|
|
"apiId": "r03szwwq41",
|
|
|
|
"domainName": "api.v2.sondehub.org",
|
|
|
|
"domainPrefix": "api",
|
|
|
|
"http": {
|
|
|
|
"method": "PUT",
|
|
|
|
"path": "/sondes/telemetry",
|
|
|
|
"protocol": "HTTP/1.1",
|
|
|
|
"sourceIp": "103.107.130.22",
|
2021-02-01 06:08:59 +00:00
|
|
|
"userAgent": "autorx-1.4.1-beta4",
|
2021-01-31 00:47:56 +00:00
|
|
|
},
|
|
|
|
"requestId": "Z_NJvh0RoAMEJaw=",
|
|
|
|
"routeKey": "PUT /sondes/telemetry",
|
|
|
|
"stage": "$default",
|
|
|
|
"time": "31/Jan/2021:00:10:25 +0000",
|
2021-02-01 06:08:59 +00:00
|
|
|
"timeEpoch": 1612051825409,
|
2021-01-31 00:47:56 +00:00
|
|
|
},
|
|
|
|
"body": "H4sIAHD1FWAC/8WbX2/byBXFv0rg5/Vg7v87eduHYh8LJNui2KIwlFjZGHDsVJbTLop+996hszVHXpECKI7hJ1G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aSagwwZBA+LpyEpqwiYibYyZwRYz4b3rhATdYJxPZcLPLjLl2NxhQ2Y1iwR2QCKTwAs5IQ0SlgpIaZc7yCkvXRbF0hsJDSQcvfRDogVCjwGBCUV4tqeGqMPmwO+txMeB0BjRm7gqSgQuXxJ9IqonD3UtmuPjp/JgZ+bBjvEQr7DML4gSr85DlKqC05m2Wt035QdLX7WRcikQ0BsIj/sO+fWA8GObAuu/peNsok3SQVkWIPBJB2pUUxRop4zamLnUgRL2jqpiVDSR0o2Iw0TbyniVg5uuCTHKs/klaQcHGhdSaJqJUnMpxdZt+FOn3iImeseXmKPexaj8SlXC89h/SuM/UVFn1zjIOiTaHFcCUy0TUQ/CWZC0mYTL8u3k1LsJE7/vbj3Zf+q5vYbDMSbqLlHP89LSO+wSrcvUNslEwbpaxN7mVLp4PyBJZymBcSGs2TtKiUMkms3kuSkTwjq/m5xKhzUOLPzSbrRIeG21Odjno8ulhPYmIvgXLIAdi0QbUjkdKxKYxGG+KZdzByKgiL3Y0pFHRFhtW6TcJBIlFV7uN6xzIIHxrSGA2AKJf/wPWvB/4NlMAAA=",
|
2021-02-01 06:08:59 +00:00
|
|
|
"isBase64Encoded": True,
|
2021-01-31 00:47:56 +00:00
|
|
|
}
|
2021-05-23 13:14:15 +00:00
|
|
|
print(lambda_handler(payload, {}))
|