grpc-swift icon indicating copy to clipboard operation
grpc-swift copied to clipboard

How can I use a proxy with ClientConnection

Open mattiaferrari02 opened this issue 2 years ago • 8 comments

What are you trying to achieve?

Describe your problem at a high level. Include code snippets if you think that would help better illustrate your problem. Without enough information, we will not be able to help you.

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

channel = ClientConnection.secure(group: self.group).connect(host: host, port: port)
client = GrpcClient(channel: channel!, defaultCallOptions: defaultCallOptions)

I would like to use a SOCKS proxy to forward my calls to the grpc server, is there a way to do that?

mattiaferrari02 avatar Apr 12 '22 12:04 mattiaferrari02

Not out-of-the-box. However, swift-nio-extras includes a SOCKS library which you can use in conjunction with the debugChannelInitializer.

This would be something like:

import NIOSOCKS

// ...

channel = ClientConnection.secure(group: self.group)
  .withDebugChannelInitializer { channel in
    // Add the SOCKS client handler, it must be the first handler in the pipeline.
    channel.pipeline.addHandler(SOCKSClientHandler(targetAddress: ...), position: .first)
  }
  .connect(host: host, port: port)

glbrntt avatar Apr 19 '22 08:04 glbrntt

Thanks for your reply, but it seems like i can't import directly NIOSOCKS like you mentioned

import NIOSOCKS // No such module 'NIOSOCKS'
import NIOExtras // No error, but i can't access 'SOCKSClientHandler'

mattiaferrari02 avatar Apr 20 '22 10:04 mattiaferrari02

You need to add a dependency to your Package.swift:

.package(url: "https://github.com/apple/swift-nio-extras.git", from: "1.9.0")

and then in the appropriate target where you're using GRPC you need to add:

    .target(
        name: "YourTargetWhichUsesGRPC",
        dependencies: [
            // ...
            .product(name: "NIOSOCKS", package: "swift-nio-extras"),
            // ...
        ]),

glbrntt avatar Apr 20 '22 10:04 glbrntt

Oh ok, sorry but i'm no swift dev, the project that i'm working on is made with react native, and we are using swift for the native-module part, so i guess we don't have a Package.swift file... Can it be done from the podfile?

mattiaferrari02 avatar Apr 20 '22 11:04 mattiaferrari02

Yes, you can depend on gRPC Swift via Cocoapods but -- and this is a big one -- gRPC Swift is about to stop supporting Cocoapods.

I believe you can still add a Swift Package Manager dependency to your workspace via File > Add Packages

glbrntt avatar Apr 20 '22 12:04 glbrntt

Ok so, how can i do it from the podfile?

mattiaferrari02 avatar Apr 20 '22 13:04 mattiaferrari02

For some reason there is no podspec published for NIOSOCKS.

We can publish one when we do the next release, however I would strongly advise you to not use Cocoapods as the next release will be the final release for which the NIO team will publish Cocoapods. The same is true of gRPC Swift: the next release will be the final one which supports Cocoapods.

glbrntt avatar Apr 20 '22 13:04 glbrntt

I will really appreciate that, thanks for the support

mattiaferrari02 avatar Apr 20 '22 14:04 mattiaferrari02