有善人站友分享 override 站点,有时候要切换站点,就用AI 糊了一个代码
在env.conf 中添加要切换的站点:
先看这里https://linux.do/t/topic/95672
[SITE]
url1 = http://192.168.1.222:8181
url2 = http://老友分享的站点
url3 = http://127.0.0.1:8181
import configparser
import tkinter as tk
from tkinter import ttk, messagebox
class ConfigManager:
def __init__(self, master):
self.master = master
self.master.title("override ja-netfilter 配置管理器 by cfbiso")
# gui界面在屏幕中心
self.master.update_idletasks()
x = (self.master.winfo_screenwidth() - self.master.winfo_reqwidth()) / 2
y = (self.master.winfo_screenheight() - self.master.winfo_reqheight()) / 2
self.master.geometry("+%d+%d" % (x, y))
self.master.geometry("680x250")
self.config = configparser.ConfigParser()
self.load_config()
self.create_widgets()
def load_config(self):
self.config.read('env.conf', encoding='utf-8')
def save_config(self):
with open('env.conf', 'w', encoding='utf-8') as configfile:
self.config.write(configfile)
def create_widgets(self):
# 当前配置
ttk.Label(self.master, text="当前配置站点:").grid(row=0, column=0, padx=5, pady=5, sticky="w")
self.current_url = tk.StringVar()
self.current_url.set(self.get_current_url())
ttk.Entry(self.master, textvariable=self.current_url, width=50, state="readonly").grid(row=0, column=1, padx=5, pady=5)
# 之前的配置站点
ttk.Label(self.master, text="之前的配置站点:").grid(row=1, column=0, padx=5, pady=5, sticky="w")
self.previous_urls = self.get_previous_urls()
self.url_combobox = ttk.Combobox(self.master, values=self.previous_urls, width=47)
self.url_combobox.grid(row=1, column=1, padx=5, pady=5)
self.url_combobox.set(self.previous_urls[0] if self.previous_urls else "")
# 切换配置按钮
ttk.Button(self.master, text="切换配置", command=self.switch_config).grid(row=1, column=2, padx=5, pady=5)
# 退出按钮
ttk.Button(self.master, text="退出", command=self.master.quit).grid(row=2, column=1, padx=5, pady=20)
def get_current_url(self):
return self.config.get('ENV', 'PREFIX,AGENT_DEBUG_OVERRIDE_PROXY_URL')
def get_previous_urls(self):
return [self.config.get('SITE', key) for key in self.config['SITE']]
def switch_config(self):
new_url = self.url_combobox.get()
if not new_url:
messagebox.showerror("错误", "请选择一个配置站点")
return
for key in self.config['ENV']:
if 'URL' in key:
value = self.config.get('ENV', key)
parts = value.split('=')
self.config.set('ENV', key, f"{parts[0]}={new_url}")
self.save_config()
self.current_url.set(new_url)
messagebox.showinfo("成功", "配置已更新")
if __name__ == "__main__":
# 检查当前目录是否包含env.conf文件,和完整目录是否包含config-jetbrains 如果没有,则提示用户需要复制到 ja-netfilter 目录下config-jetbrains目录里运行并且有env.conf 并退出程序
import os
if not os.path.exists('env.conf'):
messagebox.showerror("错误", "请将本程序复制到 ja-netfilter 目录下config-jetbrains目录里运行并且有env.conf文件, 可以详细参阅始皇陛下的 https://linux.do/t/topic/95672 ")
exit()
root = tk.Tk()
app = ConfigManager(root)
root.mainloop()