PacketCapture.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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, *)
  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(Result {
  55. try channel.pipeline.syncOperations.addHandler(writePCAPHandler, position: .first)
  56. })
  57. }
  58. }
  59. // Create a client.
  60. let echo = Echo_EchoAsyncClient(channel: channel)
  61. let messages = ["foo", "bar", "baz", "thud", "grunt", "gorp"].map { text in
  62. Echo_EchoRequest.with { $0.text = text }
  63. }
  64. do {
  65. for try await response in echo.update(messages) {
  66. print("Received response '\(response.text)'")
  67. }
  68. print("RPC completed successfully")
  69. } catch {
  70. print("RPC failed with error '\(error)'")
  71. }
  72. print("Try opening '\(path)' in Wireshark or with 'tcpdump -r \(path)'")
  73. try await echo.channel.close().get()
  74. }
  75. }