default.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. import os
  2. import tkinter as tk
  3. from tkinter import filedialog
  4. from tkinter import ttk
  5. import utils.constants as constants
  6. from utils.config import config
  7. from utils.tools import resource_path
  8. class DefaultUI:
  9. def init_ui(self, root):
  10. """
  11. Init default UI
  12. """
  13. frame_default_source_file = tk.Frame(root)
  14. frame_default_source_file.pack(fill=tk.X)
  15. frame_default_source_file_column1 = tk.Frame(frame_default_source_file)
  16. frame_default_source_file_column1.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
  17. frame_default_source_file_column2 = tk.Frame(frame_default_source_file)
  18. frame_default_source_file_column2.pack(side=tk.RIGHT, fill=tk.Y)
  19. self.source_file_label = tk.Label(
  20. frame_default_source_file_column1, text="模板文件:", width=8
  21. )
  22. self.source_file_entry = tk.Entry(frame_default_source_file_column1)
  23. self.source_file_label.pack(side=tk.LEFT, padx=4, pady=8)
  24. self.source_file_entry.pack(fill=tk.X, padx=4, expand=True)
  25. self.source_file_entry.insert(0, config.source_file)
  26. self.source_file_button = tk.ttk.Button(
  27. frame_default_source_file_column2,
  28. text="选择文件",
  29. command=self.select_source_file,
  30. )
  31. self.source_file_button.pack(side=tk.LEFT, padx=4, pady=0)
  32. frame_default_final_file = tk.Frame(root)
  33. frame_default_final_file.pack(fill=tk.X)
  34. frame_default_final_file_column1 = tk.Frame(frame_default_final_file)
  35. frame_default_final_file_column1.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
  36. frame_default_final_file_column2 = tk.Frame(frame_default_final_file)
  37. frame_default_final_file_column2.pack(side=tk.RIGHT, fill=tk.Y)
  38. self.final_file_label = tk.Label(
  39. frame_default_final_file_column1, text="结果文件:", width=8
  40. )
  41. self.final_file_entry = tk.Entry(frame_default_final_file_column1)
  42. self.final_file_label.pack(side=tk.LEFT, padx=4, pady=8)
  43. self.final_file_entry.pack(fill=tk.X, padx=4, expand=True)
  44. self.final_file_entry.insert(0, config.final_file)
  45. self.final_file_button = tk.ttk.Button(
  46. frame_default_final_file_column2,
  47. text="选择文件",
  48. command=self.select_final_file,
  49. )
  50. self.final_file_button.pack(side=tk.LEFT, padx=4, pady=0)
  51. frame_default_open_update = tk.Frame(root)
  52. frame_default_open_update.pack(fill=tk.X)
  53. frame_default_open_update_column1 = tk.Frame(frame_default_open_update)
  54. frame_default_open_update_column1.pack(side=tk.LEFT, fill=tk.Y)
  55. frame_default_open_update_column2 = tk.Frame(frame_default_open_update)
  56. frame_default_open_update_column2.pack(side=tk.RIGHT, fill=tk.Y)
  57. self.open_update_label = tk.Label(
  58. frame_default_open_update_column1, text="开启更新:", width=12
  59. )
  60. self.open_update_label.pack(side=tk.LEFT, padx=4, pady=8)
  61. self.open_update_var = tk.BooleanVar(value=config.open_update)
  62. self.open_update_checkbutton = ttk.Checkbutton(
  63. frame_default_open_update_column1,
  64. variable=self.open_update_var,
  65. onvalue=True,
  66. offvalue=False,
  67. command=self.update_open_update
  68. )
  69. self.open_update_checkbutton.pack(side=tk.LEFT, padx=4, pady=8)
  70. self.open_service_label = tk.Label(
  71. frame_default_open_update_column2, text="开启服务:", width=8
  72. )
  73. self.open_service_label.pack(side=tk.LEFT, padx=4, pady=8)
  74. self.open_service_var = tk.BooleanVar(value=config.open_service)
  75. self.open_service_checkbutton = ttk.Checkbutton(
  76. frame_default_open_update_column2,
  77. variable=self.open_service_var,
  78. onvalue=True,
  79. offvalue=False,
  80. command=self.update_open_service
  81. )
  82. self.open_service_checkbutton.pack(side=tk.LEFT, padx=4, pady=8)
  83. self.app_port_label = tk.Label(
  84. frame_default_open_update_column2, text="端口:", width=3
  85. )
  86. self.app_port_label.pack(side=tk.LEFT, padx=4, pady=8)
  87. self.app_port_entry = tk.Entry(frame_default_open_update_column2, width=8)
  88. self.app_port_entry.pack(side=tk.LEFT, padx=4, pady=8)
  89. self.app_port_entry.insert(0, config.app_port)
  90. self.app_port_entry.bind("<KeyRelease>", self.update_app_port)
  91. frame_default_open_cache = tk.Frame(root)
  92. frame_default_open_cache.pack(fill=tk.X)
  93. frame_default_open_cache_column1 = tk.Frame(frame_default_open_cache)
  94. frame_default_open_cache_column1.pack(side=tk.LEFT, fill=tk.Y)
  95. frame_default_open_cache_column2 = tk.Frame(frame_default_open_cache)
  96. frame_default_open_cache_column2.pack(side=tk.RIGHT, fill=tk.Y)
  97. self.open_use_old_result_label = tk.Label(
  98. frame_default_open_cache_column1, text="使用历史结果:", width=12
  99. )
  100. self.open_use_old_result_label.pack(side=tk.LEFT, padx=4, pady=8)
  101. self.open_use_old_result_var = tk.BooleanVar(value=config.open_use_old_result)
  102. self.open_use_old_result_checkbutton = ttk.Checkbutton(
  103. frame_default_open_cache_column1,
  104. variable=self.open_use_old_result_var,
  105. onvalue=True,
  106. offvalue=False,
  107. command=self.update_open_use_old_result,
  108. )
  109. self.open_use_old_result_checkbutton.pack(side=tk.LEFT, padx=4, pady=8)
  110. self.open_use_cache_label = tk.Label(
  111. frame_default_open_cache_column2, text="使用离线数据:", width=12
  112. )
  113. self.open_use_cache_label.pack(side=tk.LEFT, padx=4, pady=8)
  114. self.open_use_cache_var = tk.BooleanVar(value=config.open_use_cache)
  115. self.open_use_cache_checkbutton = ttk.Checkbutton(
  116. frame_default_open_cache_column2,
  117. variable=self.open_use_cache_var,
  118. onvalue=True,
  119. offvalue=False,
  120. command=self.update_open_use_cache,
  121. )
  122. self.open_use_cache_checkbutton.pack(side=tk.LEFT, padx=4, pady=8)
  123. frame_default_mode = tk.Frame(root)
  124. frame_default_mode.pack(fill=tk.X)
  125. frame_default_mode_params_column1 = tk.Frame(frame_default_mode)
  126. frame_default_mode_params_column1.pack(side=tk.LEFT, fill=tk.Y)
  127. frame_default_mode_params_column2 = tk.Frame(frame_default_mode)
  128. frame_default_mode_params_column2.pack(side=tk.RIGHT, fill=tk.Y)
  129. self.open_request_label = tk.Label(
  130. frame_default_mode_params_column1, text="开启网络请求:", width=12
  131. )
  132. self.open_request_label.pack(side=tk.LEFT, padx=4, pady=8)
  133. self.open_request_var = tk.BooleanVar(value=config.open_request)
  134. self.open_request_checkbutton = ttk.Checkbutton(
  135. frame_default_mode_params_column1,
  136. variable=self.open_request_var,
  137. onvalue=True,
  138. offvalue=False,
  139. command=self.update_open_request
  140. )
  141. self.open_request_checkbutton.pack(side=tk.LEFT, padx=4, pady=8)
  142. self.request_timeout_label = tk.Label(
  143. frame_default_mode_params_column2, text="请求超时(s):", width=12
  144. )
  145. self.request_timeout_label.pack(side=tk.LEFT, padx=4, pady=8)
  146. self.request_timeout_entry = tk.Entry(frame_default_mode_params_column2, width=8)
  147. self.request_timeout_entry.pack(side=tk.LEFT, padx=4, pady=8)
  148. self.request_timeout_entry.insert(0, config.request_timeout)
  149. self.request_timeout_entry.bind("<KeyRelease>", self.update_request_timeout)
  150. frame_default_channel = tk.Frame(root)
  151. frame_default_channel.pack(fill=tk.X)
  152. frame_default_channel_column1 = tk.Frame(frame_default_channel)
  153. frame_default_channel_column1.pack(side=tk.LEFT, fill=tk.Y)
  154. frame_default_channel_column2 = tk.Frame(frame_default_channel)
  155. frame_default_channel_column2.pack(side=tk.RIGHT, fill=tk.Y)
  156. self.urls_limit_label = tk.Label(
  157. frame_default_channel_column1, text="频道接口数量:", width=12
  158. )
  159. self.urls_limit_label.pack(side=tk.LEFT, padx=4, pady=8)
  160. self.urls_limit_entry = tk.Entry(frame_default_channel_column1, width=8)
  161. self.urls_limit_entry.pack(side=tk.LEFT, padx=4, pady=8)
  162. self.urls_limit_entry.insert(0, config.urls_limit)
  163. self.urls_limit_entry.bind("<KeyRelease>", self.update_urls_limit)
  164. self.ipv_type_label = tk.Label(
  165. frame_default_channel_column2, text="接口协议类型:", width=12
  166. )
  167. self.ipv_type_label.pack(side=tk.LEFT, padx=4, pady=8)
  168. self.ipv_type_combo = ttk.Combobox(frame_default_channel_column2, width=5)
  169. self.ipv_type_combo.pack(side=tk.LEFT, padx=4, pady=8)
  170. self.ipv_type_combo["values"] = ("IPv4", "IPv6", "全部")
  171. if config.ipv_type == "ipv4":
  172. self.ipv_type_combo.current(0)
  173. elif config.ipv_type == "ipv6":
  174. self.ipv_type_combo.current(1)
  175. else:
  176. self.ipv_type_combo.current(2)
  177. self.ipv_type_combo.bind("<<ComboboxSelected>>", self.update_ipv_type)
  178. frame_proxy_mode = tk.Frame(root)
  179. frame_proxy_mode.pack(fill=tk.X)
  180. frame_proxy_mode_params_column1 = tk.Frame(frame_proxy_mode)
  181. frame_proxy_mode_params_column1.pack(side=tk.LEFT, fill=tk.Y)
  182. frame_proxy_mode_params_column2 = tk.Frame(frame_proxy_mode)
  183. frame_proxy_mode_params_column2.pack(side=tk.RIGHT, fill=tk.Y)
  184. self.open_proxy_label = tk.Label(
  185. frame_proxy_mode_params_column1, text="开启代理查询:", width=12
  186. )
  187. self.open_proxy_label.pack(side=tk.LEFT, padx=4, pady=8)
  188. self.open_proxy_var = tk.BooleanVar(value=config.open_proxy)
  189. self.open_proxy_checkbutton = ttk.Checkbutton(
  190. frame_proxy_mode_params_column1,
  191. variable=self.open_proxy_var,
  192. onvalue=True,
  193. offvalue=False,
  194. command=self.update_open_proxy
  195. )
  196. self.open_proxy_checkbutton.pack(side=tk.LEFT, padx=4, pady=8)
  197. self.open_keep_all_label = tk.Label(
  198. frame_proxy_mode_params_column2, text="完整记录:", width=12
  199. )
  200. self.open_keep_all_label.pack(side=tk.LEFT, padx=4, pady=8)
  201. self.open_keep_all_var = tk.BooleanVar(value=config.open_keep_all)
  202. self.open_keep_all_checkbutton = ttk.Checkbutton(
  203. frame_proxy_mode_params_column2,
  204. variable=self.open_keep_all_var,
  205. onvalue=True,
  206. offvalue=False,
  207. command=self.update_open_keep_all
  208. )
  209. self.open_keep_all_checkbutton.pack(side=tk.LEFT, padx=4, pady=8)
  210. frame_m3u = tk.Frame(root)
  211. frame_m3u.pack(fill=tk.X)
  212. frame_proxy_m3u_column1 = tk.Frame(frame_m3u)
  213. frame_proxy_m3u_column1.pack(side=tk.LEFT, fill=tk.Y)
  214. frame_proxy_m3u_column2 = tk.Frame(frame_m3u)
  215. frame_proxy_m3u_column2.pack(side=tk.RIGHT, fill=tk.Y)
  216. self.open_m3u_result_label = tk.Label(
  217. frame_proxy_m3u_column1, text="M3U转换:", width=12
  218. )
  219. self.open_m3u_result_label.pack(side=tk.LEFT, padx=4, pady=8)
  220. self.open_m3u_result_var = tk.BooleanVar(value=config.open_m3u_result)
  221. self.open_m3u_result_checkbutton = ttk.Checkbutton(
  222. frame_proxy_m3u_column1,
  223. variable=self.open_m3u_result_var,
  224. onvalue=True,
  225. offvalue=False,
  226. command=self.update_open_m3u_result
  227. )
  228. self.open_m3u_result_checkbutton.pack(side=tk.LEFT, padx=4, pady=8)
  229. self.open_driver_label = tk.Label(
  230. frame_proxy_m3u_column2, text="浏览器模式:", width=12
  231. )
  232. self.open_driver_label.pack(side=tk.LEFT, padx=4, pady=8)
  233. self.open_driver_var = tk.BooleanVar(value=config.open_driver)
  234. self.open_driver_checkbutton = ttk.Checkbutton(
  235. frame_proxy_m3u_column2,
  236. variable=self.open_driver_var,
  237. onvalue=True,
  238. offvalue=False,
  239. command=self.update_open_driver
  240. )
  241. self.open_driver_checkbutton.pack(side=tk.LEFT, padx=4, pady=8)
  242. frame_default_open_update_info = tk.Frame(root)
  243. frame_default_open_update_info.pack(fill=tk.X)
  244. frame_default_open_update_info_column1 = tk.Frame(
  245. frame_default_open_update_info
  246. )
  247. frame_default_open_update_info_column1.pack(side=tk.LEFT, fill=tk.Y)
  248. frame_default_open_update_info_column2 = tk.Frame(
  249. frame_default_open_update_info
  250. )
  251. frame_default_open_update_info_column2.pack(side=tk.RIGHT, fill=tk.Y)
  252. self.open_update_time_label = tk.Label(
  253. frame_default_open_update_info_column1, text="显示更新时间:", width=12
  254. )
  255. self.open_update_time_label.pack(side=tk.LEFT, padx=4, pady=8)
  256. self.open_update_time_var = tk.BooleanVar(value=config.open_update_time)
  257. self.open_update_time_checkbutton = ttk.Checkbutton(
  258. frame_default_open_update_info_column1,
  259. variable=self.open_update_time_var,
  260. onvalue=True,
  261. offvalue=False,
  262. command=self.update_open_update_time,
  263. )
  264. self.open_update_time_checkbutton.pack(side=tk.LEFT, padx=4, pady=8)
  265. self.open_url_info_label = tk.Label(
  266. frame_default_open_update_info_column2, text="显示接口信息:", width=12
  267. )
  268. self.open_url_info_label.pack(side=tk.LEFT, padx=4, pady=8)
  269. self.open_url_info_var = tk.BooleanVar(value=config.open_url_info)
  270. self.open_url_info_checkbutton = ttk.Checkbutton(
  271. frame_default_open_update_info_column2,
  272. variable=self.open_url_info_var,
  273. onvalue=True,
  274. offvalue=False,
  275. command=self.update_open_url_info,
  276. )
  277. self.open_url_info_checkbutton.pack(side=tk.LEFT, padx=4, pady=8)
  278. frame_default_open_empty_category = tk.Frame(root)
  279. frame_default_open_empty_category.pack(fill=tk.X)
  280. frame_default_open_empty_category_column1 = tk.Frame(
  281. frame_default_open_empty_category
  282. )
  283. frame_default_open_empty_category_column1.pack(side=tk.LEFT, fill=tk.Y)
  284. frame_default_open_empty_category_column2 = tk.Frame(
  285. frame_default_open_empty_category
  286. )
  287. frame_default_open_empty_category_column2.pack(side=tk.RIGHT, fill=tk.Y)
  288. self.open_empty_category_label = tk.Label(
  289. frame_default_open_empty_category_column1, text="显示无结果分类:", width=12
  290. )
  291. self.open_empty_category_label.pack(side=tk.LEFT, padx=4, pady=8)
  292. self.open_empty_category_var = tk.BooleanVar(value=config.open_empty_category)
  293. self.open_empty_category_checkbutton = ttk.Checkbutton(
  294. frame_default_open_empty_category_column1,
  295. variable=self.open_empty_category_var,
  296. onvalue=True,
  297. offvalue=False,
  298. command=self.update_open_empty_category,
  299. )
  300. self.open_empty_category_checkbutton.pack(side=tk.LEFT, padx=4, pady=8)
  301. self.ipv6_support_label = tk.Label(
  302. frame_default_open_empty_category_column2, text="跳过IPv6检测:", width=12
  303. )
  304. self.ipv6_support_label.pack(side=tk.LEFT, padx=4, pady=8)
  305. self.ipv6_support_var = tk.BooleanVar(value=config.ipv6_support)
  306. self.ipv6_support_checkbutton = ttk.Checkbutton(
  307. frame_default_open_empty_category_column2,
  308. variable=self.ipv6_support_var,
  309. onvalue=True,
  310. offvalue=False,
  311. command=self.update_ipv6_support,
  312. )
  313. self.ipv6_support_checkbutton.pack(side=tk.LEFT, padx=4, pady=8)
  314. frame_default_url_keywords = tk.Frame(root)
  315. frame_default_url_keywords.pack(fill=tk.X)
  316. frame_default_url_keywords_column1 = tk.Frame(frame_default_url_keywords)
  317. frame_default_url_keywords_column1.pack(side=tk.LEFT, fill=tk.Y)
  318. frame_default_url_keywords_column2 = tk.Frame(frame_default_url_keywords)
  319. frame_default_url_keywords_column2.pack(side=tk.RIGHT, fill=tk.Y)
  320. self.url_keywords_whitelist_label = tk.Label(
  321. frame_default_url_keywords_column1, text="白名单:", width=12
  322. )
  323. self.url_keywords_whitelist_label.pack(side=tk.LEFT, padx=4, pady=8)
  324. self.whitelist_file_button = tk.ttk.Button(
  325. frame_default_url_keywords_column1,
  326. text="编辑",
  327. command=self.edit_whitelist_file,
  328. )
  329. self.whitelist_file_button.pack(side=tk.LEFT, padx=4, pady=0)
  330. self.url_keywords_blacklist_label = tk.Label(
  331. frame_default_url_keywords_column2, text="黑名单:", width=12
  332. )
  333. self.url_keywords_blacklist_label.pack(side=tk.LEFT, padx=4, pady=8)
  334. self.blacklist_file_button = tk.ttk.Button(
  335. frame_default_url_keywords_column2,
  336. text="编辑",
  337. command=self.edit_blacklist_file,
  338. )
  339. self.blacklist_file_button.pack(side=tk.LEFT, padx=4, pady=0)
  340. def update_open_update(self):
  341. config.set("Settings", "open_update", str(self.open_update_var.get()))
  342. def update_open_service(self):
  343. config.set("Settings", "open_service", str(self.open_update_var.get()))
  344. def update_app_port(self, event):
  345. config.set("Settings", "app_port", self.app_port_entry.get())
  346. def update_open_use_old_result(self):
  347. config.set(
  348. "Settings", "open_use_old_result", str(self.open_use_old_result_var.get())
  349. )
  350. def update_open_use_cache(self):
  351. config.set(
  352. "Settings", "open_use_cache", str(self.open_use_cache_var.get())
  353. )
  354. def select_source_file(self):
  355. filepath = filedialog.askopenfilename(
  356. initialdir=os.getcwd(), title="选择模板文件", filetypes=[("txt", "*.txt")]
  357. )
  358. if filepath:
  359. self.source_file_entry.delete(0, tk.END)
  360. self.source_file_entry.insert(0, filepath)
  361. config.set("Settings", "source_file", filepath)
  362. def select_final_file(self):
  363. filepath = filedialog.askopenfilename(
  364. initialdir=os.getcwd(), title="选择结果文件", filetypes=[("txt", "*.txt")]
  365. )
  366. if filepath:
  367. self.final_file_entry.delete(0, tk.END)
  368. self.final_file_entry.insert(0, filepath)
  369. config.set("Settings", "final_file", filepath)
  370. def update_open_request(self):
  371. config.set("Settings", "open_requests", str(self.open_request_var.get()))
  372. def update_open_driver(self):
  373. config.set("Settings", "open_driver", str(self.open_driver_var.get()))
  374. def update_open_proxy(self):
  375. config.set("Settings", "open_proxy", str(self.open_proxy_var.get()))
  376. def update_open_keep_all(self):
  377. config.set("Settings", "open_keep_all", str(self.open_keep_all_var.get()))
  378. def update_open_m3u_result(self):
  379. config.set("Settings", "open_m3u_result", str(self.open_m3u_result_var.get()))
  380. def update_request_timeout(self, event):
  381. config.set("Settings", "request_timeout", self.request_timeout_entry.get())
  382. def update_urls_limit(self, event):
  383. config.set("Settings", "urls_limit", self.urls_limit_entry.get())
  384. def update_open_update_time(self):
  385. config.set("Settings", "open_update_time", str(self.open_update_time_var.get()))
  386. def update_open_url_info(self):
  387. config.set("Settings", "open_url_info", str(self.open_url_info_var.get()))
  388. def update_open_empty_category(self):
  389. config.set(
  390. "Settings", "open_empty_category", str(self.open_empty_category_var.get())
  391. )
  392. def update_ipv6_support(self):
  393. config.set(
  394. "Settings", "ipv6_support", str(self.ipv6_support_var.get())
  395. )
  396. def update_ipv_type(self, event):
  397. config.set("Settings", "ipv_type", self.ipv_type_combo.get())
  398. def edit_whitelist_file(self):
  399. path = resource_path(constants.whitelist_path)
  400. if os.path.exists(path):
  401. os.system(f'notepad.exe {path}')
  402. def edit_blacklist_file(self):
  403. path = resource_path(constants.blacklist_path)
  404. if os.path.exists(path):
  405. os.system(f'notepad.exe {path}')
  406. def change_entry_state(self, state):
  407. for entry in [
  408. "open_update_checkbutton",
  409. "open_service_checkbutton",
  410. "app_port_entry",
  411. "open_use_old_result_checkbutton",
  412. "open_use_cache_checkbutton",
  413. "open_request_checkbutton",
  414. "open_driver_checkbutton",
  415. "open_proxy_checkbutton",
  416. "request_timeout_entry",
  417. "source_file_entry",
  418. "source_file_button",
  419. "final_file_entry",
  420. "final_file_button",
  421. "open_keep_all_checkbutton",
  422. "open_m3u_result_checkbutton",
  423. "urls_limit_entry",
  424. "open_update_time_checkbutton",
  425. "open_url_info_checkbutton",
  426. "open_empty_category_checkbutton",
  427. "ipv_type_combo",
  428. "ipv6_support_checkbutton",
  429. "whitelist_file_button",
  430. "blacklist_file_button",
  431. ]:
  432. getattr(self, entry).config(state=state)