emergency_tool.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. from route.tool.func import *
  2. # DB
  3. while 1:
  4. try:
  5. set_data = json.loads(open('data/set.json').read())
  6. if not 'db_type' in set_data:
  7. try:
  8. os.remove('data/set.json')
  9. except:
  10. print('Please delete set.json')
  11. print('----')
  12. raise
  13. else:
  14. break
  15. except:
  16. if os.getenv('NAMU_DB') != None or os.getenv('NAMU_DB_TYPE') != None:
  17. set_data = {
  18. "db" : os.getenv('NAMU_DB') if os.getenv('NAMU_DB') else 'data',
  19. "db_type" : os.getenv('NAMU_DB_TYPE') if os.getenv('NAMU_DB_TYPE') else 'sqlite'
  20. }
  21. break
  22. else:
  23. new_json = ['', '']
  24. normal_db_type = ['sqlite', 'mysql']
  25. print('DB type (sqlite, mysql) : ', end = '')
  26. new_json[0] = str(input())
  27. if new_json[0] == '' or not new_json[0] in normal_db_type:
  28. new_json[0] = 'sqlite'
  29. all_src = []
  30. for i_data in os.listdir("."):
  31. f_src = re.search("(.+)\.db$", i_data)
  32. if f_src:
  33. all_src += [f_src.groups()[0]]
  34. if all_src != []:
  35. print('DB name (' + ', '.join(all_src) + ') : ', end = '')
  36. else:
  37. print('DB name (data) : ', end = '')
  38. new_json[1] = str(input())
  39. if new_json[1] == '':
  40. new_json[1] = 'data'
  41. with open('data/set.json', 'w') as f:
  42. f.write('{ "db" : "' + new_json[1] + '", "db_type" : "' + new_json[0] + '" }')
  43. set_data = json.loads(open('data/set.json').read())
  44. break
  45. print('DB name : ' + set_data['db'])
  46. print('DB type : ' + set_data['db_type'])
  47. db_data_get(set_data['db_type'])
  48. if set_data['db_type'] == 'mysql':
  49. try:
  50. set_data_mysql = json.loads(open('data/mysql.json').read())
  51. except:
  52. new_json = ['', '']
  53. while 1:
  54. print('DB user id : ', end = '')
  55. new_json[0] = str(input())
  56. if new_json[0] != '':
  57. break
  58. while 1:
  59. print('DB password : ', end = '')
  60. new_json[1] = str(input())
  61. if new_json[1] != '':
  62. break
  63. with open('data/mysql.json', 'w') as f:
  64. f.write('{ "user" : "' + new_json[0] + '", "password" : "' + new_json[1] + '" }')
  65. set_data_mysql = json.loads(open('data/mysql.json').read())
  66. conn = pymysql.connect(
  67. host = 'localhost',
  68. user = set_data_mysql['user'],
  69. password = set_data_mysql['password'],
  70. charset = 'utf8mb4'
  71. )
  72. curs = conn.cursor()
  73. try:
  74. curs.execute(db_change('create database ? default character set utf8mb4;')%pymysql.escape_string(set_data['db']))
  75. except:
  76. pass
  77. curs.execute(db_change('use ?')%pymysql.escape_string(set_data['db']))
  78. else:
  79. conn = sqlite3.connect(set_data['db'] + '.db', check_same_thread = False)
  80. curs = conn.cursor()
  81. load_conn(conn)
  82. # Main
  83. print('----')
  84. print('1. Backlink reset')
  85. print('2. reCAPTCHA delete')
  86. print('3. Ban delete')
  87. print('4. Change host')
  88. print('5. Change port')
  89. print('6. Change skin')
  90. print('7. Change password')
  91. print('8. Reset version')
  92. print('9. Delete set.json')
  93. print('10. Change name')
  94. print('11. Delete mysql.json')
  95. print('----')
  96. print('Select : ', end = '')
  97. what_i_do = input()
  98. if what_i_do == '1':
  99. def parser(data):
  100. namumark(data[0], data[1], 1)
  101. curs.execute(db_change("delete from back"))
  102. conn.commit()
  103. curs.execute(db_change("select title, data from data"))
  104. data = curs.fetchall()
  105. num = 0
  106. for test in data:
  107. num += 1
  108. t = threading.Thread(target = parser, args = [test])
  109. t.start()
  110. t.join()
  111. if num % 10 == 0:
  112. print(num)
  113. elif what_i_do == '2':
  114. curs.execute(db_change("delete from other where name = 'recaptcha'"))
  115. curs.execute(db_change("delete from other where name = 'sec_re'"))
  116. elif what_i_do == '3':
  117. print('----')
  118. print('IP or Name : ', end = '')
  119. user_data = input()
  120. if re.search("^([0-9]{1,3}\.[0-9]{1,3})$", user_data):
  121. band = 'O'
  122. else:
  123. band = ''
  124. curs.execute(db_change("insert into rb (block, end, today, blocker, why, band) values (?, ?, ?, ?, ?, ?)"),
  125. [user_data,
  126. 'release',
  127. get_time(),
  128. 'tool:emergency',
  129. '',
  130. band
  131. ])
  132. curs.execute(db_change("delete from ban where block = ?"), [user_data])
  133. elif what_i_do == '4':
  134. print('----')
  135. print('Host : ', end = '')
  136. host = input()
  137. curs.execute(db_change("update other set data = ? where name = 'host'"), [host])
  138. elif what_i_do == '5':
  139. print('----')
  140. print('Port : ', end = '')
  141. port = int(input())
  142. curs.execute(db_change("update other set data = ? where name = 'port'"), [port])
  143. elif what_i_do == '6':
  144. print('----')
  145. print('Skin name : ', end = '')
  146. skin = input()
  147. curs.execute(db_change("update other set data = ? where name = 'skin'"), [skin])
  148. elif what_i_do == '7':
  149. print('----')
  150. print('1. sha256')
  151. print('2. sha3')
  152. print('----')
  153. print('Select : ', end = '')
  154. what_i_do = int(input())
  155. print('----')
  156. print('User name : ', end = '')
  157. user_name = input()
  158. print('----')
  159. print('User password : ', end = '')
  160. user_pw = input()
  161. if what_i_do == '1':
  162. hashed = hashlib.sha256(bytes(user_pw, 'utf-8')).hexdigest()
  163. else:
  164. if sys.version_info < (3, 6):
  165. hashed = sha3.sha3_256(bytes(user_pw, 'utf-8')).hexdigest()
  166. else:
  167. hashed = hashlib.sha3_256(bytes(user_pw, 'utf-8')).hexdigest()
  168. curs.execute(db_change("update user set pw = ? where id = ?"), [hashed, user_name])
  169. elif what_i_do == '8':
  170. curs.execute(db_change("update other set data = '00000' where name = 'ver'"))
  171. elif what_i_do == '9':
  172. try:
  173. os.remove('data/set.json')
  174. except:
  175. pass
  176. elif what_i_do == '10':
  177. print('----')
  178. print('User name : ', end = '')
  179. user_name = input()
  180. print('----')
  181. print('New name : ', end = '')
  182. new_name = input()
  183. curs.execute(db_change("update user set id = ? where id = ?"), [new_name, user_name])
  184. else:
  185. try:
  186. os.remove('data/mysql.json')
  187. except:
  188. pass
  189. conn.commit()
  190. print('----')
  191. print('OK')