wss for websockets on tls and single page application behavior (#107)

This commit is contained in:
Daniel Valdivia
2020-05-08 16:36:08 -07:00
committed by GitHub
parent 317a7ebbd3
commit cf8472b04c
5 changed files with 161 additions and 76 deletions

File diff suppressed because one or more lines are too long

View File

@@ -24,6 +24,7 @@ import { createStyles, Theme, withStyles } from "@material-ui/core/styles";
import { niceBytes } from "../../../common/utils";
import Ansi from "ansi-to-react";
import { isNull, isNullOrUndefined } from "util";
import { wsProtocol } from "../../../utils/wsUtils";
const styles = (theme: Theme) =>
createStyles({
@@ -79,7 +80,11 @@ const Logs = ({
const isDev = process.env.NODE_ENV === "development";
const port = isDev ? "9090" : url.port;
const c = new W3CWebSocket(`ws://${url.hostname}:${port}/ws/console`);
const wsProt = wsProtocol(url.protocol);
const c = new W3CWebSocket(
`${wsProt}://${url.hostname}:${port}/ws/console`
);
let interval: any | null = null;
if (c !== null) {

View File

@@ -22,6 +22,7 @@ import { traceMessageReceived, traceResetMessages } from "./actions";
import { TraceMessage } from "./types";
import { createStyles, Theme, withStyles } from "@material-ui/core/styles";
import { niceBytes } from "../../../common/utils";
import { wsProtocol } from "../../../utils/wsUtils";
const styles = (theme: Theme) =>
createStyles({
@@ -61,7 +62,8 @@ const Trace = ({
const isDev = process.env.NODE_ENV === "development";
const port = isDev ? "9090" : url.port;
const c = new W3CWebSocket(`ws://${url.hostname}:${port}/ws/trace`);
const wsProt = wsProtocol(url.protocol);
const c = new W3CWebSocket(`${wsProt}://${url.hostname}:${port}/ws/trace`);
let interval: any | null = null;
if (c !== null) {

View File

@@ -0,0 +1,22 @@
// This file is part of MinIO Console Server
// Copyright (c) 2020 MinIO, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
export const wsProtocol = (protocol: string): string => {
let wsProtocol = "ws";
if (protocol == "https:") {
wsProtocol = "wss";
}
return wsProtocol;
};

View File

@@ -27,7 +27,7 @@ import (
"github.com/minio/mcs/models"
"github.com/minio/mcs/pkg/auth"
assetfs "github.com/elazarl/go-bindata-assetfs"
assetFS "github.com/elazarl/go-bindata-assetfs"
portalUI "github.com/minio/mcs/portal-ui"
@@ -167,11 +167,44 @@ func FileServerMiddleware(next http.Handler) http.Handler {
case strings.HasPrefix(r.URL.Path, "/api"):
next.ServeHTTP(w, r)
default:
http.FileServer(&assetfs.AssetFS{
assets := assetFS.AssetFS{
Asset: portalUI.Asset,
AssetDir: portalUI.AssetDir,
AssetInfo: portalUI.AssetInfo,
Prefix: "build"}).ServeHTTP(w, r)
Prefix: "build"}
wrapHandlerSinglePageApplication(http.FileServer(&assets)).ServeHTTP(w, r)
}
})
}
type notFoundRedirectRespWr struct {
http.ResponseWriter // We embed http.ResponseWriter
status int
}
func (w *notFoundRedirectRespWr) WriteHeader(status int) {
w.status = status // Store the status for our own use
if status != http.StatusNotFound {
w.ResponseWriter.WriteHeader(status)
}
}
func (w *notFoundRedirectRespWr) Write(p []byte) (int, error) {
if w.status != http.StatusNotFound {
return w.ResponseWriter.Write(p)
}
return len(p), nil // Lie that we successfully wrote it
}
// wrapHandlerSinglePageApplication handles a http.FileServer returning a 404 and overrides it with index.html
func wrapHandlerSinglePageApplication(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
nfrw := &notFoundRedirectRespWr{ResponseWriter: w}
h.ServeHTTP(nfrw, r)
if nfrw.status == 404 {
log.Printf("Redirecting %s to index.html.", r.RequestURI)
http.Redirect(w, r, "/index.html", http.StatusFound)
}
}
}