2
0

emergency_tool.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. # Load
  2. import time
  3. import os
  4. import platform
  5. import urllib
  6. import zipfile
  7. import urllib.request
  8. from route.tool.func import *
  9. while True:
  10. data_db_load = input('Load DB (Y) [Y, N] : ')
  11. data_db_load = data_db_load.upper()
  12. if data_db_load in ('Y', 'N'):
  13. break
  14. if data_db_load == 'Y':
  15. data_db_set = class_check_json()
  16. do_db_set(data_db_set)
  17. load_db = get_db_connect()
  18. conn = load_db.__enter__()
  19. curs = conn.cursor()
  20. else:
  21. print('You can use [9, 11, 19]')
  22. # Main
  23. print('1. Backlink reset')
  24. print('2. reCAPTCHA delete')
  25. print('3. Ban delete')
  26. print('4. Change host')
  27. print('5. Change port')
  28. print('6. Change skin')
  29. print('7. Change password')
  30. print('8. Set db version')
  31. print('9. Delete set.json')
  32. print('10. Change name')
  33. print('11. Delete mysql.json')
  34. print('14. Delete Main <HEAD>')
  35. print('15. Give owner')
  36. print('16. Delete 2FA password')
  37. print('17. Change markup')
  38. print('18. Change wiki access password')
  39. print('19. Forced update')
  40. print('20. Change domain')
  41. print('21. Change TLS')
  42. print('22. Delete body top')
  43. print('23. Delete body bottom')
  44. print('24. SQLite to MySQL')
  45. print('25. Recalc exist data_set')
  46. print('26. Change update branch')
  47. print('27. Change golang port')
  48. async def main():
  49. what_i_do = input('Insert selection number (EX : 9) : ')
  50. if what_i_do == '1':
  51. try:
  52. go_num = int(input('Count (100) : '))
  53. except ValueError:
  54. go_num = 100
  55. curs.execute(db_change('select count(*) from data'))
  56. count = curs.fetchall()
  57. count = count[0][0] if count else 0
  58. num = 0
  59. title = []
  60. print('Load...')
  61. while num == 0 or len(title) == 100:
  62. curs.execute(db_change('select title from data limit ?, 100'), [num])
  63. title = curs.fetchall()
  64. print('Rest : ' + str(count - num))
  65. print('Start : ' + title[0][0])
  66. time.sleep(1)
  67. for name in title:
  68. num += 1
  69. if num % go_num == 0:
  70. print(str(num) + ' : ' + name[0])
  71. curs.execute(db_change("select data from data where title = ?"), [name[0]])
  72. data = curs.fetchall()
  73. render_lang_data = {
  74. 'toc' : 'toc',
  75. 'category' : 'category'
  76. }
  77. await class_do_render(conn, render_lang_data, '').do_render(name[0], data[0][0], 'backlink')
  78. elif what_i_do == '2':
  79. curs.execute(db_change("delete from other where name = 'recaptcha'"))
  80. curs.execute(db_change("delete from other where name = 'sec_re'"))
  81. elif what_i_do == '3':
  82. user_data = input('IP or Name : ')
  83. curs.execute(
  84. db_change(
  85. "insert into rb (block, end, today, blocker, why, band, ongoing, login) "
  86. "values (?, ?, ?, ?, ?, ?, '', '')"
  87. ),
  88. [
  89. user_data,
  90. 'release',
  91. get_time(),
  92. 'tool:emergency',
  93. '',
  94. '',
  95. ]
  96. )
  97. curs.execute(db_change("update rb set ongoing = '' where block = ?"), [user_data])
  98. elif what_i_do == '4':
  99. host = input('Host : ')
  100. curs.execute(db_change("update other set data = ? where name = 'host'"), [host])
  101. elif what_i_do == '5':
  102. port = int(input('Port : '))
  103. curs.execute(db_change("update other set data = ? where name = 'port'"), [port])
  104. elif what_i_do == '6':
  105. skin = input('Skin name : ')
  106. curs.execute(db_change("update other set data = ? where name = 'skin'"), [skin])
  107. elif what_i_do == '7':
  108. user_name = input('User name : ')
  109. user_pw = input('User password : ')
  110. hashed = pw_encode(conn, user_pw)
  111. curs.execute(db_change("update user_set set data = ? where id = ? and name = 'pw'"), [
  112. hashed,
  113. user_name
  114. ])
  115. elif what_i_do == '8':
  116. new_ver = input('Insert version (0000000) : ')
  117. if new_ver == '':
  118. new_ver = '0000000'
  119. curs.execute(db_change("update other set data = ? where name = 'ver'"), [new_ver])
  120. elif what_i_do == '9':
  121. if os.path.exists(os.path.join('data', 'set.json')):
  122. os.remove(os.path.join('data', 'set.json'))
  123. elif what_i_do == '10':
  124. user_name = input('User name : ')
  125. new_name = input('New name : ')
  126. curs.execute(
  127. db_change("update user_set set id = ? where id = ?"),
  128. [new_name, user_name]
  129. )
  130. elif what_i_do == '11':
  131. if os.path.exists(os.path.join('data', 'mysql.json')):
  132. os.remove(os.path.join('data', 'mysql.json'))
  133. elif what_i_do == '14':
  134. curs.execute(db_change('delete from other where name = "head"'))
  135. elif what_i_do == '15':
  136. user_name = input('User name : ')
  137. curs.execute(db_change("update user_set set data = 'owner' where id = ? and name = 'acl'"), [user_name])
  138. elif what_i_do == '16':
  139. user_name = input('User name : ')
  140. curs.execute(db_change('select data from user_set where name = "2fa" and id = ?'), [user_name])
  141. if curs.fetchall():
  142. curs.execute(db_change("update user_set set data = '' where name = '2fa' and id = ?"), [user_name])
  143. elif what_i_do == '17':
  144. markup = input('Markup name : ')
  145. curs.execute(db_change("update other set data = ? where name = 'markup'"), [markup])
  146. elif what_i_do == '18':
  147. wiki_access_password = input('Password : ')
  148. curs.execute(db_change("update other set data = ? where name = 'wiki_access_password'"), [wiki_access_password])
  149. elif what_i_do == '19':
  150. curs.execute(db_change('select data from other where name = "update"'))
  151. up_data = curs.fetchall()
  152. up_data = up_data[0][0] if up_data and up_data[0][0] in ['stable', 'beta', 'dev', 'dont_use'] else 'stable'
  153. if platform.system() == 'Linux' or platform.system() == 'Darwin':
  154. ok = []
  155. ok += [os.system('git remote rm origin')]
  156. ok += [os.system('git remote add origin https://github.com/opennamu/opennamu.git')]
  157. ok += [os.system('git fetch --depth=1 origin ' + up_data)]
  158. ok += [os.system('git reset --hard origin/' + up_data)]
  159. for for_a in ok[1:]:
  160. if for_a != 0:
  161. print('Error : update failed')
  162. break
  163. elif platform.system() == 'Windows':
  164. os.system('rd /s /q route')
  165. urllib.request.urlretrieve('https://github.com/opennamu/opennamu/archive/' + up_data + '.zip', 'update.zip')
  166. zipfile.ZipFile('update.zip').extractall('')
  167. ok = os.system('xcopy /y /s /r opennamu-' + up_data + ' .')
  168. if ok == 0:
  169. os.system('rd /s /q opennamu-' + up_data)
  170. os.system('del update.zip')
  171. else:
  172. print('Error : update failed')
  173. elif what_i_do == '20':
  174. domain = input('Domain (EX : 2du.pythonanywhere.com) : ')
  175. curs.execute(db_change('delete from other where name = "domain"'))
  176. curs.execute(db_change('insert into other (name, data, coverage) values ("domain", ?, "")'), [domain])
  177. elif what_i_do == '21':
  178. tls_v = input('TLS (http) [http, https] : ')
  179. if not tls_v in ['http', 'https']:
  180. tls_v = 'http'
  181. curs.execute(db_change('delete from other where name = "http_select"'))
  182. curs.execute(db_change('insert into other (name, data, coverage) values ("http_select", ?, "")'), [tls_v])
  183. elif what_i_do == '22':
  184. curs.execute(db_change('delete from other where name = "body"'))
  185. elif what_i_do == '23':
  186. curs.execute(db_change('delete from other where name = "bottom_body"'))
  187. elif what_i_do == '24':
  188. load_db = get_db_connect('mysql')
  189. mysql_conn = load_db.__enter__()
  190. mysql_curs = mysql_conn.cursor()
  191. load_db = get_db_connect('sqlite')
  192. sqlite_conn = load_db.__enter__()
  193. sqlite_curs = sqlite_conn.cursor()
  194. create_data = get_db_table_list()
  195. for create_table in create_data:
  196. create = ['test'] + create_data[create_table]
  197. create_r = ', '.join(['%s' for _ in create])
  198. create = ', '.join(create)
  199. mysql_curs.execute(db_change('delete from ' + create_table))
  200. sqlite_curs.execute(db_change('select ' + create + ' from ' + create_table))
  201. db_data = sqlite_curs.fetchall()
  202. if db_data:
  203. mysql_curs.executemany("insert into " + create_table + " (" + create + ") values (" + create_r + ")", db_data)
  204. elif what_i_do == '25':
  205. curs.execute(db_change("select distinct doc_name from data_set where doc_rev = 'not_exist' or doc_rev = ''"))
  206. for for_a in curs.fetchall():
  207. data_set_exist = ''
  208. curs.execute(db_change("select title from data where title = ?"), [for_a[0]])
  209. if not curs.fetchall():
  210. data_set_exist = 'not_exist'
  211. curs.execute(db_change("update data_set set doc_rev = ? where doc_name = ? and (doc_rev = '' or doc_rev = 'not_exist')"), [data_set_exist, for_a[0]])
  212. elif what_i_do == '26':
  213. up_data = input('Insert branch name (beta) [stable, beta, dev] : ')
  214. if not up_data in ['stable', 'beta', 'dev', 'dont_use']:
  215. up_data = 'beta'
  216. curs.execute(db_change('delete from other where name = "update"'))
  217. curs.execute(db_change('insert into other (name, data, coverage) values ("update", ?, "")'), [up_data])
  218. elif what_i_do == '27':
  219. port = int(input('Port : '))
  220. curs.execute(db_change("update other set data = ? where name = 'golang_port'"), [port])
  221. else:
  222. raise ValueError(what_i_do)
  223. if data_db_load == 'Y':
  224. try:
  225. conn.commit()
  226. except:
  227. pass
  228. print('OK')
  229. if __name__ == "__main__":
  230. asyncio.run(main())