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