Browse Source

Added proxy support

tastytea 10 months ago
parent
commit
86a614dd61
Signed by: tastytea <tastytea@tastytea.de> GPG Key ID: 59346E0EA35C67E5
5 changed files with 82 additions and 1 deletions
  1. 1
    1
      CMakeLists.txt
  2. 40
    0
      examples/example15_proxy.cpp
  3. 17
    0
      src/http.cpp
  4. 5
    0
      src/mastodon-cpp.cpp
  5. 19
    0
      src/mastodon-cpp.hpp

+ 1
- 1
CMakeLists.txt View File

@@ -1,6 +1,6 @@
1 1
 cmake_minimum_required (VERSION 3.7)
2 2
 project (mastodon-cpp
3
-         VERSION 0.14.0
3
+         VERSION 0.15.0
4 4
          LANGUAGES CXX
5 5
 )
6 6
 

+ 40
- 0
examples/example15_proxy.cpp View File

@@ -0,0 +1,40 @@
1
+/*  This file is part of mastodon-cpp.
2
+ *  This example shows how to use a proxy.
3
+ */
4
+
5
+#include <iostream>
6
+#include <vector>
7
+#include <string>
8
+#include <cstdint>
9
+#ifdef MASTODON_CPP
10
+    #include "mastodon-cpp.hpp"
11
+#else
12
+    #include <mastodon-cpp/mastodon-cpp.hpp>
13
+#endif
14
+
15
+using std::cout;
16
+using Mastodon::API;
17
+
18
+int main(int argc, char *argv[])
19
+{
20
+    if (argc < 3)
21
+    {
22
+        std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
23
+        return 1;
24
+    }
25
+
26
+    Mastodon::API masto(argv[1], argv[2]);
27
+    // SOCKS5 Proxy. Proxy resolves URL hostname.
28
+    masto.set_proxy("socks5h://[fd12::1a]:1080/");
29
+    // HTTPS proxy with username and password
30
+    masto.set_proxy("https://localhost:3128", "user5:supersecurepassword");
31
+    std::string answer;
32
+    std::uint16_t ret;
33
+
34
+    ret = masto.get(API::v1::accounts_verify_credentials, answer);
35
+
36
+    cout << "Return code: " << ret << '\n';
37
+    std::cout << answer << '\n';
38
+
39
+    return 0;
40
+}

+ 17
- 0
src/http.cpp View File

@@ -35,6 +35,8 @@ API::http::http(const API &api, const string &instance,
35 35
 , _instance(instance)
36 36
 , _access_token(access_token)
37 37
 , _cancel_stream(false)
38
+, _proxy("")
39
+, _proxy_userpw("")
38 40
 {
39 41
     curlpp::initialize();
40 42
 }
@@ -70,6 +72,15 @@ const uint_fast16_t API::http::request(const method &meth,
70 72
         ttdebug << "User-Agent: " << parent.get_useragent() << "\n";
71 73
         request.setOpt<curlopts::UserAgent>(parent.get_useragent());
72 74
 
75
+        if (!_proxy.empty())
76
+        {
77
+            request.setOpt<curlopts::Proxy>(_proxy);
78
+            if (!_proxy_userpw.empty())
79
+            {
80
+                request.setOpt<curlopts::ProxyUserPwd>(_proxy_userpw);
81
+            }
82
+        }
83
+
73 84
         if (!_access_token.empty())
74 85
         {
75 86
             headers.push_back("Authorization: Bearer " + _access_token);
@@ -217,3 +228,9 @@ std::mutex &API::http::get_mutex()
217 228
 {
218 229
     return _mutex;
219 230
 }
231
+
232
+const void API::http::set_proxy(const string &proxy, const string &userpw)
233
+{
234
+    _proxy = proxy;
235
+    _proxy_userpw = userpw;
236
+}

+ 5
- 0
src/mastodon-cpp.cpp View File

@@ -571,3 +571,8 @@ const string API::unescape_html(const string &html)
571 571
 
572 572
     return output;
573 573
 }
574
+
575
+const void API::set_proxy(const string &proxy, const string &userpw)
576
+{
577
+    _http.set_proxy(proxy, userpw);
578
+}

+ 19
- 0
src/mastodon-cpp.hpp View File

@@ -146,6 +146,16 @@ public:
146 146
          */
147 147
         std::mutex &get_mutex();
148 148
 
149
+        /*!
150
+         *  @brief  Sets the proxy.
151
+         *
152
+         *  @param  proxy    See `man 3 CURLOPT_PROXY`
153
+         *  @param  userpw   See `man 3 CURLOPT_PROXYUSERPWD` (optional)
154
+         *
155
+         *  @since  0.15.0
156
+         */
157
+        const void set_proxy(const string &proxy, const string &userpw = "");
158
+
149 159
     private:
150 160
         const API &parent;
151 161
         const string _instance;
@@ -153,6 +163,8 @@ public:
153 163
         string _headers;
154 164
         bool _cancel_stream;
155 165
         std::mutex _mutex;
166
+        string _proxy;
167
+        string _proxy_userpw;
156 168
 
157 169
         const size_t callback_write(char* data, size_t size, size_t nmemb,
158 170
                                     string *oss);
@@ -395,6 +407,13 @@ public:
395 407
      */
396 408
     static const string unescape_html(const string &html);
397 409
 
410
+    /*!
411
+     *  @brief  Calls Mastodon::API::http::set_proxy()
412
+     *
413
+     *  @since  0.15.0
414
+     */
415
+    const void set_proxy(const string &proxy, const string &userpw = "");
416
+
398 417
     /*!
399 418
      *  @brief  Make a GET request which doesn't require parameters.
400 419
      *

Loading…
Cancel
Save