[LIB-13] Several options for working through a proxy server
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / proxy / client / SocksRequestClient.java
1 /*
2  * Copyright (c) 2019-2020. Developed by Hedgecode.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.hedgecode.chess.scanner.proxy.client;
18
19 import java.net.InetSocketAddress;
20 import java.util.HashMap;
21 import java.util.Map;
22
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;
30
31 import org.hedgecode.chess.scanner.ScannerConstants;
32 import org.hedgecode.chess.scanner.proxy.ProxyParams;
33 import org.hedgecode.chess.scanner.request.RequestClient;
34
35 /**
36  * SocksRequestClient
37  *
38  * @author Dmitry Samoshin aka gotty
39  */
40 public class SocksRequestClient implements RequestClient {
41
42     private final Map<String, ConnectionSocketFactory> socketFactories =
43             new HashMap<String, ConnectionSocketFactory>()
44             {
45                 {
46                     put( ScannerConstants.PROXY_HTTP, new SocksSocketFactory() );
47                     put( ScannerConstants.PROXY_HTTPS, new SSLSocksSocketFactory() );
48                 }
49             };
50
51     private final HttpClientConnectionManager connectionManager;
52     private /*final*/ HttpClientContext clientContext;
53     private final InetSocketAddress proxySocketAddress;
54
55
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()
61             );
62         }
63         connectionManager = new PoolingHttpClientConnectionManager(
64                 registryBuilder.build(),
65                 new LocalDnsResolver()
66         );
67         proxySocketAddress = new InetSocketAddress(
68                 params.getHost(), params.getPort()
69         );
70
71 /*
72         clientContext = HttpClientContext.create();
73         clientContext.setAttribute(
74                 ScannerConstants.PROXY_SOCKS_ADDRESS,
75                 new InetSocketAddress(
76                         params.getHost(), params.getPort()
77                 )
78         );
79 */
80     }
81
82     @Override
83     public CloseableHttpClient getClient() {
84         return HttpClients.custom()
85                 .setConnectionManager(connectionManager)
86                 .build();
87     }
88
89     @Override
90     public HttpClientContext getContext() {
91         clientContext = HttpClientContext.create();
92         clientContext.setAttribute(
93                 ScannerConstants.PROXY_SOCKS_ADDRESS,
94                 proxySocketAddress
95         );
96         return clientContext;
97     }
98
99 }