PacketCapture.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright 2020, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import ArgumentParser
  17. import EchoModel
  18. import GRPC
  19. import NIOCore
  20. import NIOExtras
  21. import NIOPosix
  22. @main
  23. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  24. struct PCAP: AsyncParsableCommand {
  25. @Option(help: "The port to connect to")
  26. var port = 1234
  27. func run() async throws {
  28. // Create an `EventLoopGroup`.
  29. let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  30. defer {
  31. try! group.syncShutdownGracefully()
  32. }
  33. // The filename for the .pcap file to write to.
  34. let path = "packet-capture-example.pcap"
  35. let fileSink = try NIOWritePCAPHandler.SynchronizedFileSink.fileSinkWritingToFile(
  36. path: path
  37. ) { error in
  38. print("Failed to write with error '\(error)' for path '\(path)'")
  39. }
  40. // Ensure that we close the file sink when we're done with it.
  41. defer {
  42. try! fileSink.syncClose()
  43. }
  44. let channel = try GRPCChannelPool.with(
  45. target: .host("localhost", port: self.port),
  46. transportSecurity: .plaintext,
  47. eventLoopGroup: group
  48. ) {
  49. $0.debugChannelInitializer = { channel in
  50. // Create the PCAP handler and add it to the start of the channel pipeline. If this example
  51. // used TLS we would likely want to place the handler in a different position in the
  52. // pipeline so that the captured packets in the trace would not be encrypted.
  53. let writePCAPHandler = NIOWritePCAPHandler(mode: .client, fileSink: fileSink.write(buffer:))
  54. return channel.eventLoop.makeCompletedFuture(
  55. Result {
  56. try channel.pipeline.syncOperations.addHandler(writePCAPHandler, position: .first)
  57. }
  58. )
  59. }
  60. }
  61. // Create a client.
  62. let echo = Echo_EchoAsyncClient(channel: channel)
  63. let messages = ["foo", "bar", "baz", "thud", "grunt", "gorp"].map { text in
  64. Echo_EchoRequest.with { $0.text = text }
  65. }
  66. do {
  67. for try await response in echo.update(messages) {
  68. print("Received response '\(response.text)'")
  69. }
  70. print("RPC completed successfully")
  71. } catch {
  72. print("RPC failed with error '\(error)'")
  73. }
  74. print("Try opening '\(path)' in Wireshark or with 'tcpdump -r \(path)'")
  75. try await echo.channel.close().get()
  76. }
  77. }