mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-11-15 03:31:15 +03:00
[FEATURE] Add newHttpClientBuilder()
This commit is contained in:
parent
e28c9773fc
commit
d686d9a388
1 changed files with 20 additions and 10 deletions
|
@ -6,6 +6,7 @@
|
|||
import pro.gravit.utils.helper.LogHelper;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLParameters;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
|
@ -55,6 +56,24 @@ public static ThreadFactory getDaemonThreadFactory(String name) {
|
|||
};
|
||||
}
|
||||
|
||||
public static HttpClient.Builder newHttpClientBuilder() {
|
||||
try {
|
||||
if(isCertificatePinning) {
|
||||
return HttpClient.newBuilder()
|
||||
.sslContext(makeSSLContext())
|
||||
.version(isNoHttp2 ? HttpClient.Version.HTTP_1_1 : HttpClient.Version.HTTP_2)
|
||||
.followRedirects(HttpClient.Redirect.NORMAL);
|
||||
} else {
|
||||
return HttpClient.newBuilder()
|
||||
.version(isNoHttp2 ? HttpClient.Version.HTTP_1_1 : HttpClient.Version.HTTP_2)
|
||||
.followRedirects(HttpClient.Redirect.NORMAL);
|
||||
}
|
||||
} catch (NoSuchAlgorithmException | CertificateException | KeyStoreException | IOException |
|
||||
KeyManagementException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static SSLSocketFactory makeSSLSocketFactory() throws NoSuchAlgorithmException, CertificateException, KeyStoreException, IOException, KeyManagementException {
|
||||
if (sslSocketFactory != null) return sslSocketFactory;
|
||||
SSLContext sslContext = makeSSLContext();
|
||||
|
@ -110,17 +129,8 @@ public static Downloader newDownloader(ExecutorService executor) {
|
|||
if (executor == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
HttpClient.Builder builder = HttpClient.newBuilder()
|
||||
.version(isNoHttp2 ? HttpClient.Version.HTTP_1_1 : HttpClient.Version.HTTP_2)
|
||||
.followRedirects(HttpClient.Redirect.NORMAL)
|
||||
HttpClient.Builder builder = newHttpClientBuilder()
|
||||
.executor(executor);
|
||||
if (isCertificatePinning) {
|
||||
try {
|
||||
builder.sslContext(makeSSLContext());
|
||||
} catch (Exception e) {
|
||||
throw new SecurityException(e);
|
||||
}
|
||||
}
|
||||
HttpClient client = builder.build();
|
||||
return new Downloader(client, executor);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue