Newer
Older
from flask import Flask, request, session, g
from flask_restful import Api, Resource
from .tunnelstat import SSHSession
app.config['PERMANENT_SESSION_LIFETIME'] = datetime.timedelta(seconds=300)
app.config['SESSION_REFRESH_EACH_REQUEST'] = True
app.config['SESSION_COOKIE_NAME'] = 'tessession'
app.config['SESSION_COOKIE_SECURE'] = True
app.config['SESSION_COOKIE_HTTPONLY'] = True
app.config['SESSION_COOKIE_SAMESITE'] = None
if 'FLASK_ENV' in os.environ and os.environ['FLASK_ENV'] == 'development':
DEV=True
#app.config['SECRET_KEY'] = 'notverysecret'
app.config['SECRET_KEY'] = os.urandom(12).hex()
app.config['DEBUG'] = True
else:
DEV=False
app.config['SECRET_KEY'] = os.urandom(12).hex()
app.config['DEBUG'] = False
CORS(app, supports_credentials=True, origin="localhost:4200")
islocal = True
from . import apiendpoints
@app.before_request
def before_request():
g.start = time.time()
@app.after_request
def after_cleanup_sessions(response):
import hashlib
events = logging.getLogger('connection_events')
Chris Hines
committed
slist = []
klist = []
try:
# kvsessionextension.cleanup_sessions(app)
_ttl = app.config['PERMANENT_SESSION_LIFETIME']
if isinstance(_ttl, int):
ttl = datetime.timedelta(seconds=_ttl)
else:
ttl = _ttl
for k, sshsess in list(sshsessions.items()):
if datetime.datetime.now() > sshsess.last + ttl:
logger.debug("session last seen at {} now {}".format(sshsess.last,datetime.datetime.now()))
Chris Hines
committed
slist.append(sshsess)
klist.append(k)
sessionId = hashlib.sha1(k.encode()).hexdigest()
events.info('disconnect {}'.format(json.dumps({'sessid':sessionId})))
Chris Hines
committed
# Two keys may point at the same session. In particular an authentication token and our twsproxyauth cookie can both identify a session
for k in klist:
del sshsessions[k]
for s in slist:
try:
s.kill()
except Exception as e:
logger.error('failed to cleanup old session {}'.format(e))
import traceback
logger.error(e)
logger.error(traceback.format_exc())
except Exception as e:
logger.error('failed to cleanup old sessions {}'.format(e))
import traceback
logger.error(e)
logger.error(traceback.format_exc())
if __name__ == '__main__':