Various Updates
* Table name changed due to using IP as partition key instead of time and need a new DB * Use HTTPS instead of HTTP * Formatting fixes * Fixes issue with client IP not shared with modinfo code
This commit is contained in:
parent
940b3e54bd
commit
3ec29f5ca2
|
|
@ -138,3 +138,4 @@ dmypy.json
|
||||||
# Cython debug symbols
|
# Cython debug symbols
|
||||||
cython_debug/
|
cython_debug/
|
||||||
|
|
||||||
|
.idea/
|
||||||
|
|
|
||||||
31
main.py
31
main.py
|
|
@ -1,11 +1,15 @@
|
||||||
import boto3
|
import boto3
|
||||||
|
|
||||||
|
# TODO: Next time I work on this, I should make it serverless with API Gateway and a Lambda.
|
||||||
|
# But, alas, I don't feel like investing much in this since nobody cares about the old version.
|
||||||
|
# This project will be in KTLO until October 24, 2023 at which point I will shut down the Eln1 stats server.
|
||||||
|
|
||||||
from flask import Flask, render_template, request
|
from flask import Flask, render_template, request
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
dynamodb = boto3.resource('dynamodb', endpoint_url="http://dynamodb.us-east-1.amazonaws.com")
|
dynamodb = boto3.resource('dynamodb', endpoint_url="https://dynamodb.us-east-1.amazonaws.com")
|
||||||
table = dynamodb.Table('LogSieve')
|
table = dynamodb.Table('eln1_stats')
|
||||||
|
|
||||||
|
|
||||||
HTTP_X_REAL_IP = "HTTP_X_REAL_IP"
|
HTTP_X_REAL_IP = "HTTP_X_REAL_IP"
|
||||||
|
|
@ -13,8 +17,8 @@ WHITELISTED = ["date", "ip", "user_agent", "type", "mod_version", "game_lang", "
|
||||||
"cable_factor_multiplier", "cable_resistance_multiplier", "explosions_enabled", "replicators_enabled"]
|
"cable_factor_multiplier", "cable_resistance_multiplier", "explosions_enabled", "replicators_enabled"]
|
||||||
|
|
||||||
|
|
||||||
def putModinfo(client_ip, user_agent):
|
def put_mod_info(client_ip, user_agent):
|
||||||
response = table.put_item(
|
return table.put_item(
|
||||||
Item={
|
Item={
|
||||||
"date": datetime.now().isoformat(),
|
"date": datetime.now().isoformat(),
|
||||||
"ip": client_ip,
|
"ip": client_ip,
|
||||||
|
|
@ -22,20 +26,19 @@ def putModinfo(client_ip, user_agent):
|
||||||
"type": "modinfo"
|
"type": "modinfo"
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
return response
|
|
||||||
|
|
||||||
|
|
||||||
def putStat(client_ip, user_agent, data):
|
def put_stat(client_ip, user_agent, data):
|
||||||
data["date"] = datetime.now().isoformat()
|
data["date"] = datetime.now().isoformat()
|
||||||
data["ip"] = client_ip
|
data["ip"] = client_ip
|
||||||
data["user_agent"] = user_agent
|
data["user_agent"] = user_agent
|
||||||
data["type"] = "stats"
|
data["type"] = "stats"
|
||||||
# Prevents bad stuff, plus limits record sizes on the DB.
|
# Prevents bad stuff, plus limits record sizes on the DB. Using 2K since that should be well above necessary,
|
||||||
data = {k: v for k, v in data.items() if v not in WHITELISTED or len(v) > 100}
|
# but also well below 4K cutoff for using more than 1 RCU/WCU per item.
|
||||||
response = table.put_item(
|
data = {k: v for k, v in data.items() if v not in WHITELISTED or len(v) > 2000}
|
||||||
|
return table.put_item(
|
||||||
Item=data
|
Item=data
|
||||||
)
|
)
|
||||||
return response
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
|
|
@ -46,8 +49,12 @@ def root():
|
||||||
@app.route('/modinfo.json')
|
@app.route('/modinfo.json')
|
||||||
def modinfo():
|
def modinfo():
|
||||||
client_ip = request.remote_addr
|
client_ip = request.remote_addr
|
||||||
|
|
||||||
|
if HTTP_X_REAL_IP in request.environ:
|
||||||
|
client_ip = request.environ[HTTP_X_REAL_IP]
|
||||||
|
|
||||||
user_agent = request.headers.get("User-Agent")
|
user_agent = request.headers.get("User-Agent")
|
||||||
print(putModinfo(client_ip, user_agent))
|
print(put_mod_info(client_ip, user_agent))
|
||||||
return render_template("modinfo.json")
|
return render_template("modinfo.json")
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -75,7 +82,7 @@ def stat():
|
||||||
# Remove fields that are null
|
# Remove fields that are null
|
||||||
data = {k: v for k, v in data.items() if v is not None}
|
data = {k: v for k, v in data.items() if v is not None}
|
||||||
|
|
||||||
print(putStat(client_ip, request.headers.get("User-Agent"), data))
|
print(put_stat(client_ip, request.headers.get("User-Agent"), data))
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue