| 1 |
package com.github.markusbernhardt.proxy.search.env; |
| 2 |
|
| 3 |
import java.net.ProxySelector; |
| 4 |
import java.util.Properties; |
| 5 |
|
| 6 |
import com.github.markusbernhardt.proxy.ProxySearchStrategy; |
| 7 |
import com.github.markusbernhardt.proxy.selector.misc.ProtocolDispatchSelector; |
| 8 |
import com.github.markusbernhardt.proxy.selector.whitelist.ProxyBypassListSelector; |
| 9 |
import com.github.markusbernhardt.proxy.util.Logger; |
| 10 |
import com.github.markusbernhardt.proxy.util.ProxyUtil; |
| 11 |
import com.github.markusbernhardt.proxy.util.Logger.LogLevel; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class EnvProxySearchStrategy implements ProxySearchStrategy { |
| 30 |
|
| 31 |
private String httpEnv; |
| 32 |
private String httpsEnv; |
| 33 |
private String ftpEnv; |
| 34 |
private String noProxyEnv; |
| 35 |
|
| 36 |
private String httpProxy; |
| 37 |
private String httpsProxy; |
| 38 |
private String ftpProxy; |
| 39 |
private String noProxy; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public EnvProxySearchStrategy() { |
| 46 |
this("http_proxy", "https_proxy", "ftp_proxy", "no_proxy"); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public EnvProxySearchStrategy(String httpEnv, String httpsEnv, String ftpEnv, String noProxyEnv) { |
| 63 |
super(); |
| 64 |
this.httpEnv = httpEnv; |
| 65 |
this.httpsEnv = httpsEnv; |
| 66 |
this.ftpEnv = ftpEnv; |
| 67 |
this.noProxyEnv = noProxyEnv; |
| 68 |
|
| 69 |
loadProxySettings(); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
private void loadProxySettings() { |
| 77 |
this.httpProxy = System.getenv(this.httpEnv); |
| 78 |
this.httpsProxy = System.getenv(this.httpsEnv); |
| 79 |
this.ftpProxy = System.getenv(this.ftpEnv); |
| 80 |
this.noProxy = System.getenv(this.noProxyEnv); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public Properties readSettings() { |
| 90 |
Properties result = new Properties(); |
| 91 |
result.setProperty(this.httpEnv, this.httpProxy); |
| 92 |
result.setProperty(this.httpsEnv, this.httpsProxy); |
| 93 |
result.setProperty(this.ftpEnv, this.ftpProxy); |
| 94 |
result.setProperty(this.noProxyEnv, this.noProxy); |
| 95 |
return result; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
@Override |
| 105 |
public ProxySelector getProxySelector() { |
| 106 |
|
| 107 |
Logger.log(getClass(), LogLevel.TRACE, "Inspecting environment variables."); |
| 108 |
|
| 109 |
|
| 110 |
ProxySelector httpPS = ProxyUtil.parseProxySettings(this.httpProxy); |
| 111 |
if (httpPS == null) { |
| 112 |
return null; |
| 113 |
} |
| 114 |
|
| 115 |
Logger.log(getClass(), LogLevel.TRACE, "Http Proxy is {0}", this.httpProxy); |
| 116 |
ProtocolDispatchSelector ps = new ProtocolDispatchSelector(); |
| 117 |
ps.setSelector("http", httpPS); |
| 118 |
|
| 119 |
ProxySelector httpsPS = ProxyUtil.parseProxySettings(this.httpsProxy); |
| 120 |
Logger.log(getClass(), LogLevel.TRACE, "Https Proxy is {0}", httpsPS == null ? this.httpsProxy : httpsPS); |
| 121 |
ps.setSelector("https", httpsPS != null ? httpsPS : httpPS); |
| 122 |
|
| 123 |
ProxySelector ftpPS = ProxyUtil.parseProxySettings(this.ftpProxy); |
| 124 |
if (ftpPS != null) { |
| 125 |
Logger.log(getClass(), LogLevel.TRACE, "Ftp Proxy is {0}", this.ftpProxy); |
| 126 |
ps.setSelector("ftp", ftpPS); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
ProxySelector result = ps; |
| 131 |
if (this.noProxy != null && this.noProxy.trim().length() > 0) { |
| 132 |
Logger.log(getClass(), LogLevel.TRACE, "Using proxy bypass list: {0}", this.noProxy); |
| 133 |
result = new ProxyBypassListSelector(this.noProxy, ps); |
| 134 |
} |
| 135 |
|
| 136 |
return result; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
@Override |
| 146 |
public String getName() { |
| 147 |
return "env"; |
| 148 |
} |
| 149 |
|
| 150 |
} |