Autopsy  4.20.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
ProxySettings.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2023 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package com.basistech.df.cybertriage.autopsy.ctapi;
20 
21 import java.net.*;
22 import java.util.*;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import java.util.prefs.PreferenceChangeListener;
26 import java.util.prefs.Preferences;
27 import org.netbeans.api.keyring.Keyring;
28 import org.openide.util.*;
29 import org.openide.util.lookup.ServiceProvider;
30 
35 public class ProxySettings {
36 
37  public static final String PROXY_HTTP_HOST = "proxyHttpHost"; // NOI18N
38  public static final String PROXY_HTTP_PORT = "proxyHttpPort"; // NOI18N
39  public static final String PROXY_HTTPS_HOST = "proxyHttpsHost"; // NOI18N
40  public static final String PROXY_HTTPS_PORT = "proxyHttpsPort"; // NOI18N
41  public static final String PROXY_SOCKS_HOST = "proxySocksHost"; // NOI18N
42  public static final String PROXY_SOCKS_PORT = "proxySocksPort"; // NOI18N
43  public static final String NOT_PROXY_HOSTS = "proxyNonProxyHosts"; // NOI18N
44  public static final String PROXY_TYPE = "proxyType"; // NOI18N
45  public static final String USE_PROXY_AUTHENTICATION = "useProxyAuthentication"; // NOI18N
46  public static final String PROXY_AUTHENTICATION_USERNAME = "proxyAuthenticationUsername"; // NOI18N
47  public static final String PROXY_AUTHENTICATION_PASSWORD = "proxyAuthenticationPassword"; // NOI18N
48  public static final String USE_PROXY_ALL_PROTOCOLS = "useProxyAllProtocols"; // NOI18N
49  public static final String DIRECT = "DIRECT"; // NOI18N
50  public static final String PAC = "PAC"; // NOI18N
51 
52  public static final String SYSTEM_PROXY_HTTP_HOST = "systemProxyHttpHost"; // NOI18N
53  public static final String SYSTEM_PROXY_HTTP_PORT = "systemProxyHttpPort"; // NOI18N
54  public static final String SYSTEM_PROXY_HTTPS_HOST = "systemProxyHttpsHost"; // NOI18N
55  public static final String SYSTEM_PROXY_HTTPS_PORT = "systemProxyHttpsPort"; // NOI18N
56  public static final String SYSTEM_PROXY_SOCKS_HOST = "systemProxySocksHost"; // NOI18N
57  public static final String SYSTEM_PROXY_SOCKS_PORT = "systemProxySocksPort"; // NOI18N
58  public static final String SYSTEM_NON_PROXY_HOSTS = "systemProxyNonProxyHosts"; // NOI18N
59  public static final String SYSTEM_PAC = "systemPAC"; // NOI18N
60 
61  // Only for testing purpose (Test connection in General options panel)
62  public static final String TEST_SYSTEM_PROXY_HTTP_HOST = "testSystemProxyHttpHost"; // NOI18N
63  public static final String TEST_SYSTEM_PROXY_HTTP_PORT = "testSystemProxyHttpPort"; // NOI18N
64  public static final String HTTP_CONNECTION_TEST_URL = "https://netbeans.apache.org";// NOI18N
65 
66  private static String presetNonProxyHosts;
67 
69  public static final int DIRECT_CONNECTION = 0;
70 
72  public static final int AUTO_DETECT_PROXY = 1; // as default
73 
75  public static final int MANUAL_SET_PROXY = 2;
76 
78  public static final int AUTO_DETECT_PAC = 3;
79 
81  public static final int MANUAL_SET_PAC = 4;
82 
83  private static final Logger LOGGER = Logger.getLogger(ProxySettings.class.getName());
84 
85  private static Preferences getPreferences() {
86  return NbPreferences.forModule (ProxySettings.class);
87  }
88 
89 
90  public static String getHttpHost () {
91  return normalizeProxyHost (getPreferences ().get (PROXY_HTTP_HOST, ""));
92  }
93 
94  public static String getHttpPort () {
95  return getPreferences ().get (PROXY_HTTP_PORT, "");
96  }
97 
98  public static String getHttpsHost () {
99  if (useProxyAllProtocols ()) {
100  return getHttpHost ();
101  } else {
102  return getPreferences ().get (PROXY_HTTPS_HOST, "");
103  }
104  }
105 
106  public static String getHttpsPort () {
107  if (useProxyAllProtocols ()) {
108  return getHttpPort ();
109  } else {
110  return getPreferences ().get (PROXY_HTTPS_PORT, "");
111  }
112  }
113 
114  public static String getSocksHost () {
115  if (useProxyAllProtocols ()) {
116  return getHttpHost ();
117  } else {
118  return getPreferences ().get (PROXY_SOCKS_HOST, "");
119  }
120  }
121 
122  public static String getSocksPort () {
123  if (useProxyAllProtocols ()) {
124  return getHttpPort ();
125  } else {
126  return getPreferences ().get (PROXY_SOCKS_PORT, "");
127  }
128  }
129 
130  public static String getNonProxyHosts () {
131  String hosts = getPreferences ().get (NOT_PROXY_HOSTS, getDefaultUserNonProxyHosts ());
132  return compactNonProxyHosts(hosts);
133  }
134 
135  public static int getProxyType () {
136  int type = getPreferences ().getInt (PROXY_TYPE, AUTO_DETECT_PROXY);
137  if (AUTO_DETECT_PROXY == type) {
138  type = ProxySettings.getSystemPac() != null ? AUTO_DETECT_PAC : AUTO_DETECT_PROXY;
139  }
140  return type;
141  }
142 
143 
144  public static String getSystemHttpHost() {
145  return getPreferences().get(SYSTEM_PROXY_HTTP_HOST, "");
146  }
147 
148  public static String getSystemHttpPort() {
149  return getPreferences().get(SYSTEM_PROXY_HTTP_PORT, "");
150  }
151 
152  public static String getSystemHttpsHost() {
153  return getPreferences().get(SYSTEM_PROXY_HTTPS_HOST, "");
154  }
155 
156  public static String getSystemHttpsPort() {
157  return getPreferences().get(SYSTEM_PROXY_HTTPS_PORT, "");
158  }
159 
160  public static String getSystemSocksHost() {
161  return getPreferences().get(SYSTEM_PROXY_SOCKS_HOST, "");
162  }
163 
164  public static String getSystemSocksPort() {
165  return getPreferences().get(SYSTEM_PROXY_SOCKS_PORT, "");
166  }
167 
168  public static String getSystemNonProxyHosts() {
169  return getPreferences().get(SYSTEM_NON_PROXY_HOSTS, getModifiedNonProxyHosts(""));
170  }
171 
172  public static String getSystemPac() {
173  return getPreferences().get(SYSTEM_PAC, null);
174  }
175 
176 
177  public static String getTestSystemHttpHost() {
178  return getPreferences().get(TEST_SYSTEM_PROXY_HTTP_HOST, "");
179  }
180 
181  public static String getTestSystemHttpPort() {
182  return getPreferences().get(TEST_SYSTEM_PROXY_HTTP_PORT, "");
183  }
184 
185 
186  public static boolean useAuthentication () {
187  return getPreferences ().getBoolean (USE_PROXY_AUTHENTICATION, false);
188  }
189 
190  public static boolean useProxyAllProtocols () {
191  return getPreferences ().getBoolean (USE_PROXY_ALL_PROTOCOLS, false);
192  }
193 
194  public static String getAuthenticationUsername () {
195  return getPreferences ().get (PROXY_AUTHENTICATION_USERNAME, "");
196  }
197 
198  public static char[] getAuthenticationPassword () {
199  String old = getPreferences().get(PROXY_AUTHENTICATION_PASSWORD, null);
200  if (old != null) {
201  getPreferences().remove(PROXY_AUTHENTICATION_PASSWORD);
202  setAuthenticationPassword(old.toCharArray());
203  }
204  char[] pwd = Keyring.read(PROXY_AUTHENTICATION_PASSWORD);
205  return pwd != null ? pwd : new char[0];
206  }
207 
208  public static void setAuthenticationPassword(char[] password) {
209  Keyring.save(ProxySettings.PROXY_AUTHENTICATION_PASSWORD, password,
210  // XXX consider including getHttpHost and/or getHttpsHost
211  NbBundle.getMessage(ProxySettings.class, "ProxySettings.password.description")); // NOI18N
212  }
213 
214  public static void addPreferenceChangeListener (PreferenceChangeListener l) {
215  getPreferences ().addPreferenceChangeListener (l);
216  }
217 
218  public static void removePreferenceChangeListener (PreferenceChangeListener l) {
219  getPreferences ().removePreferenceChangeListener (l);
220  }
221 
222  private static String getPresetNonProxyHosts () {
223  if (presetNonProxyHosts == null) {
224  presetNonProxyHosts = System.getProperty ("http.nonProxyHosts", ""); // NOI18N
225  }
226  return presetNonProxyHosts;
227  }
228 
229  private static String getDefaultUserNonProxyHosts () {
231  }
232 
233 
234  private static String concatProxies(String... proxies) {
235  StringBuilder sb = new StringBuilder();
236  for (String n : proxies) {
237  if (n == null) {
238  continue;
239  }
240  n = n.trim();
241  if (n.isEmpty()) {
242  continue;
243  }
244  if (sb.length() > 0 && sb.charAt(sb.length() - 1) != '|') { // NOI18N
245  if (!n.startsWith("|")) { // NOI18N
246  sb.append('|'); // NOI18N
247  }
248  }
249  sb.append(n);
250  }
251  return sb.toString();
252  }
253 
254  private static String getModifiedNonProxyHosts (String systemPreset) {
255  String fromSystem = systemPreset.replace (";", "|").replace (",", "|"); //NOI18N
256  String fromUser = getPresetNonProxyHosts () == null ? "" : getPresetNonProxyHosts ().replace (";", "|").replace (",", "|"); //NOI18N
257  if (Utilities.isWindows ()) {
258  fromSystem = addReguralToNonProxyHosts (fromSystem);
259  }
260  final String staticNonProxyHosts = NbBundle.getMessage(ProxySettings.class, "StaticNonProxyHosts"); // NOI18N
261  String nonProxy = concatProxies(fromUser, fromSystem, staticNonProxyHosts); // NOI18N
262  String localhost;
263  try {
264  localhost = InetAddress.getLocalHost().getHostName();
265  if (!"localhost".equals(localhost)) { // NOI18N
266  nonProxy = nonProxy + "|" + localhost; // NOI18N
267  } else {
268  // Avoid this error when hostname == localhost:
269  // Error in http.nonProxyHosts system property: sun.misc.REException: localhost is a duplicate
270  }
271  }
272  catch (UnknownHostException e) {
273  // OK. Sometimes a hostname is assigned by DNS, but a computer
274  // is later pulled off the network. It may then produce a bogus
275  // name for itself which can't actually be resolved. Normally
276  // "localhost" is aliased to 127.0.0.1 anyway.
277  }
278  /* per Milan's agreement it's removed. See issue #89868
279  try {
280  String localhost2 = InetAddress.getLocalHost().getCanonicalHostName();
281  if (!"localhost".equals(localhost2) && !localhost2.equals(localhost)) { // NOI18N
282  nonProxy = nonProxy + "|" + localhost2; // NOI18N
283  } else {
284  // Avoid this error when hostname == localhost:
285  // Error in http.nonProxyHosts system property: sun.misc.REException: localhost is a duplicate
286  }
287  }
288  catch (UnknownHostException e) {
289  // OK. Sometimes a hostname is assigned by DNS, but a computer
290  // is later pulled off the network. It may then produce a bogus
291  // name for itself which can't actually be resolved. Normally
292  // "localhost" is aliased to 127.0.0.1 anyway.
293  }
294  */
295  return compactNonProxyHosts (nonProxy);
296  }
297 
298 
299  // avoid duplicate hosts
300  private static String compactNonProxyHosts (String hosts) {
301  StringTokenizer st = new StringTokenizer(hosts, ","); //NOI18N
302  StringBuilder nonProxyHosts = new StringBuilder();
303  while (st.hasMoreTokens()) {
304  String h = st.nextToken().trim();
305  if (h.length() == 0) {
306  continue;
307  }
308  if (nonProxyHosts.length() > 0) {
309  nonProxyHosts.append("|"); // NOI18N
310  }
311  nonProxyHosts.append(h);
312  }
313  st = new StringTokenizer (nonProxyHosts.toString(), "|"); //NOI18N
314  Set<String> set = new HashSet<String> ();
315  StringBuilder compactedProxyHosts = new StringBuilder();
316  while (st.hasMoreTokens ()) {
317  String t = st.nextToken ();
318  if (set.add (t.toLowerCase (Locale.US))) {
319  if (compactedProxyHosts.length() > 0) {
320  compactedProxyHosts.append('|'); // NOI18N
321  }
322  compactedProxyHosts.append(t);
323  }
324  }
325  return compactedProxyHosts.toString();
326  }
327 
328  private static String addReguralToNonProxyHosts (String nonProxyHost) {
329  StringTokenizer st = new StringTokenizer (nonProxyHost, "|"); // NOI18N
330  StringBuilder reguralProxyHosts = new StringBuilder();
331  while (st.hasMoreTokens ()) {
332  String t = st.nextToken ();
333  if (t.indexOf ('*') == -1) { //NOI18N
334  t = t + '*'; //NOI18N
335  }
336  if (reguralProxyHosts.length() > 0)
337  reguralProxyHosts.append('|'); // NOI18N
338  reguralProxyHosts.append(t);
339  }
340 
341  return reguralProxyHosts.toString();
342  }
343 
344  public static String normalizeProxyHost (String proxyHost) {
345  if (proxyHost.toLowerCase (Locale.US).startsWith ("http://")) { // NOI18N
346  return proxyHost.substring (7, proxyHost.length ());
347  } else {
348  return proxyHost;
349  }
350  }
351 
352  private static InetSocketAddress analyzeProxy(URI uri) {
353  Parameters.notNull("uri", uri); // NOI18N
354  List<Proxy> proxies = ProxySelector.getDefault().select(uri);
355  assert proxies != null : "ProxySelector cannot return null for " + uri; // NOI18N
356  assert !proxies.isEmpty() : "ProxySelector cannot return empty list for " + uri; // NOI18N
357  String protocol = uri.getScheme();
358  Proxy p = proxies.get(0);
359  if (Proxy.Type.DIRECT == p.type()) {
360  // return null for DIRECT proxy
361  return null;
362  }
363  if (protocol == null
364  || ((protocol.startsWith("http") || protocol.equals("ftp")) && Proxy.Type.HTTP == p.type()) // NOI18N
365  || !(protocol.startsWith("http") || protocol.equals("ftp"))) { // NOI18N
366  if (p.address() instanceof InetSocketAddress) {
367  // check is
368  //assert ! ((InetSocketAddress) p.address()).isUnresolved() : p.address() + " must be resolved address.";
369  return (InetSocketAddress) p.address();
370  } else {
371  LOGGER.log(Level.INFO, p.address() + " is not instanceof InetSocketAddress but " + p.address().getClass()); // NOI18N
372  return null;
373  }
374  } else {
375  return null;
376  }
377  }
378 
379  public static void reload() {
380  Reloader reloader = Lookup.getDefault().lookup(Reloader.class);
381  reloader.reload();
382  }
383 
384  @ServiceProvider(service = NetworkSettings.ProxyCredentialsProvider.class, position = 1000)
385  public static class NbProxyCredentialsProvider extends NetworkSettings.ProxyCredentialsProvider {
386 
387  @Override
388  public String getProxyHost(URI u) {
389  if (getPreferences() == null) {
390  return null;
391  }
392  InetSocketAddress sa = analyzeProxy(u);
393  return sa == null ? null : sa.getHostName();
394  }
395 
396  @Override
397  public String getProxyPort(URI u) {
398  if (getPreferences() == null) {
399  return null;
400  }
401  InetSocketAddress sa = analyzeProxy(u);
402  return sa == null ? null : Integer.toString(sa.getPort());
403  }
404 
405  @Override
406  protected String getProxyUserName(URI u) {
407  if (getPreferences() == null) {
408  return null;
409  }
411  }
412 
413  @Override
414  protected char[] getProxyPassword(URI u) {
415  if (getPreferences() == null) {
416  return null;
417  }
419  }
420 
421  @Override
422  protected boolean isProxyAuthentication(URI u) {
423  if (getPreferences() == null) {
424  return false;
425  }
426  return getPreferences().getBoolean(USE_PROXY_AUTHENTICATION, false);
427  }
428 
429  }
430 
439  public abstract static class Reloader {
440 
444  public abstract void reload();
445  }
446 }
static void addPreferenceChangeListener(PreferenceChangeListener l)
static String addReguralToNonProxyHosts(String nonProxyHost)
static void removePreferenceChangeListener(PreferenceChangeListener l)
static String getModifiedNonProxyHosts(String systemPreset)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2022 Basis Technology. Generated on: Tue Aug 1 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.