fix: improve compatibility with Windows 7

This commit is contained in:
ltaoo committed 2026-02-08 20:51:40 +08:00
1 parent 7b17f67b35
commit 413ce47806
2 files changed
+44 -41

No files matched your search

+27 -26
View File
@@ -3,7 +3,6 @@
package certificate
import (
"encoding/json"
"errors"
"fmt"
"os"
@@ -12,28 +11,30 @@ import (
)
func fetchCertificates() ([]Certificate, error) {
cmd := "$certs = Get-ChildItem Cert:\\LocalMachine\\Root | Select-Object Thumbprint, Subject; @($certs) | ConvertTo-Json -Compress"
// PowerShell 2.0 compatible command
cmd := "Get-ChildItem Cert:\\LocalMachine\\Root | ForEach-Object { $_.Thumbprint + \"###\" + $_.Subject }"
ps := exec.Command("powershell.exe", "-NoProfile", "-Command", cmd)
output, err2 := ps.CombinedOutput()
if err2 != nil {
return nil, errors.New(fmt.Sprintf("获取证书时发生错误,%v\n", err2.Error()))
}
type WindowsCert struct {
Thumbprint string `json:"Thumbprint"`
Subject string `json:"Subject"`
}
var raw_arr []WindowsCert
if err := json.Unmarshal(output, &raw_arr); err != nil {
var one WindowsCert
if err2 := json.Unmarshal(output, &one); err2 != nil {
return nil, errors.New(fmt.Sprintf("解析证书列表失败,%v\n", err.Error()))
}
raw_arr = []WindowsCert{one}
return nil, fmt.Errorf("获取证书时发生错误,%v\n", err2.Error())
}
var certificates []Certificate
for _, pc := range raw_arr {
lines := strings.Split(string(output), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
parts := strings.SplitN(line, "###", 2)
if len(parts) < 2 {
continue
}
thumbprint := parts[0]
subject_str := parts[1]
subj := CertificateSubject{}
pairs := strings.Split(pc.Subject, ",")
pairs := strings.Split(subject_str, ",")
for _, p := range pairs {
kv := strings.SplitN(strings.TrimSpace(p), "=", 2)
if len(kv) != 2 {
@@ -57,7 +58,7 @@ func fetchCertificates() ([]Certificate, error) {
}
}
certificates = append(certificates, Certificate{
Thumbprint: pc.Thumbprint,
Thumbprint: thumbprint,
Subject: subj,
})
}
@@ -67,20 +68,20 @@ func fetchCertificates() ([]Certificate, error) {
func installCertificate(cert_data []byte) error {
cert_file, err := os.CreateTemp("", "SunnyRoot.cer")
if err != nil {
return errors.New(fmt.Sprintf("没有创建证书的权限,%v\n", err.Error()))
return fmt.Errorf("没有创建证书的权限,%v\n", err.Error())
}
defer os.Remove(cert_file.Name())
if _, err := cert_file.Write(cert_data); err != nil {
return errors.New(fmt.Sprintf("获取证书失败,%v\n", err.Error()))
return fmt.Errorf("获取证书失败,%v\n", err.Error())
}
if err := cert_file.Close(); err != nil {
return errors.New(fmt.Sprintf("生成证书失败,%v\n", err.Error()))
return fmt.Errorf("生成证书失败,%v\n", err.Error())
}
cmd := fmt.Sprintf("Import-Certificate -FilePath '%s' -CertStoreLocation Cert:\\LocalMachine\\Root", cert_file.Name())
ps := exec.Command("powershell.exe", "-NoProfile", "-Command", cmd)
output, err2 := ps.CombinedOutput()
// Use certutil for Windows 7 compatibility
cmd := exec.Command("certutil", "-addstore", "Root", cert_file.Name())
output, err2 := cmd.CombinedOutput()
if err2 != nil {
return errors.New(fmt.Sprintf("安装证书时发生错误,%v\n", string(output)))
return fmt.Errorf("安装证书时发生错误,%v\n", string(output))
}
return nil
}
@@ -106,7 +107,7 @@ func uninstallCertificate(name string) error {
ps := exec.Command("powershell.exe", "-NoProfile", "-Command", cmd)
output, err2 := ps.CombinedOutput()
if err2 != nil {
return errors.New(fmt.Sprintf("删除证书时发生错误,%v\n", string(output)))
return fmt.Errorf("删除证书时发生错误,%v\n", string(output))
}
return nil
}
+17 -15
View File
@@ -10,20 +10,21 @@ import (
func enable_proxy(args ProxySettings) error {
args = merge_default_settings(args)
path := `HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings`
path := `HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings`
proxy_server_url := fmt.Sprintf("%v:%v", args.Hostname, args.Port)
// # 启用代理
// Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" -Name ProxyEnable -Value 1
// Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" -Name ProxyServer -Value "127.0.0.1:8080"
cmd := fmt.Sprintf(`Set-ItemProperty -Path "%v" -Name ProxyEnable -Value 1`, path)
ps := exec.Command("powershell.exe", "-Command", cmd)
output, err := ps.CombinedOutput()
// reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f
// reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /t REG_SZ /d "127.0.0.1:8080" /f
// 使用 reg 命令替代 powershell,以提高兼容性(支持 Win7)并提升性能
cmd := exec.Command("reg", "add", path, "/v", "ProxyEnable", "/t", "REG_DWORD", "/d", "1", "/f")
output, err := cmd.CombinedOutput()
if err != nil {
return errors.New(fmt.Sprintf("设置系统代理时发生错误,%v\n", string(output)))
return fmt.Errorf("设置系统代理时发生错误,%v\n", string(output))
}
cmd = fmt.Sprintf(`Set-ItemProperty -Path "%v" -Name ProxyServer -Value %v`, path, proxy_server_url)
ps = exec.Command("powershell.exe", "-Command", cmd)
output, err = ps.CombinedOutput()
cmd = exec.Command("reg", "add", path, "/v", "ProxyServer", "/t", "REG_SZ", "/d", proxy_server_url, "/f")
output, err = cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("设置 HTTP 代理失败,%v", string(output))
}
@@ -31,12 +32,13 @@ func enable_proxy(args ProxySettings) error {
}
func disable_proxy(args ProxySettings) error {
path := `HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings`
path := `HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings`
// # 禁用代理
// Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" -Name ProxyEnable -Value 0
cmd := fmt.Sprintf(`Set-ItemProperty -Path "%v" -Name ProxyEnable -Value 0`, path)
ps := exec.Command("powershell.exe", "-Command", cmd)
output, err := ps.CombinedOutput()
// reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f
// 使用 reg 命令替代 powershell
cmd := exec.Command("reg", "add", path, "/v", "ProxyEnable", "/t", "REG_DWORD", "/d", "0", "/f")
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("设置 HTTP 代理失败,%v", string(output))
}