emergency_tool.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. import time
  2. from route.tool.func import *
  3. version_list = json.loads(open('version.json', encoding='utf8').read())
  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("(.+)\.db$", i_data)
  38. if f_src:
  39. all_src += [f_src.groups()[0]]
  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[0] = str(input())
  60. if new_json[0] != '':
  61. break
  62. while 1:
  63. print('DB password : ', end = '')
  64. new_json[1] = str(input())
  65. if new_json[1] != '':
  66. break
  67. with open('data/mysql.json', 'w', encoding='utf8') as f:
  68. f.write('{ "user" : "' + new_json[0] + '", "password" : "' + new_json[1] + '" }')
  69. set_data_mysql = json.loads(open('data/mysql.json', encoding='utf8').read())
  70. conn = pymysql.connect(
  71. host = 'localhost',
  72. user = set_data_mysql['user'],
  73. password = set_data_mysql['password'],
  74. charset = 'utf8mb4'
  75. )
  76. curs = conn.cursor()
  77. try:
  78. curs.execute(db_change('create database ? default character set utf8mb4;')%pymysql.escape_string(set_data['db']))
  79. except:
  80. pass
  81. curs.execute(db_change('use ?')%pymysql.escape_string(set_data['db']))
  82. else:
  83. conn = sqlite3.connect(set_data['db'] + '.db', check_same_thread = False)
  84. curs = conn.cursor()
  85. load_conn(conn)
  86. create_data = {}
  87. create_data['all_data'] = [
  88. 'data',
  89. 'cache_data',
  90. 'history',
  91. 'rd',
  92. 'user',
  93. 'user_set',
  94. 'ban',
  95. 'topic',
  96. 'rb',
  97. 'back',
  98. 'custom',
  99. 'other',
  100. 'alist',
  101. 're_admin',
  102. 'alarm',
  103. 'ua_d',
  104. 'filter',
  105. 'scan',
  106. 'acl',
  107. 'inter',
  108. 'html_filter',
  109. 'oauth_conn',
  110. 'user_application'
  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. 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 int(version_list['master']['c_ver']) > int(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['user_application'] = ['id', 'pw', 'date', 'encode', 'question', 'answer', 'ip', 'ua', 'token', 'email']
  139. create_data['ban'] = ['block', 'end', 'why', 'band', 'login']
  140. create_data['topic'] = ['id', 'title', 'sub', 'data', 'date', 'ip', 'block', 'top', 'code']
  141. create_data['rb'] = ['block', 'end', 'today', 'blocker', 'why', 'band']
  142. create_data['back'] = ['title', 'link', 'type']
  143. create_data['custom'] = ['user', 'css']
  144. create_data['other'] = ['name', 'data', 'coverage']
  145. create_data['alist'] = ['name', 'acl']
  146. create_data['re_admin'] = ['who', 'what', 'time']
  147. create_data['alarm'] = ['name', 'data', 'date']
  148. create_data['ua_d'] = ['name', 'ip', 'ua', 'today', 'sub']
  149. create_data['filter'] = ['name', 'regex', 'sub']
  150. create_data['scan'] = ['user', 'title']
  151. create_data['acl'] = ['title', 'decu', 'dis', 'view', 'why']
  152. create_data['inter'] = ['title', 'link', 'icon']
  153. create_data['html_filter'] = ['html', 'kind', 'plus']
  154. create_data['oauth_conn'] = ['provider', 'wiki_id', 'sns_id', 'name', 'picture']
  155. for create_table in create_data['all_data']:
  156. for create in create_data[create_table]:
  157. try:
  158. curs.execute(db_change('select ' + create + ' from ' + create_table + ' limit 1'))
  159. except:
  160. curs.execute(db_change("alter table " + create_table + " add " + create + " longtext default ''"))
  161. try:
  162. curs.execute(db_change('create index index_' + create_table + '_' + create + ' on ' + create_table + '(' + create + ')'))
  163. except:
  164. pass
  165. update()
  166. curs.execute(db_change('delete from other where name = "ver"'))
  167. curs.execute(db_change('insert into other (name, data) values ("ver", ?)'), [version_list['master']['c_ver']])
  168. conn.commit()
  169. # Main
  170. print('----')
  171. print('1. Backlink reset')
  172. print('2. reCAPTCHA delete')
  173. print('3. Ban delete')
  174. print('4. Change host')
  175. print('5. Change port')
  176. print('6. Change skin')
  177. print('7. Change password')
  178. print('8. Reset version')
  179. print('9. Delete set.json')
  180. print('10. Change name')
  181. print('11. Delete mysql.json')
  182. print('12. All title count reset')
  183. print('----')
  184. print('Select : ', end = '')
  185. what_i_do = input()
  186. if what_i_do == '1':
  187. print('----')
  188. print('All delete (Y) [Y, N] : ', end = '')
  189. go_num = input()
  190. if not go_num == 'N':
  191. curs.execute(db_change("delete from back"))
  192. conn.commit()
  193. print('----')
  194. print('Count (100) : ', end = '')
  195. try:
  196. go_num = int(input())
  197. except:
  198. go_num = 100
  199. num = 0
  200. print('----')
  201. print('Load...')
  202. curs.execute(db_change("select title from data d where not exists (select title from back where link = d.title)"))
  203. title = curs.fetchall()
  204. print('----')
  205. print('Rest : ' + str(len(title)))
  206. time.sleep(1)
  207. print('----')
  208. for name in title:
  209. num += 1
  210. if num % go_num == 0:
  211. print(str(num) + ' : ' + name[0])
  212. if num % 100 == 0:
  213. conn.commit()
  214. curs.execute(db_change("select data from data where title = ?"), [name[0]])
  215. data = curs.fetchall()
  216. render_do(name[0], data[0][0], 3, None)
  217. elif what_i_do == '2':
  218. curs.execute(db_change("delete from other where name = 'recaptcha'"))
  219. curs.execute(db_change("delete from other where name = 'sec_re'"))
  220. elif what_i_do == '3':
  221. print('----')
  222. print('IP or Name : ', end = '')
  223. user_data = input()
  224. if re.search("^([0-9]{1,3}\.[0-9]{1,3})$", user_data):
  225. band = 'O'
  226. else:
  227. band = ''
  228. curs.execute(db_change("insert into rb (block, end, today, blocker, why, band) values (?, ?, ?, ?, ?, ?)"),
  229. [user_data,
  230. 'release',
  231. get_time(),
  232. 'tool:emergency',
  233. '',
  234. band
  235. ])
  236. curs.execute(db_change("delete from ban where block = ?"), [user_data])
  237. elif what_i_do == '4':
  238. print('----')
  239. print('Host : ', end = '')
  240. host = input()
  241. curs.execute(db_change("update other set data = ? where name = 'host'"), [host])
  242. elif what_i_do == '5':
  243. print('----')
  244. print('Port : ', end = '')
  245. port = int(input())
  246. curs.execute(db_change("update other set data = ? where name = 'port'"), [port])
  247. elif what_i_do == '6':
  248. print('----')
  249. print('Skin name : ', end = '')
  250. skin = input()
  251. curs.execute(db_change("update other set data = ? where name = 'skin'"), [skin])
  252. elif what_i_do == '7':
  253. print('----')
  254. print('1. sha256')
  255. print('2. sha3')
  256. print('----')
  257. print('Select : ', end = '')
  258. what_i_do = int(input())
  259. print('----')
  260. print('User name : ', end = '')
  261. user_name = input()
  262. print('----')
  263. print('User password : ', end = '')
  264. user_pw = input()
  265. if what_i_do == '1':
  266. hashed = hashlib.sha256(bytes(user_pw, 'utf-8')).hexdigest()
  267. else:
  268. if sys.version_info < (3, 6):
  269. hashed = sha3.sha3_256(bytes(user_pw, 'utf-8')).hexdigest()
  270. else:
  271. hashed = hashlib.sha3_256(bytes(user_pw, 'utf-8')).hexdigest()
  272. curs.execute(db_change("update user set pw = ? where id = ?"), [hashed, user_name])
  273. elif what_i_do == '8':
  274. curs.execute(db_change("update other set data = '00000' where name = 'ver'"))
  275. elif what_i_do == '9':
  276. try:
  277. os.remove('data/set.json')
  278. except:
  279. pass
  280. elif what_i_do == '10':
  281. print('----')
  282. print('User name : ', end = '')
  283. user_name = input()
  284. print('----')
  285. print('New name : ', end = '')
  286. new_name = input()
  287. curs.execute(db_change("update user set id = ? where id = ?"), [new_name, user_name])
  288. elif what_i_do == '11':
  289. try:
  290. os.remove('data/mysql.json')
  291. except:
  292. pass
  293. else:
  294. curs.execute(db_change("select count(title) from data"))
  295. count_data = curs.fetchall()
  296. if count_data:
  297. count_data = count_data[0][0]
  298. else:
  299. count_data = 0
  300. curs.execute(db_change('delete from other where name = "count_all_title"'))
  301. curs.execute(db_change('insert into other (name, data) values ("count_all_title", ?)'), [str(count_data)])
  302. conn.commit()
  303. print('----')
  304. print('OK')