完整代码:

package main

import ( "fmt" "io/ioutil" "net/http" "net/url" "os" "path/filepath" "runtime"

"github.com/therecipe/qt/core"
"github.com/therecipe/qt/widgets"

)

func downloadFile() { var proxyUrl string if runtime.GOOS == "windows" { proxyUrl = "http://127.0.0.1:8080" } else { proxyUrl = "http://127.0.0.1:1087" } proxy, err := url.Parse(proxyUrl) if err != nil { fmt.Println("Failed to parse proxy url:", err) return } client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxy)}}

// 下载到桌面
desktop, err := os.UserHomeDir()
if err != nil {
    fmt.Println("Failed to get desktop path:", err)
    return
}
filename := filepath.Join(desktop, "mitmproxy-ca-cert.p12")

// 发送请求并下载文件
resp, err := client.Get("http://mitm.it/cert/p12")
if err != nil {
    fmt.Println("Failed to download file:", err)
    return
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
    fmt.Println("Failed to read response body:", err)
    return
}

err = ioutil.WriteFile(filename, body, 0644)
if err != nil {
    fmt.Println("Failed to save file:", err)
    return
}

fmt.Println("File downloaded to", filename)

}

func setIni(name string) { path := filepath.Join(os.Getenv("USERPROFILE"), "HC.ini") // 获取文件路径 err := ioutil.WriteFile(path, []byte("[general]"), 0644) if err != nil { fmt.Println("Failed to save configuration file:", err) return }

fmt.Println("Config file saved to", path)

}

func main() { var err error var appWindow *widgets.QMainWindow

// Create application window
if runtime.GOOS == "windows" {
    appWindow, err = widgets.NewQMainWindow(nil, core.Qt__Window)
    if err != nil {
        fmt.Println("Failed to create main window:", err)
        return
    }
} else {
    appWindow, err = widgets.NewQMainWindow(nil, core.Qt__Widget)
    if err != nil {
        fmt.Println("Failed to create window:", err)
        return
    }
}
defer appWindow.Close()

// Create UI
rootView := widgets.NewQWidget(nil, 0)
rootView.SetLayout(widgets.NewQVBoxLayout())
rootView.SetContentsMargins(10, 10, 10, 10)

nameLabel := widgets.NewQLabel(nil, 0)
nameLabel.SetText("编号:")
rootView.Layout().AddWidget(nameLabel)

nameEdit := widgets.NewLineEdit(nil)
rootView.Layout().AddWidget(nameEdit)
nameEdit.SetPlaceholderText("请输入编号")

downloadButton := widgets.NewQPushButton(nil)
downloadButton.SetText("下载证书并设置")
rootView.Layout().AddWidget(downloadButton)
downloadButton.SetEnabled(true)

downloadButton.ConnectClicked(func(_ bool) {
    if nameEdit.Text() == "" {
        widgets.QMessageBox_Critical(appWindow, "错误", "请输入编号", widgets.QMessageBox__Ok, widgets.QMessageBox__Ok)
        return
    }

    downloadFile()
    setIni(nameEdit.Text())

    // Show message
    widgets.QMessageBox_Information(appWindow, "信息", "设置完成,证书已下载到桌面,请手动安装。", widgets.QMessageBox__Ok, widgets.QMessageBox__Ok)

    // Close window
    appWindow.Close()
})

// Set root view
appWindow.SetCentralWidget(rootView)

// Center window
appWindow.Resize2(300, 100)
appWindow.Center()
appWindow.Show()

widgets.QApplication_Exec() // Start event loop
整理下面代码并输出完整代码import fmt ioioutil nethttp neturl os pathfilepath runtime func download_file var proxyUrl string if runtimeGOOS == windows proxyUrl = http1270018080 else proxyUrl = http1270011087

原文地址: https://www.cveoy.top/t/topic/eq9v 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录