|
20 | 20 | import org.apache.dubbo.common.io.Bytes; |
21 | 21 | import org.apache.dubbo.common.logger.ErrorTypeAwareLogger; |
22 | 22 | import org.apache.dubbo.common.logger.LoggerFactory; |
| 23 | +import org.apache.dubbo.common.ssl.AuthPolicy; |
23 | 24 | import org.apache.dubbo.common.ssl.CertManager; |
24 | 25 | import org.apache.dubbo.common.ssl.ProviderCert; |
25 | 26 | import org.apache.dubbo.remoting.ChannelHandler; |
|
45 | 46 | import io.netty.handler.ssl.SslHandshakeCompletionEvent; |
46 | 47 | import io.netty.util.AttributeKey; |
47 | 48 |
|
| 49 | +import static org.apache.dubbo.common.constants.LoggerCodeConstants.CONFIG_SSL_CONNECT_INSECURE; |
48 | 50 | import static org.apache.dubbo.common.constants.LoggerCodeConstants.INTERNAL_ERROR; |
49 | 51 |
|
50 | 52 | public class NettyPortUnificationServerHandler extends ByteToMessageDecoder { |
@@ -120,8 +122,27 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t |
120 | 122 | ProviderCert providerConnectionConfig = |
121 | 123 | certManager.getProviderConnectionConfig(url, ctx.channel().remoteAddress()); |
122 | 124 |
|
123 | | - if (providerConnectionConfig != null && isSsl(in)) { |
124 | | - enableSsl(ctx, providerConnectionConfig); |
| 125 | + if (providerConnectionConfig != null && canDetectSsl(in)) { |
| 126 | + if (isSsl(in)) { |
| 127 | + enableSsl(ctx, providerConnectionConfig); |
| 128 | + } else { |
| 129 | + // check server should load TLS or not |
| 130 | + if (providerConnectionConfig.getAuthPolicy() != AuthPolicy.NONE) { |
| 131 | + byte[] preface = new byte[in.readableBytes()]; |
| 132 | + in.readBytes(preface); |
| 133 | + LOGGER.error( |
| 134 | + CONFIG_SSL_CONNECT_INSECURE, |
| 135 | + "client request server without TLS", |
| 136 | + "", |
| 137 | + String.format( |
| 138 | + "Downstream=%s request without TLS preface, but server require it. " + "preface=%s", |
| 139 | + ctx.channel().remoteAddress(), Bytes.bytes2hex(preface))); |
| 140 | + |
| 141 | + // Untrusted connection; discard everything and close the connection. |
| 142 | + in.clear(); |
| 143 | + ctx.close(); |
| 144 | + } |
| 145 | + } |
125 | 146 | } else { |
126 | 147 | Set<String> supportedProtocolNames = new HashSet<>(protocols.keySet()); |
127 | 148 | supportedProtocolNames.retainAll(urlMapper.keySet()); |
@@ -177,6 +198,11 @@ private void enableSsl(ChannelHandlerContext ctx, ProviderCert providerConnectio |
177 | 198 | p.remove(this); |
178 | 199 | } |
179 | 200 |
|
| 201 | + private boolean canDetectSsl(ByteBuf buf) { |
| 202 | + // at least 5 bytes to determine if data is encrypted |
| 203 | + return detectSsl && buf.readableBytes() >= 5; |
| 204 | + } |
| 205 | + |
180 | 206 | private boolean isSsl(ByteBuf buf) { |
181 | 207 | // at least 5 bytes to determine if data is encrypted |
182 | 208 | if (detectSsl && buf.readableBytes() >= 5) { |
|
0 commit comments