Skip to content

Commit cf8472b

Browse files
authored
wss for websockets on tls and single page application behavior (#107)
1 parent 317a7eb commit cf8472b

File tree

5 files changed

+161
-76
lines changed

5 files changed

+161
-76
lines changed

portal-ui/bindata_assetfs.go

Lines changed: 94 additions & 71 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

portal-ui/src/screens/Console/Logs/Logs.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { createStyles, Theme, withStyles } from "@material-ui/core/styles";
2424
import { niceBytes } from "../../../common/utils";
2525
import Ansi from "ansi-to-react";
2626
import { isNull, isNullOrUndefined } from "util";
27+
import { wsProtocol } from "../../../utils/wsUtils";
2728

2829
const styles = (theme: Theme) =>
2930
createStyles({
@@ -79,7 +80,11 @@ const Logs = ({
7980
const isDev = process.env.NODE_ENV === "development";
8081
const port = isDev ? "9090" : url.port;
8182

82-
const c = new W3CWebSocket(`ws://${url.hostname}:${port}/ws/console`);
83+
const wsProt = wsProtocol(url.protocol);
84+
85+
const c = new W3CWebSocket(
86+
`${wsProt}://${url.hostname}:${port}/ws/console`
87+
);
8388

8489
let interval: any | null = null;
8590
if (c !== null) {

portal-ui/src/screens/Console/Trace/Trace.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { traceMessageReceived, traceResetMessages } from "./actions";
2222
import { TraceMessage } from "./types";
2323
import { createStyles, Theme, withStyles } from "@material-ui/core/styles";
2424
import { niceBytes } from "../../../common/utils";
25+
import { wsProtocol } from "../../../utils/wsUtils";
2526

2627
const styles = (theme: Theme) =>
2728
createStyles({
@@ -61,7 +62,8 @@ const Trace = ({
6162
const isDev = process.env.NODE_ENV === "development";
6263
const port = isDev ? "9090" : url.port;
6364

64-
const c = new W3CWebSocket(`ws://${url.hostname}:${port}/ws/trace`);
65+
const wsProt = wsProtocol(url.protocol);
66+
const c = new W3CWebSocket(`${wsProt}://${url.hostname}:${port}/ws/trace`);
6567

6668
let interval: any | null = null;
6769
if (c !== null) {

portal-ui/src/utils/wsUtils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This file is part of MinIO Console Server
2+
// Copyright (c) 2020 MinIO, Inc.
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
export const wsProtocol = (protocol: string): string => {
17+
let wsProtocol = "ws";
18+
if (protocol == "https:") {
19+
wsProtocol = "wss";
20+
}
21+
return wsProtocol;
22+
};

restapi/configure_mcs.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"github.com/minio/mcs/models"
2828
"github.com/minio/mcs/pkg/auth"
2929

30-
assetfs "github.com/elazarl/go-bindata-assetfs"
30+
assetFS "github.com/elazarl/go-bindata-assetfs"
3131

3232
portalUI "github.com/minio/mcs/portal-ui"
3333

@@ -167,11 +167,44 @@ func FileServerMiddleware(next http.Handler) http.Handler {
167167
case strings.HasPrefix(r.URL.Path, "/api"):
168168
next.ServeHTTP(w, r)
169169
default:
170-
http.FileServer(&assetfs.AssetFS{
170+
assets := assetFS.AssetFS{
171171
Asset: portalUI.Asset,
172172
AssetDir: portalUI.AssetDir,
173173
AssetInfo: portalUI.AssetInfo,
174-
Prefix: "build"}).ServeHTTP(w, r)
174+
Prefix: "build"}
175+
wrapHandlerSinglePageApplication(http.FileServer(&assets)).ServeHTTP(w, r)
176+
175177
}
176178
})
177179
}
180+
181+
type notFoundRedirectRespWr struct {
182+
http.ResponseWriter // We embed http.ResponseWriter
183+
status int
184+
}
185+
186+
func (w *notFoundRedirectRespWr) WriteHeader(status int) {
187+
w.status = status // Store the status for our own use
188+
if status != http.StatusNotFound {
189+
w.ResponseWriter.WriteHeader(status)
190+
}
191+
}
192+
193+
func (w *notFoundRedirectRespWr) Write(p []byte) (int, error) {
194+
if w.status != http.StatusNotFound {
195+
return w.ResponseWriter.Write(p)
196+
}
197+
return len(p), nil // Lie that we successfully wrote it
198+
}
199+
200+
// wrapHandlerSinglePageApplication handles a http.FileServer returning a 404 and overrides it with index.html
201+
func wrapHandlerSinglePageApplication(h http.Handler) http.HandlerFunc {
202+
return func(w http.ResponseWriter, r *http.Request) {
203+
nfrw := &notFoundRedirectRespWr{ResponseWriter: w}
204+
h.ServeHTTP(nfrw, r)
205+
if nfrw.status == 404 {
206+
log.Printf("Redirecting %s to index.html.", r.RequestURI)
207+
http.Redirect(w, r, "/index.html", http.StatusFound)
208+
}
209+
}
210+
}

0 commit comments

Comments
 (0)