Creates main.py
This creates main.py, which is run in an EC2 instance and receives queries
This commit is contained in:
parent
12320fcdab
commit
714109a5ae
|
|
@ -0,0 +1,84 @@
|
||||||
|
import boto3
|
||||||
|
|
||||||
|
from flask import Flask, render_template, request
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
dynamodb = boto3.resource('dynamodb', endpoint_url="http://dynamodb.us-east-1.amazonaws.com")
|
||||||
|
table = dynamodb.Table('LogSieve')
|
||||||
|
|
||||||
|
|
||||||
|
HTTP_X_REAL_IP = "HTTP_X_REAL_IP"
|
||||||
|
WHITELISTED = ["date", "ip", "user_agent", "type", "mod_version", "game_lang", "uuid", "player_name",
|
||||||
|
"cable_factor_multiplier", "cable_resistance_multiplier", "explosions_enabled", "replicators_enabled"]
|
||||||
|
|
||||||
|
|
||||||
|
def putModinfo(client_ip, user_agent):
|
||||||
|
response = table.put_item(
|
||||||
|
Item={
|
||||||
|
"date": datetime.now().isoformat(),
|
||||||
|
"ip": client_ip,
|
||||||
|
"user_agent": user_agent,
|
||||||
|
"type": "modinfo"
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
def putStat(client_ip, user_agent, data):
|
||||||
|
data["date"] = datetime.now().isoformat()
|
||||||
|
data["ip"] = client_ip
|
||||||
|
data["user_agent"] = user_agent
|
||||||
|
data["type"] = "stats"
|
||||||
|
# Prevents bad stuff, plus limits record sizes on the DB.
|
||||||
|
data = {k: v for k, v in data.items() if v not in WHITELISTED or len(v) > 100}
|
||||||
|
response = table.put_item(
|
||||||
|
Item=data
|
||||||
|
)
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
def root():
|
||||||
|
return "Nothing to see here!"
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/modinfo.json')
|
||||||
|
def modinfo():
|
||||||
|
client_ip = request.remote_addr
|
||||||
|
user_agent = request.headers.get("User-Agent")
|
||||||
|
print(putModinfo(client_ip, user_agent))
|
||||||
|
return render_template("modinfo.json")
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/stat')
|
||||||
|
def stat():
|
||||||
|
# Test string for browser
|
||||||
|
# http://127.0.0.1:5000/stat?version=1.2.3&lang=en_us&uuid=5
|
||||||
|
|
||||||
|
data = {}
|
||||||
|
|
||||||
|
client_ip = request.remote_addr
|
||||||
|
|
||||||
|
if HTTP_X_REAL_IP in request.environ:
|
||||||
|
client_ip = request.environ[HTTP_X_REAL_IP]
|
||||||
|
|
||||||
|
data["mod_version"] = request.args.get("version")
|
||||||
|
data["game_lang"] = request.args.get("lang")
|
||||||
|
data["uuid"] = request.args.get("uuid")
|
||||||
|
data["player_name"] = request.args.get("name")
|
||||||
|
data["cable_factor_multiplier"] = request.args.get("cableFactor")
|
||||||
|
data["cable_resistance_multiplier"] = request.args.get("cableResistanceMultiplier")
|
||||||
|
data["explosions_enabled"] = request.args.get("explosions")
|
||||||
|
data["replicators_enabled"] = request.args.get("repOn")
|
||||||
|
|
||||||
|
# Remove fields that are null
|
||||||
|
data = {k: v for k, v in data.items() if v is not None}
|
||||||
|
|
||||||
|
print(putStat(client_ip, request.headers.get("User-Agent"), data))
|
||||||
|
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app.run()
|
||||||
Loading…
Reference in New Issue