Skip to content

Commit

Permalink
enhance(ui): refine proxy test notification
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf authored and tiensonqin committed Dec 19, 2022
1 parent 6fc5a26 commit f9730b1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
29 changes: 22 additions & 7 deletions src/electron/electron/handler.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -555,6 +569,7 @@
(.reload web-content)))

(defmethod handle :setHttpsAgent [^js _win [_ opts]]
(prn ::opts opts)
(utils/set-fetch-agent opts))

;;;;;;;;;;;;;;;;;;;;;;;
Expand Down
2 changes: 2 additions & 0 deletions src/electron/electron/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))))
Expand Down
34 changes: 21 additions & 13 deletions src/main/frontend/components/plugins.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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 %))))}]]]

Expand All @@ -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)}]
Expand All @@ -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
Expand Down

0 comments on commit f9730b1

Please sign in to comment.