2
0

TLSVerificationHandler.swift 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright 2019, 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 Logging
  17. import NIOCore
  18. import NIOTLS
  19. /// Application protocol identifiers for ALPN.
  20. internal enum GRPCApplicationProtocolIdentifier {
  21. static let gRPC = "grpc-exp"
  22. static let h2 = "h2"
  23. static let http1_1 = "http/1.1"
  24. static let client = [gRPC, h2]
  25. static let server = [gRPC, h2, http1_1]
  26. static func isHTTP2Like(_ value: String) -> Bool {
  27. switch value {
  28. case self.gRPC, self.h2:
  29. return true
  30. default:
  31. return false
  32. }
  33. }
  34. static func isHTTP1(_ value: String) -> Bool {
  35. return value == self.http1_1
  36. }
  37. }
  38. internal class TLSVerificationHandler: ChannelInboundHandler, RemovableChannelHandler {
  39. typealias InboundIn = Any
  40. private let logger: Logger
  41. init(logger: Logger) {
  42. self.logger = logger
  43. }
  44. func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) {
  45. if let tlsEvent = event as? TLSUserEvent {
  46. switch tlsEvent {
  47. case let .handshakeCompleted(negotiatedProtocol: .some(`protocol`)):
  48. self.logger.debug("TLS handshake completed, negotiated protocol: \(`protocol`)")
  49. case .handshakeCompleted(negotiatedProtocol: nil):
  50. self.logger.debug("TLS handshake completed, no protocol negotiated")
  51. case .shutdownCompleted:
  52. ()
  53. }
  54. }
  55. context.fireUserInboundEventTriggered(event)
  56. }
  57. }