Просмотр исходного кода

enhance(ui): refine proxy test notification

Andelf 3 лет назад
Родитель
Сommit
f9730b1685

+ 22 - 7
src/electron/electron/handler.cljs

@@ -357,13 +357,27 @@
 (defmethod handle :getLogseqDotDirRoot []
   (utils/get-ls-dotdir-root))
 
-(defmethod handle :testProxyUrl [win [_ url]]
-  (p/let [resp (utils/fetch url)
-          code (.-status resp)]
-    (js/console.debug resp code)
-    (if (<= 200 code 299)
-      (utils/send-to-renderer win :notification {:type "success" :payload (str "Success: " url " status " code)})
-      (utils/send-to-renderer win :notification {:type "error" :payload (str "Failed: " url " status " code)}))))
+(defmethod handle :getProxy [^js window]
+  (if-let [sess (.. window -webContents -session)]
+    (p/let [proxy (.resolveProxy sess "https://www.google.com")]
+      proxy)
+    (p/resolved nil)))
+
+(defmethod handle :testProxyUrl [_win [_ url]]
+  (let [start-ms (.getTime (js/Date.))]
+    (-> (utils/fetch url)
+        (p/timeout 5000)
+        (p/then (fn [resp]
+                  (let [code (.-status resp)
+                        response-ms (- (.getTime (js/Date.)) start-ms)]
+                    (if (<= 200 code 299)
+                      #js {:code code
+                           :response-ms response-ms}
+                      (p/rejected (js/Error. (str "HTTP status " code)))))))
+        (p/catch (fn [e]
+                   (if (instance? p/TimeoutException e)
+                     (p/rejected (js/Error. "Timeout"))
+                     (p/rejected e)))))))
 
 (defmethod handle :httpFetchJSON [_win [_ url options]]
   (p/let [res (utils/fetch url options)
@@ -555,6 +569,7 @@
     (.reload web-content)))
 
 (defmethod handle :setHttpsAgent [^js _win [_ opts]]
+  (prn ::opts opts)
   (utils/set-fetch-agent opts))
 
 ;;;;;;;;;;;;;;;;;;;;;;;

+ 2 - 0
src/electron/electron/utils.cljs

@@ -31,6 +31,7 @@
 (defn fetch
   ([url] (fetch url nil))
   ([url options]
+   (prn ::debug-fetch @*fetchAgent)
    (_fetch url (bean/->js (merge options {:agent @*fetchAgent})))))
 
 (defn get-ls-dotdir-root
@@ -54,6 +55,7 @@
 
 (defn set-fetch-agent
   [{:keys [protocol host port] :as opts}]
+  (prn ::set-fetch-agent opts)
   (reset! *fetchAgent
           (when (and protocol host port)
             (new HttpsProxyAgent (str protocol "://" host ":" port))))

+ 21 - 13
src/main/frontend/components/plugins.cljs

@@ -386,21 +386,26 @@
      [:h1.mb-2.text-2xl.font-bold (t :settings-page/network-proxy)]
      [:div.p-2
       [:p [:label [:strong (t :type)]
-           (ui/select [{:label "Disabled" :value "" :selected disabled?}
-                       {:label "http" :value "http" :selected (= protocol "http")}
-                       {:label "socks5" :value "socks5" :selected (= protocol "socks5")}]
-                      #(set-opts!
-                        (assoc opts :protocol (if (= "disabled" (util/safe-lower-case %)) nil %))) nil)]]
+           (ui/select [{:label "Default" :value "default" :selected disabled?}
+                       {:label "HTTP" :value "http" :selected (= protocol "http")}
+                       {:label "SOCKS5" :value "socks5" :selected (= protocol "socks5")}]
+                      #(set-opts! (assoc opts :protocol (if (= % "default") nil %))))]]
       [:p.flex
-       [:label.pr-4 [:strong (t :host)]
+       [:label.pr-4
+        {:class (if disabled? "opacity-50" nil)}
+        [:strong (t :host)]
         [:input.form-input.is-small
-         {:value     (:host opts) :disabled disabled?
+         {:value     (:host opts)
+          :disabled  disabled?
           :on-change #(set-opts!
                        (assoc opts :host (util/trim-safe (util/evalue %))))}]]
 
-       [:label [:strong (t :port)]
+       [:label
+        {:class (if disabled? "opacity-50" nil)}
+        [:strong (t :port)]
         [:input.form-input.is-small
-         {:value     (:port opts) :type "number" :disabled disabled?
+         {:value     (:port opts) :type "number" :min 1 :max 65535
+          :disabled  disabled?
           :on-change #(set-opts!
                        (assoc opts :port (util/trim-safe (util/evalue %))))}]]]
 
@@ -411,7 +416,7 @@
          {:ref         *test-input
           :list        "proxy-test-url-datalist"
           :type        "url"
-          :placeholder "http://"
+          :placeholder "https://"
           :on-change   #(set-opts!
                          (assoc opts :test (util/trim-safe (util/evalue %))))
           :value       (:test opts)}]
@@ -422,13 +427,16 @@
 
        (ui/button (if testing? (ui/loading "Testing") "Test URL")
                   :intent "logseq" :large? false
-                  :style {:margin-top 0 :padding "5px 15px"}
                   :on-click #(let [val (util/trim-safe (.-value (rum/deref *test-input)))]
                                (when (and (not testing?) (not (string/blank? val)))
                                  (set-testing?! true)
                                  (-> (p/let [_ (ipc/ipc :setHttpsAgent opts)
-                                             _ (ipc/ipc :testProxyUrl val)])
-                                     (p/catch (fn [e] (notification/show! (str e) :error)))
+                                             result (ipc/ipc :testProxyUrl val)]
+                                       (js->clj result :keywordize-keys true))
+                                     (p/then (fn [{:keys [code response-ms]}]
+                                               (notification/show! (str "Success! Status " code " in " response-ms "ms.") :success)))
+                                     (p/catch (fn [e]
+                                                (notification/show! (str e) :error)))
                                      (p/finally (fn [] (set-testing?! false)))))))]
 
       [:p.pt-2