emergency_tool.py 6.8 KB

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