From 575fb5b4e228f06c6e9adae8ecfde1ecb6854f63 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 26 May 2018 23:52:22 +0200 Subject: [PATCH] Added proxy support #1 --- CMakeLists.txt | 2 +- README.md | 11 +++++++++-- src/expandurl-mastodon.hpp | 4 ++++ src/masto.cpp | 23 +++++++++++++++++++++++ 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f16bf1e..9f57e4d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (expandurl-mastodon - VERSION 0.6.5 + VERSION 0.7.0 LANGUAGES CXX ) diff --git a/README.md b/README.md index 9fe7ab0..3a8a774 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ or to [@tastytea@soc.ialis.me](https://soc.ialis.me/@tastytea). * C++ compiler (tested: gcc 6.4) * [cmake](https://cmake.org/) (tested: 3.9.6) * [curlpp](http://www.curlpp.org/) (tested: 0.8.1) - * [mastodon-cpp](https://github.com/tastytea/mastodon-cpp) (at least: 0.13.1) + * [mastodon-cpp](https://github.com/tastytea/mastodon-cpp) (at least: 0.15.0) * [jsoncpp](https://github.com/open-source-parsers/jsoncpp) (tested: 1.8.4) ## Get sourcecode @@ -59,9 +59,16 @@ and an access token is generated. The config file can be found in { "account": "expandurl@example.social", - "access_token": "abc123" + "access_token": "abc123", + "proxy": + { + "url": "socks5h://[::1]:1080/", + "user": "user23", + "password": "supersecure" + } } +If you want to use a proxy, you have to edit the configuration file manually. After the configuration file is generated, you can start expandurl-mastodon as daemon. diff --git a/src/expandurl-mastodon.hpp b/src/expandurl-mastodon.hpp index 64cf809..2c6092e 100644 --- a/src/expandurl-mastodon.hpp +++ b/src/expandurl-mastodon.hpp @@ -94,10 +94,14 @@ private: bool _running; Json::Value _config; const string _configfilepath; + string _proxy; + string _proxy_user; + string _proxy_password; const bool read_config(); const bool write_config(); const bool register_app(); + const void set_proxy(Easy &masto); }; #endif // EXPANDURL_MASTODON_HPP diff --git a/src/masto.cpp b/src/masto.cpp index 805d555..65ff4ec 100644 --- a/src/masto.cpp +++ b/src/masto.cpp @@ -34,6 +34,9 @@ Listener::Listener() , _running(false) , _configfilepath(static_cast(getenv("HOME")) + "/.config/expandurl-mastodon.json") +, _proxy("") +, _proxy_user("") +, _proxy_password("") { if (read_config()) @@ -41,6 +44,7 @@ Listener::Listener() _masto = std::make_unique(_instance, _access_token); _masto->set_useragent(static_cast("expandurl-mastodon/") + global::version); + set_proxy(*_masto); } else { @@ -85,6 +89,9 @@ const bool Listener::read_config() _instance = _config["account"].asString(); _instance = _instance.substr(_instance.find('@') + 1); _access_token = _config["access_token"].asString(); + _proxy = _config["proxy"]["url"].asString(); + _proxy_user = _config["proxy"]["user"].asString(); + _proxy_password = _config["proxy"]["password"].asString(); return true; } @@ -115,6 +122,7 @@ const void Listener::start() Easy masto(_instance, _access_token); masto.set_useragent(static_cast("expandurl-mastodon/") + global::version); + set_proxy(masto); ret = masto.get_stream(Mastodon::API::v1::streaming_user, _stream, _ptr); syslog(LOG_DEBUG, "Connection lost."); if (ret != 0 && ret != 14) // 14 means canceled by user @@ -372,3 +380,18 @@ const bool Listener::register_app() return false; } + +const void Listener::set_proxy(Mastodon::Easy &masto) +{ + if (!_proxy.empty()) + { + if (!_proxy_user.empty()) + { + masto.set_proxy(_proxy, _proxy_user + ':' + _proxy_password); + } + else + { + masto.set_proxy(_proxy); + } + } +}