Skip to content

How can I use ManagedChannel with a proxy? #9057

Closed

Description

@mattiaferrari02

Is there a way to specify a proxy connection to use to connect to a grpc server?
This is the code that i'm currently using to instanciate the client used for the connection with the server

ManagedChannel channel = ManagedChannelBuilder.forAddress(serverAddress, serverPort).build();
MyClientGrpc.ClientBlockingStub = MyClientGrpc.newBlockingStub(channel);

I would like to use this proxy for the connection with the server:

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));

Is there a way to do that?

Activity

  1. changed the title [-]How can I use ManagedChannel with a proxy[/-] [+]How can I use ManagedChannel with a proxy?[/+] on Apr 5, 2022
  2. ejona86 commented on Apr 6, 2022

    @ejona86
    Member

    Preference order of how to configure proxies (the first two are generic Java):

    • -Dhttps.proxyHost and -Dhttps.proxyPort (and -Dhttp.nonProxyHosts, which is used for both http and https). Java docs. Note that gRPC will observe the https configuration, even when doing plaintext, as it will use the HTTP CONNECT proxing protocol
    • ProxySelector.setDefault(). Use this if you need more complex configuration logic
    • gRPC's ProxyDetector. This is best suited for libraries that are going to a fixed destination and they know they need a proxy. This happens, but is generally rare
  3. mattiaferrari02 commented on Apr 12, 2022

    @mattiaferrari02
    Author

    Ok I think that I'm interested in the third case you mentioned, but now how can I use that ProxyDetector class?

    Preference order of how to configure proxies (the first two are generic Java):

    • -Dhttps.proxyHost and -Dhttps.proxyPort (and -Dhttp.nonProxyHosts, which is used for both http and https). Java docs. Note that gRPC will observe the https configuration, even when doing plaintext, as it will use the HTTP CONNECT proxing protocol
    • ProxySelector.setDefault(). Use this if you need more complex configuration logic
    • gRPC's ProxyDetector. This is best suited for libraries that are going to a fixed destination and they know they need a proxy. This happens, but is generally rare
  4. mattiaferrari02 commented on Apr 12, 2022

    @mattiaferrari02
    Author

    Also, is it possible to connect using a SOCKS proxy instead of a HTTP/HTTPS proxy?

  5. ejona86 commented on Apr 12, 2022

    @ejona86
    Member

    Did you take a look at the ProxyDetector documentation? Return null if the address (which is generally hostname + port) shouldn't be proxied and otherwise return HttpConnectProxiedSocketAddress.

    SOCKS is not currently supported.

  6. ejona86 commented on Apr 19, 2022

    @ejona86
    Member

    Seems like this is resolved. If not, comment, and it can be reopened.

    If you require SOCKS (not just nice to have) then that is probably best to open a feature request issue for.

  7. locked as resolved and limited conversation to collaborators on Jul 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions