2
0

emergency_tool.py 12 KB

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