RXSM update server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

__init__.py 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Created 2019-09-10 by NGnius
  2. from flask import Flask, Blueprint, jsonify, request
  3. import sqlite3 as dblib
  4. from hashlib import sha512
  5. from os import getcwd
  6. from threading import get_ident
  7. from time import time
  8. database_path = "rxsm-update.db"
  9. database_connections = dict()
  10. app = Flask("rxsm-update-server")
  11. def get_or_create_connection():
  12. global database_connections, database_path
  13. thread_id = get_ident()
  14. if thread_id not in database_connections:
  15. database_connections[thread_id] = dblib.connect(database_path)
  16. return database_connections[thread_id]
  17. @app.route('/release', methods=['POST'])
  18. def create_or_modify_release():
  19. try:
  20. req_json = request.get_json(force=True)
  21. token = req_json['token']
  22. version = req_json['version']
  23. url = req_json['url']
  24. except:
  25. return jsonify({'status':400, 'reason':'Invalid request; missing parameter or invalid JSON'}), 400
  26. with open(".token", "r") as f:
  27. master_token = f.read()
  28. if sha512(token.encode()).hexdigest() != master_token:
  29. return jsonify({'status':403, 'reason':'Permission denied; invalid token'}), 403
  30. db_connection = get_or_create_connection()
  31. cursor = db_connection.cursor()
  32. cursor.execute("SELECT version FROM releases WHERE version=?", (version,))
  33. result = cursor.fetchone()
  34. operation = ""
  35. if result is None:
  36. cursor.execute("INSERT INTO releases (version, url, created_date) VALUES (?,?,?)", (version, url, int(time())))
  37. operation = "create"
  38. else:
  39. cursor.execute("UPDATE releases SET url=?, created_date=? WHERE version=?", (url, int(time()), version))
  40. operation = "update"
  41. db_connection.commit()
  42. return jsonify({'status':200, 'reason':'Version %s %sd successfully' % (version, operation)}), 200
  43. # always try to create db table(s)
  44. conn = get_or_create_connection()
  45. cursor = conn.cursor()
  46. cursor.execute(\
  47. """CREATE TABLE IF NOT EXISTS releases (
  48. id INTEGER PRIMARY KEY,
  49. version TEXT UNIQUE NOT NULL,
  50. url TEXT NOT NULL,
  51. created_date INTEGER NOT NULL
  52. )""" )
  53. conn.commit()
  54. if __name__ == '__main__':
  55. print("Working directory: " + getcwd())
  56. app.run(host='0.0.0.0', port=9080, threaded=True)