emergency_tool.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. # Load
  2. import time
  3. from route.tool.func import *
  4. while 1:
  5. print('Load DB (Y) [Y, N] : ', end = '')
  6. data_db_load = input()
  7. if data_db_load in ['Y', 'N']:
  8. break
  9. if data_db_load == 'Y':
  10. # Init-DB
  11. if os.path.exists(os.path.join('data', 'set.json')):
  12. db_set_list = ['db', 'db_type']
  13. set_data = json.loads(open(
  14. os.path.join('data', 'set.json'),
  15. encoding = 'utf8'
  16. ).read())
  17. for i in db_set_list:
  18. if not i in set_data:
  19. print('Please delete set.json')
  20. print('----')
  21. raise
  22. print('DB name : ' + set_data['db'])
  23. print('DB type : ' + set_data['db_type'])
  24. elif os.getenv('NAMU_DB') or os.getenv('NAMU_DB_TYPE'):
  25. set_data = {}
  26. if os.getenv('NAMU_DB'):
  27. set_data['db'] = os.getenv('NAMU_DB')
  28. else:
  29. set_data['db'] = 'data'
  30. if os.getenv('NAMU_DB_TYPE'):
  31. set_data['db'] = os.getenv('NAMU_DB_TYPE')
  32. else:
  33. set_data['db'] = 'sqlite'
  34. print('DB name : ' + set_data['db'])
  35. print('DB type : ' + set_data['db_type'])
  36. else:
  37. set_data = {}
  38. normal_db_type = ['sqlite', 'mysql']
  39. print('DB type (' + normal_db_type[0] + ') [' + ', '.join(normal_db_type) + '] : ', end = '')
  40. data_get = str(input())
  41. if data_get == '' or not data_get in normal_db_type:
  42. set_data['db_type'] = 'sqlite'
  43. else:
  44. set_data['db_type'] = data_get
  45. all_src = []
  46. if set_data['db_type'] == 'sqlite':
  47. for i_data in os.listdir("."):
  48. f_src = re.search(r"(.+)\.db$", i_data)
  49. if f_src:
  50. all_src += [f_src.group(1)]
  51. print('DB name (data) [' + ', '.join(all_src) + '] : ', end = '')
  52. data_get = str(input())
  53. if data_get == '':
  54. set_data['db'] = 'data'
  55. else:
  56. set_data['db'] = data_get
  57. with open(os.path.join('data', 'set.json'), 'w', encoding = 'utf8') as f:
  58. f.write(json.dumps(set_data))
  59. data_db_set = {}
  60. data_db_set['name'] = set_data['db']
  61. data_db_set['type'] = set_data['db_type']
  62. if data_db_set['type'] == 'mysql':
  63. if not os.path.exists(os.path.join('data', 'mysql.json')):
  64. db_set_list = ['user', 'password', 'host', 'port']
  65. set_data = json.loads(
  66. open(
  67. os.path.join('data', 'mysql.json'),
  68. encoding = 'utf8'
  69. ).read()
  70. )
  71. for i in db_set_list:
  72. if not i in set_data:
  73. print('Please delete mysql.json')
  74. print('----')
  75. raise
  76. print('DB user ID : ', end = '')
  77. set_data_mysql['user'] = str(input())
  78. print('DB password : ', end = '')
  79. set_data_mysql['password'] = str(input())
  80. print('DB host (localhost) : ', end = '')
  81. set_data_mysql['host'] = str(input())
  82. if set_data_mysql['host'] == '':
  83. set_data_mysql['host'] = 'localhost'
  84. print('DB port (3306) : ', end = '')
  85. set_data_mysql['port'] = str(input())
  86. if set_data_mysql['port'] == '':
  87. set_data_mysql['port'] = '3306'
  88. with open(
  89. os.path.join('data', 'mysql.json'),
  90. 'w',
  91. encoding = 'utf8'
  92. ) as f:
  93. f.write(json.dumps(set_data_mysql))
  94. data_db_set['mysql_user'] = set_data_mysql['user']
  95. data_db_set['mysql_pw'] = set_data_mysql['password']
  96. if 'host' in set_data_mysql:
  97. data_db_set['mysql_host'] = set_data_mysql['host']
  98. else:
  99. data_db_set['mysql_host'] = 'localhost'
  100. if 'port' in set_data_mysql:
  101. data_db_set['mysql_port'] = set_data_mysql['port']
  102. else:
  103. data_db_set['mysql_port'] = '3306'
  104. db_data_get(data_db_set['type'])
  105. load_db = get_db_connect_old(data_db_set)
  106. conn = load_db.db_load()
  107. curs = conn.cursor()
  108. else:
  109. print('----')
  110. print('You can use [9, 11]')
  111. # Main
  112. print('----')
  113. print('1. Backlink reset')
  114. print('2. reCAPTCHA delete')
  115. print('3. Ban delete')
  116. print('4. Change host')
  117. print('5. Change port')
  118. print('6. Change skin')
  119. print('7. Change password')
  120. print('8. Change version')
  121. print('9. Delete set.json')
  122. print('10. Change name')
  123. print('11. Delete mysql.json')
  124. print('12. All title count reset')
  125. print('13. Cache data reset')
  126. print('14. Delete Main <HEAD>')
  127. print('15. Give owner')
  128. print('16. Delete 2FA password')
  129. print('17. Change markup')
  130. print('----')
  131. print('Select : ', end = '')
  132. what_i_do = input()
  133. if what_i_do == '1':
  134. print('----')
  135. print('All delete (Y) [Y, N] : ', end = '')
  136. go_num = input()
  137. if not go_num == 'N':
  138. curs.execute(db_change("delete from back"))
  139. conn.commit()
  140. print('----')
  141. print('Count (100) : ', end = '')
  142. try:
  143. go_num = int(input())
  144. except:
  145. go_num = 100
  146. num = 0
  147. print('----')
  148. print('Load...')
  149. curs.execute(db_change("" + \
  150. "select title from data d " + \
  151. "where not exists (" + \
  152. "select title from back where link = d.title limit 1" + \
  153. ")" + \
  154. ""))
  155. title = curs.fetchall()
  156. print('----')
  157. print('Rest : ' + str(len(title)))
  158. print('Start : ' + title[0][0])
  159. time.sleep(1)
  160. print('----')
  161. for name in title:
  162. num += 1
  163. if num % go_num == 0:
  164. print(str(num) + ' : ' + name[0])
  165. if num % 100 == 0:
  166. conn.commit()
  167. curs.execute(db_change("select data from data where title = ?"), [name[0]])
  168. data = curs.fetchall()
  169. get_class_render = class_do_render(conn)
  170. get_class_render.do_render(name[0], data[0][0], 'backlink', '')
  171. elif what_i_do == '2':
  172. curs.execute(db_change("delete from other where name = 'recaptcha'"))
  173. curs.execute(db_change("delete from other where name = 'sec_re'"))
  174. elif what_i_do == '3':
  175. print('----')
  176. print('IP or Name : ', end = '')
  177. user_data = input()
  178. curs.execute(db_change("" + \
  179. "insert into rb (block, end, today, blocker, why, band) " + \
  180. "values (?, ?, ?, ?, ?, ?)"
  181. ), [
  182. user_data,
  183. 'release',
  184. get_time(),
  185. 'tool:emergency',
  186. '',
  187. ''
  188. ])
  189. curs.execute(db_change("update rb set ongoing = '' where block = ?"), [user_data])
  190. elif what_i_do == '4':
  191. print('----')
  192. print('Host : ', end = '')
  193. host = input()
  194. curs.execute(db_change("update other set data = ? where name = 'host'"), [host])
  195. elif what_i_do == '5':
  196. print('----')
  197. print('Port : ', end = '')
  198. port = int(input())
  199. curs.execute(db_change("update other set data = ? where name = 'port'"), [port])
  200. elif what_i_do == '6':
  201. print('----')
  202. print('Skin name : ', end = '')
  203. skin = input()
  204. curs.execute(db_change("update other set data = ? where name = 'skin'"), [skin])
  205. elif what_i_do == '7':
  206. print('----')
  207. print('1. sha256')
  208. print('2. sha3')
  209. print('----')
  210. print('Select : ', end = '')
  211. what_i_do = int(input())
  212. print('----')
  213. print('User name : ', end = '')
  214. user_name = input()
  215. print('----')
  216. print('User password : ', end = '')
  217. user_pw = input()
  218. if what_i_do == '1':
  219. hashed = hashlib.sha256(bytes(user_pw, 'utf-8')).hexdigest()
  220. else:
  221. if sys.version_info < (3, 6):
  222. hashed = sha3.sha3_256(bytes(user_pw, 'utf-8')).hexdigest()
  223. else:
  224. hashed = hashlib.sha3_256(bytes(user_pw, 'utf-8')).hexdigest()
  225. curs.execute(db_change("update user_set set data = ? where id = ? and name = 'pw'"), [
  226. hashed,
  227. user_name
  228. ])
  229. elif what_i_do == '8':
  230. print('----')
  231. print('Insert version (0000000) : ', end = '')
  232. new_ver = input()
  233. if new_ver == '':
  234. new_ver == '0000000'
  235. curs.execute(db_change("update other set data = ? where name = 'ver'"), [new_ver])
  236. elif what_i_do == '9':
  237. if os.path.exists(os.path.join('data', 'set.json')):
  238. os.remove(os.path.join('data', 'set.json'))
  239. elif what_i_do == '10':
  240. print('----')
  241. print('User name : ', end = '')
  242. user_name = input()
  243. print('----')
  244. print('New name : ', end = '')
  245. new_name = input()
  246. curs.execute(db_change("update user_set set id = ? where id = ?"), [
  247. new_name,
  248. user_name
  249. ])
  250. elif what_i_do == '11':
  251. if os.path.exists(os.path.join('data', 'mysql.json')):
  252. os.remove(os.path.join('data', 'mysql.json'))
  253. elif what_i_do == '12':
  254. curs.execute(db_change("select count(*) from data"))
  255. count_data = curs.fetchall()
  256. if count_data:
  257. count_data = count_data[0][0]
  258. else:
  259. count_data = 0
  260. curs.execute(db_change('delete from other where name = "count_all_title"'))
  261. curs.execute(db_change('insert into other (name, data) values ("count_all_title", ?)'), [
  262. str(count_data)
  263. ])
  264. elif what_i_do == '13':
  265. curs.execute(db_change('delete from cache_data'))
  266. elif what_i_do == '14':
  267. curs.execute(db_change('delete from other where name = "head"'))
  268. elif what_i_do == '15':
  269. print('----')
  270. print('User name : ', end = '')
  271. user_name = input()
  272. curs.execute(db_change("update user_set set data = 'owner' where id = ? and name = 'acl'"), [user_name])
  273. elif what_i_do == '16':
  274. print('----')
  275. print('User name : ', end = '')
  276. user_name = input()
  277. curs.execute(db_change('select data from user_set where name = "2fa" and id = ?'), [user_name])
  278. if curs.fetchall():
  279. curs.execute(db_change("update user_set set data = '' where name = '2fa' and id = ?"), [user_name])
  280. else:
  281. print('----')
  282. print('Markup name : ', end = '')
  283. markup = input()
  284. curs.execute(db_change("update other set data = ? where name = 'markup'"), [markup])
  285. if data_db_load == 'Y':
  286. conn.commit()
  287. print('----')
  288. print('OK')