• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 The gRPC Authors
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 
17 package io.grpc.netty;
18 
19 import io.grpc.ChannelLogger;
20 import io.grpc.netty.ProtocolNegotiators.ClientTlsHandler;
21 import io.grpc.netty.ProtocolNegotiators.GrpcNegotiationHandler;
22 import io.grpc.netty.ProtocolNegotiators.WaitUntilActiveHandler;
23 import io.netty.channel.ChannelHandler;
24 import io.netty.handler.ssl.SslContext;
25 import io.netty.util.AsciiString;
26 
27 /**
28  * Internal accessor for {@link ProtocolNegotiators}.
29  */
30 public final class InternalProtocolNegotiators {
31 
InternalProtocolNegotiators()32   private InternalProtocolNegotiators() {}
33 
34   /**
35    * Returns a {@link ProtocolNegotiator} that ensures the pipeline is set up so that TLS will
36    * be negotiated, the {@code handler} is added and writes to the {@link io.netty.channel.Channel}
37    * may happen immediately, even before the TLS Handshake is complete.
38    */
tls(SslContext sslContext)39   public static InternalProtocolNegotiator.ProtocolNegotiator tls(SslContext sslContext) {
40     final io.grpc.netty.ProtocolNegotiator negotiator = ProtocolNegotiators.tls(sslContext);
41     final class TlsNegotiator implements InternalProtocolNegotiator.ProtocolNegotiator {
42 
43       @Override
44       public AsciiString scheme() {
45         return negotiator.scheme();
46       }
47 
48       @Override
49       public ChannelHandler newHandler(GrpcHttp2ConnectionHandler grpcHandler) {
50         return negotiator.newHandler(grpcHandler);
51       }
52 
53       @Override
54       public void close() {
55         negotiator.close();
56       }
57     }
58 
59     return new TlsNegotiator();
60   }
61 
62   /**
63    * Returns a {@link ProtocolNegotiator} that ensures the pipeline is set up so that TLS will be
64    * negotiated, the server TLS {@code handler} is added and writes to the {@link
65    * io.netty.channel.Channel} may happen immediately, even before the TLS Handshake is complete.
66    */
serverTls(SslContext sslContext)67   public static InternalProtocolNegotiator.ProtocolNegotiator serverTls(SslContext sslContext) {
68     final io.grpc.netty.ProtocolNegotiator negotiator = ProtocolNegotiators.serverTls(sslContext);
69     final class ServerTlsNegotiator implements InternalProtocolNegotiator.ProtocolNegotiator {
70 
71       @Override
72       public AsciiString scheme() {
73         return negotiator.scheme();
74       }
75 
76       @Override
77       public ChannelHandler newHandler(GrpcHttp2ConnectionHandler grpcHandler) {
78         return negotiator.newHandler(grpcHandler);
79       }
80 
81       @Override
82       public void close() {
83         negotiator.close();
84       }
85     }
86 
87     return new ServerTlsNegotiator();
88   }
89 
90   /** Returns a {@link ProtocolNegotiator} for plaintext client channel. */
plaintext()91   public static InternalProtocolNegotiator.ProtocolNegotiator plaintext() {
92     final io.grpc.netty.ProtocolNegotiator negotiator = ProtocolNegotiators.plaintext();
93     final class PlaintextNegotiator implements InternalProtocolNegotiator.ProtocolNegotiator {
94 
95       @Override
96       public AsciiString scheme() {
97         return negotiator.scheme();
98       }
99 
100       @Override
101       public ChannelHandler newHandler(GrpcHttp2ConnectionHandler grpcHandler) {
102         return negotiator.newHandler(grpcHandler);
103       }
104 
105       @Override
106       public void close() {
107         negotiator.close();
108       }
109     }
110 
111     return new PlaintextNegotiator();
112   }
113 
114   /** Returns a {@link ProtocolNegotiator} for plaintext server channel. */
serverPlaintext()115   public static InternalProtocolNegotiator.ProtocolNegotiator serverPlaintext() {
116     final io.grpc.netty.ProtocolNegotiator negotiator = ProtocolNegotiators.serverPlaintext();
117     final class ServerPlaintextNegotiator implements InternalProtocolNegotiator.ProtocolNegotiator {
118 
119       @Override
120       public AsciiString scheme() {
121         return negotiator.scheme();
122       }
123 
124       @Override
125       public ChannelHandler newHandler(GrpcHttp2ConnectionHandler grpcHandler) {
126         return negotiator.newHandler(grpcHandler);
127       }
128 
129       @Override
130       public void close() {
131         negotiator.close();
132       }
133     }
134 
135     return new ServerPlaintextNegotiator();
136   }
137 
138   /**
139    * Internal version of {@link WaitUntilActiveHandler}.
140    */
waitUntilActiveHandler(ChannelHandler next, ChannelLogger negotiationLogger)141   public static ChannelHandler waitUntilActiveHandler(ChannelHandler next,
142       ChannelLogger negotiationLogger) {
143     return new WaitUntilActiveHandler(next, negotiationLogger);
144   }
145 
146   /**
147    * Internal version of {@link GrpcNegotiationHandler}.
148    */
grpcNegotiationHandler(GrpcHttp2ConnectionHandler next)149   public static ChannelHandler grpcNegotiationHandler(GrpcHttp2ConnectionHandler next) {
150     return new GrpcNegotiationHandler(next);
151   }
152 
clientTlsHandler( ChannelHandler next, SslContext sslContext, String authority, ChannelLogger negotiationLogger)153   public static ChannelHandler clientTlsHandler(
154       ChannelHandler next, SslContext sslContext, String authority,
155       ChannelLogger negotiationLogger) {
156     return new ClientTlsHandler(next, sslContext, authority, null, negotiationLogger);
157   }
158 
159   public static class ProtocolNegotiationHandler
160       extends ProtocolNegotiators.ProtocolNegotiationHandler {
161 
ProtocolNegotiationHandler(ChannelHandler next, String negotiatorName, ChannelLogger negotiationLogger)162     protected ProtocolNegotiationHandler(ChannelHandler next, String negotiatorName,
163         ChannelLogger negotiationLogger) {
164       super(next, negotiatorName, negotiationLogger);
165     }
166 
ProtocolNegotiationHandler(ChannelHandler next, ChannelLogger negotiationLogger)167     protected ProtocolNegotiationHandler(ChannelHandler next, ChannelLogger negotiationLogger) {
168       super(next, negotiationLogger);
169     }
170   }
171 }
172