remove useless code line
[proxy-mode.git] / proxy-mode.el
blob675e99d512bd2b9387337150d447dc153ecac7eb
1 ;;; proxy-mode.el --- A minor mode to toggle proxy.
3 ;; Authors: stardiviner <numbchild@gmail.com>
4 ;; Package-Requires: ((emacs "25"))
5 ;; Package-Version: 0.1
6 ;; Keywords: comm proxy
7 ;; homepage: https://repo.or.cz/proxy-mode.git
9 ;; You should have received a copy of the GNU General Public License
10 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
12 ;;; Commentary:
14 ;;; toggle proxy-mode to use proxy.
15 ;;; [M-x proxy-mode RET]
17 ;;; Code:
19 (require 'url-gw)
20 (require 'socks)
21 (require 'cl-lib)
23 (defgroup proxy-mode nil
24 "A minor mode to toggle proxy."
25 :prefix "proxy-mode-"
26 :group 'proxy)
28 (defcustom proxy-mode-rules-alist nil
29 "A list of rules for proxy."
30 :type 'alist
31 :group 'proxy-mode)
33 (defvar proxy-mode-proxy-types
34 '(("Set Emacs url.el library HTTP request proxy" . emacs-url-proxy)
35 ("Set Emacs socks.el library proxy" . emacs-socks-proxy)
36 ("Set environment variable HTTP_PROXY" . env-http-proxy))
37 "A list of `proxy-mode' supported proxy types.")
39 (defvar proxy-mode-proxy-type nil
40 "Currently enabled proxy type.")
42 ;; Privoxy
43 (defcustom proxy-mode-env-http-service
44 (unless (getenv "HTTP_PROXY") "http://localhost:7980")
45 "Customize HTTP_PROXY environment variable value."
46 :type 'string
47 :safe #'stringp
48 :group 'proxy-mode)
50 (defcustom proxy-mode-url-proxy-services
51 (unless (default-value 'url-proxy-services)
52 '(("http" . "127.0.0.1:7890")
53 ("https" . "127.0.0.1:7890")
54 ("ftp" . "127.0.0.1:7890")
55 ;; don't use `localhost', avoid robe server (For Ruby) can't response.
56 ("no_proxy" . "127.0.0.1")
57 ("no_proxy" . "^.*\\(baidu\\|sina)\\.com")))
58 "Customize usual Emacs network http request through `url-proxy-services' proxy rules."
59 :type 'alist
60 :safe #'nested-alist-p
61 :group 'proxy-mode)
63 (defcustom proxy-mode-socks-proxy-server
64 (unless (default-value 'socks-server) '("Default server" "127.0.0.1" 1080 5))
65 "Customize the `socks-server' value for Emacs library `socks'."
66 :type 'list
67 :safe #'listp
68 :group 'proxy-mode)
70 (defun proxy-mode-lighter-func ()
71 (format " proxy[%s]" proxy-mode-proxy-type))
73 ;;; ----------------------- environment variable HTTP_PROXY proxy ----------------------------
75 (defun proxy-mode-env-proxy-enable ()
76 "Enable HTTP proxy."
77 ;; `setenv' works by modifying ‘process-environment’.
78 (setenv "HTTP_PROXY" proxy-mode-env-http-service)
79 (setenv "HTTPS_PROXY" proxy-mode-env-http-service)
80 (setq proxy-mode-proxy-type 'env-http-proxy)
82 ;; TODO: how to `setenv' buffer locally?
83 ;; this will make `proxy-mode-env-proxy-enable' invalid.
84 ;; (make-local-variable 'process-environment)
85 ;; (add-to-list 'process-environment (format "HTTP_PROXY=%s" ))
86 ;; (add-to-list 'process-environment (format "HTTPS_PROXY=%s" ))
88 (message
89 (format
90 "[proxy-mode] environment variable \"HTTP_PROXY\", \"HTTPS_PROXY\" proxy %s enabled."
91 (getenv "HTTP_PROXY"))))
93 (defun proxy-mode--env-proxy-disable ()
94 "Disable HTTP proxy."
95 (setenv "HTTP_PROXY" nil)
96 (setenv "HTTPS_PROXY" nil)
97 (setq proxy-mode-proxy-type nil)
98 (message (format "[proxy-mode] environment variable \"HTTP_PROXY\", \"HTTPS_PROXY\" proxy disabled.")))
100 ;;; -------------------------- url.el HTTP request proxy --------------------------------------
102 (defun proxy-mode-url-proxy-enable ()
103 "Enable url.el proxy by set `url-proxy-services'."
104 (setq url-proxy-services proxy-mode-url-proxy-services)
105 (setq proxy-mode-proxy-type 'emacs-url-proxy)
106 (message (format "[proxy-mode] url.el proxy %s enabled." (car proxy-mode-url-proxy-services))))
108 (defun proxy-mode-url-proxy-disable ()
109 "Disable url.el proxy by unset `url-proxy-services'."
110 (setq url-proxy-services nil)
111 (setq proxy-mode-proxy-type nil)
112 (message (format "[proxy-mode] url.el proxy disabled.")))
114 ;;; ---------------------------- socks.el proxy -----------------------------------------------
116 (defun proxy-mode-socks-proxy-enable ()
117 "Enable socks.el proxy.
118 NOTE: it only works for http:// connections. Not work for https:// connections."
119 (require 'url-gw)
120 (require 'socks)
121 (setq url-gateway-method 'socks)
122 (setq socks-noproxy '("localhost" "192.168.*" "10.*"))
123 (setq socks-server proxy-mode-socks-proxy-server)
124 (setq proxy-mode-proxy-type 'emacs-socks-proxy)
125 (message "[proxy-mode] socks.el proxy %s enabled." proxy-mode-socks-proxy-server))
127 (defun proxy-mode-socks-proxy-disable ()
128 "Disable socks.el proxy."
129 (setq url-gateway-method 'native)
130 (setq proxy-mode-proxy-type nil)
131 (message "[proxy-mode] socks.el proxy disabled."))
133 ;;; ------------------------------------------------------------------------------------------
135 (defun proxy-mode-select-proxy ()
136 "Select proxy type."
137 (if proxy-mode-proxy-type
138 (message "proxy-mode is already enabled.")
139 (setq proxy-mode-proxy-type
140 (cdr (assoc
141 (completing-read "Select proxy service to enable: "
142 (mapcar 'car proxy-mode-proxy-types))
143 proxy-mode-proxy-types)))))
145 (defun proxy-mode-enable ()
146 "Enable proxy-mode."
147 (cl-case proxy-mode-proxy-type
148 (emacs-url-proxy (proxy-mode-url-proxy-enable))
149 (emacs-socks-proxy (proxy-mode-socks-proxy-enable))
150 (env-http-proxy (proxy-mode-env-proxy-enable))))
152 (defun proxy-mode-disable ()
153 "Disable proxy-mode."
154 (cl-case proxy-mode-proxy-type
155 (emacs-url-proxy (proxy-mode-url-proxy-disable))
156 (emacs-socks-proxy (proxy-mode-socks-proxy-disable))
157 (env-http-proxy (proxy-mode--env-proxy-disable))))
159 (defvar proxy-mode-map nil)
161 ;;;###autoload
162 (define-minor-mode proxy-mode
163 "A minor mode to set variable proxy for Emacs."
164 :require 'proxy-mode
165 :init-value nil
166 :lighter (:eval (proxy-mode-lighter-func))
167 :group 'proxy-mode
168 :keymap proxy-mode-map
169 (if proxy-mode
170 (progn
171 (proxy-mode-select-proxy)
172 (proxy-mode-enable))
173 (proxy-mode-disable)))
177 (provide 'proxy-mode)
179 ;;; proxy-mode.el ends here