2
0

emergency_tool.py 8.8 KB

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