perf: set page optimization、API request method(adaptation linux)、Linux certificate installation optimization

This commit is contained in:
putyy
2025-05-10 16:56:54 +08:00
parent 7793f83ea3
commit a37bde428d
17 changed files with 239 additions and 140 deletions

View File

@@ -23,7 +23,7 @@ type App struct {
LockFile string `json:"-"`
PublicCrt []byte `json:"-"`
PrivateKey []byte `json:"-"`
IsProxy bool `json:"-"`
IsProxy bool `json:"IsProxy"`
}
var (

16
core/bind.go Normal file
View File

@@ -0,0 +1,16 @@
package core
type Bind struct {
}
func NewBind() *Bind {
return &Bind{}
}
func (b *Bind) Config() *ResponseData {
return httpServerOnce.buildResp(1, "ok", globalConfig)
}
func (b *Bind) AppInfo() *ResponseData {
return httpServerOnce.buildResp(1, "ok", appOnce)
}

View File

@@ -42,13 +42,8 @@ func (h *HttpServer) run() {
}
fmt.Println("Service started, listening http://" + globalConfig.Host + ":" + globalConfig.Port)
if err1 := http.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Host == "127.0.0.1:"+globalConfig.Port && strings.Contains(r.URL.Path, "/cert") {
w.Header().Set("Content-Type", "application/x-x509-ca-data")
w.Header().Set("Content-Disposition", "attachment;filename=res-downloader-public.crt")
w.Header().Set("Content-Transfer-Encoding", "binary")
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(appOnce.PublicCrt)))
w.WriteHeader(http.StatusOK)
_, err = io.Copy(w, io.NopCloser(bytes.NewReader(appOnce.PublicCrt)))
if r.Host == "127.0.0.1:"+globalConfig.Port && HandleApi(w, r) {
} else {
proxyOnce.Proxy.ServeHTTP(w, r) // 代理
}
@@ -58,6 +53,15 @@ func (h *HttpServer) run() {
}
}
func (h *HttpServer) downCert(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/x-x509-ca-data")
w.Header().Set("Content-Disposition", "attachment;filename=res-downloader-public.crt")
w.Header().Set("Content-Transfer-Encoding", "binary")
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(appOnce.PublicCrt)))
w.WriteHeader(http.StatusOK)
io.Copy(w, io.NopCloser(bytes.NewReader(appOnce.PublicCrt)))
}
func (h *HttpServer) preview(w http.ResponseWriter, r *http.Request) {
realURL := r.URL.Query().Get("url")
if realURL == "" {
@@ -115,7 +119,7 @@ func (h *HttpServer) send(t string, data interface{}) {
runtime.EventsEmit(appOnce.ctx, "event", string(jsonData))
}
func (h *HttpServer) writeJson(w http.ResponseWriter, data ResponseData) {
func (h *HttpServer) writeJson(w http.ResponseWriter, data *ResponseData) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
err := json.NewEncoder(w).Encode(data)
@@ -134,12 +138,7 @@ func (h *HttpServer) error(w http.ResponseWriter, args ...interface{}) {
if len(args) > 1 {
data = args[1]
}
h.writeJson(w, ResponseData{
Code: 0,
Message: message,
Data: data,
})
h.writeJson(w, h.buildResp(0, message, data))
}
func (h *HttpServer) success(w http.ResponseWriter, args ...interface{}) {
@@ -153,12 +152,15 @@ func (h *HttpServer) success(w http.ResponseWriter, args ...interface{}) {
if len(args) > 1 {
message = args[1].(string)
}
h.writeJson(w, h.buildResp(1, message, data))
}
h.writeJson(w, ResponseData{
Code: 1,
func (h *HttpServer) buildResp(code int, message string, data interface{}) *ResponseData {
return &ResponseData{
Code: code,
Message: message,
Data: data,
})
}
}
func (h *HttpServer) openDirectoryDialog(w http.ResponseWriter, r *http.Request) {

View File

@@ -60,6 +60,8 @@ func HandleApi(w http.ResponseWriter, r *http.Request) bool {
httpServerOnce.wxFileDecode(w, r)
case "/api/batch-import":
httpServerOnce.batchImport(w, r)
case "/api/cert":
httpServerOnce.downCert(w, r)
}
return true
}

View File

@@ -64,24 +64,23 @@ func (s *SystemSetup) setProxy() error {
return err
}
is := false
errs := ""
isSuccess := false
var errs strings.Builder
for _, serviceName := range services {
cmds := [][]string{
commands := [][]string{
{"networksetup", "-setwebproxy", serviceName, "127.0.0.1", globalConfig.Port},
{"networksetup", "-setsecurewebproxy", serviceName, "127.0.0.1", globalConfig.Port},
}
for _, args := range cmds {
if output, err := s.runCommand(args); err != nil {
errs = errs + "output:" + string(output) + " err:" + err.Error() + "\n"
fmt.Println("setProxy:", output, " err:", err.Error())
for _, cmd := range commands {
if output, err := s.runCommand(cmd); err != nil {
errs.WriteString(fmt.Sprintf("cmd: %v\noutput: %s\nerr: %s\n", cmd, output, err))
} else {
is = true
isSuccess = true
}
}
}
if is {
if isSuccess {
return nil
}
@@ -94,24 +93,23 @@ func (s *SystemSetup) unsetProxy() error {
return err
}
is := false
errs := ""
isSuccess := false
var errs strings.Builder
for _, serviceName := range services {
cmds := [][]string{
commands := [][]string{
{"networksetup", "-setwebproxystate", serviceName, "off"},
{"networksetup", "-setsecurewebproxystate", serviceName, "off"},
}
for _, args := range cmds {
if output, err := s.runCommand(args); err != nil {
errs = errs + "output:" + string(output) + " err:" + err.Error() + "\n"
fmt.Println("unsetProxy:", output, " err:", err.Error())
for _, cmd := range commands {
if output, err := s.runCommand(cmd); err != nil {
errs.WriteString(fmt.Sprintf("cmd: %v\noutput: %s\nerr: %s\n", cmd, output, err))
} else {
is = true
isSuccess = true
}
}
}
if is {
if isSuccess {
return nil
}

View File

@@ -6,8 +6,22 @@ import (
"bytes"
"fmt"
"os/exec"
"strings"
)
func (s *SystemSetup) getLinuxDistro() (string, error) {
data, err := os.ReadFile("/etc/os-release")
if err != nil {
return "", err
}
for _, line := range strings.Split(string(data), "\n") {
if strings.HasPrefix(line, "ID=") {
return strings.Trim(strings.TrimPrefix(line, "ID="), "\""), nil
}
}
return "", fmt.Errorf("could not determine linux distribution")
}
func (s *SystemSetup) runCommand(args []string) ([]byte, error) {
if len(args) == 0 {
return nil, fmt.Errorf("no command provided")
@@ -33,27 +47,32 @@ func (s *SystemSetup) setProxy() error {
{"gsettings", "set", "org.gnome.system.proxy.https", "host", "127.0.0.1"},
{"gsettings", "set", "org.gnome.system.proxy.https", "port", globalConfig.Port},
}
is := false
errs := ""
isSuccess := false
var errs strings.Builder
for _, cmd := range commands {
if output, err := s.runCommand(cmd); err != nil {
errs = errs + "output:" + string(output) + " err:" + err.Error() + "\n"
fmt.Println(err)
errs.WriteString(fmt.Sprintf("cmd: %v\noutput: %s\nerr: %s\n", cmd, output, err))
} else {
is = true
isSuccess = true
}
}
if is {
if isSuccess {
return nil
}
return fmt.Errorf("failed to set proxy for any active network service, errs:%s", errs)
return fmt.Errorf("failed to set proxy:\n%s", errs.String())
}
func (s *SystemSetup) unsetProxy() error {
cmd := []string{"gsettings", "set", "org.gnome.system.proxy", "mode", "none"}
output, err := s.runCommand(cmd)
return fmt.Errorf("failed to unset proxy for any active network service, errs output:" + string(output) + " err:" + err.Error())
if err != nil {
return fmt.Errorf("failed to unset proxy: %s\noutput: %s", err.Error(), string(output))
}
return nil
}
func (s *SystemSetup) installCert() (string, error) {
@@ -62,35 +81,56 @@ func (s *SystemSetup) installCert() (string, error) {
return "", err
}
actions := [][]string{
{"cp", "-f", s.CertFile, "/usr/local/share/ca-certificates/" + appOnce.AppName + ".crt"},
{"update-ca-certificates"},
{"cp", "-f", s.CertFile, "/usr/share/ca-certificates/trust-source/anchors/" + appOnce.AppName + ".crt"},
{"update-ca-trust"},
{"trust", "extract-compat"},
{"cp", "-f", s.CertFile, "/etc/pki/ca-trust/source/anchors/" + appOnce.AppName + ".crt"},
{"update-ca-trust"},
{"cp", "-f", s.CertFile, "/etc/ssl/ca-certificates/" + appOnce.AppName + ".crt"},
{"update-ca-certificates"},
distro, err := getLinuxDistro()
if err != nil {
return "", fmt.Errorf("detect distro failed: %w", err)
}
is := false
outs := ""
errs := ""
for _, action := range actions {
if output, err1 := s.runCommand(action); err1 != nil {
outs += string(output) + "\n"
errs += err1.Error() + "\n"
fmt.Printf("Failed to execute %v: %v\n", action, err1)
continue
certName := appOnce.AppName + ".crt"
var certPath string
if distro == "deepin" {
certDir := "/usr/share/ca-certificates/" + appOnce.AppName
certPath = certDir + "/" + certName
s.runCommand([]string{"mkdir", "-p", certDir})
} else {
certPath = "/usr/local/share/ca-certificates/" + certName
}
var outs, errs strings.Builder
isSuccess := false
if output, err := s.runCommand([]string{"cp", "-f", s.CertFile, certPath}); err != nil {
errs.WriteString(fmt.Sprintf("copy cert failed: %s\n%s\n", err.Error(), output))
} else {
isSuccess = true
outs.Write(output)
}
if distro == "deepin" {
confPath := "/etc/ca-certificates.conf"
checkCmd := []string{"grep", "-qxF", certName, confPath}
if _, err := s.runCommand(checkCmd); err != nil {
echoCmd := []string{"bash", "-c", fmt.Sprintf("echo '%s' >> %s", certName, confPath)}
if output, err := s.runCommand(echoCmd); err != nil {
errs.WriteString(fmt.Sprintf("append conf failed: %s\n%s\n", err.Error(), output))
} else {
isSuccess = true
outs.Write(output)
}
}
is = true
}
if is {
if output, err := s.runCommand([]string{"update-ca-certificates"}); err != nil {
errs.WriteString(fmt.Sprintf("update failed: %s\n%s\n", err.Error(), output))
} else {
isSuccess = true
outs.Write(output)
}
if isSuccess {
return "", nil
}
return outs, fmt.Errorf("Certificate installation failed, errs:%s", errs)
return outs.String(), fmt.Errorf("certificate installation failed:\n%s", errs.String())
}