summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/util/ProxyConfigurator.java
blob: 8011eaec7750e3a0424a5e599b6986fb36c86a61 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package org.openslx.dozmod.util;

import java.io.IOException;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.Socket;
import java.text.MessageFormat;
import java.util.concurrent.atomic.AtomicReference;

import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.impl.conn.SystemDefaultRoutePlanner;
import org.apache.http.protocol.HttpContext;
import org.apache.http.ssl.SSLContexts;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openslx.bwlp.thrift.iface.MasterServer;
import org.openslx.dozmod.App;
import org.openslx.dozmod.authentication.ShibbolethEcp;
import org.openslx.thrifthelper.ThriftManager;
import org.openslx.util.Util;

import com.btr.proxy.search.ProxySearch;
import com.btr.proxy.search.wpad.WpadProxySearchStrategy;
import com.btr.proxy.util.Logger.LogBackEnd;
import com.btr.proxy.util.Logger.LogLevel;

/**
 * Configures the proxy
 * 
 * @author Jonathan Bauer
 */

public class ProxyConfigurator {

	/**
	 * Logger for this class
	 */
	private final static Logger LOGGER = LogManager.getLogger(ProxyConfigurator.class);
	
	private static AtomicReference<CloseableHttpClient> apacheClient = new AtomicReference<>();
	
	/**
	 * Initialization method.
	 */
	public static void init() {
		MasterServer.Client masterClient = ThriftManager.getNewMasterClient(null, App.getMasterServerAddress(),
				App.THRIFT_PORT, 4000);
		if (masterClient != null) {
			try {
				masterClient.ping();
				try {
					masterClient.getInputProtocol().getTransport().close();
					masterClient.getOutputProtocol().getTransport().close();
				} catch (Throwable e) {
				}
			} catch (Exception e) {
				masterClient = null;
			}
		}
		// To be discussed: Let's bail out if the master server is reachable
		if (masterClient != null && testHttpsMaster()) {
			LOGGER.info("Not setting up proxy because master server seems reachable.");
			return;
		}
		
		// Build special HTTP Client Builder for apache and try again
		apacheClient.set(createSlxBuilder().build());
		if (masterClient != null && testHttpsMaster()) {
			LOGGER.info("Not setting up proxy since master server is reachable with custom client builder");
			return;
		}
		
		// first setup the logger of proxy-vole
		com.btr.proxy.util.Logger.setBackend(new LogBackEnd() {

			public void log(Class<?> clazz, LogLevel loglevel, String msg, Object... params) {
				Level priority;
				switch (loglevel) {
				case ERROR:
					priority = Level.ERROR;
					break;
				case WARNING:
					priority = Level.WARN;
					break;
				case INFO:
					priority = Level.INFO;
					break;
				default:
					priority = Level.DEBUG;
				}
				LogManager.getLogger(clazz).log(priority, MessageFormat.format(msg, params));
			}

			public boolean isLogginEnabled(LogLevel logLevel) {
				return true;
			}
		});

		LOGGER.info("Master server not directly reachable; trying to determine proxy");
		// try to find local proxy settings
		ProxySearch proxySearch = ProxySearch.getDefaultProxySearch();
		ProxySelector myProxySelector = proxySearch.getProxySelector();

		if (myProxySelector == null) {
			// didn't work, try WPAD detection
			LOGGER.error("No suitable proxy settings found, trying WPAD...");
			WpadProxySearchStrategy ss = new WpadProxySearchStrategy();
			myProxySelector = ss.getProxySelector();
		}
		// final check to see if WPAD actually worked
		if (myProxySelector != null) {
			ProxySelector.setDefault(myProxySelector);
			LOGGER.debug("Proxy initialised.");
			Util.sleep(10);
		} else {
			LOGGER.error("Could not find a suitable proxy!");
		}
	}
	
	/**
	 * Get the HttpClient to be used with apache
	 * httpclient.
	 *
	 * @return client
	 */
	public static CloseableHttpClient getClient() {
		CloseableHttpClient inst = apacheClient.get();
		if (inst != null)
			return inst;
		inst = createShortTimeoutBuilder().build();
		apacheClient.compareAndSet(null, inst);
		return inst;
	}

	private static SSLConnectionSocketFactory createSslFactory() {
		// TODO: Geht nich
		for (String proto : new String[] { "TLSv1.2", "TLSv1.1", "TLS" }) {
		try {
			return new SSLConnectionSocketFactory(SSLContexts.custom().setProtocol(proto).build());
		} catch (Exception e) {
			LOGGER.warn(proto + " not available", e);
		}
		}
		return SSLConnectionSocketFactory.getSystemSocketFactory();
	}

	private static HttpClientBuilder createShortTimeoutBuilder() {
		HttpClientBuilder builder = HttpClientBuilder.create().setSSLSocketFactory(createSslFactory());
		builder.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(8000).build());
		PoolingHttpClientConnectionManager pm = new PoolingHttpClientConnectionManager();
		pm.setDefaultMaxPerRoute(4);
		builder.setConnectionManager(pm);
		return builder;
	}

	private static HttpClientBuilder createSlxBuilder() {
		HttpClientBuilder builder = HttpClientBuilder.create();
		builder.setRoutePlanner(new SlxRoutePlanner(null));
		builder.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(8000).build());
		Registry<ConnectionSocketFactory> csf = RegistryBuilder.<ConnectionSocketFactory> create()
				.register("http", new SlxSocketFactory())
				.register("https", createSslFactory())
				.build();
		PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(csf);
		cm.setDefaultMaxPerRoute(4);
		builder.setConnectionManager(cm);
		return builder;
	}

	private static boolean testHttpsMaster() {
		RequestConfig requestConfig = RequestConfig.custom()
				.setConnectionRequestTimeout(3000)
				.setConnectTimeout(3000)
				.setSocketTimeout(3000)
				.build();
		HttpGet httpGet = new HttpGet(ShibbolethEcp.BWLP_SP.toString());
		httpGet.setConfig(requestConfig);
		httpGet.setHeader("Accept", "text/html, application/vnd.paos+xml");
		httpGet.setHeader("PAOS",
				"ver=\"urn:liberty:paos:2003-08\";\"urn:oasis:names:tc:SAML:2.0:profiles:SSO:ecp\"");
		try {
			HttpResponse response = getClient().execute(httpGet);
			LOGGER.debug("Master server replies with " + response.getStatusLine().getStatusCode());
			return response.getStatusLine().getStatusCode() == 200;
		} catch (Exception e) {
			LOGGER.debug("Cannot reach master server via HTTPS", e);
			return false;
		}
	}

	private static class SlxSocketFactory extends PlainConnectionSocketFactory {
		@Override
		public Socket createSocket(HttpContext context) throws IOException {
			Object obj = context.getAttribute("openslx.l7proxy");
			if (obj instanceof HttpRoute) {
				HttpRoute route = (HttpRoute) obj;
				if (route != null && route.getProxyHost() != null) {
					return new Socket(Proxy.NO_PROXY);
				}
			}
			return new Socket();
		}
	}

	private static class SlxRoutePlanner extends SystemDefaultRoutePlanner {
		public SlxRoutePlanner(ProxySelector proxySelector) {
			super(proxySelector);
		}

		@Override
		public HttpRoute determineRoute(HttpHost host, HttpRequest request, HttpContext context)
				throws HttpException {
			HttpRoute route = super.determineRoute(host, request, context);
			context.setAttribute("openslx.l7proxy", route);
			return route;
		}
	}
	
}