Skip to content

Commit 99b1539

Browse files
feat: add support for SOCKS5 proxy (#1063)
* Added socks5_proxy to options * correct options freeing * Update CHANGELOG.md * Add socks5_proxy attribute to curl_transport_state * formatting * refactor to use single proxy attribute * format * Added back deprecated http_proxy API * formatting * refactor sentry_transport_winhttp.c to use opts->proxy * formatting * update crashpad dependency * fix: update minimum libcurl version check to 7.21.7 * update crashpad dependency * test: add proxy integration tests * get latest crashpad changes * feat: add example.c run-args for http and socks5 proxy * test: changed test name + added more mitmdump startup time * chore: format * test: add mitmproxy to requirements.txt * test: add skip for proxy test on non-MacOS platforms * test: move socksproxy-off assert into AssertionError check for Linux test * test: change expected http log length to match platform * test: order of operations * test: run http proxy tests on all platforms * test: add special case for Windows Autoproxy * apply suggestions from code review * chore: added comments about proxy behaviour * cleaned up test and moved comments to proper place (let's hope I didn't mess up CI) * chore: fix minor comment typo * chore: added api documentation * Update include/sentry.h Co-authored-by: Mischan Toosarani-Hausberger <[email protected]> * Update CONTRIBUTING.md Co-authored-by: Mischan Toosarani-Hausberger <[email protected]> * update crashpad submodule * update crashpad submodule (again) * updated CHANGELOG.md * updated CHANGELOG.md again --------- Co-authored-by: Mischan Toosarani-Hausberger <[email protected]>
1 parent d431ab9 commit 99b1539

File tree

15 files changed

+230
-25
lines changed

15 files changed

+230
-25
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
**Features**:
6+
- Add SOCKS5 proxy support for macOS and Linux. ([#1063](https://github.com/getsentry/sentry-native/pull/1063))
7+
38
## 0.7.15
49

510
**Fixes**:

CONTRIBUTING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ The example currently supports the following commands:
146146
- `discarding-on-crash`: Installs an `on_crash()` callback that discards the crash event.
147147
- `override-sdk-name`: Changes the SDK name via the options at runtime.
148148
- `stack-overflow`: Provokes a stack-overflow.
149+
- `http-proxy`: Uses a localhost `HTTP` proxy on port 8080.
150+
- `socks5-proxy`: Uses a localhost `SOCKS5` proxy on port 1080.
149151

150152
Only on Windows using crashpad with its WER handler module:
151153

examples/example.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,14 @@ main(int argc, char **argv)
236236
sentry_options_set_sdk_name(options, "sentry.native.android.flutter");
237237
}
238238

239+
if (has_arg(argc, argv, "http-proxy")) {
240+
sentry_options_set_proxy(options, "http://127.0.0.1:8080");
241+
}
242+
243+
if (has_arg(argc, argv, "socks5-proxy")) {
244+
sentry_options_set_proxy(options, "socks5://127.0.0.1:1080");
245+
}
246+
239247
sentry_init(options);
240248

241249
if (!has_arg(argc, argv, "no-setup")) {

include/sentry.h

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -972,17 +972,43 @@ SENTRY_API void sentry_options_set_dist_n(
972972
SENTRY_API const char *sentry_options_get_dist(const sentry_options_t *opts);
973973

974974
/**
975-
* Configures the http proxy.
975+
* Configures the proxy.
976976
*
977-
* The given proxy has to include the full scheme, eg. `http://some.proxy/`.
977+
* The given proxy has to include the full scheme,
978+
* eg. `http://some.proxy/` or `socks5://some.proxy/`.
979+
*
980+
* Not every transport behaves the same way when configuring a proxy.
981+
* On Windows if a transport can't connect to the proxy it will fall back on a
982+
* connection without proxy. This is also true for the crashpad_handler
983+
* transport on macOS for a socks proxy, but not for a http proxy.
984+
* All transports that use libcurl (Linux and the Native SDK transport on macOS)
985+
* will honor the proxy settings and not fall back.
986+
*/
987+
SENTRY_API void sentry_options_set_proxy(
988+
sentry_options_t *opts, const char *proxy);
989+
SENTRY_API void sentry_options_set_proxy_n(
990+
sentry_options_t *opts, const char *proxy, size_t proxy_len);
991+
992+
/**
993+
* Returns the configured proxy.
994+
*/
995+
SENTRY_API const char *sentry_options_get_proxy(const sentry_options_t *opts);
996+
997+
/**
998+
* Configures the proxy.
999+
*
1000+
* This is a **deprecated** alias for `sentry_options_set_proxy(_n)`.
1001+
*
1002+
* The given proxy has to include the full scheme,
1003+
* eg. `http://some.proxy/.
9781004
*/
9791005
SENTRY_API void sentry_options_set_http_proxy(
9801006
sentry_options_t *opts, const char *proxy);
9811007
SENTRY_API void sentry_options_set_http_proxy_n(
9821008
sentry_options_t *opts, const char *proxy, size_t proxy_len);
9831009

9841010
/**
985-
* Returns the configured http proxy.
1011+
* Returns the configured proxy.
9861012
*/
9871013
SENTRY_API const char *sentry_options_get_http_proxy(
9881014
const sentry_options_t *opts);

src/backends/sentry_backend_crashpad.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,8 @@ crashpad_backend_startup(
440440
SENTRY_TRACEF("using minidump URL \"%s\"", minidump_url);
441441
}
442442
bool success = data->client->StartHandler(handler, database, database,
443-
minidump_url ? minidump_url : "",
444-
options->http_proxy ? options->http_proxy : "", annotations, arguments,
443+
minidump_url ? minidump_url : "", options->proxy ? options->proxy : "",
444+
annotations, arguments,
445445
/* restartable */ true,
446446
/* asynchronous_start */ false, attachments);
447447
sentry_free(minidump_url);

src/sentry_options.c

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ sentry_options_free(sentry_options_t *opts)
9090
sentry_free(opts->user_agent);
9191
sentry_free(opts->environment);
9292
sentry_free(opts->dist);
93-
sentry_free(opts->http_proxy);
93+
sentry_free(opts->proxy);
9494
sentry_free(opts->ca_certs);
9595
sentry_free(opts->transport_thread_name);
9696
sentry__path_free(opts->database_path);
@@ -235,25 +235,44 @@ sentry_options_get_dist(const sentry_options_t *opts)
235235
return opts->dist;
236236
}
237237

238+
void
239+
sentry_options_set_proxy_n(
240+
sentry_options_t *opts, const char *proxy, size_t proxy_len)
241+
{
242+
sentry_free(opts->proxy);
243+
opts->proxy = sentry__string_clone_n(proxy, proxy_len);
244+
}
245+
246+
void
247+
sentry_options_set_proxy(sentry_options_t *opts, const char *proxy)
248+
{
249+
sentry_free(opts->proxy);
250+
opts->proxy = sentry__string_clone(proxy);
251+
}
252+
253+
const char *
254+
sentry_options_get_proxy(const sentry_options_t *opts)
255+
{
256+
return opts->proxy;
257+
}
258+
238259
void
239260
sentry_options_set_http_proxy_n(
240261
sentry_options_t *opts, const char *proxy, size_t proxy_len)
241262
{
242-
sentry_free(opts->http_proxy);
243-
opts->http_proxy = sentry__string_clone_n(proxy, proxy_len);
263+
sentry_options_set_proxy_n(opts, proxy, proxy_len);
244264
}
245265

246266
void
247267
sentry_options_set_http_proxy(sentry_options_t *opts, const char *proxy)
248268
{
249-
sentry_free(opts->http_proxy);
250-
opts->http_proxy = sentry__string_clone(proxy);
269+
sentry_options_set_proxy(opts, proxy);
251270
}
252271

253272
const char *
254273
sentry_options_get_http_proxy(const sentry_options_t *opts)
255274
{
256-
return opts->http_proxy;
275+
return sentry_options_get_proxy(opts);
257276
}
258277

259278
void

src/sentry_options.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ typedef struct sentry_options_s {
3535
char *release;
3636
char *environment;
3737
char *dist;
38-
char *http_proxy;
38+
char *proxy;
3939
char *ca_certs;
4040
char *transport_thread_name;
4141
char *sdk_name;

src/transports/sentry_transport_curl.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ typedef struct curl_transport_state_s {
1717
sentry_dsn_t *dsn;
1818
CURL *curl_handle;
1919
char *user_agent;
20-
char *http_proxy;
20+
char *proxy;
2121
char *ca_certs;
2222
sentry_rate_limiter_t *ratelimiter;
2323
bool debug;
@@ -54,7 +54,7 @@ sentry__curl_bgworker_state_free(void *_state)
5454
sentry__rate_limiter_free(state->ratelimiter);
5555
sentry_free(state->ca_certs);
5656
sentry_free(state->user_agent);
57-
sentry_free(state->http_proxy);
57+
sentry_free(state->proxy);
5858
sentry_free(state);
5959
}
6060

@@ -85,7 +85,7 @@ sentry__curl_transport_start(
8585
};
8686

8787
if (!sentry__check_min_version(
88-
curl_version, (sentry_version_t) { 7, 10, 7 })) {
88+
curl_version, (sentry_version_t) { 7, 21, 7 })) {
8989
SENTRY_WARNF("`libcurl` is at unsupported version `%u.%u.%u`",
9090
curl_version.major, curl_version.minor, curl_version.patch);
9191
return 1;
@@ -101,7 +101,7 @@ sentry__curl_transport_start(
101101
curl_bgworker_state_t *state = sentry__bgworker_get_state(bgworker);
102102

103103
state->dsn = sentry__dsn_incref(options->dsn);
104-
state->http_proxy = sentry__string_clone(options->http_proxy);
104+
state->proxy = sentry__string_clone(options->proxy);
105105
state->user_agent = sentry__string_clone(options->user_agent);
106106
state->ca_certs = sentry__string_clone(options->ca_certs);
107107
state->curl_handle = curl_easy_init();
@@ -215,8 +215,8 @@ sentry__curl_send_task(void *_envelope, void *_state)
215215
curl_easy_setopt(curl, CURLOPT_HEADERDATA, (void *)&info);
216216
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback);
217217

218-
if (state->http_proxy) {
219-
curl_easy_setopt(curl, CURLOPT_PROXY, state->http_proxy);
218+
if (state->proxy) {
219+
curl_easy_setopt(curl, CURLOPT_PROXY, state->proxy);
220220
}
221221
if (state->ca_certs) {
222222
curl_easy_setopt(curl, CURLOPT_CAINFO, state->ca_certs);

src/transports/sentry_transport_winhttp.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ sentry__winhttp_transport_start(
6969
sentry__bgworker_setname(bgworker, opts->transport_thread_name);
7070

7171
// ensure the proxy starts with `http://`, otherwise ignore it
72-
if (opts->http_proxy
73-
&& strstr(opts->http_proxy, "http://") == opts->http_proxy) {
74-
const char *ptr = opts->http_proxy + 7;
72+
if (opts->proxy && strstr(opts->proxy, "http://") == opts->proxy) {
73+
const char *ptr = opts->proxy + 7;
7574
const char *slash = strchr(ptr, '/');
7675
if (slash) {
7776
char *copy = sentry__string_clone_n(ptr, slash - ptr);

0 commit comments

Comments
 (0)