From 05fee6d1ee5480fd6e7c3c7a1fc3d31b7fe87c0a Mon Sep 17 00:00:00 2001 From: Kai Zheng Date: Sun, 21 Apr 2024 16:40:47 +0800 Subject: [PATCH 1/2] [format] format code --- src/main/clojure/clojure/tools/nrepl.cljr | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/clojure/clojure/tools/nrepl.cljr b/src/main/clojure/clojure/tools/nrepl.cljr index 1738f95..3377108 100644 --- a/src/main/clojure/clojure/tools/nrepl.cljr +++ b/src/main/clojure/clojure/tools/nrepl.cljr @@ -28,11 +28,11 @@ opts (assoc opts :xform (get opts :xform middleware/default-xform)) - host-entry (Dns/GetHostEntry ^String host) + host-entry (Dns/GetHostEntry ^String host) ip-address (first (.AddressList host-entry)) - ip-endpoint (IPEndPoint. ^IPAddress ip-address (int port)) + ip-endpoint (IPEndPoint. ^IPAddress ip-address (int port)) tcp-listener (doto (TcpListener. ip-endpoint) (.Start)) ;; start required here in order to pick up .LocalEndPoint - local-port (.Port ^IPEndPoint (.LocalEndPoint (.Server tcp-listener)))] + local-port (.Port ^IPEndPoint (.LocalEndPoint (.Server tcp-listener)))] (when-not quiet (println (format "Started nREPL server at %s:%d" (.Address ip-endpoint) local-port))) {:socket tcp-listener From 0406c30a374675685ec190adf0bee61888df8117 Mon Sep 17 00:00:00 2001 From: Kai Zheng Date: Sun, 21 Apr 2024 16:50:41 +0800 Subject: [PATCH 2/2] [fixed] resolve 127.0.0.1 correctly --- src/main/clojure/clojure/tools/nrepl.cljr | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/clojure/clojure/tools/nrepl.cljr b/src/main/clojure/clojure/tools/nrepl.cljr index 3377108..ef40241 100644 --- a/src/main/clojure/clojure/tools/nrepl.cljr +++ b/src/main/clojure/clojure/tools/nrepl.cljr @@ -4,7 +4,7 @@ [clojure.tools.nrepl.server.middleware :as middleware] [clojure.string :as string]) (:import [System.Net Dns IPEndPoint IPAddress] - [System.Net.Sockets TcpListener] )) + [System.Net.Sockets TcpListener AddressFamily] )) (set! *warn-on-reflection* true) @@ -28,8 +28,15 @@ opts (assoc opts :xform (get opts :xform middleware/default-xform)) + ;; Calling GetHostEntry with 127.0.0.1 will return all local machine IPs without + ;; the loopback IP, whereas with localhost will return the loopback IP. + host (if (= "127.0.0.1" host) "localhost" host) host-entry (Dns/GetHostEntry ^String host) - ip-address (first (.AddressList host-entry)) + ip-address (->> host-entry + .AddressList + (filter #(= (.AddressFamily ^IPAddress %) + AddressFamily/InterNetwork)) + first) ip-endpoint (IPEndPoint. ^IPAddress ip-address (int port)) tcp-listener (doto (TcpListener. ip-endpoint) (.Start)) ;; start required here in order to pick up .LocalEndPoint local-port (.Port ^IPEndPoint (.LocalEndPoint (.Server tcp-listener)))]