PacketCapture.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #if compiler(>=5.6)
  17. import ArgumentParser
  18. import EchoModel
  19. import GRPC
  20. import NIOCore
  21. import NIOExtras
  22. import NIOPosix
  23. @main
  24. @available(macOS 10.15, *)
  25. struct PCAP: AsyncParsableCommand {
  26. @Option(help: "The port to connect to")
  27. var port = 1234
  28. func run() async throws {
  29. // Create an `EventLoopGroup`.
  30. let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  31. defer {
  32. try! group.syncShutdownGracefully()
  33. }
  34. // The filename for the .pcap file to write to.
  35. let path = "packet-capture-example.pcap"
  36. let fileSink = try NIOWritePCAPHandler.SynchronizedFileSink.fileSinkWritingToFile(
  37. path: path
  38. ) { error in
  39. print("Failed to write with error '\(error)' for path '\(path)'")
  40. }
  41. // Ensure that we close the file sink when we're done with it.
  42. defer {
  43. try! fileSink.syncClose()
  44. }
  45. let channel = try GRPCChannelPool.with(
  46. target: .host("localhost", port: self.port),
  47. transportSecurity: .plaintext,
  48. eventLoopGroup: group
  49. ) {
  50. $0.debugChannelInitializer = { channel in
  51. // Create the PCAP handler and add it to the start of the channel pipeline. If this example
  52. // used TLS we would likely want to place the handler in a different position in the
  53. // pipeline so that the captured packets in the trace would not be encrypted.
  54. let writePCAPHandler = NIOWritePCAPHandler(mode: .client, fileSink: fileSink.write(buffer:))
  55. return channel.eventLoop.makeCompletedFuture(Result {
  56. try channel.pipeline.syncOperations.addHandler(writePCAPHandler, position: .first)
  57. })
  58. }
  59. }
  60. // Create a client.
  61. let echo = Echo_EchoAsyncClient(channel: channel)
  62. let messages = ["foo", "bar", "baz", "thud", "grunt", "gorp"].map { text in
  63. Echo_EchoRequest.with { $0.text = text }
  64. }
  65. do {
  66. for try await response in echo.update(messages) {
  67. print("Received response '\(response.text)'")
  68. }
  69. print("RPC completed successfully")
  70. } catch {
  71. print("RPC failed with error '\(error)'")
  72. }
  73. print("Try opening '\(path)' in Wireshark or with 'tcpdump -r \(path)'")
  74. try await echo.channel.close().get()
  75. }
  76. }
  77. #else
  78. @main
  79. enum PCAP {
  80. static func main() {
  81. print("This example requires Swift >= 5.6")
  82. }
  83. }
  84. #endif // compiler(>=5.6)