rubenlagus / TelegramBots

Java library to create bots using Telegram Bots API
https://telegram.me/JavaBotsApi
MIT License
4.68k stars 1.18k forks source link

HTTP Proxy not auth #629

Closed WinSide closed 4 years ago

WinSide commented 5 years ago

When using HTTP proxy I got exception

Exception in thread "main" java.lang.InternalError: Should not reach here
    at java.net.HttpConnectSocketImpl.doTunneling(HttpConnectSocketImpl.java:181)
    at java.net.HttpConnectSocketImpl.doTunnel(HttpConnectSocketImpl.java:168)
    at java.net.HttpConnectSocketImpl.access$200(HttpConnectSocketImpl.java:44)
    at java.net.HttpConnectSocketImpl$2.run(HttpConnectSocketImpl.java:151)
    at java.net.HttpConnectSocketImpl$2.run(HttpConnectSocketImpl.java:149)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.HttpConnectSocketImpl.privilegedDoTunnel(HttpConnectSocketImpl.java:148)
    at java.net.HttpConnectSocketImpl.connect(HttpConnectSocketImpl.java:111)
    at java.net.Socket.connect(Socket.java:589)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:339)
    at org.telegram.telegrambots.facilities.proxysocketfactorys.HttpSSLConnectionSocketFactory.connectSocket(HttpSSLConnectionSocketFactory.java:38)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:359)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
    at org.telegram.telegrambots.bots.DefaultAbsSender.sendHttpPostRequest(DefaultAbsSender.java:765)
    at org.telegram.telegrambots.bots.DefaultAbsSender.sendMethodRequest(DefaultAbsSender.java:761)
    at org.telegram.telegrambots.bots.DefaultAbsSender.sendApiMethod(DefaultAbsSender.java:716)
    at org.telegram.telegrambots.meta.bots.AbsSender.execute(AbsSender.java:47)
    at org.telegram.telegrambots.util.WebhookUtils.clearWebhook(WebhookUtils.java:74)
    at org.telegram.telegrambots.bots.TelegramLongPollingBot.clearWebhook(TelegramLongPollingBot.java:25)
    at org.telegram.telegrambots.meta.TelegramBotsApi.registerBot(TelegramBotsApi.java:120)
    at ru.winside.tgbottest.TGBotTestMain.main(TGBotTestMain.java:42)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at java.net.HttpConnectSocketImpl.doTunneling(HttpConnectSocketImpl.java:179)
    ... 27 more
Caused by: java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.0 407 Proxy Authentication Required"
    at sun.net.www.protocol.http.HttpURLConnection.doTunneling(HttpURLConnection.java:2142)
    ... 32 more

Code from Wiki - https://github.com/rubenlagus/TelegramBots/wiki/Using-Http-Proxy#with-authentication

Barrya42 commented 5 years ago

Have the same problem.

denred0 commented 5 years ago

same problem

Flexeus commented 5 years ago

Same. Help please

Raschudesny commented 5 years ago

same problem

Raschudesny commented 5 years ago

Okay, this problem is caused by this bug https://bugs.openjdk.java.net/browse/JDK-8173620 I've solved it by using following workaround

try {
         (DefaultBotSession) telegramBotsApi.registerBot(telegramBot);
        } catch (Exception e) {
            // logging
        } catch (InternalError e) {
            // https://bugs.openjdk.java.net/browse/JDK-8173620
            if (e.getCause() instanceof InvocationTargetException && e.getCause().getCause() instanceof IOException) {
                IOException cause = (IOException) e.getCause().getCause();
                // logging 
            }
            // if it's not jdk bugged error then propagate it
            throw e;
        }
rubenlagus commented 5 years ago

If the bug is in openJDK, I guess we can close this issue, right? @WinSide can you check if the workaround works for you as well?

Barrya42 commented 4 years ago

Does Someone try this solution? Somebody prove this to close issure.

galimru commented 4 years ago

This bug was resolved in OpenJDK since 13 version. However, I think we should include a workaround to the library because the most popular java version still 8-11.

The workaround mentioned by Raschudesny works only with registration webhook but doesn't help when long-polling bot getting updates.

@rubenlagus please can you review and accept my PR #719