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