View Javadoc
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   * Utility class managing proxy settings.
15   * The proxy configuration is saved as preferences and applied to the JVM.
16   */
17  public class ProxyUtil {
18      
19      private static final Logger LOGGER = LogManager.getRootLogger();
20      
21      /**
22       * Proxy IP address or name.
23       */
24      private String proxyAddressHttp;
25      private String proxyAddressHttps;
26  
27      /**
28       * Proxy port number.
29       */
30      private String proxyPortHttp;
31      private String proxyPortHttps;
32      
33      /**
34       * True if connection is proxified.
35       */
36      private boolean isUsingProxyHttp = false;
37      private boolean isUsingProxyHttps = false;
38      
39      private static final String PROPERTIES_HTTP_PROXY_HOST = "http.proxyHost";
40      private static final String PROPERTIES_HTTP_PROXY_PORT = "http.proxyPort";
41      private static final String PROPERTIES_HTTPS_PROXY_HOST = "https.proxyHost";
42      private static final String PROPERTIES_HTTPS_PROXY_PORT = "https.proxyPort";
43      
44      /**
45       * Save proxy configuration into the JVM preferences.
46       * @param isUsingProxyHttp whether the connection is using a proxy
47       * @param proxyAddressHttp IP address or name of the proxy
48       * @param proxyPortHttp port number of proxy
49       */
50      public void setPreferences(
51          boolean isUsingProxyHttp, String proxyAddressHttp, String proxyPortHttp,
52          boolean isUsingProxyHttps, String proxyAddressHttps, String proxyPortHttps
53      ) {
54          // Set the application proxy settings
55          this.setUsingProxyHttp(isUsingProxyHttp);
56          this.setProxyAddressHttp(proxyAddressHttp);
57          this.setProxyPortHttp(proxyPortHttp);
58          
59          this.setUsingProxyHttps(isUsingProxyHttps);
60          this.setProxyAddressHttps(proxyAddressHttps);
61          this.setProxyPortHttps(proxyPortHttps);
62  
63          // Save the settings in the JVM
64          Preferences prefs = Preferences.userRoot().node(InjectionModel.class.getName());
65          prefs.putBoolean("isUsingProxy", this.isUsingProxyHttp());
66          prefs.put("proxyAddress", this.getProxyAddressHttp());
67          prefs.put("proxyPort", this.getProxyPortHttp());
68          
69          prefs.putBoolean("isUsingProxyHttps", this.isUsingProxyHttps());
70          prefs.put("proxyAddressHttps", this.getProxyAddressHttps());
71          prefs.put("proxyPortHttps", this.getProxyPortHttps());
72  
73          // Change the JVM configuration
74          if (this.isUsingProxyHttp()) {
75              System.setProperty(ProxyUtil.PROPERTIES_HTTP_PROXY_HOST, this.getProxyAddressHttp());
76              System.setProperty(ProxyUtil.PROPERTIES_HTTP_PROXY_PORT, this.getProxyPortHttp());
77          } else {
78              System.setProperty(ProxyUtil.PROPERTIES_HTTP_PROXY_HOST, StringUtils.EMPTY);
79              System.setProperty(ProxyUtil.PROPERTIES_HTTP_PROXY_PORT, StringUtils.EMPTY);
80          }
81          if (this.isUsingProxyHttps()) {
82              System.setProperty(ProxyUtil.PROPERTIES_HTTPS_PROXY_HOST, this.getProxyAddressHttps());
83              System.setProperty(ProxyUtil.PROPERTIES_HTTPS_PROXY_PORT, this.getProxyPortHttps());
84          } else {
85              System.setProperty(ProxyUtil.PROPERTIES_HTTPS_PROXY_HOST, StringUtils.EMPTY);
86              System.setProperty(ProxyUtil.PROPERTIES_HTTPS_PROXY_PORT, StringUtils.EMPTY);
87          }
88      }
89      
90      /**
91       * Initialize proxy information from JVM already saved preferences.
92       */
93      public void initProxy() {
94          // Use Preferences API to persist proxy configuration
95          var preferences = Preferences.userRoot().node(InjectionModel.class.getName());
96  
97          // Default proxy disabled
98          this.setUsingProxyHttp(preferences.getBoolean("isUsingProxy", false));
99          this.setUsingProxyHttps(preferences.getBoolean("isUsingProxyHttps", false));
100 
101         // Default proxy config
102         this.setProxyAddressHttp(preferences.get("proxyAddress", "127.0.0.1"));
103         this.setProxyPortHttp(preferences.get("proxyPort", "8118"));
104         
105         this.setProxyAddressHttps(preferences.get("proxyAddressHttps", "127.0.0.1"));
106         this.setProxyPortHttps(preferences.get("proxyPortHttps", "8118"));
107         
108         // Change the JVM configuration
109         if (this.isUsingProxyHttp()) {
110             System.setProperty(ProxyUtil.PROPERTIES_HTTP_PROXY_HOST, this.getProxyAddressHttp());
111             System.setProperty(ProxyUtil.PROPERTIES_HTTP_PROXY_PORT, this.getProxyPortHttp());
112         }
113         if (this.isUsingProxyHttps()) {
114             System.setProperty(ProxyUtil.PROPERTIES_HTTPS_PROXY_HOST, this.getProxyAddressHttps());
115             System.setProperty(ProxyUtil.PROPERTIES_HTTPS_PROXY_PORT, this.getProxyPortHttps());
116         }
117     }
118     
119     /**
120      * Check if enabled proxies are up when application starts,
121      * injection begins,- checking IP, sending reports.
122      * Display logs except when sending unhandled exception.
123      * @param showOnConsole whether the message should be presented to the user
124      * @return true if enabled proxies are up
125      */
126     public boolean isNotLive(ShowOnConsole showOnConsole) {
127         var isLive = true;
128         if (
129             this.isUsingProxyHttp()
130             && StringUtils.isNotEmpty(this.getProxyAddressHttp())
131             && StringUtils.isNotEmpty(this.getProxyPortHttp())
132         ) {
133             isLive = this.isSocketOn(showOnConsole, this.getProxyAddressHttp(), this.getProxyPortHttp(), "HTTP");
134         }
135         if (
136             this.isUsingProxyHttps()
137             && StringUtils.isNotEmpty(this.getProxyAddressHttps())
138             && StringUtils.isNotEmpty(this.getProxyPortHttps())
139         ) {
140             isLive = this.isSocketOn(showOnConsole, this.getProxyAddressHttps(), this.getProxyPortHttps(), "HTTPS");
141         }
142         return !isLive;
143     }
144     
145     private boolean isSocketOn(ShowOnConsole showOnConsole, String address, String port, String protocol) {
146         var isSocketOn = true;
147         try {
148             var socket = new Socket(address, Integer.parseInt(port));
149             socket.close();
150             this.logStatus(showOnConsole, address, port, protocol);
151         } catch (Exception e) {
152             isSocketOn = false;
153             this.logStatus(showOnConsole, address, port, protocol, e);
154         }
155         return isSocketOn;
156     }
157     
158     private void logStatus(ShowOnConsole showOnConsole, String address, String port, String protocol) {
159         if (showOnConsole == ShowOnConsole.YES) {
160             LOGGER.log(
161                 LogLevelUtil.CONSOLE_SUCCESS,
162                 "Connection successful to {} proxy {}:{}",
163                 () -> protocol,
164                 () -> address,
165                 () ->port
166             );
167         }
168     }
169     
170     private void logStatus(ShowOnConsole showOnConsole, String address, String port, String protocol, Exception e) {
171         if (showOnConsole == ShowOnConsole.YES) {
172             String message = Optional.ofNullable(e.getMessage()).orElse(StringUtils.EMPTY);
173             LOGGER.log(
174                 LogLevelUtil.CONSOLE_ERROR,
175                 () -> String.format(
176                     "Connection to %s proxy %s:%s failed, verify your proxy settings: %s",
177                     protocol,
178                     address,
179                     port,
180                     message.replace(e.getClass().getName() +": ", StringUtils.EMPTY)
181                 )
182             );
183         }
184     }
185     
186     
187     // Getters and setters
188     
189     public String getProxyAddressHttp() {
190         return this.proxyAddressHttp;
191     }
192 
193     public void setProxyAddressHttp(String proxyAddressHttp) {
194         this.proxyAddressHttp = proxyAddressHttp;
195     }
196 
197     public String getProxyPortHttp() {
198         return this.proxyPortHttp;
199     }
200 
201     public void setProxyPortHttp(String proxyPortHttp) {
202         this.proxyPortHttp = proxyPortHttp;
203     }
204 
205     public boolean isUsingProxyHttp() {
206         return this.isUsingProxyHttp;
207     }
208 
209     public void setUsingProxyHttp(boolean isUsingProxyHttp) {
210         this.isUsingProxyHttp = isUsingProxyHttp;
211     }
212 
213     public String getProxyAddressHttps() {
214         return this.proxyAddressHttps;
215     }
216 
217     public void setProxyAddressHttps(String proxyAddressHttps) {
218         this.proxyAddressHttps = proxyAddressHttps;
219     }
220 
221     public String getProxyPortHttps() {
222         return this.proxyPortHttps;
223     }
224 
225     public void setProxyPortHttps(String proxyPortHttps) {
226         this.proxyPortHttps = proxyPortHttps;
227     }
228 
229     public boolean isUsingProxyHttps() {
230         return this.isUsingProxyHttps;
231     }
232 
233     public void setUsingProxyHttps(boolean isUsingProxyHttps) {
234         this.isUsingProxyHttps = isUsingProxyHttps;
235     }
236 }