implemented authorization via http proxy
This commit is contained in:
parent
9adb3921e5
commit
378c8aaddc
@ -13,6 +13,7 @@ import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.telegram.telegrambots.api.methods.BotApiMethod;
|
||||
import org.telegram.telegrambots.api.methods.groupadministration.SetChatPhoto;
|
||||
@ -61,11 +62,23 @@ public abstract class DefaultAbsSender extends AbsSender {
|
||||
super();
|
||||
this.exe = Executors.newFixedThreadPool(options.getMaxThreads());
|
||||
this.options = options;
|
||||
httpclient = HttpClientBuilder.create()
|
||||
.setSSLHostnameVerifier(new NoopHostnameVerifier())
|
||||
.setConnectionTimeToLive(70, TimeUnit.SECONDS)
|
||||
.setMaxConnTotal(100)
|
||||
.build();
|
||||
|
||||
if (options.getCredentialsProvider() != null) {
|
||||
httpclient = HttpClientBuilder.create()
|
||||
.setProxy(options.getHttpProxy())
|
||||
.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())
|
||||
.setDefaultCredentialsProvider(options.getCredentialsProvider())
|
||||
.setSSLHostnameVerifier(new NoopHostnameVerifier())
|
||||
.setConnectionTimeToLive(70, TimeUnit.SECONDS)
|
||||
.setMaxConnTotal(100)
|
||||
.build();
|
||||
} else {
|
||||
httpclient = HttpClientBuilder.create()
|
||||
.setSSLHostnameVerifier(new NoopHostnameVerifier())
|
||||
.setConnectionTimeToLive(70, TimeUnit.SECONDS)
|
||||
.setMaxConnTotal(100)
|
||||
.build();
|
||||
}
|
||||
|
||||
requestConfig = options.getRequestConfig();
|
||||
|
||||
@ -87,6 +100,29 @@ public abstract class DefaultAbsSender extends AbsSender {
|
||||
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 {
|
||||
|
@ -1,5 +1,7 @@
|
||||
package org.telegram.telegrambots.bots;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.CredentialsProvider;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.telegram.telegrambots.ApiConstants;
|
||||
import org.telegram.telegrambots.generics.BotOptions;
|
||||
@ -21,6 +23,9 @@ public class DefaultBotOptions implements BotOptions {
|
||||
private String baseUrl;
|
||||
private List<String> allowedUpdates;
|
||||
|
||||
private CredentialsProvider credentialsProvider;
|
||||
private HttpHost httpProxy;
|
||||
|
||||
public DefaultBotOptions() {
|
||||
maxThreads = 1;
|
||||
baseUrl = ApiConstants.BASE_URL;
|
||||
@ -82,4 +87,20 @@ public class DefaultBotOptions implements BotOptions {
|
||||
public void setExponentialBackOff(ExponentialBackOff exponentialBackOff) {
|
||||
this.exponentialBackOff = exponentialBackOff;
|
||||
}
|
||||
|
||||
public CredentialsProvider getCredentialsProvider() {
|
||||
return credentialsProvider;
|
||||
}
|
||||
|
||||
public void setCredentialsProvider(CredentialsProvider credentialsProvider) {
|
||||
this.credentialsProvider = credentialsProvider;
|
||||
}
|
||||
|
||||
public HttpHost getHttpProxy() {
|
||||
return httpProxy;
|
||||
}
|
||||
|
||||
public void setHttpProxy(HttpHost httpProxy) {
|
||||
this.httpProxy = httpProxy;
|
||||
}
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ public abstract class TelegramLongPollingBot extends DefaultAbsSender implements
|
||||
|
||||
@Override
|
||||
public void clearWebhook() throws TelegramApiRequestException {
|
||||
try (CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).build()) {
|
||||
try (CloseableHttpClient httpclient = this.createHttpClient()) {
|
||||
String url = getOptions().getBaseUrl() + getBotToken() + "/" + SetWebhook.PATH;
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
httpGet.setConfig(getOptions().getRequestConfig());
|
||||
|
@ -44,7 +44,7 @@ public abstract class TelegramWebhookBot extends DefaultAbsSender implements Web
|
||||
|
||||
@Override
|
||||
public void setWebhook(String url, String publicCertificatePath) throws TelegramApiRequestException {
|
||||
try (CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).build()) {
|
||||
try (CloseableHttpClient httpclient = this.createHttpClient()) {
|
||||
String requestUrl = getBaseUrl() + SetWebhook.PATH;
|
||||
|
||||
HttpPost httppost = new HttpPost(requestUrl);
|
||||
|
@ -12,6 +12,7 @@ import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.json.JSONException;
|
||||
import org.telegram.telegrambots.ApiConstants;
|
||||
@ -145,13 +146,32 @@ public class DefaultBotSession implements BotSession {
|
||||
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 = HttpClientBuilder.create()
|
||||
.setSSLHostnameVerifier(new NoopHostnameVerifier())
|
||||
.setConnectionTimeToLive(70, TimeUnit.SECONDS)
|
||||
.setMaxConnTotal(100)
|
||||
.build();
|
||||
httpclient = createHttpClient();
|
||||
requestConfig = options.getRequestConfig();
|
||||
exponentialBackOff = options.getExponentialBackOff();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user