From 6e0e98f0bc89a2c9a434c9a1e837750a371f6d1e Mon Sep 17 00:00:00 2001 From: "F. Jason Park" Date: Sat, 9 Sep 2023 06:22:33 -0700 Subject: [PATCH 0/4] *** NOT A PATCH *** *** BLURB HERE *** F. Jason Park (4): Don't hard code server ports in SOCKS tests Improve SOCKS error handling and add support for 4a [POC] Simplify network-stream openers in socks.el [POC] Integrate the socks and url libraries doc/misc/url.texi | 8 +- etc/NEWS | 7 ++ lisp/net/socks.el | 141 ++++++++++++++++++++++++++++------- lisp/url/url-gw.el | 8 +- lisp/url/url-http.el | 19 ++--- lisp/url/url-methods.el | 8 +- lisp/url/url-proxy.el | 22 ++++-- lisp/url/url-vars.el | 20 ++++- test/lisp/net/socks-tests.el | 84 ++++++++++++++++----- 9 files changed, 248 insertions(+), 69 deletions(-) Interdiff: diff --git a/lisp/url/url-http.el b/lisp/url/url-http.el index 42cfb9959a7..47c785a0735 100644 --- a/lisp/url/url-http.el +++ b/lisp/url/url-http.el @@ -1392,7 +1392,10 @@ url-http (url-port url))) (_ (if (and url-http-proxy - (not (string-prefix-p "socks" (url-type url-http-proxy))) + ;; Set to "http" by `url-find-proxy-for-url' for + ;; any matching non-blacklisted, non-SOCKS scheme + ;; in `url-proxy-services', including "https". + (equal "http" (url-type url-http-proxy)) (string= "https" (url-type url-current-object))) (url-https-proxy-connect connection) (set-process-sentinel connection @@ -1476,7 +1479,7 @@ url-http-async-sentinel ((string= (substring why 0 4) "open") (setq url-http-connection-opened t) (if (and url-http-proxy - (not (string-prefix-p "socks" (url-type url-http-proxy))) + (equal "http" (url-type url-http-proxy)) (string= "https" (url-type url-current-object))) (url-https-proxy-connect proc) (condition-case error diff --git a/lisp/url/url-methods.el b/lisp/url/url-methods.el index 9643e992044..9592307aea8 100644 --- a/lisp/url/url-methods.el +++ b/lisp/url/url-methods.el @@ -92,7 +92,6 @@ url-scheme-register-proxy ;; Then check if its a fully specified URL ((string-match url-nonrelative-link env-proxy) (setq urlobj (url-generic-parse-url env-proxy)) - (setf (url-type urlobj) "http") (setf (url-target urlobj) nil)) ;; Finally, fall back on the assumption that its just a hostname (t @@ -103,8 +102,11 @@ url-scheme-register-proxy (if (and (not cur-proxy) urlobj) (progn (setq url-proxy-services - (cons (cons scheme (format "%s:%d" (url-host urlobj) - (url-port urlobj))) + (cons (cons scheme (if (member (url-type urlobj) + url-proxy-full-address-types) + (url-recreate-url urlobj) + (format "%s:%d" (url-host urlobj) + (url-port urlobj)))) url-proxy-services)) (message "Using a proxy for %s..." scheme))))) diff --git a/lisp/url/url-proxy.el b/lisp/url/url-proxy.el index c9c5a7aacac..b1583523cc6 100644 --- a/lisp/url/url-proxy.el +++ b/lisp/url/url-proxy.el @@ -25,9 +25,6 @@ (require 'url-parse) -(defconst url-proxy--socks-scheme-regexp - (rx bot "socks" (? (or "4" "4a" "5" "5h")) "://")) - (defun url-default-find-proxy-for-url (urlobj host) (cond ((or (and (assoc "no_proxy" url-proxy-services) @@ -38,13 +35,12 @@ url-default-find-proxy-for-url (equal "www" (url-type urlobj))) "DIRECT") ((and-let* ((found (assoc (url-type urlobj) url-proxy-services))) - (concat (if (string-match url-proxy--socks-scheme-regexp (cdr found)) - "SOCKS " + (concat (if-let ((non-scheme (string-search "://" (cdr found))) + (scheme (substring (cdr found) 0 non-scheme)) + ((member scheme url-proxy-full-address-types))) + (concat scheme " ") "PROXY ") (cdr found)))) - ;; - ;; Should check for socks - ;; (t "DIRECT"))) @@ -62,9 +58,9 @@ url-find-proxy-for-url ((string-match "^DIRECT" proxy) nil) ((string-match "^PROXY +" proxy) (concat "http://" (substring proxy (match-end 0)) "/")) - ((string-match "^SOCKS +" proxy) + ((string-match (rx bot "SOCKS" (** 0 2 alnum) " ") proxy) (if-let ((m (substring proxy (match-end 0))) - ((string-match url-proxy--socks-scheme-regexp m))) + ((string-search "://" m))) m (concat "socks://" m))) (t diff --git a/lisp/url/url-vars.el b/lisp/url/url-vars.el index 87dfdb9916c..f10158d66a1 100644 --- a/lisp/url/url-vars.el +++ b/lisp/url/url-vars.el @@ -189,18 +189,27 @@ url-mail-command :type 'function :group 'url) +(defvar url-proxy-full-address-types + '("socks" "socks5" "socks5h" "socks4" "socks4a") + "Schemes for URL types preserved in `url-proxy-services' entries. +When dynamically adding a new `url-proxy-services' entry derived +from the environment, Emacs only retains the host and port +portions unless the URL's scheme appears in this variable's +value.") + (defcustom url-proxy-services nil "An alist of schemes and proxy servers that gateway them. Looks like ((\"http\" . \"hostname:portnumber\") ...). This is set up -from the ACCESS_proxy environment variables. Depending on the -gateway type, Emacs may expect certain server values to specfiy a -\"scheme\", for example, \"proxyscheme://hostname:portnumber\", -in which \"proxyscheme\" is something like \"socks5\". As of -Emacs 30.1, this only applies to SOCKS servers." +from the ACCESS_proxy environment variables. Certain gateway +types need server values to take the form of full URLs in order +to convey addtional information about for the proxy connection +itself, for example, SCHEME://USER@HOSTNAME:PORTNUMBER, in which +SCHEME is something like \"socks5\". As of Emacs 30.1, this only +applies to SCHEMEs appearing in the variable +`url-proxy-full-address-types'." :type '(repeat (cons :format "%v" (string :tag "Protocol") (string :tag "Proxy"))) - :version "30.1" :group 'url) (defcustom url-standalone-mode nil @@ -317,7 +326,7 @@ url-using-proxy "Either nil or the fully qualified proxy URL in use, e.g. https://www.example.com/. Beware that some functions, such as `url-proxy' and `url-http-end-of-document-sentinel', set this to -a `url' struct.") +a `url' struct object.") (defcustom url-news-server nil "The default news server from which to get newsgroups/articles. -- 2.41.0