2 * Copyright (c) 2019-2020. Developed by Hedgecode.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.hedgecode.chess.scanner.proxy.client;
19 import java.net.InetSocketAddress;
20 import java.util.HashMap;
23 import org.apache.http.client.protocol.HttpClientContext;
24 import org.apache.http.config.RegistryBuilder;
25 import org.apache.http.conn.HttpClientConnectionManager;
26 import org.apache.http.conn.socket.ConnectionSocketFactory;
27 import org.apache.http.impl.client.CloseableHttpClient;
28 import org.apache.http.impl.client.HttpClients;
29 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
31 import org.hedgecode.chess.scanner.ScannerConstants;
32 import org.hedgecode.chess.scanner.proxy.ProxyParams;
33 import org.hedgecode.chess.scanner.request.RequestClient;
38 * @author Dmitry Samoshin aka gotty
40 public class SocksRequestClient implements RequestClient {
42 private final Map<String, ConnectionSocketFactory> socketFactories =
43 new HashMap<String, ConnectionSocketFactory>()
46 put( ScannerConstants.PROXY_HTTP, new SocksSocketFactory() );
47 put( ScannerConstants.PROXY_HTTPS, new SSLSocksSocketFactory() );
51 private final HttpClientConnectionManager connectionManager;
52 private /*final*/ HttpClientContext clientContext;
53 private final InetSocketAddress proxySocketAddress;
56 public SocksRequestClient(ProxyParams params) {
57 RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
58 for (Map.Entry<String, ConnectionSocketFactory> socketFactory : socketFactories.entrySet()) {
59 registryBuilder.register(
60 socketFactory.getKey(), socketFactory.getValue()
63 connectionManager = new PoolingHttpClientConnectionManager(
64 registryBuilder.build(),
65 new LocalDnsResolver()
67 proxySocketAddress = new InetSocketAddress(
68 params.getHost(), params.getPort()
72 clientContext = HttpClientContext.create();
73 clientContext.setAttribute(
74 ScannerConstants.PROXY_SOCKS_ADDRESS,
75 new InetSocketAddress(
76 params.getHost(), params.getPort()
83 public CloseableHttpClient getClient() {
84 return HttpClients.custom()
85 .setConnectionManager(connectionManager)
90 public HttpClientContext getContext() {
91 clientContext = HttpClientContext.create();
92 clientContext.setAttribute(
93 ScannerConstants.PROXY_SOCKS_ADDRESS,