|
|
|
@@ -21,13 +21,23 @@ import threading
|
|
|
|
|
import logging
|
|
|
|
|
import netaddr
|
|
|
|
|
import os
|
|
|
|
|
import uuid
|
|
|
|
|
import collections
|
|
|
|
|
import pylru
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
from os import scandir, walk
|
|
|
|
|
except ImportError:
|
|
|
|
|
from scandir import scandir, walk
|
|
|
|
|
|
|
|
|
|
from octoprint.settings import settings
|
|
|
|
|
import octoprint.server
|
|
|
|
|
import octoprint.users
|
|
|
|
|
import octoprint.plugin
|
|
|
|
|
import octoprint.util
|
|
|
|
|
|
|
|
|
|
from werkzeug.contrib.cache import BaseCache
|
|
|
|
|
from werkzeug.datastructures import CallbackDict
|
|
|
|
|
|
|
|
|
|
from past.builtins import basestring
|
|
|
|
|
|
|
|
|
@@ -476,6 +486,168 @@ class OctoPrintFlaskResponse(flask.Response):
|
|
|
|
|
flask.Response.set_cookie(self, key, expires=0, max_age=0, path=path, domain=domain)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#~~ Sessions
|
|
|
|
|
|
|
|
|
|
class ServerSideSession(CallbackDict, flask.sessions.SessionMixin):
|
|
|
|
|
|
|
|
|
|
def __init__(self, sid, path, serializer, persistent=True, new=False):
|
|
|
|
|
def on_update(self):
|
|
|
|
|
self.modified = True
|
|
|
|
|
CallbackDict.__init__(self, on_update=on_update)
|
|
|
|
|
|
|
|
|
|
self.sid = sid
|
|
|
|
|
self.path = path
|
|
|
|
|
|
|
|
|
|
self.new = new
|
|
|
|
|
self.modified = False
|
|
|
|
|
|
|
|
|
|
self._logger = logging.getLogger(__name__ + ".sessions.session")
|
|
|
|
|
self._persistent = persistent
|
|
|
|
|
self._serializer = serializer
|
|
|
|
|
self._lock = threading.RLock()
|
|
|
|
|
|
|
|
|
|
self.read()
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def persistent(self):
|
|
|
|
|
return self._persistent
|
|
|
|
|
|
|
|
|
|
def read(self):
|
|
|
|
|
with self._lock:
|
|
|
|
|
if not os.path.exists(self.path):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
with open(self.path, "rb") as f:
|
|
|
|
|
data = self._serializer.loads(f.read())
|
|
|
|
|
|
|
|
|
|
self.clear()
|
|
|
|
|
self.update(data)
|
|
|
|
|
self.modified = False
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
except:
|
|
|
|
|
self._logger.exception("Could not read session file for session {}".format(self.sid))
|
|
|
|
|
|
|
|
|
|
def save(self):
|
|
|
|
|
if not self.persistent:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
from octoprint.util import atomic_write
|
|
|
|
|
with self._lock:
|
|
|
|
|
try:
|
|
|
|
|
with atomic_write(self.path, mode="wb") as f:
|
|
|
|
|
f.write(self._serializer.dumps(dict(self)))
|
|
|
|
|
self.modified = False
|
|
|
|
|
except:
|
|
|
|
|
self._logger.exception("Could not write session file for session {}".format(self.sid))
|
|
|
|
|
|
|
|
|
|
self._logger.debug("Saved session file for session {}".format(self.sid))
|
|
|
|
|
|
|
|
|
|
def remove(self):
|
|
|
|
|
with self._lock:
|
|
|
|
|
if not os.path.exists(self.path):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
os.remove(self.path)
|
|
|
|
|
self._logger.debug("Removed session file for session {}".format(self.sid))
|
|
|
|
|
except:
|
|
|
|
|
self._logger.exception("Could not remove session file for session {}".format(self.sid))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ServerSideSessionInterface(flask.sessions.SessionInterface):
|
|
|
|
|
|
|
|
|
|
serializer = flask.sessions.session_json_serializer
|
|
|
|
|
|
|
|
|
|
session_class = ServerSideSession
|
|
|
|
|
|
|
|
|
|
def __init__(self, directory, persistent_callback=None, min_cleanup_interval=300):
|
|
|
|
|
self.directory = directory
|
|
|
|
|
self.persistent_callback = persistent_callback
|
|
|
|
|
self.min_cleanup_interval = min_cleanup_interval
|
|
|
|
|
|
|
|
|
|
self._cleanup_lock = threading.RLock()
|
|
|
|
|
self._last_cleanup = None
|
|
|
|
|
|
|
|
|
|
self._logger = logging.getLogger(__name__ + ".sessions.interface")
|
|
|
|
|
|
|
|
|
|
def open_session(self, app, request):
|
|
|
|
|
self.cleanup_stale_sessions(app)
|
|
|
|
|
|
|
|
|
|
persistent = self.persistent_callback(app, request) if callable(self.persistent_callback) else True
|
|
|
|
|
|
|
|
|
|
sid = request.cookies.get(app.session_cookie_name)
|
|
|
|
|
try:
|
|
|
|
|
uuid.UUID(octoprint.util.to_str(sid))
|
|
|
|
|
except:
|
|
|
|
|
sid = None
|
|
|
|
|
|
|
|
|
|
is_new = False
|
|
|
|
|
if not sid:
|
|
|
|
|
sid = self.generate_sid()
|
|
|
|
|
is_new = True
|
|
|
|
|
if persistent:
|
|
|
|
|
self._logger.debug("Generated a new SID {} for path {}".format(sid, request.path))
|
|
|
|
|
|
|
|
|
|
return self.session_class(sid, self.path_for_sid(sid), self.serializer, persistent=persistent, new=is_new)
|
|
|
|
|
|
|
|
|
|
def save_session(self, app, session, response):
|
|
|
|
|
domain = self.get_cookie_domain(app)
|
|
|
|
|
path = self.get_cookie_path(app)
|
|
|
|
|
|
|
|
|
|
if not session:
|
|
|
|
|
if session.persistent:
|
|
|
|
|
session.remove()
|
|
|
|
|
if session.modified:
|
|
|
|
|
response.delete_cookie(app.session_cookie_name,
|
|
|
|
|
domain=domain, path=path)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
session.save()
|
|
|
|
|
|
|
|
|
|
httponly = self.get_cookie_httponly(app)
|
|
|
|
|
secure = self.get_cookie_secure(app)
|
|
|
|
|
expires = self.get_expiration_time(app, session)
|
|
|
|
|
response.set_cookie(app.session_cookie_name, session.sid,
|
|
|
|
|
httponly=httponly, secure=secure, expires=expires,
|
|
|
|
|
domain=domain, path=path)
|
|
|
|
|
|
|
|
|
|
def generate_sid(self):
|
|
|
|
|
while True:
|
|
|
|
|
sid = str(uuid.uuid4())
|
|
|
|
|
if not os.path.exists(self.path_for_sid(sid)):
|
|
|
|
|
return sid
|
|
|
|
|
|
|
|
|
|
def cleanup_stale_sessions(self, app):
|
|
|
|
|
if self._last_cleanup is not None and self._last_cleanup - time.time() < self.min_cleanup_interval:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
maxage = flask.sessions.total_seconds(app.permanent_session_lifetime)
|
|
|
|
|
cutoff = time.time() - maxage
|
|
|
|
|
|
|
|
|
|
to_clean = []
|
|
|
|
|
with self._cleanup_lock:
|
|
|
|
|
for entry in scandir(self.directory):
|
|
|
|
|
try:
|
|
|
|
|
if entry.stat().st_mtime < cutoff:
|
|
|
|
|
to_clean.append(entry.path)
|
|
|
|
|
except:
|
|
|
|
|
self._logger.exception("Error checking session file for session {} for staleness".format(entry.name))
|
|
|
|
|
|
|
|
|
|
for path in to_clean:
|
|
|
|
|
try:
|
|
|
|
|
os.remove(path)
|
|
|
|
|
self._logger.debug("Cleaned up session file for stale session {}".format(os.path.basename(path)))
|
|
|
|
|
except:
|
|
|
|
|
self._logger.exception("Error while removing session file for stale session {}".format(os.path.basename(path)))
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
self._last_cleanup = time.time()
|
|
|
|
|
|
|
|
|
|
def path_for_sid(self, sid):
|
|
|
|
|
return os.path.join(self.directory, sid)
|
|
|
|
|
|
|
|
|
|
#~~ passive login helper
|
|
|
|
|
|
|
|
|
|
def passive_login():
|
|
|
|
@@ -609,7 +781,7 @@ def cached(timeout=5 * 60, key=lambda: "view:%s" % flask.request.path, unless=No
|
|
|
|
|
def decorator(f):
|
|
|
|
|
@functools.wraps(f)
|
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
logger = logging.getLogger(__name__ + ".cache")
|
|
|
|
|
|
|
|
|
|
cache_key = key()
|
|
|
|
|
|
|
|
|
|