emergency_tool.py 10 KB

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