Skip to content

Commit

Permalink
fixup! feat(sync): impl proxy support
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf committed Dec 16, 2022
1 parent 23d2542 commit 7452f70
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 89 deletions.
4 changes: 2 additions & 2 deletions src/electron/electron/core.cljs
Expand Up @@ -2,7 +2,7 @@
(:require [electron.handler :as handler]
[electron.search :as search]
[electron.updater :refer [init-updater] :as updater]
[electron.utils :refer [*win mac? linux? dev? get-win-from-sender restore-user-fetch-agent
[electron.utils :refer [*win mac? linux? dev? get-win-from-sender
decode-protected-assets-schema-path get-graph-name send-to-renderer]
:as utils]
[electron.url :refer [logseq-url-handler]]
Expand Down Expand Up @@ -300,7 +300,7 @@
_ (reset! *win win)]
(logger/info (str "Logseq App(" (.getVersion app) ") Starting... "))

(restore-user-fetch-agent)
(utils/<restore-proxy-settings)

(js-utils/disableXFrameOptions win)

Expand Down
21 changes: 9 additions & 12 deletions src/electron/electron/handler.cljs
Expand Up @@ -362,18 +362,18 @@
proxy)
(p/resolved nil)))

(defmethod handle :setProxy [^js window options]
;; options: {:type "system" | "direct" | "socks5" | "http" | "pac_script" }
;;
(let [{:keys [type host port]
:or {type "system"}} options]

))
(defmethod handle :setProxy [_win [_ options]]
;; options: {:type "system" | "direct" | "socks5" | "http" | ... }
(p/do!
(utils/<set-proxy options)
(utils/save-proxy-settings options)))

(defmethod handle :testProxyUrl [_win [_ url options]]
(let [start-ms (.getTime (js/Date.))]
;; FIXME: better not to set proxy while testing url
(let [_ (utils/<set-proxy options)
start-ms (.getTime (js/Date.))]
(-> (utils/fetch url)
(p/timeout 5000)
(p/timeout 10000)
(p/then (fn [resp]
(let [code (.-status resp)
response-ms (- (.getTime (js/Date.)) start-ms)]
Expand Down Expand Up @@ -575,9 +575,6 @@
(when-let [web-content (.-webContents win)]
(.reload web-content)))

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

;;;;;;;;;;;;;;;;;;;;;;;
;; file-sync-rs-apis ;;
;;;;;;;;;;;;;;;;;;;;;;;
Expand Down
106 changes: 45 additions & 61 deletions src/electron/electron/utils.cljs
Expand Up @@ -54,38 +54,22 @@
(map #(path/join plugins-root (.-name %))))]
dirs))

(defn set-fetch-agent
"Set proxy for fetch, and rsapi"
([opts]
(set-fetch-agent opts true))
([{:keys [protocol host port] :as opts} persist-to-config?]
(if (and protocol host port)
(let [proxy-url (str protocol "://" host ":" port)]
(logger/info "set proxy to" proxy-url)
(reset! *fetchAgent (new HttpsProxyAgent proxy-url))
(rsapi/setProxy proxy-url))
(do
(logger/info "reset proxy")
(reset! *fetchAgent nil)
(rsapi/setProxy nil)))
(when persist-to-config?
(cfgs/set-item! :settings/agent opts))))

(defn- set-fetch-agent-proxy
"Set proxy for fetch agent(plugin system)"
"Set proxy for fetch agent(plugin system)
protocol: http | socks5"
[{:keys [protocol host port]}]
(if (and protocol host port)
(if (and protocol host port (or (= protocol "http") (= protocol "socks5")))
(let [proxy-url (str protocol "://" host ":" port)]
(reset! *fetchAgent (new HttpsProxyAgent proxy-url)))
(reset! *fetchAgent nil)))

(defn- set-rsapi-proxy
"Set proxy for Logseq Sync(rsapi)"
[{:keys [protocol host port]}]
(if (and protocol host port)
(if (and protocol host port (or (= protocol "http") (= protocol "socks5")))
(let [proxy-url (str protocol "://" host ":" port)]
(rsapi/setProxy proxy-url)))
(rsapi/setProxy nil))
(rsapi/setProxy proxy-url))
(rsapi/setProxy nil)))

(defn <set-electron-proxy
"Set proxy for electron
Expand Down Expand Up @@ -121,7 +105,6 @@
(if sess
(p/do!
(.setProxy sess config)
(logger/info "set proxy to" config)
(.forceReloadProxyConfig sess))
(p/resolved nil)))))

Expand Down Expand Up @@ -160,53 +143,54 @@

(defn <set-proxy
"Set proxy for electron, fetch, and rsapi"
([{:keys [type host port] :or {type "system"}}]
(logger/info "set proxy to" type host port)
([{:keys [type host port] :or {type "system"} :as opts}]
(logger/info "set proxy to" opts)
(cond
(= type "system")
(p/let [_ (<set-electron-proxy {:type "system"})
proxy (<get-system-proxy)
_ (prn ::<set-proxy proxy)]
(set-fetch-agent proxy)



(reset! *fetchAgent nil)
(rsapi/setProxy nil)
(cfgs/set-item! :settings/agent nil))
proxy (<get-system-proxy)]
(set-fetch-agent-proxy proxy)
(set-rsapi-proxy proxy))

(or (= type "direct") (= type "socks5") (= type "http"))
(let [proxy-url (str type "://" host ":" port)]
(logger/info "set proxy to" proxy-url)
(reset! *fetchAgent (new HttpsProxyAgent proxy-url))
(rsapi/setProxy proxy-url)
(cfgs/set-item! :settings/agent {:protocol type :host host :port port})
(<set-electron-proxy {:type type :host host :port port}))

:else
(= type "direct")
(do
(logger/info "reset proxy")
(reset! *fetchAgent nil)
(rsapi/setProxy nil)
(cfgs/set-item! :settings/agent nil)
(<set-electron-proxy {:type "direct"})))))
(<set-electron-proxy {:type "direct"})
(set-fetch-agent-proxy nil)
(set-rsapi-proxy nil))

