1 /*
2  * Copyright (C) 2019, HuntLabs
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 
18 module hunt.database.driver.postgresql.impl.InitiateSslHandler;
19 
20 import hunt.net.buffer.ByteBuf;
21 // import io.netty.buffer.Unpooled;
22 // import io.netty.channel.ChannelHandlerContext;
23 // import io.netty.channel.ChannelInboundHandlerAdapter;
24 // import io.netty.handler.codec.DecoderException;
25 // import io.vertx.core.Promise;
26 // import hunt.database.driver.postgresql.impl.codec.PgProtocolConstants;
27 // import hunt.database.base.impl.SocketConnectionBase;
28 // import io.vertx.core.VertxException;
29 
30 // class InitiateSslHandler : ChannelInboundHandlerAdapter {
31 
32 //     private static final int code = 80877103;
33 //     private final SocketConnectionBase conn;
34 //     private final Promise!(Void) upgradePromise;
35 
36 //     InitiateSslHandler(SocketConnectionBase conn, Promise!(Void) upgradePromise) {
37 //         this.conn = conn;
38 //         this.upgradePromise = upgradePromise;
39 //     }
40 
41 //     override
42 //     void channelActive(ChannelHandlerContext ctx) throws Exception {
43 //         ByteBuf byteBuf = Unpooled.buffer();
44 //         byteBuf.writeInt(0);
45 //         byteBuf.writeInt(code);
46 // //    out.writeInt(0x12345679);
47 //         byteBuf.setInt(0, byteBuf.writerIndex());
48 //         ctx.writeAndFlush(byteBuf);
49 //         super.channelActive(ctx);
50 //     }
51 
52 //     override
53 //     void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
54 //         // This must be a single byte buffer - after that follow the SSL handshake
55 //         ByteBuf byteBuf = (ByteBuf) msg;
56 //         byte b = byteBuf.getByte(0);
57 //         byteBuf.release();
58 //         switch (b) {
59 //             case PgProtocolConstants.MESSAGE_TYPE_SSL_YES: {
60 //                 conn.socket().upgradeToSsl(v -> {
61 //                     ctx.pipeline().remove(this);
62 //                     upgradePromise.complete();
63 //                 });
64 //                 break;
65 //             }
66 //             case PgProtocolConstants.MESSAGE_TYPE_SSL_NO: {
67 //                 upgradePromise.fail(new Exception("Postgres Server does not handle SSL connection"));
68 //                 break;
69 //             }
70 //             default:
71 //                 upgradePromise.fail(new Exception("Invalid SSL connection message"));
72 //                 break;
73 //         }
74 //     }
75 
76 //     override
77 //     void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
78 //         if (cause instanceof DecoderException) {
79 //             DecoderException err = (DecoderException) cause;
80 //             cause = err.getCause();
81 //         }
82 //         upgradePromise.tryFail(cause);
83 //     }
84 
85 //     override
86 //     void channelInactive(ChannelHandlerContext ctx) throws Exception {
87 //         super.channelInactive(ctx);
88 //         // Work around for https://github.com/eclipse-vertx/vert.x/issues/2748
89 //         upgradePromise.tryFail(new VertxException("SSL handshake failed", true));
90 //     }
91 // }