Skip to content

Commit

Permalink
reworked proxy using
Browse files Browse the repository at this point in the history
  • Loading branch information
bvn13 committed Apr 17, 2018
1 parent b9c32c5 commit 28f80e1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.telegram.telegrambots.exceptions.TelegramApiException;
import org.telegram.telegrambots.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.exceptions.TelegramApiValidationException;
import org.telegram.telegrambots.facilities.TelegramHttpClientBuilder;
import org.telegram.telegrambots.updateshandlers.DownloadFileCallback;
import org.telegram.telegrambots.updateshandlers.SentCallback;

Expand Down Expand Up @@ -63,7 +64,7 @@ protected DefaultAbsSender(DefaultBotOptions options) {
this.exe = Executors.newFixedThreadPool(options.getMaxThreads());
this.options = options;

httpclient = createHttpClient();
httpclient = TelegramHttpClientBuilder.build(options);

requestConfig = options.getRequestConfig();

Expand All @@ -85,29 +86,6 @@ public final DefaultBotOptions getOptions() {
return options;
}

protected CloseableHttpClient createHttpClient() {
CloseableHttpClient localClient = null;

if (options.getCredentialsProvider() != null) {
localClient = HttpClientBuilder.create()
.setProxy(options.getHttpProxy())
.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())
.setDefaultCredentialsProvider(options.getCredentialsProvider())
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.setConnectionTimeToLive(70, TimeUnit.SECONDS)
.setMaxConnTotal(100)
.build();
} else {
localClient = HttpClientBuilder.create()
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.setConnectionTimeToLive(70, TimeUnit.SECONDS)
.setMaxConnTotal(100)
.build();
}

return localClient;
}

// Send Requests

public final java.io.File downloadFile(String filePath) throws TelegramApiException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.telegram.telegrambots.ApiContext;
import org.telegram.telegrambots.api.methods.updates.SetWebhook;
import org.telegram.telegrambots.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.facilities.TelegramHttpClientBuilder;
import org.telegram.telegrambots.generics.LongPollingBot;

import java.io.IOException;
Expand All @@ -36,7 +37,7 @@ public TelegramLongPollingBot(DefaultBotOptions options) {

@Override
public void clearWebhook() throws TelegramApiRequestException {
try (CloseableHttpClient httpclient = this.createHttpClient()) {
try (CloseableHttpClient httpclient = TelegramHttpClientBuilder.build(getOptions())) {
String url = getOptions().getBaseUrl() + getBotToken() + "/" + SetWebhook.PATH;
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(getOptions().getRequestConfig());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.telegram.telegrambots.ApiContext;
import org.telegram.telegrambots.api.methods.updates.SetWebhook;
import org.telegram.telegrambots.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.facilities.TelegramHttpClientBuilder;
import org.telegram.telegrambots.generics.WebhookBot;

import java.io.File;
Expand Down Expand Up @@ -44,7 +45,7 @@ public TelegramWebhookBot(DefaultBotOptions options) {

@Override
public void setWebhook(String url, String publicCertificatePath) throws TelegramApiRequestException {
try (CloseableHttpClient httpclient = this.createHttpClient()) {
try (CloseableHttpClient httpclient = TelegramHttpClientBuilder.build(getOptions())) {
String requestUrl = getBaseUrl() + SetWebhook.PATH;

HttpPost httppost = new HttpPost(requestUrl);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.telegram.telegrambots.facilities;

import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
import org.telegram.telegrambots.bots.DefaultBotOptions;

import java.util.concurrent.TimeUnit;

/**
* Created by bvn13 on 17.04.2018.
*/
public class TelegramHttpClientBuilder {

public static CloseableHttpClient build(DefaultBotOptions options) {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create()
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.setConnectionTimeToLive(70, TimeUnit.SECONDS)
.setMaxConnTotal(100);

if (options.getHttpProxy() != null) {

httpClientBuilder.setProxy(options.getHttpProxy());

if (options.getCredentialsProvider() != null) {
httpClientBuilder
.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())
.setDefaultCredentialsProvider(options.getCredentialsProvider());
}

}

return httpClientBuilder.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.facilities.TelegramHttpClientBuilder;
import org.telegram.telegrambots.generics.*;
import org.telegram.telegrambots.logging.BotLogger;

Expand Down Expand Up @@ -146,32 +147,9 @@ public ReaderThread(UpdatesSupplier updatesSupplier, Object lock) {
this.lock = lock;
}

protected CloseableHttpClient createHttpClient() {
CloseableHttpClient localClient = null;

if (options.getCredentialsProvider() != null) {
localClient = HttpClientBuilder.create()
.setProxy(options.getHttpProxy())
.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())
.setDefaultCredentialsProvider(options.getCredentialsProvider())
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.setConnectionTimeToLive(70, TimeUnit.SECONDS)
.setMaxConnTotal(100)
.build();
} else {
localClient = HttpClientBuilder.create()
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.setConnectionTimeToLive(70, TimeUnit.SECONDS)
.setMaxConnTotal(100)
.build();
}

return localClient;
}

@Override
public synchronized void start() {
httpclient = createHttpClient();
httpclient = TelegramHttpClientBuilder.build(options);
requestConfig = options.getRequestConfig();
exponentialBackOff = options.getExponentialBackOff();

Expand Down

0 comments on commit 28f80e1

Please sign in to comment.