|
@@ -1,11 +1,13 @@
|
|
|
(ns electron.utils
|
|
|
- (:require [clojure.string :as string]
|
|
|
+ (:require ["@logseq/rsapi" :as rsapi]
|
|
|
+ ["electron" :refer [app BrowserWindow]]
|
|
|
["fs-extra" :as fs]
|
|
|
["path" :as path]
|
|
|
+ [cljs-bean.core :as bean]
|
|
|
+ [clojure.string :as string]
|
|
|
[electron.configs :as cfgs]
|
|
|
[electron.logger :as logger]
|
|
|
- [cljs-bean.core :as bean]
|
|
|
- ["electron" :refer [app BrowserWindow]]))
|
|
|
+ [promesa.core :as p]))
|
|
|
|
|
|
(defonce *win (atom nil)) ;; The main window
|
|
|
|
|
@@ -31,7 +33,6 @@
|
|
|
(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
|
|
@@ -53,18 +54,143 @@
|
|
|
(map #(path/join plugins-root (.-name %))))]
|
|
|
dirs))
|
|
|
|
|
|
-(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))))
|
|
|
- (cfgs/set-item! :settings/agent opts))
|
|
|
+(defn- set-fetch-agent-proxy
|
|
|
+ "Set proxy for fetch agent(plugin system)
|
|
|
+ protocol: http | socks5"
|
|
|
+ [{:keys [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 (or (= protocol "http") (= protocol "socks5")))
|
|
|
+ (let [proxy-url (str protocol "://" host ":" port)]
|
|
|
+ (rsapi/setProxy proxy-url))
|
|
|
+ (rsapi/setProxy nil)))
|
|
|
+
|
|
|
+(defn <set-electron-proxy
|
|
|
+ "Set proxy for electron
|
|
|
+ type: system | direct | socks5 | http"
|
|
|
+ ([{:keys [type host port] :or {type "system"}}]
|
|
|
+ (let [->proxy-rules (fn [type host port]
|
|
|
+ (cond
|
|
|
+ (= type "http")
|
|
|
+ (str "http=" host ":" port ";https=" host ":" port)
|
|
|
+ (= type "socks5")
|
|
|
+ (str "http=socks5://" host ":" port ";https=socks5://" host ":" port)
|
|
|
+ (or (= type "socks") (= type "socks4"))
|
|
|
+ (str "http=socks://" host ":" port ";https=socks://" host ":" port)
|
|
|
+ (= type "direct")
|
|
|
+ "direct://"
|
|
|
+ :else
|
|
|
+ nil))
|
|
|
+ config (cond
|
|
|
+ (= type "system")
|
|
|
+ #js {:mode "system"}
|
|
|
+
|
|
|
+ (= type "direct")
|
|
|
+ #js {:mode "direct"}
|
|
|
+
|
|
|
+ (or (= type "socks5") (= type "http"))
|
|
|
+ #js {:mode "fixed_servers"
|
|
|
+ :proxyRules (->proxy-rules type host port)
|
|
|
+ :proxyBypassRules "<local>"}
|
|
|
+
|
|
|
+ :else
|
|
|
+ #js {:mode "system"})
|
|
|
+ sess (.. ^js @*win -webContents -session)]
|
|
|
+ (if sess
|
|
|
+ (p/do!
|
|
|
+ (.setProxy sess config)
|
|
|
+ (.forceReloadProxyConfig sess))
|
|
|
+ (p/resolved nil)))))
|
|
|
+
|
|
|
+(defn- parse-pac-rule
|
|
|
+ "Parse Proxy Auto Config(PAC) line"
|
|
|
+ [line]
|
|
|
+ (let [parts (string/split line #"[ :]")
|
|
|
+ type (first parts)]
|
|
|
+ (cond
|
|
|
+ (= type "DIRECT")
|
|
|
+ nil
|
|
|
+
|
|
|
+ (and (contains? #{"PROXY" "HTTP" "SOCKS"} type)
|
|
|
+ (>= (count parts) 3))
|
|
|
+ {:protocol (if (= type "SOCKS") "socks5" "http")
|
|
|
+ :host (nth parts 1)
|
|
|
+ :port (nth parts 2)}
|
|
|
+
|
|
|
+ :else
|
|
|
+ (do
|
|
|
+ (logger/warn "Unknown PAC rule:" line)
|
|
|
+ nil))))
|
|
|
|
|
|
-(defn restore-user-fetch-agent
|
|
|
+
|
|
|
+(defn <get-system-proxy
|
|
|
+ "Get system proxy for url, requires proxy to be set to system"
|
|
|
+ ([] (<get-system-proxy "https://www.google.com"))
|
|
|
+ ([for-url]
|
|
|
+ (when-let [sess (.. ^js @*win -webContents -session)]
|
|
|
+ (p/let [proxy (.resolveProxy sess for-url)
|
|
|
+ pac-opts (->> (string/split proxy #";")
|
|
|
+ (map parse-pac-rule)
|
|
|
+ (remove nil?))]
|
|
|
+ (when (seq pac-opts)
|
|
|
+ (first pac-opts))))))
|
|
|
+
|
|
|
+(defn <set-proxy
|
|
|
+ "Set proxy for electron, fetch, and rsapi"
|
|
|
+ ([{: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)]
|
|
|
+ (set-fetch-agent-proxy proxy)
|
|
|
+ (set-rsapi-proxy proxy))
|
|
|
+
|
|
|
+ (= type "direct")
|
|
|
+ (do
|
|
|
+ (<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-proxy-settings
|
|
|
+ "Restore proxy settings from configs.edn"
|
|
|
[]
|
|
|
- (when-let [agent (cfgs/get-item :settings/agent)]
|
|
|
- (set-fetch-agent agent)))
|
|
|
+ (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"
|