From 86a614dd616ed4ba458844633852fa4bc71d3b87 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 26 May 2018 22:34:31 +0200 Subject: [PATCH] Added proxy support --- CMakeLists.txt | 2 +- examples/example15_proxy.cpp | 40 ++++++++++++++++++++++++++++++++++++ src/http.cpp | 17 +++++++++++++++ src/mastodon-cpp.cpp | 5 +++++ src/mastodon-cpp.hpp | 19 +++++++++++++++++ 5 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 examples/example15_proxy.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b8d9299..c768530 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.14.0 + VERSION 0.15.0 LANGUAGES CXX ) diff --git a/examples/example15_proxy.cpp b/examples/example15_proxy.cpp new file mode 100644 index 0000000..1db27f5 --- /dev/null +++ b/examples/example15_proxy.cpp @@ -0,0 +1,40 @@ +/* This file is part of mastodon-cpp. + * This example shows how to use a proxy. + */ + +#include +#include +#include +#include +#ifdef MASTODON_CPP + #include "mastodon-cpp.hpp" +#else + #include +#endif + +using std::cout; +using Mastodon::API; + +int main(int argc, char *argv[]) +{ + if (argc < 3) + { + std::cerr << "usage: " << argv[0] << " \n"; + return 1; + } + + Mastodon::API masto(argv[1], argv[2]); + // SOCKS5 Proxy. Proxy resolves URL hostname. + masto.set_proxy("socks5h://[fd12::1a]:1080/"); + // HTTPS proxy with username and password + masto.set_proxy("https://localhost:3128", "user5:supersecurepassword"); + std::string answer; + std::uint16_t ret; + + ret = masto.get(API::v1::accounts_verify_credentials, answer); + + cout << "Return code: " << ret << '\n'; + std::cout << answer << '\n'; + + return 0; +} diff --git a/src/http.cpp b/src/http.cpp index 1e62404..5f3c489 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -35,6 +35,8 @@ API::http::http(const API &api, const string &instance, , _instance(instance) , _access_token(access_token) , _cancel_stream(false) +, _proxy("") +, _proxy_userpw("") { curlpp::initialize(); } @@ -70,6 +72,15 @@ const uint_fast16_t API::http::request(const method &meth, ttdebug << "User-Agent: " << parent.get_useragent() << "\n"; request.setOpt(parent.get_useragent()); + if (!_proxy.empty()) + { + request.setOpt(_proxy); + if (!_proxy_userpw.empty()) + { + request.setOpt(_proxy_userpw); + } + } + if (!_access_token.empty()) { headers.push_back("Authorization: Bearer " + _access_token); @@ -217,3 +228,9 @@ std::mutex &API::http::get_mutex() { return _mutex; } + +const void API::http::set_proxy(const string &proxy, const string &userpw) +{ + _proxy = proxy; + _proxy_userpw = userpw; +} diff --git a/src/mastodon-cpp.cpp b/src/mastodon-cpp.cpp index c124714..5843e66 100644 --- a/src/mastodon-cpp.cpp +++ b/src/mastodon-cpp.cpp @@ -571,3 +571,8 @@ const string API::unescape_html(const string &html) return output; } + +const void API::set_proxy(const string &proxy, const string &userpw) +{ + _http.set_proxy(proxy, userpw); +} diff --git a/src/mastodon-cpp.hpp b/src/mastodon-cpp.hpp index fd7ce60..3bae46b 100644 --- a/src/mastodon-cpp.hpp +++ b/src/mastodon-cpp.hpp @@ -146,6 +146,16 @@ public: */ std::mutex &get_mutex(); + /*! + * @brief Sets the proxy. + * + * @param proxy See `man 3 CURLOPT_PROXY` + * @param userpw See `man 3 CURLOPT_PROXYUSERPWD` (optional) + * + * @since 0.15.0 + */ + const void set_proxy(const string &proxy, const string &userpw = ""); + private: const API &parent; const string _instance; @@ -153,6 +163,8 @@ public: string _headers; bool _cancel_stream; std::mutex _mutex; + string _proxy; + string _proxy_userpw; const size_t callback_write(char* data, size_t size, size_t nmemb, string *oss); @@ -395,6 +407,13 @@ public: */ static const string unescape_html(const string &html); + /*! + * @brief Calls Mastodon::API::http::set_proxy() + * + * @since 0.15.0 + */ + const void set_proxy(const string &proxy, const string &userpw = ""); + /*! * @brief Make a GET request which doesn't require parameters. *