-
Notifications
You must be signed in to change notification settings - Fork 6
/
sockets.el
119 lines (103 loc) · 4.01 KB
/
sockets.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
;; -*- lexical-binding:t -*-
;;;;
;; Nore Emacs
;; https://github.com/junjiemars/.emacs.d
;;;;
;; sockets.el
;;;;
(defmacro-if-feature% socks)
(defmacro when-feature-socks% (&rest body)
"When \\=`socks\\=', do BODY."
(if-feature-socks%
`(progn% ,@body)
`(comment ,@body)))
(defmacro when-fn-url-open-stream% (&rest body)
"When% \\=`url-open-stream\\='."
`(when-feature-socks%
(when-fn% 'url-open-stream 'url-gw
,@body)))
(when-fn-url-open-stream%
(defadvice url-open-stream
(around url-open-stream-around first compile disable)
"Fix the \\=`url-gateway-method\\=' bug in \\=`url-http\\='."
(if-version% <= 25
(let ((gateway-method
(if (eq 'socks url-gateway-method)
'socks
gateway-method)))
ad-do-it)
(let ((url-gateway-method *url-gateway-method*))
ad-do-it))))
(when-feature-socks%
(when-version% > 25
(defvar *url-gateway-method* nil
"Alias of \\=`url-gateway-method\\=' used to fix bug in
\\=`url-http\\='.")))
(when-feature-socks%
(defvar *open-network-stream* nil
"Alias of \\=`open-network-stream\\=' used to fix bug in
\\=`url-http\\='."))
(when-fn-url-open-stream%
(defun ad*-activate-url-open-stream (&optional activate)
(if activate
(progn
(ad-enable-advice #'url-open-stream 'around
"url-open-stream-around")
(ad-activate #'url-open-stream t))
(ad-deactivate #'url-open-stream)
(ad-disable-advice #'url-open-stream 'around
"url-open-stream-around")
(ad-clear-cache #'url-open-stream))))
(when-feature-socks%
(defun toggle-socks! (&optional arg)
"Toggle \\=`url-gatewary-method\\=' to socks or native.\n
With prefix argument ARG, \\=`url-gatewary-method\\=' via socks
if ARG is greater than 1, otherwise via native."
(interactive "p")
(let ((native (lambda ()
(setq% url-gateway-method 'native 'url-vars)
(setq% socks-server
(list "Default server"
nil
(*self-env-spec* :get :socks :port)
(*self-env-spec* :get :socks :version))
'socks)
(when *open-network-stream*
(setf (symbol-function 'open-network-stream)
*open-network-stream*))))
(socks (lambda ()
(setq% url-gateway-method 'socks 'url-vars)
(setq% socks-server
(list "Default server"
(*self-env-spec* :get :socks :server)
(*self-env-spec* :get :socks :port)
(*self-env-spec* :get :socks :version))
'socks)
(setf (symbol-function 'open-network-stream)
#'socks-open-network-stream)))
(ver (if (null arg) 4 arg)))
;; (require 'url)
(require 'socks)
(unless *open-network-stream*
(setf (symbol-value '*open-network-stream*)
(symbol-function 'open-network-stream)))
(if (= 1 ver)
(funcall (if (eq url-gateway-method 'native) socks native))
(funcall (if (> ver 1) socks native)))
(let ((activate (eq 'socks url-gateway-method)))
(when-version% > 25
(setq *url-gateway-method* url-gateway-method))
(when-fn-url-open-stream%
(ad*-activate-url-open-stream activate))
(message "socks%s as url gateway %s"
(list (*self-env-spec* :get :socks :server)
(*self-env-spec* :get :socks :port)
(*self-env-spec* :get :socks :version))
(if activate "enabled" "disabled"))))))
(defun self-socks-init! ()
"Initialize :socks spec from \\=`*self-env-spec*\\='."
(when-feature-socks%
(when (*self-env-spec* :get :socks :allowed)
(toggle-socks!))))
(provide 'sockets)
;; end of sockets.el file