1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
| from flask import Flask, session, request, render_template, jsonify, redirect, url_for from cachelib.file import FileSystemCache from flask_session import Session from secrets import token_hex from os.path import join from pydash import set_ from hashlib import md5 import json import os
set_form_html = """ <!DOCTYPE html> <html> <head> <title>添加备忘录</title> </head> <body> <form id="myForm"> <input type="hidden" name="name" value="notes"> <textarea type="notes" id="notes" name="value"></textarea><br><br> <button type="button" onclick="send()">提交</button> </form> <script> function send() { const form = document.getElementById('myForm'); const formData = new FormData(form);
const jsonData = {}; formData.forEach((value, key) => { jsonData[key] = value; });
fetch('/set', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(jsonData) }) .then(response => { console.log('Response:', response); }) .catch(error => { console.error('Error:', error); }); location.reload(); } </script> </body> </html> """
note_render_html = """<!DOCTYPE html> <html> <head> <title>备忘录</title> </head> <body> <h1>下列是IP来自 {{remote_addr}} 的用户设置的备忘录: </h1> <h2> {{ notes }} </h2> <br> <br> <h3> 添加备忘录 </h3> <form id="myForm"> <input type="hidden" name="name" value="notes"> <textarea type="notes" id="notes" name="value"></textarea><br><br> <button type="button" onclick="send()">提交</button> </form> <script> function send() { const form = document.getElementById('myForm'); const formData = new FormData(form);
const jsonData = {}; formData.forEach((value, key) => { jsonData[key] = value; });
fetch('/set', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(jsonData) }) .then(response => { console.log('Response:', response); }) .catch(error => { console.error('Error:', error); }); location.reload(); } </script> </body> </html>"""
class Notes: def __init__(self, remote_addr=""): self.note_hash = md5(remote_addr.encode()).hexdigest()[20:30] try: self.notes = json.load(open(f"templates/{self.note_hash}.json", "r")) except: self.notes = "" if not os.path.exists(f"templates/{self.note_hash}.html"): open(f"templates/{self.note_hash}.html", "w").write(note_render_html)
def get_notes(self): return self.notes
def update(self): try: self.notes = json.load(open(f"templates/{self.note_hash}.json", "r")) except Exception as e: print(e) self.notes = ""
def save_notes(self): json.dump(self.notes, open(f"templates/{self.note_hash}.json", "w"))
class LocalCache(FileSystemCache): def _get_filename(self, key: str) -> str: if ".." in key: key = token_hex(8) return join(self._path, key)
app = Flask(__name__) app.config["SESSION_PERMANENT"] = False app.config["SESSION_TYPE"] = "filesystem"
@app.route("/", methods=["GET"]) def index(): return redirect(url_for('get'))
@app.route("/set", methods=["GET", "POST"]) def set(): notes = Notes(request.remote_addr) if request.method == "GET": return set_form_html else: body = request.json key, value = body['name'], body['value'] set_(notes, key, value) return render_template(f"{notes.note_hash}.html", notes=notes.get_notes(), remote_addr=request.remote_addr)
@app.route("/get", methods=["GET"]) def get(): if app.jinja_loader.searchpath != ["/app/templates"]: app.jinja_loader.searchpath = ["/app/templates"] if app.jinja_loader.encoding != "utf-8": app.jinja_loader.encoding = "utf-8" notes = Notes(request.remote_addr) return render_template(f"{notes.note_hash}.html", notes=notes.notes, remote_addr=request.remote_addr)
if __name__ == "__main__": app.run("0.0.0.0", 6000, debug=True)
|