(or (= type "socks5") (= type "http"))
(do
(<set-electron-proxy {:type type :host host :port port})
(set-fetch-agent-proxy {:protocol type :host host :port port})
(set-rsapi-proxy {:protocol type :host host :port port}))

:else
(logger/error "Unknown proxy type:" type))))

(defn restore-user-fetch-agent
"Restore user's proxy settings"
(defn <restore-proxy-settings
"Restore proxy settings from configs.edn"
[]
(let [agent-opts (cfgs/get-item :settings/agent)]
(if (and agent-opts (not-empty (:protocol agent-opts)))
(set-fetch-agent agent-opts)
(when-let [sess (.. ^js @*win -webContents -session)]
(p/let [proxy (.resolveProxy sess "https://www.google.com")
pac-opts (->> (string/split proxy #";")
(map parse-pac-rule)
(remove nil?))]
(when (seq pac-opts)
(logger/info "Using system wide proxy:" proxy)
(set-fetch-agent (first pac-opts) false)))))))
(let [settings (cfgs/get-item :settings/agent)
settings (cond
(:type settings)
settings

;; migration from old config
(not-empty (:protocol settings))
(assoc settings :type (:protocol settings))

:else
{:type "system"})]
(logger/info "restore proxy settings" settings)
(<set-proxy settings)))

(defn save-proxy-settings
"Save proxy settings to configs.edn"
[{:keys [type host port test] :or {type "system"}}]
(if (or (= type "system") (= type "direct"))
(cfgs/set-item! :settings/agent {:type type :test test})
(cfgs/set-item! :settings/agent {:type type :protocol type :host host :port port :test test})))


(defn ignored-path?
"Ignore given path from file-watcher notification"
Expand Down
21 changes: 11 additions & 10 deletions src/main/frontend/components/plugins.cljs
Expand Up @@ -377,19 +377,21 @@
:target "_blank"))

(rum/defc user-proxy-settings-panel
[{:keys [protocol] :as agent-opts}]
(let [[opts set-opts!] (rum/use-state agent-opts)
[{:keys [protocol type] :as agent-opts}]
(let [type (or (not-empty type) (not-empty protocol) "system")
[opts set-opts!] (rum/use-state agent-opts)
[testing? set-testing?!] (rum/use-state false)
*test-input (rum/create-ref)
disabled? (string/blank? (:protocol opts))]
disabled? (or (= (:type opts) "system") (= (:type opts) "direct"))]
[:div.cp__settings-network-proxy-panel
[:h1.mb-2.text-2xl.font-bold (t :settings-page/network-proxy)]
[:div.p-2
[:p [:label [:strong (t :type)]
(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 %))))]]
(ui/select [{:label "System" :value "system" :selected (= type "system")}
{:label "Direct" :value "direct" :selected (= type "direct")}
{:label "HTTP" :value "http" :selected (= type "http")}
{:label "SOCKS5" :value "socks5" :selected (= type "socks5")}]
#(set-opts! (assoc opts :type % :protocol %)))]]
[:p.flex
[:label.pr-4
{:class (if disabled? "opacity-50" nil)}
Expand Down Expand Up @@ -430,8 +432,7 @@
: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)
result (ipc/ipc :testProxyUrl val)]
(-> (p/let [result (ipc/ipc :testProxyUrl val opts)]
(js->clj result :keywordize-keys true))
(p/then (fn [{:keys [code response-ms]}]
(notification/show! (str "Success! Status " code " in " response-ms "ms.") :success)))
Expand All @@ -442,7 +443,7 @@
[:p.pt-2
(ui/button (t :save)
:on-click (fn []
(p/let [_ (ipc/ipc :setHttpsAgent opts)]
(p/let [_ (ipc/ipc :setProxy opts)]
(state/set-state! [:electron/user-cfgs :settings/agent] opts)
(state/close-sub-modal! :https-proxy-panel))))]]]))

Expand Down
11 changes: 7 additions & 4 deletions src/main/frontend/components/settings.cljs
Expand Up @@ -524,10 +524,13 @@
true))

(rum/defc user-proxy-settings
[{:keys [protocol host port] :as agent-opts}]
(ui/button [:span
(when-let [e (and protocol host port (str protocol "://" host ":" port))]
[:strong.pr-1 e])
[{:keys [type protocol host port] :as agent-opts}]
(ui/button [:span.flex.items-center
[:strong.pr-1
(case type
"system" "System Default"
"direct" "Direct"
(and protocol host port (str protocol "://" host ":" port)))]
(ui/icon "edit")]
:small? true
:on-click #(state/set-sub-modal!
Expand Down

0 comments on commit 7452f70

Please sign in to comment.