diff --git a/core/app.go b/core/app.go index 2626407..a7faccc 100644 --- a/core/app.go +++ b/core/app.go @@ -23,7 +23,7 @@ type App struct { LockFile string `json:"-"` PublicCrt []byte `json:"-"` PrivateKey []byte `json:"-"` - IsProxy bool `json:"-"` + IsProxy bool `json:"IsProxy"` } var ( diff --git a/core/bind.go b/core/bind.go new file mode 100644 index 0000000..b20af32 --- /dev/null +++ b/core/bind.go @@ -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) +} diff --git a/core/http.go b/core/http.go index 1a5c4cd..39654e3 100644 --- a/core/http.go +++ b/core/http.go @@ -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) { diff --git a/core/middleware.go b/core/middleware.go index 18def54..edb672a 100644 --- a/core/middleware.go +++ b/core/middleware.go @@ -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 } diff --git a/core/system_darwin.go b/core/system_darwin.go index 5cd1e1b..1807fd8 100644 --- a/core/system_darwin.go +++ b/core/system_darwin.go @@ -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 } diff --git a/core/system_linux.go b/core/system_linux.go index 246bb69..b8d10c7 100644 --- a/core/system_linux.go +++ b/core/system_linux.go @@ -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()) } diff --git a/frontend/components.d.ts b/frontend/components.d.ts index 52b0414..5a06d7a 100644 --- a/frontend/components.d.ts +++ b/frontend/components.d.ts @@ -31,8 +31,6 @@ declare module 'vue' { NModal: typeof import('naive-ui')['NModal'] NModalProvider: typeof import('naive-ui')['NModalProvider'] NNotificationProvider: typeof import('naive-ui')['NNotificationProvider'] - NRadio: typeof import('naive-ui')['NRadio'] - NRadioGroup: typeof import('naive-ui')['NRadioGroup'] NScrollbar: typeof import('naive-ui')['NScrollbar'] NSelect: typeof import('naive-ui')['NSelect'] NSpace: typeof import('naive-ui')['NSpace'] diff --git a/frontend/src/api/request.ts b/frontend/src/api/request.ts index 99bd2b7..beb87a7 100644 --- a/frontend/src/api/request.ts +++ b/frontend/src/api/request.ts @@ -1,37 +1,39 @@ import type {AxiosResponse, InternalAxiosRequestConfig} from 'axios' import axios from 'axios' +import {useIndexStore} from "@/stores"; +import {computed} from "vue"; interface RequestOptions { - url: string; - method: 'get' | 'post' | 'put' | 'delete'; // 根据需要扩展 - params?: Record; - data?: Record; + url: string + method: 'get' | 'post' | 'put' | 'delete' + params?: Record + data?: Record } const instance = axios.create({ baseURL: "/", -}); +}) instance.interceptors.request.use( (config: InternalAxiosRequestConfig) => { - return config; + return config }, (error) => { - return Promise.reject(error); + return Promise.reject(error) } -); +) instance.interceptors.response.use( (response: AxiosResponse) => { - return response.data; + return response.data }, (error) => { return Promise.reject(error); } -); +) const request = ({url, method, params, data}: RequestOptions): Promise => { - return instance({url, method, params, data}); -}; + return instance({url, method, params, data, baseURL: window.$baseUrl}) +} -export default request; \ No newline at end of file +export default request \ No newline at end of file diff --git a/frontend/src/components/Footer.vue b/frontend/src/components/Footer.vue index 582501d..3a3eebf 100644 --- a/frontend/src/components/Footer.vue +++ b/frontend/src/components/Footer.vue @@ -47,7 +47,7 @@
{{ store.appInfo.Copyright }}
- + @@ -60,11 +60,14 @@