/
frontend_manage.py
150 lines (127 loc) · 4.02 KB
/
frontend_manage.py
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
#! /usr/bin/env python3
from os import environ
import os
import sys
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from app import app
from app.models import *
import logging
from logging.handlers import TimedRotatingFileHandler
import bcrypt
import uuid
script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
manager = Manager(app)
class DB(object):
def __init__(self, metadata):
self.metadata = metadata
migrate = Migrate(app, DB(db.metadata))
manager.add_command('db', MigrateCommand)
@manager.command
def create_admin_user(username, password):
"""
Create an admin user.
"""
newuser = User()
newuser.username = username
newuser.password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
newuser.role = 'Admin'
try:
db.session.add(newuser)
db.session.commit()
print('User {} created.'.format(username))
return True
except Exception as e:
print('Failed to create user {}'.format(username))
db.session.rollback()
return False
@manager.command
def reset_password(username, password):
"""
Create an admin user.
"""
theuser = db.session.query(User).filter(User.username == username).first()
theuser.password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
try:
db.session.merge(theuser)
db.session.commit()
print('User {} Update.'.format(username))
return True
except Exception as e:
print('Failed to update user {}'.format(username))
db.session.rollback()
return False
@manager.command
def create_invite_code():
"""
Create an invite code
"""
invite = Invites()
invite.invite_code = str(uuid.uuid4())
invite.active = True
try:
db.session.add(invite)
db.session.commit()
print('Invite code created: {}'.format(invite.invite_code))
except Exception as e:
db.session.rollback()
print("Failed to create invite code.")
@manager.command
def initdb():
"""
Initialize the database and create all tables.
"""
print("[+] Initializing database...")
print("[+] Creating tables...")
db.create_all(bind=None)
print('[+] Done!')
@manager.command
def seed():
"""
Seed the database with the initial data required.
"""
# Populate the database with basic user roles.
print('[+] Splinkle sprinkle!!!')
roles = [
Role(id=10, title="Admin"),
Role(id=3, title="User"),
Role(id=0, title="None")
]
for role in roles:
db.session.merge(role)
db.session.commit()
print('[+] Done.')
@manager.command
def run():
"""
Run the server.
"""
# Set up the logger.
if not os.path.isdir(os.path.join(script_dir, 'logs')):
os.makedirs(os.path.join(script_dir, 'logs'))
# Format the logs.
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s")
# Enable the logs to split files at midnight.
handler = TimedRotatingFileHandler(
os.path.join(script_dir, 'logs', 'TorSpider.log'),
when='midnight', backupCount=7, interval=1)
handler.setLevel(app.config['LOG_LEVEL'])
handler.setFormatter(formatter)
log = logging.getLogger('werkzeug')
log.setLevel(app.config['LOG_LEVEL'])
log.addHandler(handler)
app.logger.addHandler(handler)
app.logger.setLevel(app.config['APP_LOG_LEVEL'])
# Set up the app server, port, and configuration.
port = int(environ.get('PORT', app.config['LISTEN_PORT']))
addr = environ.get('LISTEN_ADDR', app.config['LISTEN_ADDR'])
if app.config['USETLS']:
context = (app.config['CERT_FILE'], app.config['CERT_KEY_FILE'])
app.run(host=addr, port=port, threaded=True, ssl_context=context)
else:
app.run(host=addr, port=port, threaded=True)
if __name__ == '__main__':
if sys.version_info[0] < 3:
raise Exception("Please use Python version 3 to run this script.")
manager.run()