Skip to content

Commit

Permalink
Ensure WebSocket URL is properly built for multiple use-cases.
Browse files Browse the repository at this point in the history
This fixes allinurl#2487 where passing --ws-url=wss://HOST.DOMAIN/ws/ would end up
appending the default port to the end of path. e.g., wss://HOST.DOMAIN/ws/:7890

Now it will check if the given URL contains the port else it will append it to
hostname. A few scenarios:

--ws-url=wss://HOST.DOMAIN/ws/ => wss://HOST.DOMAIN:7890/ws/
--ws-url=ws://HOST.DOMAIN/ws/  => ws(s)://HOST.DOMAIN:7890/ws/
--ws-url=HOST.DOMAIN:3843/ws/  => ws(s)://HOST.DOMAIN:3843/ws/
--ws-url=HOST.DOMAIN:3843/ws/  => ws(s)://HOST.DOMAIN:3843/ws/
                               => ws(s)://hostname:7890 or ws(s)://localhost:7890
  • Loading branch information
allinurl committed Mar 30, 2023
1 parent 2396151 commit fe22981
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions resources/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,21 @@ window.GoAccess = window.GoAccess || {
this.setWebSocket(wsConn);
},

buildWSURI: function (wsConn) {
var url = null;
if (!wsConn.url || !wsConn.port)
return null;
url = /^wss?:\/\//i.test(wsConn.url) ? wsConn.url : window.location.protocol === "https:" ? 'wss://' + wsConn.url : 'ws://' + wsConn.url;
return new URL(url).protocol + '//' + new URL(url).hostname + ':' + wsConn.port + new URL(url).pathname;
},

setWebSocket: function (wsConn) {
var host = null, pingId = null;
host = wsConn.url ? wsConn.url : window.location.hostname ? window.location.hostname : "localhost";
var str = /^(wss?:\/\/)?[^\/]+:[0-9]{1,5}\//.test(host + "/") ? host : String(host + ':' + wsConn.port);
var host = null, pingId = null, uri = null, defURI = null, str = null;

defURI = window.location.hostname ? window.location.hostname + ':' + wsConn.port : "localhost" + ':' + wsConn.port;
uri = wsConn.url && /^(wss?:\/\/)?[^\/]+:[0-9]{1,5}/.test(wsConn.url) ? wsConn.url : this.buildWSURI(wsConn);

str = uri || defURI;
str = !/^wss?:\/\//i.test(str) ? (window.location.protocol === "https:" ? 'wss://' : 'ws://') + str : str;

var socket = new WebSocket(str);
Expand Down

0 comments on commit fe22981

Please sign in to comment.