|
1 | 1 | (ns clj-http.conn-mgr |
2 | 2 | "Utility methods for Scheme registries and HTTP connection managers" |
3 | 3 | (:require [clojure.java.io :as io]) |
4 | | - (:import (java.security KeyStore) |
| 4 | + (:import (java.net Socket Proxy Proxy$Type InetSocketAddress) |
| 5 | + (java.security KeyStore) |
5 | 6 | (java.security.cert X509Certificate) |
6 | 7 | (javax.net.ssl SSLSession SSLSocket) |
7 | 8 | (org.apache.http.conn ClientConnectionManager) |
8 | 9 | (org.apache.http.conn.params ConnPerRouteBean) |
9 | | - (org.apache.http.conn.ssl AllowAllHostnameVerifier SSLSocketFactory |
10 | | - TrustStrategy X509HostnameVerifier) |
| 10 | + (org.apache.http.conn.ssl AllowAllHostnameVerifier SSLSocketFactory |
| 11 | + TrustStrategy X509HostnameVerifier |
| 12 | + SSLContexts) |
11 | 13 | (org.apache.http.conn.scheme PlainSocketFactory |
12 | 14 | SchemeRegistry Scheme) |
13 | 15 | (org.apache.http.impl.conn BasicClientConnectionManager |
|
42 | 44 | (^boolean verify [_ ^String _ ^SSLSession _] |
43 | 45 | true)))) |
44 | 46 |
|
| 47 | +;; New Generic Socket Factories that can support socks proxy |
| 48 | +(defn SSLGenericSocketFactory |
| 49 | + "Given a function that returns a new socket, create an SSLSocketFactory the will use that socket" |
| 50 | + (^SSLSocketFactory [socket-factory] |
| 51 | + (proxy |
| 52 | + [SSLSocketFactory] [(SSLContexts/createDefault)] |
| 53 | + (connectSocket [socket remoteAddress localAddress params] |
| 54 | + (let [^SSLSocketFactory this this] ; avoid reflection |
| 55 | + (proxy-super connectSocket (socket-factory) remoteAddress localAddress params)))))) |
| 56 | + |
| 57 | +(defn PlainGenericSocketFactory |
| 58 | + "Given a Function that returns a new socket, create a PlainSocketFactory that will use that socket" |
| 59 | + (^PlainSocketFactory [socket-factory] |
| 60 | + (proxy |
| 61 | + [PlainSocketFactory] [] |
| 62 | + (createSocket [params] |
| 63 | + (socket-factory))))) |
| 64 | + |
| 65 | +(defn socks-proxied-socket |
| 66 | + "Create a Socket proxied through socks, using the given hostname and port" |
| 67 | + [^String hostname ^Integer port] |
| 68 | + (Socket. (Proxy. Proxy$Type/SOCKS (InetSocketAddress. hostname port)))) |
| 69 | + |
| 70 | +(defn make-socks-proxied-conn-manager |
| 71 | + "Given an optional hostname and a port, create a connection manager that's proxied using a SOCKS proxy" |
| 72 | + [^String hostname ^Integer port] |
| 73 | + (let [socket-factory #(socks-proxied-socket hostname port) |
| 74 | + reg (doto |
| 75 | + (SchemeRegistry.) |
| 76 | + (.register (Scheme. "https" 443 (SSLGenericSocketFactory socket-factory))) |
| 77 | + (.register (Scheme. "http" 80 (PlainGenericSocketFactory socket-factory))))] |
| 78 | + (PoolingClientConnectionManager. reg))) |
| 79 | + |
45 | 80 | (def insecure-scheme-registry |
46 | 81 | (doto (SchemeRegistry.) |
47 | 82 | (.register (Scheme. "http" 80 (PlainSocketFactory/getSocketFactory))) |
|
0 commit comments