1 package com.jsql.util;
2
3 import com.jsql.model.InjectionModel;
4 import com.jsql.util.GitUtil.ShowOnConsole;
5 import org.apache.commons.lang3.StringUtils;
6 import org.apache.logging.log4j.LogManager;
7 import org.apache.logging.log4j.Logger;
8
9 import java.net.Socket;
10 import java.util.Optional;
11 import java.util.prefs.Preferences;
12
13
14
15
16
17 public class ProxyUtil {
18
19
20
21
22 private static final Logger LOGGER = LogManager.getRootLogger();
23
24 private final String httpProxyDefaultAddress;
25 private final String httpProxyDefaultPort;
26 private final String httpsProxyDefaultAddress;
27 private final String httpsProxyDefaultPort;
28
29
30
31
32 private String proxyAddressHttp;
33 private String proxyAddressHttps;
34
35
36
37
38 private String proxyPortHttp;
39 private String proxyPortHttps;
40
41
42
43
44 private boolean isUsingProxyHttp = false;
45 private boolean isUsingProxyHttps = false;
46
47 private static final String PROPERTIES_HTTP_PROXY_HOST = "http.proxyHost";
48 private static final String PROPERTIES_HTTP_PROXY_PORT = "http.proxyPort";
49 private static final String PROPERTIES_HTTPS_PROXY_HOST = "https.proxyHost";
50 private static final String PROPERTIES_HTTPS_PROXY_PORT = "https.proxyPort";
51
52 public ProxyUtil(InjectionModel injectionModel) {
53
54 this.httpProxyDefaultAddress = injectionModel.getMediatorUtils().getPropertiesUtil().getProperties().getProperty("http.proxy.default.ip");
55 this.httpProxyDefaultPort = injectionModel.getMediatorUtils().getPropertiesUtil().getProperties().getProperty("http.proxy.default.port");
56 this.httpsProxyDefaultAddress = injectionModel.getMediatorUtils().getPropertiesUtil().getProperties().getProperty("https.proxy.default.ip");
57 this.httpsProxyDefaultPort = injectionModel.getMediatorUtils().getPropertiesUtil().getProperties().getProperty("https.proxy.default.port");
58 }
59
60
61
62
63
64
65
66 public void setPreferences(
67 boolean isUsingProxyHttp, String proxyAddressHttp, String proxyPortHttp,
68 boolean isUsingProxyHttps, String proxyAddressHttps, String proxyPortHttps
69 ) {
70
71
72 this.setUsingProxyHttp(isUsingProxyHttp);
73 this.setProxyAddressHttp(proxyAddressHttp);
74 this.setProxyPortHttp(proxyPortHttp);
75
76 this.setUsingProxyHttps(isUsingProxyHttps);
77 this.setProxyAddressHttps(proxyAddressHttps);
78 this.setProxyPortHttps(proxyPortHttps);
79
80
81 Preferences prefs = Preferences.userRoot().node(InjectionModel.class.getName());
82 prefs.putBoolean("isUsingProxy", this.isUsingProxyHttp());
83 prefs.put("proxyAddress", this.getProxyAddressHttp());
84 prefs.put("proxyPort", this.getProxyPortHttp());
85
86 prefs.putBoolean("isUsingProxyHttps", this.isUsingProxyHttps());
87 prefs.put("proxyAddressHttps", this.getProxyAddressHttps());
88 prefs.put("proxyPortHttps", this.getProxyPortHttps());
89
90
91 if (this.isUsingProxyHttp()) {
92
93 System.setProperty(ProxyUtil.PROPERTIES_HTTP_PROXY_HOST, this.getProxyAddressHttp());
94 System.setProperty(ProxyUtil.PROPERTIES_HTTP_PROXY_PORT, this.getProxyPortHttp());
95
96 } else {
97
98 System.setProperty(ProxyUtil.PROPERTIES_HTTP_PROXY_HOST, "");
99 System.setProperty(ProxyUtil.PROPERTIES_HTTP_PROXY_PORT, "");
100 }
101
102 if (this.isUsingProxyHttps()) {
103
104 System.setProperty(ProxyUtil.PROPERTIES_HTTPS_PROXY_HOST, this.getProxyAddressHttps());
105 System.setProperty(ProxyUtil.PROPERTIES_HTTPS_PROXY_PORT, this.getProxyPortHttps());
106
107 } else {
108
109 System.setProperty(ProxyUtil.PROPERTIES_HTTPS_PROXY_HOST, "");
110 System.setProperty(ProxyUtil.PROPERTIES_HTTPS_PROXY_PORT, "");
111 }
112 }
113
114
115
116
117 public void initializeProxy() {
118
119
120 var preferences = Preferences.userRoot().node(InjectionModel.class.getName());
121
122
123 this.setUsingProxyHttp(preferences.getBoolean("isUsingProxy", false));
124 this.setUsingProxyHttps(preferences.getBoolean("isUsingProxyHttps", false));
125
126
127 this.setProxyAddressHttp(preferences.get("proxyAddress", this.httpProxyDefaultAddress));
128 this.setProxyPortHttp(preferences.get("proxyPort", this.httpProxyDefaultPort));
129
130 this.setProxyAddressHttps(preferences.get("proxyAddressHttps", this.httpsProxyDefaultAddress));
131 this.setProxyPortHttps(preferences.get("proxyPortHttps", this.httpsProxyDefaultPort));
132
133
134 if (this.isUsingProxyHttp()) {
135
136 System.setProperty(ProxyUtil.PROPERTIES_HTTP_PROXY_HOST, this.getProxyAddressHttp());
137 System.setProperty(ProxyUtil.PROPERTIES_HTTP_PROXY_PORT, this.getProxyPortHttp());
138 }
139
140 if (this.isUsingProxyHttps()) {
141
142 System.setProperty(ProxyUtil.PROPERTIES_HTTPS_PROXY_HOST, this.getProxyAddressHttps());
143 System.setProperty(ProxyUtil.PROPERTIES_HTTPS_PROXY_PORT, this.getProxyPortHttps());
144 }
145 }
146
147
148
149
150
151
152
153
154 public boolean isNotLive(ShowOnConsole showOnConsole) {
155
156 var isLive = true;
157
158 if (
159 this.isUsingProxyHttp()
160 && StringUtils.isNotEmpty(this.getProxyAddressHttp())
161 && StringUtils.isNotEmpty(this.getProxyPortHttp())
162 ) {
163 isLive = this.isSocketOn(showOnConsole, this.getProxyAddressHttp(), this.getProxyPortHttp(), "HTTP");
164 }
165
166 if (
167 this.isUsingProxyHttps()
168 && StringUtils.isNotEmpty(this.getProxyAddressHttps())
169 && StringUtils.isNotEmpty(this.getProxyPortHttps())
170 ) {
171 isLive = this.isSocketOn(showOnConsole, this.getProxyAddressHttps(), this.getProxyPortHttps(), "HTTPS");
172 }
173
174 return !isLive;
175 }
176
177 private boolean isSocketOn(ShowOnConsole showOnConsole, String address, String port, String protocol) {
178
179 var isSocketOn = true;
180
181 try {
182 var socket = new Socket(address, Integer.parseInt(port));
183 socket.close();
184
185 this.logStatus(showOnConsole, address, port, protocol);
186
187 } catch (Exception e) {
188
189 isSocketOn = false;
190 this.logStatus(showOnConsole, address, port, protocol, e);
191 }
192
193 return isSocketOn;
194 }
195
196 private void logStatus(ShowOnConsole showOnConsole, String address, String port, String protocol) {
197 if (showOnConsole == ShowOnConsole.YES) {
198 LOGGER.log(
199 LogLevelUtil.CONSOLE_SUCCESS,
200 "Connection successful to {} proxy {}:{}",
201 () -> protocol,
202 () -> address,
203 () ->port
204 );
205 }
206 }
207
208 private void logStatus(ShowOnConsole showOnConsole, String address, String port, String protocol, Exception e) {
209 if (showOnConsole == ShowOnConsole.YES) {
210
211 String message = Optional.ofNullable(e.getMessage()).orElse(StringUtils.EMPTY);
212
213 LOGGER.log(
214 LogLevelUtil.CONSOLE_ERROR,
215 () -> String.format(
216 "Connection to %s proxy %s:%s failed with error \"%s\", verify your proxy settings",
217 protocol,
218 address,
219 port,
220 message.replace(e.getClass().getName() +": ", StringUtils.EMPTY)
221 )
222 );
223 }
224 }
225
226
227
228
229 public String getProxyAddressHttp() {
230 return this.proxyAddressHttp;
231 }
232
233 public void setProxyAddressHttp(String proxyAddressHttp) {
234 this.proxyAddressHttp = proxyAddressHttp;
235 }
236
237 public String getProxyPortHttp() {
238 return this.proxyPortHttp;
239 }
240
241 public void setProxyPortHttp(String proxyPortHttp) {
242 this.proxyPortHttp = proxyPortHttp;
243 }
244
245 public boolean isUsingProxyHttp() {
246 return this.isUsingProxyHttp;
247 }
248
249 public void setUsingProxyHttp(boolean isUsingProxyHttp) {
250 this.isUsingProxyHttp = isUsingProxyHttp;
251 }
252
253 public String getProxyAddressHttps() {
254 return this.proxyAddressHttps;
255 }
256
257 public void setProxyAddressHttps(String proxyAddressHttps) {
258 this.proxyAddressHttps = proxyAddressHttps;
259 }
260
261 public String getProxyPortHttps() {
262 return this.proxyPortHttps;
263 }
264
265 public void setProxyPortHttps(String proxyPortHttps) {
266 this.proxyPortHttps = proxyPortHttps;
267 }
268
269 public boolean isUsingProxyHttps() {
270 return this.isUsingProxyHttps;
271 }
272
273 public void setUsingProxyHttps(boolean isUsingProxyHttps) {
274 this.isUsingProxyHttps = isUsingProxyHttps;
275 }
276 }