grpc-swift
grpc-swift copied to clipboard
How can I use a proxy with ClientConnection
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?
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)
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'
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"),
// ...
]),
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?
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
Ok so, how can i do it from the podfile?
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.
I will really appreciate that, thanks for the support