-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
404 lines (377 loc) · 13.7 KB
/
setup.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
from itsdangerous import JSONWebSignatureSerializer
from time import time
from hashids import Hashids
import mysql.connector
import requests
import hashlib
def getUserInput(variable_name, default=None):
user_input = input(f"Input {variable_name} (Default: {default})>>")
if user_input == "":
return default
else:
return user_input
def showHelpMessage():
messages = [
"NuxtImageBoard setup wizard",
"Usage: python setup.py <operation_name>",
"",
"[Supported operations]",
"maindb:create",
" Create tables needed for working",
"maindb:drop",
" Drop all tables created from this wizard",
"subdb:create",
" Create subapi tables needed for working",
"subdb:drop",
" Drop all subapi tables created from this wizard",
"mainadmin:create",
" Create a new admin for main api",
"mainuser:create",
" Create a new user for main api",
"subadmin:create",
" Create a new admin for sub api",
"subuser:create",
" Create a new user for sub api",
"invite:create",
" Create new invite(s)"
]
print("\n".join(messages))
class NuxtImageBoardSetup():
def __init__(self, api_db_config, toy_db_config, api_config, toymoney_config, headless=False):
self.main_conn = mysql.connector.connect(**api_db_config)
self.sub_conn = mysql.connector.connect(**toy_db_config)
self.main_cursor = self.main_conn.cursor()
self.sub_cursor = self.sub_conn.cursor()
self.api_config = api_config
self.toymoney_config = toymoney_config
if headless:
self.createDatabase()
self.createMainApiUser(self.createSubApiUser())
def closeDatabase(self):
self.main_cursor.close()
self.sub_cursor.close()
self.main_conn.close()
self.sub_conn.close()
def createMainDatabase(self):
print("Creating main database...")
with open("init_maindb.sql", "r", encoding="utf8") as f:
# Note: execute with multi=True returns generator object
for _ in self.main_cursor.execute(f.read(), multi=True):
pass
self.main_conn.commit()
print("Create main database success!")
def createSubDatabase(self):
print("Creating sub database...")
with open("init_subdb.sql", "r", encoding="utf8") as f:
# Note: execute with multi=True returns generator object
for _ in self.sub_cursor.execute(f.read(), multi=True):
pass
self.sub_conn.commit()
print("Create sub database success!")
def dropMainDatabase(self):
tables = [
"data_comment",
"data_illust",
"data_invite",
"data_log",
"data_mute",
"data_mylist",
"data_news",
"data_notify",
"data_ranking",
"data_replace",
"data_tag",
"data_upload",
"data_user",
"data_view",
"data_wiki",
"info_artist",
"info_mylist",
"info_tag"
]
for table_name in tables:
self.main_cursor.execute(f"DROP TABLE {table_name}")
self.main_conn.commit()
def dropSubDatabase(self):
tables = [
"transactions",
"user_inventories",
"users",
"machines_inventories",
"machines",
"airdrops",
"products"
]
for table_name in tables:
self.sub_cursor.execute(f"DROP TABLE {table_name}")
self.sub_conn.commit()
def dropDatabaseWithWizard(self, is_sub=False):
print("Are you sure you want to drop the database?")
print("ALL DATAS WILL BE REMOVED!")
confirm = getUserInput("y to continue", "n")
if confirm == "y":
if not is_sub:
self.dropMainDatabase()
else:
self.dropSubDatabase()
print("Success")
def dropMainDatabaseWithWizard(self):
self.dropDatabaseWithWizard(False)
def dropSubDatabaseWithWizard(self):
self.dropDatabaseWithWizard(True)
def createSubApiAdmin(self, name, password, money=2147483647, id=1):
serializer = JSONWebSignatureSerializer(
self.toymoney_config["salt_jwt"]
)
password = self.toymoney_config["salt_password"]+password
password = hashlib.sha256(password.encode("utf8")).hexdigest()
token = serializer.dumps({
'id': 1,
'seq': 1,
'is_admin': 1
}).decode('utf-8')
self.sub_cursor.execute(
"""INSERT INTO users
(id, name, money, password, authorization_key, authorization_seq, is_admin)
VALUES (%s, %s, %s, %s, %s, 1, 1)""",
(id, name, money, password, token)
)
self.sub_conn.commit()
return token
def createSubApiAdminWithWizard(self):
print("Creating sub api admin...")
username = getUserInput("username", "nbadmin")
password = getUserInput("password", "nbadmin")
id = int(getUserInput("id", "1"))
toyapi_key = self.createSubApiAdmin(username, password, id=id)
print("Create sub api admin success!")
print(f"Api key: {toyapi_key}")
def createSubApiUser(self, display_id="nbadmin"):
toyApiResp = requests.post(
f"{self.toymoney_config['endpoint']}/users/create",
json={
"name": display_id,
"password": self.toymoney_config['salt_password'] + display_id
},
headers={
"Authorization": "Bearer " + self.toymoney_config['token']
}
)
if toyApiResp.status_code != 200:
raise Exception("ToyMoneyへのリクエストに失敗しました")
resp = toyApiResp.json()
return resp["apiKey"]
def createSubApiUserWithWizard(self):
print("Creating sub api user...")
username = getUserInput("username", "nbadmin")
toyapi_key = self.createSubApiUser(username)
print("Create sub api user success!")
print(f"Api key: {toyapi_key}")
def generateMainApiKey(self, accountID):
self.main_cursor.execute(
"SELECT userApiSeq,userPermission FROM data_user WHERE userID=%s",
(accountID,)
)
apiSeq, apiPermission = self.main_cursor.fetchall()[0]
serializer = JSONWebSignatureSerializer(
self.api_config["salt_jwt"]
)
token = serializer.dumps({
'id': accountID,
'seq': apiSeq + 1,
'permission': apiPermission
}).decode('utf-8')
self.main_cursor.execute(
"""UPDATE data_user SET userApiSeq=userApiSeq+1, userApiKey=%s
WHERE userID=%s""",
(token, accountID)
)
self.main_conn.commit()
return token
def createMainApiUser(
self,
toyapi_key,
display_id="nbadmin",
username="nbadmin",
password="nbadmin",
permission=5
):
# パスワードをハッシュ化
password = self.api_config["salt_pass"]+password
password = hashlib.sha256(password.encode("utf8")).hexdigest()
# ユーザーを追加
self.main_cursor.execute(
"""INSERT INTO data_user
(userDisplayID, userName, userPassword,
userToyApiKey, userPermission)
VALUES (%s,%s,%s,%s,%s)""",
(display_id, username, password, toyapi_key, permission)
)
# ユーザー内部IDを取得
self.main_cursor.execute(
"""SELECT userID FROM data_user
WHERE userDisplayID=%s AND userPassword=%s""",
(display_id, password)
)
user_id = self.main_cursor.fetchall()[0][0]
api_key = self.generateMainApiKey(user_id)
# 自分自身の招待を作成(しないとgetSelfAccountで詰まる)
self.main_cursor.execute(
"""INSERT INTO data_invite
(inviter, invitee, inviteCode, inviteCreated, inviteUsed)
VALUES
(%s, %s, 'SELF', current_timestamp(), current_timestamp())""",
(user_id, user_id)
)
# マイリストの作成
self.main_cursor.execute(
"""INSERT INTO info_mylist
(mylistName, mylistDescription, userID)
VALUES (%s,%s,%s)""",
(f"{username}のマイリスト", "", user_id)
)
self.main_conn.commit()
return user_id, api_key
def createMainApiUserWithWizard(self):
print("Creating main api user...")
display_id = getUserInput("display id", "nbadmin")
username = getUserInput("username", "nbadmin")
password = getUserInput("password", "nbadmin")
permission = getUserInput("permission", "9")
toyapi_key = getUserInput("toyapi_key", "auto generate")
if toyapi_key == "auto generate":
toyapi_key = cl.createSubApiUser(display_id)
else:
self.toymoney_config["token"] = toyapi_key
user_id, api_key = cl.createMainApiUser(
toyapi_key,
display_id, username, password, permission
)
print("Create user success!")
print(f"User id: {user_id}")
print(f"Api key: {api_key}")
def createMainApiAdmin(
self,
toyapi_key,
display_id="nbadmin",
username="nbadmin",
password="nbadmin",
permission=9
):
self.createMainApiUser(toyapi_key, display_id, username, password, permission)
def createMainApiAdminWithWizard(self):
print("Creating main api admin...")
display_id = getUserInput("display id", "nbadmin")
username = getUserInput("username", "nbadmin")
password = getUserInput("password", "nbadmin")
toyapi_key = getUserInput("toyapi_key", "auto generate")
if toyapi_key == "auto generate":
toyapi_key = cl.createSubApiUser(display_id)
else:
self.toymoney_config["token"] = toyapi_key
user_id, api_key = cl.createMainApiUser(
toyapi_key,
display_id, username, password, 9
)
print("Create admin success!")
print(f"User id: {user_id}")
print(f"Api key: {api_key}")
def createInvite(self, user_id, invite_code="RANDOM", code_count=1):
invite_codes = []
for _ in range(code_count):
if invite_code == "RANDOM":
hash_gen = Hashids(
salt=self.api_config['salt_invite'],
min_length=8
)
code = hash_gen.encode(int(time()) + user_id)
else:
code = invite_code
invite_codes.append(code)
self.main_cursor.execute(
"""INSERT INTO data_invite
(inviter, inviteCode) VALUES (%s, %s)""",
(user_id, code)
)
self.main_conn.commit()
return invite_codes
def createInviteWithWizard(self):
print("Creating invite...")
user_id = int(getUserInput("user id", "1"))
invite_code = getUserInput("invite code", "RANDOM")
code_count = int(getUserInput("code count", "1"))
codes = cl.createInvite(
user_id,
invite_code,
code_count
)
print("Create invite success!")
print("Invite codes:")
print("\n".join(codes))
if __name__ == "__main__":
# Read commandline params
import sys
args = sys.argv
if len(args) != 2:
showHelpMessage()
sys.exit()
supported_operations = [
"maindb:create", "maindb:drop",
"subdb:create", "subdb:drop",
"mainuser:create", "mainadmin:create",
"subuser:create", "subadmin:create",
"invite:create"
]
if args[1] not in supported_operations:
print("Unsupported operation.\nCheck your syntax.\n")
showHelpMessage()
sys.exit()
# Read environment values
from os import environ
from dotenv import load_dotenv
load_dotenv(verbose=True, override=True)
api_db_config = {
'host': environ.get('DB_HOST'),
'port': environ.get('PORT_API_MARIA'),
'user': environ.get('DB_USER'),
'password': environ.get('DB_PASS'),
'database': environ.get('DB_NAME')
}
toy_db_config = {
'host': environ.get('DB_HOST'),
'port': environ.get('PORT_TOY_MARIA'),
'user': environ.get('DB_USER'),
'password': environ.get('DB_PASS'),
'database': environ.get('TOYMONEY_DB')
}
api_config = {
'salt_jwt': environ.get('SALT_JWT'),
'salt_pass': environ.get('SALT_PASS'),
'salt_invite': environ.get('SALT_INVITE')
}
toymoney_config = {
'endpoint': environ.get('TOYMONEY_ENDPOINT'),
'token': environ.get('TOYMONEY_TOKEN'),
'salt_jwt': environ.get('TOYMONEY_SALT'),
'salt_password': environ.get('TOYMONEY_PASSWORD_HEAD')
}
cl = NuxtImageBoardSetup(
api_db_config,
toy_db_config,
api_config,
toymoney_config
)
# Doing selected operation
supported_operations = {
"maindb:create": cl.createMainDatabase,
"maindb:drop": cl.dropMainDatabaseWithWizard,
"subdb:create": cl.createSubDatabase,
"subdb:drop": cl.dropSubDatabaseWithWizard,
"mainuser:create": cl.createMainApiUserWithWizard,
"mainadmin:create": cl.createMainApiAdminWithWizard,
"subuser:create": cl.createSubApiUserWithWizard,
"subadmin:create": cl.createSubApiAdminWithWizard,
"invite:create": cl.createInviteWithWizard,
}
supported_operations[args[1]]()
cl.closeDatabase()