emergency_tool.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. from route.tool.func import *
  2. version_list = json.loads(open('version.json').read())
  3. # DB
  4. while 1:
  5. try:
  6. set_data = json.loads(open('data/set.json').read())
  7. if not 'db_type' in set_data:
  8. try:
  9. os.remove('data/set.json')
  10. except:
  11. print('Please delete set.json')
  12. print('----')
  13. raise
  14. else:
  15. break
  16. except:
  17. if os.getenv('NAMU_DB') != None or os.getenv('NAMU_DB_TYPE') != None:
  18. set_data = {
  19. "db" : os.getenv('NAMU_DB') if os.getenv('NAMU_DB') else 'data',
  20. "db_type" : os.getenv('NAMU_DB_TYPE') if os.getenv('NAMU_DB_TYPE') else 'sqlite'
  21. }
  22. break
  23. else:
  24. new_json = ['', '']
  25. normal_db_type = ['sqlite', 'mysql']
  26. print('DB type (sqlite, mysql) : ', end = '')
  27. new_json[0] = str(input())
  28. if new_json[0] == '' or not new_json[0] in normal_db_type:
  29. new_json[0] = 'sqlite'
  30. all_src = []
  31. for i_data in os.listdir("."):
  32. f_src = re.search("(.+)\.db$", i_data)
  33. if f_src:
  34. all_src += [f_src.groups()[0]]
  35. if all_src != []:
  36. print('DB name (' + ', '.join(all_src) + ') : ', end = '')
  37. else:
  38. print('DB name (data) : ', end = '')
  39. new_json[1] = str(input())
  40. if new_json[1] == '':
  41. new_json[1] = 'data'
  42. with open('data/set.json', 'w') as f:
  43. f.write('{ "db" : "' + new_json[1] + '", "db_type" : "' + new_json[0] + '" }')
  44. set_data = json.loads(open('data/set.json').read())
  45. break
  46. print('DB name : ' + set_data['db'])
  47. print('DB type : ' + set_data['db_type'])
  48. db_data_get(set_data['db_type'])
  49. if set_data['db_type'] == 'mysql':
  50. try:
  51. set_data_mysql = json.loads(open('data/mysql.json').read())
  52. except:
  53. new_json = ['', '']
  54. while 1:
  55. print('DB user id : ', end = '')
  56. new_json[0] = str(input())
  57. if new_json[0] != '':
  58. break
  59. while 1:
  60. print('DB password : ', end = '')
  61. new_json[1] = str(input())
  62. if new_json[1] != '':
  63. break
  64. with open('data/mysql.json', 'w') as f:
  65. f.write('{ "user" : "' + new_json[0] + '", "password" : "' + new_json[1] + '" }')
  66. set_data_mysql = json.loads(open('data/mysql.json').read())
  67. conn = pymysql.connect(
  68. host = 'localhost',
  69. user = set_data_mysql['user'],
  70. password = set_data_mysql['password'],
  71. charset = 'utf8mb4'
  72. )
  73. curs = conn.cursor()
  74. try:
  75. curs.execute(db_change('create database ? default character set utf8mb4;')%pymysql.escape_string(set_data['db']))
  76. except:
  77. pass
  78. curs.execute(db_change('use ?')%pymysql.escape_string(set_data['db']))
  79. else:
  80. conn = sqlite3.connect(set_data['db'] + '.db', check_same_thread = False)
  81. curs = conn.cursor()
  82. load_conn(conn)
  83. if os.path.exists(set_data['db'] + '.db'):
  84. setup_tool = 0
  85. else:
  86. setup_tool = 1
  87. create_data = {}
  88. create_data['all_data'] = [
  89. 'data',
  90. 'cache_data',
  91. 'history',
  92. 'rd',
  93. 'user',
  94. 'user_set',
  95. 'ban',
  96. 'topic',
  97. 'rb',
  98. 'back',
  99. 'custom',
  100. 'other',
  101. 'alist',
  102. 're_admin',
  103. 'alarm',
  104. 'ua_d',
  105. 'filter',
  106. 'scan',
  107. 'acl',
  108. 'inter',
  109. 'html_filter',
  110. 'oauth_conn',
  111. ]
  112. for i in create_data['all_data']:
  113. try:
  114. curs.execute(db_change('select test from ' + i + ' limit 1'))
  115. except:
  116. try:
  117. curs.execute(db_change('create table ' + i + '(test longtext)'))
  118. except:
  119. curs.execute(db_change("alter table " + i + " add test longtext default ''"))
  120. if setup_tool == 0:
  121. try:
  122. curs.execute(db_change('select data from other where name = "ver"'))
  123. ver_set_data = curs.fetchall()
  124. if not ver_set_data:
  125. setup_tool = 1
  126. else:
  127. if version_list['master']['c_ver'] > ver_set_data[0][0]:
  128. setup_tool = 1
  129. except:
  130. setup_tool = 1
  131. if setup_tool != 0:
  132. create_data['data'] = ['title', 'data']
  133. create_data['cache_data'] = ['title', 'data']
  134. create_data['history'] = ['id', 'title', 'data', 'date', 'ip', 'send', 'leng', 'hide', 'type']
  135. create_data['rd'] = ['title', 'sub', 'date', 'band', 'stop', 'agree']
  136. create_data['user'] = ['id', 'pw', 'acl', 'date', 'encode']
  137. create_data['user_set'] = ['name', 'id', 'data']
  138. create_data['ban'] = ['block', 'end', 'why', 'band', 'login']
  139. create_data['topic'] = ['id', 'title', 'sub', 'data', 'date', 'ip', 'block', 'top', 'code']
  140. create_data['rb'] = ['block', 'end', 'today', 'blocker', 'why', 'band']
  141. create_data['back'] = ['title', 'link', 'type']
  142. create_data['custom'] = ['user', 'css']
  143. create_data['other'] = ['name', 'data', 'coverage']
  144. create_data['alist'] = ['name', 'acl']
  145. create_data['re_admin'] = ['who', 'what', 'time']
  146. create_data['alarm'] = ['name', 'data', 'date']
  147. create_data['ua_d'] = ['name', 'ip', 'ua', 'today', 'sub']
  148. create_data['filter'] = ['name', 'regex', 'sub']
  149. create_data['scan'] = ['user', 'title']
  150. create_data['acl'] = ['title', 'decu', 'dis', 'view', 'why']
  151. create_data['inter'] = ['title', 'link', 'icon']
  152. create_data['html_filter'] = ['html', 'kind', 'plus']
  153. create_data['oauth_conn'] = ['provider', 'wiki_id', 'sns_id', 'name', 'picture']
  154. for create_table in create_data['all_data']:
  155. for create in create_data[create_table]:
  156. try:
  157. curs.execute(db_change('select ' + create + ' from ' + create_table + ' limit 1'))
  158. except:
  159. curs.execute(db_change("alter table " + create_table + " add " + create + " longtext default ''"))
  160. try:
  161. curs.execute(db_change('create index index_' + create_table + '_' + create + ' on ' + create_table + '(' + create + ')'))
  162. except:
  163. pass
  164. update()
  165. # Main
  166. print('----')
  167. print('1. Backlink reset')
  168. print('2. reCAPTCHA delete')
  169. print('3. Ban delete')
  170. print('4. Change host')
  171. print('5. Change port')
  172. print('6. Change skin')
  173. print('7. Change password')
  174. print('8. Reset version')
  175. print('9. Delete set.json')
  176. print('10. Change name')
  177. print('11. Delete mysql.json')
  178. print('12. All title count reset')
  179. print('----')
  180. print('Select : ', end = '')
  181. what_i_do = input()
  182. if what_i_do == '1':
  183. curs.execute(db_change("delete from back"))
  184. conn.commit()
  185. curs.execute(db_change("select title, data from data"))
  186. data = curs.fetchall()
  187. num = 0
  188. for test in data:
  189. num += 1
  190. if num % 100 == 0:
  191. print(num)
  192. render_do(test[0], test[1], 1, None)
  193. elif what_i_do == '2':
  194. curs.execute(db_change("delete from other where name = 'recaptcha'"))
  195. curs.execute(db_change("delete from other where name = 'sec_re'"))
  196. elif what_i_do == '3':
  197. print('----')
  198. print('IP or Name : ', end = '')
  199. user_data = input()
  200. if re.search("^([0-9]{1,3}\.[0-9]{1,3})$", user_data):
  201. band = 'O'
  202. else:
  203. band = ''
  204. curs.execute(db_change("insert into rb (block, end, today, blocker, why, band) values (?, ?, ?, ?, ?, ?)"),
  205. [user_data,
  206. 'release',
  207. get_time(),
  208. 'tool:emergency',
  209. '',
  210. band
  211. ])
  212. curs.execute(db_change("delete from ban where block = ?"), [user_data])
  213. elif what_i_do == '4':
  214. print('----')
  215. print('Host : ', end = '')
  216. host = input()
  217. curs.execute(db_change("update other set data = ? where name = 'host'"), [host])
  218. elif what_i_do == '5':
  219. print('----')
  220. print('Port : ', end = '')
  221. port = int(input())
  222. curs.execute(db_change("update other set data = ? where name = 'port'"), [port])
  223. elif what_i_do == '6':
  224. print('----')
  225. print('Skin name : ', end = '')
  226. skin = input()
  227. curs.execute(db_change("update other set data = ? where name = 'skin'"), [skin])
  228. elif what_i_do == '7':
  229. print('----')
  230. print('1. sha256')
  231. print('2. sha3')
  232. print('----')
  233. print('Select : ', end = '')
  234. what_i_do = int(input())
  235. print('----')
  236. print('User name : ', end = '')
  237. user_name = input()
  238. print('----')
  239. print('User password : ', end = '')
  240. user_pw = input()
  241. if what_i_do == '1':
  242. hashed = hashlib.sha256(bytes(user_pw, 'utf-8')).hexdigest()
  243. else:
  244. if sys.version_info < (3, 6):
  245. hashed = sha3.sha3_256(bytes(user_pw, 'utf-8')).hexdigest()
  246. else:
  247. hashed = hashlib.sha3_256(bytes(user_pw, 'utf-8')).hexdigest()
  248. curs.execute(db_change("update user set pw = ? where id = ?"), [hashed, user_name])
  249. elif what_i_do == '8':
  250. curs.execute(db_change("update other set data = '00000' where name = 'ver'"))
  251. elif what_i_do == '9':
  252. try:
  253. os.remove('data/set.json')
  254. except:
  255. pass
  256. elif what_i_do == '10':
  257. print('----')
  258. print('User name : ', end = '')
  259. user_name = input()
  260. print('----')
  261. print('New name : ', end = '')
  262. new_name = input()
  263. curs.execute(db_change("update user set id = ? where id = ?"), [new_name, user_name])
  264. elif what_i_do == '11':
  265. try:
  266. os.remove('data/mysql.json')
  267. except:
  268. pass
  269. else:
  270. curs.execute(db_change("select count(title) from data"))
  271. count_data = curs.fetchall()
  272. if count_data:
  273. count_data = count_data[0][0]
  274. else:
  275. count_data = 0
  276. curs.execute(db_change('delete from other where name = "count_all_title"'))
  277. curs.execute(db_change('insert into other (name, data) values ("count_all_title", ?)'), [str(count_data)])
  278. conn.commit()
  279. print('----')
  280. print('OK')