Skip to content

how to set PROXY when use sentry dio #1280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
haozi5460 opened this issue Feb 10, 2023 · 5 comments
Closed

how to set PROXY when use sentry dio #1280

haozi5460 opened this issue Feb 10, 2023 · 5 comments

Comments

@haozi5460
Copy link

In my App, I use Dio to make network requests, but when I integrate Sentry_dio to monitor, I cannot set the network proxy.

Error display:
_dio.httpClientAdapter is SentryDioClientAdapter , not sub type of DefaultHttpClientAdapter.

The code is like: #1015
as written.

String proxy = "PROXY ${ipController.text}:${portController.text}";

(dio?.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
(client) {
client.findProxy = (url) {
return proxy;
};

//Trust certificate for https proxy
client.badCertificateCallback = (cert, host, port){
return true;
};
return client;
};

@haozi5460
Copy link
Author

When I was looking for the answer, I didn't find anything useful. SentryDioClientAdapter is not exposed to the outside world and cannot be used.

#1015 also said that there is no solution.

Later I found a way to replace it myself:

  1. When you initialize Dio(), it will create a DefaultHttpClientAdapter instance for you by default and assign it to dio.httpClientAdapter.

  2. We can create a field to save dio.httpClientAdapter in the tool class, such as:

late HttpClientAdapter httpClientAdapter;
httpClientAdapter = dio.httpClientAdapter;

Note: This step needs to be placed before dio.addSentry(), because addSentry will wrap httpClientAdapter as SentryDioClientAdapter.

  1. In the place where the agent is set, you can first judge whether the dio.httpClientAdapter is the DefaultHttpClientAdapter type. If not, you can reassign the saved httpClientAdapter. After the agent is set, set dio.addSentry()
    if(!(dio?.httpClientAdapter is DefaultHttpClientAdapter)){
    dio?.httpClientAdapter = HttpRequest().httpClientAdapter;
    }

@haozi5460
Copy link
Author

String proxy = "PROXY ${ipController.text}:${portController.text}";

if(!(dio?.httpClientAdapter is DefaultHttpClientAdapter)){
  dio?.httpClientAdapter = HttpRequest().httpClientAdapter;
}

(dio?.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
    (client) {
  client.findProxy = (url) {
    return proxy;
  };

  //Trust certificate for https proxy
  client.badCertificateCallback = (cert, host, port){
    return true;
  };
  return client;
};

if(Channel.currentChannel == APPChannel.DEV){
  dio?.addSentry(
    captureFailedRequests: true,
    maxRequestBodySize: MaxRequestBodySize.always,
    maxResponseBodySize: MaxResponseBodySize.always,
  );
}

Here is how to dynamically add network proxy for Dio

@marandaneto
Copy link
Contributor

@haozi5460 as mentioned here #1015
Proxy settings should be directly set on Dio if possible,SentryDioClientAdapter is just s wrapper on top of Dio.

@haozi5460
Copy link
Author

wrapper

I know this, but after addSentry adds extensions, DefaultHttpClientAdapter becomes SentryDioClientAdapter, and SentryDioClientAdapter is not open to the outside world and cannot be called externally.

@mikewolfd
Copy link

bump, this is still a problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants