Skip to content

Commit e79ac64

Browse files
authored
Merge pull request #352 from Tomat7/master
Add method setProxy() for CurlHttpClient
2 parents 68c7931 + 5b0c0b5 commit e79ac64

5 files changed

Lines changed: 126 additions & 1 deletion

File tree

include/tgbot/net/CurlHttpClient.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,19 @@ class TGBOT_API CurlHttpClient : public HttpClient {
4747
*/
4848
std::mutex curlHandlesMutex;
4949

50+
/**
51+
* @brief Proxy URL (NULL = no proxy).
52+
*/
53+
void setProxy(const char* url = NULL, long timeout = 20L) {
54+
_proxyUrl = url;
55+
_connectTimeout = timeout;
56+
}
57+
5058
private:
5159
const HttpParser _httpParser;
60+
const char* _proxyUrl = NULL;
61+
long _connectTimeout = 20L;
62+
5263
};
5364

5465
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
cmake_minimum_required(VERSION 3.10.2)
2+
project(echobot-curl-proxy-carousel)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
7+
set(Boost_USE_MULTITHREADED ON)
8+
9+
find_package(Threads REQUIRED)
10+
find_package(OpenSSL REQUIRED)
11+
find_package(Boost COMPONENTS system REQUIRED)
12+
find_package(CURL)
13+
include_directories(/usr/local/include ${OPENSSL_INCLUDE_DIR} ${Boost_INCLUDE_DIR})
14+
if (CURL_FOUND)
15+
include_directories(${CURL_INCLUDE_DIRS})
16+
add_definitions(-DHAVE_CURL)
17+
endif()
18+
19+
add_executable(echobot-curl-proxy-carousel src/main.cpp)
20+
21+
target_link_libraries(echobot-curl-proxy-carousel /usr/local/lib/libTgBot.a ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES} ${CURL_LIBRARIES})
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM reo7sp/tgbot-cpp
2+
MAINTAINER Oleg Morozenkov <m@oleg.rocks>
3+
4+
WORKDIR /usr/src/echobot-curl-proxy-carousel
5+
COPY . .
6+
RUN cmake .
7+
RUN make -j4
8+
CMD ./echobot-curl-proxy-carousel
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Example of using array of proxies wth tgbot-cpp library.
2+
// Based on original code (c) Oleg Morozenkov [reo7sp] https://github.com/reo7sp
3+
// https://github.com/reo7sp/tgbot-cpp/blob/master/samples/echobot-curl-client/src/main.cpp
4+
5+
#include <csignal>
6+
#include <cstdio>
7+
#include <cstdlib>
8+
#include <exception>
9+
#include <string>
10+
#include <vector>
11+
12+
#ifndef HAVE_CURL
13+
#define HAVE_CURL
14+
#endif
15+
16+
#include <tgbot/net/CurlHttpClient.h>
17+
#include <tgbot/tgbot.h>
18+
19+
using namespace std;
20+
using namespace TgBot;
21+
22+
#define CONNECT_TIMEOUT 10L
23+
24+
int main() {
25+
// Filling array of proxies.
26+
vector<const char*> proxies;
27+
// NULL = no proxy, direct connection to API.
28+
proxies.push_back(NULL); // [0]
29+
// All proxy-URLs below are fake. Use yuor own.
30+
proxies.push_back("socks5://user:password@10.20.30.40:1080"); // [1]
31+
proxies.push_back("http://user:password@192.168.50.70:3128"); // [2]
32+
proxies.push_back("http://user:password@192.168.80.90:3128"); // [3]
33+
// Choose "staring" index of proxy. In this example [0] - NULL=no proxy.
34+
size_t proxy_now = 0;
35+
36+
string token(getenv("TOKEN"));
37+
printf("Token: %s\n", token.c_str());
38+
39+
CurlHttpClient curlHttpClient;
40+
Bot bot(token, curlHttpClient);
41+
42+
bot.getEvents().onCommand("start", [&bot](Message::Ptr message) {
43+
bot.getApi().sendMessage(message->chat->id, "Hi!");
44+
});
45+
bot.getEvents().onAnyMessage([&bot](Message::Ptr message) {
46+
printf("User wrote %s\n", message->text.c_str());
47+
if (StringTools::startsWith(message->text, "/start")) return;
48+
bot.getApi().sendMessage(message->chat->id,
49+
"Your message is: " + message->text);
50+
});
51+
52+
signal(SIGINT, [](int s) {
53+
printf("SIGINT got: %i\n", s);
54+
exit(0);
55+
});
56+
57+
while (true) {
58+
try {
59+
printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str());
60+
bot.getApi().deleteWebhook();
61+
62+
TgLongPoll longPoll(bot);
63+
while (true) {
64+
printf("Long poll started\n");
65+
longPoll.start();
66+
}
67+
} catch (exception &e) {
68+
printf("Proxy[%li]: %s error\n", proxy_now, proxies[proxy_now]);
69+
printf("%s\n", e.what());
70+
// Assumption:
71+
// the reason of exception was - disconnect,
72+
// connection timeout or other network problem.
73+
// Trying to switch (cycle) to next proxy in array.
74+
proxy_now++;
75+
if (proxy_now >= proxies.size()) proxy_now = 0;
76+
curlHttpClient.setProxy(proxies[proxy_now], CONNECT_TIMEOUT);
77+
printf("Switch proxy[%li]: %s\n", proxy_now, proxies[proxy_now]);
78+
}
79+
}
80+
return 0;
81+
}
82+
83+
// eof

src/net/CurlHttpClient.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ std::string CurlHttpClient::makeRequest(const Url& url, const std::vector<HttpRe
4444
CURL* curl = getCurlHandle(this);
4545

4646
curl_easy_reset(curl);
47-
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 20);
47+
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, _connectTimeout);
4848
curl_easy_setopt(curl, CURLOPT_TIMEOUT, _timeout);
49+
curl_easy_setopt(curl, CURLOPT_PROXY, _proxyUrl);
50+
4951
std::string u = url.protocol + "://" + url.host + url.path;
5052
if (args.empty()) {
5153
u += "?" + url.query;

0 commit comments

Comments
 (0)