This repository has been archived by the owner on Apr 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
/
db_utils.py
142 lines (112 loc) · 4.18 KB
/
db_utils.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
import datetime
import uuid
from .models import WhaleConfig, WhaleContainer
from CTFd.models import (
db
)
class DBUtils:
@staticmethod
def get_all_configs():
configs = WhaleConfig.query.all()
result = {}
for c in configs:
result[str(c.key)] = str(c.value)
return result
@staticmethod
def save_all_configs(configs):
for c in configs:
q = db.session.query(WhaleConfig)
q = q.filter(WhaleConfig.key == c[0])
record = q.one_or_none()
if record:
record.value = c[1]
db.session.commit()
else:
config = WhaleConfig(key=c[0], value=c[1])
db.session.add(config)
db.session.commit()
db.session.close()
@staticmethod
def create_new_container(user_id, challenge_id, flag, port=0):
uuid_code = uuid.uuid4()
container = WhaleContainer(user_id=user_id, challenge_id=challenge_id, flag=flag, uuid=uuid_code, port=port)
db.session.add(container)
db.session.commit()
db.session.close()
return str(uuid_code)
@staticmethod
def get_current_containers(user_id):
q = db.session.query(WhaleContainer)
q = q.filter(WhaleContainer.user_id == user_id)
records = q.all()
if len(records) == 0:
return None
return records[0]
@staticmethod
def get_container_by_port(port):
q = db.session.query(WhaleContainer)
q = q.filter(WhaleContainer.port == port)
records = q.all()
if len(records) == 0:
return None
return records[0]
@staticmethod
def remove_current_container(user_id):
q = db.session.query(WhaleContainer)
q = q.filter(WhaleContainer.user_id == user_id)
# records = q.all()
# for r in records:
# pass
q.delete()
db.session.commit()
db.session.close()
@staticmethod
def renew_current_container(user_id, challenge_id):
q = db.session.query(WhaleContainer)
q = q.filter(WhaleContainer.user_id == user_id)
q = q.filter(WhaleContainer.challenge_id == challenge_id)
records = q.all()
if len(records) == 0:
return
configs = DBUtils.get_all_configs()
timeout = int(configs.get("docker_timeout", "3600"))
r = records[0]
r.start_time = r.start_time + datetime.timedelta(seconds=timeout)
if r.start_time > datetime.datetime.now():
r.start_time = datetime.datetime.now()
r.renew_count += 1
db.session.commit()
db.session.close()
@staticmethod
def get_all_expired_container():
configs = DBUtils.get_all_configs()
timeout = int(configs.get("docker_timeout", "3600"))
q = db.session.query(WhaleContainer)
q = q.filter(WhaleContainer.start_time < datetime.datetime.now() - datetime.timedelta(seconds=timeout))
return q.all()
@staticmethod
def get_all_alive_container():
configs = DBUtils.get_all_configs()
timeout = int(configs.get("docker_timeout", "3600"))
q = db.session.query(WhaleContainer)
q = q.filter(WhaleContainer.start_time >= datetime.datetime.now() - datetime.timedelta(seconds=timeout))
return q.all()
@staticmethod
def get_all_container():
q = db.session.query(WhaleContainer)
return q.all()
@staticmethod
def get_all_alive_container_page(page_start, page_end):
configs = DBUtils.get_all_configs()
timeout = int(configs.get("docker_timeout", "3600"))
q = db.session.query(WhaleContainer)
q = q.filter(WhaleContainer.start_time >= datetime.datetime.now() - datetime.timedelta(seconds=timeout))
q = q.slice(page_start, page_end)
return q.all()
@staticmethod
def get_all_alive_container_count():
configs = DBUtils.get_all_configs()
timeout = int(configs.get("docker_timeout", "3600"))
q = db.session.query(WhaleContainer)
q = q.filter(WhaleContainer.start_time >= datetime.datetime.now() - datetime.timedelta(seconds=timeout))
return q.count()