Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2222 from gmt2001/patch-70
Patch 70
- Loading branch information
Showing
12 changed files
with
292 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
source/com/gmt2001/httpwsserver/HttpOptionalSslHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (C) 2016-2019 phantombot.tv | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.gmt2001.httpwsserver; | ||
|
||
import io.netty.channel.ChannelHandler; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.handler.ssl.OptionalSslHandler; | ||
import io.netty.handler.ssl.SslContext; | ||
|
||
/** | ||
* Detects Non-SSL HTTP connections and activates the SSL Redirect handler, when SSL is enabled | ||
* | ||
* @author gmt2001 | ||
*/ | ||
public class HttpOptionalSslHandler extends OptionalSslHandler { | ||
|
||
/** | ||
* Default Constructor | ||
*/ | ||
HttpOptionalSslHandler(SslContext sslContext) { | ||
super(sslContext); | ||
} | ||
|
||
@Override | ||
protected ChannelHandler newNonSslHandler(ChannelHandlerContext context) { | ||
context.pipeline().replace("pagehandler", "httpsslredirect", new HttpSslRedirectHandler()); | ||
context.pipeline().replace("wshandler", "wssslerror", new WsSslErrorHandler()); | ||
return null; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
source/com/gmt2001/httpwsserver/HttpSslRedirectHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright (C) 2016-2019 phantombot.tv | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.gmt2001.httpwsserver; | ||
|
||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.SimpleChannelInboundHandler; | ||
import io.netty.handler.codec.http.FullHttpRequest; | ||
import io.netty.handler.codec.http.FullHttpResponse; | ||
import io.netty.handler.codec.http.HttpHeaderNames; | ||
import io.netty.handler.codec.http.HttpResponseStatus; | ||
import org.sqlite.SQLiteConfig; | ||
|
||
/** | ||
* Redirects HTTP requests to HTTPS, when SSL is enabled | ||
* | ||
* @author gmt2001 | ||
*/ | ||
public class HttpSslRedirectHandler extends SimpleChannelInboundHandler<FullHttpRequest> { | ||
|
||
/** | ||
* Default Constructor | ||
*/ | ||
HttpSslRedirectHandler() { | ||
super(); | ||
} | ||
|
||
/** | ||
* Redirects non-SSL requests to SSL | ||
* | ||
* @param ctx The {@link ChannelHandlerContext} of the session | ||
* @param req The {@link FullHttpRequest} containing the request | ||
* @throws Exception Passes any thrown exceptions up the stack | ||
*/ | ||
@Override | ||
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception { | ||
if (!req.decoderResult().isSuccess()) { | ||
HttpServerPageHandler.sendHttpResponse(ctx, req, HttpServerPageHandler.prepareHttpResponse(HttpResponseStatus.BAD_REQUEST, null, null)); | ||
return; | ||
} | ||
|
||
String host = req.headers().get(HttpHeaderNames.HOST); | ||
|
||
if (host != null && !host.isBlank()) { | ||
String uri = "https://" + host + req.uri(); | ||
|
||
com.gmt2001.Console.debug.println("301: " + uri); | ||
|
||
FullHttpResponse res = HttpServerPageHandler.prepareHttpResponse(HttpResponseStatus.MOVED_PERMANENTLY, null, null); | ||
|
||
res.headers().set(HttpHeaderNames.LOCATION, uri); | ||
|
||
HttpServerPageHandler.sendHttpResponse(ctx, req, res); | ||
} else { | ||
HttpServerPageHandler.sendHttpResponse(ctx, req, HttpServerPageHandler.prepareHttpResponse(HttpResponseStatus.FORBIDDEN, "HTTPS Required".getBytes(), null)); | ||
} | ||
} | ||
|
||
/** | ||
* Handles exceptions that are thrown up the stack | ||
* | ||
* @param ctx The {@link ChannelHandlerContext} of the session | ||
* @param cause The exception | ||
*/ | ||
@Override | ||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { | ||
com.gmt2001.Console.debug.printOrLogStackTrace(cause); | ||
ctx.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright (C) 2016-2019 phantombot.tv | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.gmt2001.httpwsserver; | ||
|
||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.SimpleChannelInboundHandler; | ||
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; | ||
import io.netty.handler.codec.http.websocketx.WebSocketFrame; | ||
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler.HandshakeComplete; | ||
import org.json.JSONStringer; | ||
|
||
/** | ||
* Processes WebSocket frames and passes successful ones to the appropriate registered final handler | ||
* | ||
* @author gmt2001 | ||
*/ | ||
public class WsSslErrorHandler extends SimpleChannelInboundHandler<WebSocketFrame> { | ||
|
||
WsSslErrorHandler() { | ||
super(); | ||
} | ||
|
||
/** | ||
* Handles incoming WebSocket frames and passes them to the appropriate {@link WsFrameHandler} | ||
* | ||
* @param ctx The {@link ChannelHandlerContext} of the session | ||
* @param req The {@link WebSocketFrame} containing the request frame | ||
* @throws Exception Passes any thrown exceptions up the stack | ||
*/ | ||
@Override | ||
protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception { | ||
|
||
} | ||
|
||
/** | ||
* Captures {@link HandshakeComplete} events and saves the {@link WsFrameHandler} URI to the session | ||
* | ||
* If a handler is not available for the requested path, then {@code 404 NOT FOUND} is sent back to the client using JSON:API format | ||
* | ||
* @param ctx The {@link ChannelHandlerContext} of the session | ||
* @param evt The event object | ||
* @throws Exception Passes any thrown exceptions up the stack | ||
*/ | ||
@Override | ||
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { | ||
if (evt instanceof HandshakeComplete) { | ||
JSONStringer jsonObject = new JSONStringer(); | ||
jsonObject.object().key("errors").array().object() | ||
.key("status").value("403") | ||
.key("title").value("Forbidden") | ||
.key("detail").value("WSS Required") | ||
.endObject().endArray().endObject(); | ||
|
||
ctx.channel().writeAndFlush(new TextWebSocketFrame(jsonObject.toString())); | ||
ctx.close(); | ||
} | ||
} | ||
|
||
/** | ||
* Handles exceptions that are thrown up the stack | ||
* | ||
* @param ctx The {@link ChannelHandlerContext} of the session | ||
* @param cause The exception | ||
*/ | ||
@Override | ||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { | ||
com.gmt2001.Console.debug.printOrLogStackTrace(cause); | ||
ctx.close(); | ||
} | ||
} |
Oops, something went wrong.