Skip to content

Commit

Permalink
wss for websockets on tls and single page application behavior (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvaldivia authored May 8, 2020
1 parent 317a7eb commit cf8472b
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 76 deletions.
165 changes: 94 additions & 71 deletions portal-ui/bindata_assetfs.go

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion portal-ui/src/screens/Console/Logs/Logs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion portal-ui/src/screens/Console/Trace/Trace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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) {
Expand Down
22 changes: 22 additions & 0 deletions portal-ui/src/utils/wsUtils.ts
Original file line number Diff line number Diff line change
@@ -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;
};
39 changes: 36 additions & 3 deletions restapi/configure_mcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit cf8472b

Please sign in to comment.