| 139 | } |
| 140 | |
| 141 | private static void setupUnirest() { |
| 142 | String proxyHost = CommonConfigVars.get(CommonConfigVars.PROXY_HOST); |
| 143 | int proxyPort = CommonConfigVars.getInt(CommonConfigVars.PROXY_PORT); |
| 144 | if (proxyHost != null && proxyPort > 0) { |
| 145 | Unirest.config().proxy(new Proxy(proxyHost, proxyPort)); |
| 146 | } |
| 147 | |
| 148 | int connectTimeout = CommonConfigVars.getInt(CommonConfigVars.CONNECT_TIMEOUT); |
| 149 | if (connectTimeout > 0) { |
| 150 | Unirest.config().connectTimeout(connectTimeout); |
| 151 | } |
| 152 | |
| 153 | int requestTimeout = CommonConfigVars.getInt(CommonConfigVars.REQUEST_TIMEOUT); |
| 154 | if (requestTimeout > 0) { |
| 155 | Unirest.config().requestTimeout(requestTimeout); |
| 156 | } |
| 157 | |
| 158 | int connectTTL = CommonConfigVars.getInt(CommonConfigVars.CONNECT_TTL); |
| 159 | if (connectTTL > 0) { |
| 160 | Unirest.config().connectionTTL(connectTTL, TimeUnit.MILLISECONDS); |
| 161 | } |
| 162 | |
| 163 | boolean verifyCerts = CommonConfigVars.getBool(CommonConfigVars.VERIFY_CERTS); |
| 164 | Unirest.config().disableHostNameVerification(!verifyCerts); |
| 165 | Unirest.config().verifySsl(verifyCerts); |
| 166 | |
| 167 | File certsPath = CommonConfigVars.getFile(CommonConfigVars.SSL_PATH); |
| 168 | try { |
| 169 | CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
| 170 | Collection<? extends Certificate> certs = null; |
| 171 | try (FileInputStream stream = new FileInputStream(certsPath)) { |
| 172 | certs = cf.generateCertificates(stream); |
| 173 | } |
| 174 | if (certs != null && !certs.isEmpty()) { |
| 175 | KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); |
| 176 | trustStore.load(null, null); |
| 177 | int i = 0; |
| 178 | for (Certificate cert : certs) { |
| 179 | trustStore.setCertificateEntry("cert-" + i, cert); |
| 180 | i++; |
| 181 | } |
| 182 | |
| 183 | TrustManagerFactory tf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 184 | tf.init(trustStore); |
| 185 | |
| 186 | SSLContext context = SSLContext.getInstance("TLS"); |
| 187 | context.init(null, tf.getTrustManagers(), new SecureRandom()); |
| 188 | Unirest.config().sslContext(context); |
| 189 | |
| 190 | LOGGER.info("Loaded {} SSL certs", i); |
| 191 | } else { |
| 192 | LOGGER.warn("No SSL certs found at {}", certsPath.getAbsolutePath()); |
| 193 | } |
| 194 | } catch (CertificateException | IOException | KeyStoreException | NoSuchAlgorithmException | |
| 195 | KeyManagementException ex) { |
| 196 | LOGGER.warn("Could not load SSL certs at {}", certsPath.getAbsolutePath(), ex); |
| 197 | } |
| 198 | |