Debian Codegraph @

src/main/java/com/github/markusbernhardt/proxy/util/ProxyUtil.java

raw symbols SCIP
1 package com.github.markusbernhardt.proxy.util;
2
3 import java.net.MalformedURLException;
4 import java.net.Proxy;
5 import java.net.URL;
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.List;
9
10 import com.github.markusbernhardt.proxy.selector.fixed.FixedProxySelector;
11 import com.github.markusbernhardt.proxy.selector.pac.PacProxySelector;
12 import com.github.markusbernhardt.proxy.selector.pac.PacScriptSource;
13 import com.github.markusbernhardt.proxy.selector.pac.UrlPacScriptSource;
14
15 /*****************************************************************************
16 * Small helper class for some common utility methods.
17 *
18 * @author Markus Bernhardt, Copyright 2016
19 * @author Bernd Rosstauscher, Copyright 2009
20 ****************************************************************************/
21
22 public class ProxyUtil {
23
24 public static final int DEFAULT_PROXY_PORT = 80;
25
26 private static List<Proxy> noProxyList;
27
28 /*************************************************************************
29 * Parse host and port out of a proxy variable.
30 *
31 * @param proxyVar
32 * the proxy string. example: http://192.168.10.9:8080/
33 * @return a FixedProxySelector using this settings, null on parse error.
34 ************************************************************************/
35
36 public static FixedProxySelector parseProxySettings(String proxyVar) {
37 if (proxyVar == null || proxyVar.trim().length() == 0) {
38 return null;
39 }
40
41 try {
42 // Protocol missing then assume http and provide it
43 if (proxyVar.indexOf(":/") == -1) {
44 proxyVar = "http://" + proxyVar;
45 }
46
47 URL url = new URL(proxyVar);
48 String host = cleanIPv6(url.getHost());
49
50 int port = url.getPort();
51 if (port == -1) {
52 port = DEFAULT_PROXY_PORT;
53 }
54 return new FixedProxySelector(host.trim(), port);
55 } catch (MalformedURLException e) {
56 Logger.log(ProxyUtil.class, Logger.LogLevel.WARNING, "Cannot parse Proxy Settings {0}", proxyVar);
57 return null;
58 }
59 }
60
61 /*************************************************************************
62 * Gets an unmodifiable proxy list that will have as it's only entry an
63 * DIRECT proxy.
64 *
65 * @return a list with a DIRECT proxy in it.
66 ************************************************************************/
67
68 public static synchronized List<Proxy> noProxyList() {
69 if (noProxyList == null) {
70 ArrayList<Proxy> list = new ArrayList<Proxy>(1);
71 list.add(Proxy.NO_PROXY);
72 noProxyList = Collections.unmodifiableList(list);
73 }
74 return noProxyList;
75 }
76
77 /*************************************************************************
78 * Build a PAC proxy selector for the given URL.
79 *
80 * @param url
81 * to fetch the PAC script from.
82 * @return a PacProxySelector or null if it is not possible to build a
83 * working selector.
84 ************************************************************************/
85
86 public static PacProxySelector buildPacSelectorForUrl(String url) {
87 PacProxySelector result = null;
88 PacScriptSource pacSource = new UrlPacScriptSource(url);
89 if (pacSource.isScriptValid()) {
90 result = new PacProxySelector(pacSource);
91 }
92 return result;
93 }
94
95 /*************************************************************************
96 * This method can be used to cleanup an IPv6 address. It will remove the
97 * surrounding square brackets if found. e.g. [2001:4860:0:2001::68] will be
98 * returned as 2001:4860:0:2001::68
99 *
100 * @param hostOrIP
101 * to cleanup
102 * @return the raw host or IP without any IPv6 brackets.
103 ************************************************************************/
104
105 public static String cleanIPv6(String hostOrIP) {
106 if (hostOrIP == null) {
107 return null;
108 }
109 hostOrIP = hostOrIP.trim();
110 if (hostOrIP.startsWith("[")) {
111 hostOrIP = hostOrIP.substring(1);
112 }
113 if (hostOrIP.endsWith("]")) {
114 hostOrIP = hostOrIP.substring(0, hostOrIP.length() - 1);
115 }
116 return hostOrIP;
117 }
118
119 }