From: Matteo Nastasi Date: Mon, 26 Dec 2016 13:59:11 +0000 (+0000) Subject: add websocketsec(ure) as transport layer X-Git-Tag: v5.6.0~39 X-Git-Url: https://mop.ddnsfree.com/gitweb/?p=brisk.git;a=commitdiff_plain;h=fc19a0f063cf2e544a61a58b4ac31254f91d6ba5 add websocketsec(ure) as transport layer --- diff --git a/INSTALL.sh b/INSTALL.sh index ee329b0..a161720 100755 --- a/INSTALL.sh +++ b/INSTALL.sh @@ -368,6 +368,10 @@ done for i in $(find web -name '.htaccess' -o -name '*.php' -o -name '*.phh' -o -name '*.pho' -o -name '*.css' -o -name '*.js' -o -name '*.mp3' -o -name '*.swf' -o -name 'terms-of-service*' | sed 's/^....//g'); do install -m 644 "web/$i" "${web_path}__/$i" done + +# hardlink for nginx managed websocket files. +ln "${web_path}__/xynt_test01.php" "${web_path}__/xynt_test01_wss.php" + if [ "$test_add" = "TRUE" ]; then for i in $(find webtest -name '.htaccess' -o -name '*.php' -o -name '*.phh' -o -name '*.pho' -o -name '*.css' -o -name '*.js' -o -name '*.mp3' -o -name '*.swf' -o -name 'terms-of-service*' | sed 's/^........//g'); do install -m 644 "webtest/$i" "${web_path}__/$i" diff --git a/web/Obj/transports.phh b/web/Obj/transports.phh index 5f269d7..69fe089 100644 --- a/web/Obj/transports.phh +++ b/web/Obj/transports.phh @@ -93,7 +93,7 @@ class Transport_template { class Transport_websocket { protected $magicGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; - function Transport_websocket() { + function Transport_websocket($secure = FALSE) { $this->headerOriginRequired = false; $this->headerSecWebSocketProtocolRequired = false; $this->headerSecWebSocketExtensionsRequired = false; @@ -386,15 +386,19 @@ class Transport_websocket { } if (!isset($headers['Host']) || !$this->checkHost($headers['Host'])) { + // error_log('bad 1'); $headers_out['HTTP-Response'] = "400 Bad Request"; } if (!isset($headers['Upgrade']) || strtolower($headers['Upgrade']) != 'websocket') { + // error_log('bad 2 ' . $headers['Upgrade']); $headers_out['HTTP-Response'] = "400 Bad Request"; } if (!isset($headers['Connection']) || strpos(strtolower($headers['Connection']), 'upgrade') === FALSE) { + // error_log('bad 3'); $headers_out['HTTP-Response'] = "400 Bad Request"; } if (!isset($headers['Sec-Websocket-Key'])) { + // error_log('bad 4'); $headers_out['HTTP-Response'] = "400 Bad Request"; } else { } @@ -410,11 +414,13 @@ class Transport_websocket { if ( ($this->headerSecWebSocketProtocolRequired && !isset($headers['Sec-Websocket-Protocol'])) || ($this->headerSecWebSocketProtocolRequired && !$this->checkWebsocProtocol($headers['Sec-Websocket-Protocol']))) { + // error_log('bad 5'); $headers_out['HTTP-Response'] = "400 Bad Request"; } if ( ($this->headerSecWebSocketExtensionsRequired && !isset($headers['Sec-Websocket-Extensions'])) || ($this->headerSecWebSocketExtensionsRequired && !$this->checkWebsocExtensions($headers['Sec-Websocket-Extensions'])) ) { + // error_log('bad 6'); $headers_out['HTTP-Response'] = "400 Bad Request"; } diff --git a/web/commons.js b/web/commons.js index 7dcf922..8f3253c 100644 --- a/web/commons.js +++ b/web/commons.js @@ -1393,7 +1393,8 @@ function url_complete(parent, url) } // alert("host: ["+host+"] path: ["+path+"]"); - if (url.substring(0,6) == 'http:/' || url.substring(0,7) == 'https:/' || url.substring(0,4) == 'ws:/') { + console.log('URL: ' + url); + if (url.substring(0,6) == 'http:/' || url.substring(0,7) == 'https:/' || url.substring(0,4) == 'ws:/' || url.substring(0,5) == 'wss:/') { return (url); } else if (url.substring(0,1) == '/') { diff --git a/web/xynt-streaming.js b/web/xynt-streaming.js index 610d701..788a783 100644 --- a/web/xynt-streaming.js +++ b/web/xynt-streaming.js @@ -5,7 +5,16 @@ // function transport_ws(doc, xynt_streaming, page) { - this.name = "WebSocket"; + // if four arguments manage if WS or WSS connection + if (arguments.length > 3) + this.is_secure = arguments[3]; + else + this.is_secure = false; + + if (this.is_secure) + this.name = "WebSocketSecure"; + else + this.name = "WebSocket"; this.ctx_new = ""; var self = this; @@ -13,7 +22,7 @@ function transport_ws(doc, xynt_streaming, page) this.failed = false; this.xynt_streaming = xynt_streaming; try { -this.xynt_streaming.log("PAGE: "+page); + this.xynt_streaming.log("PAGE: "+page); this.ws = new WebSocket(page); this.ws.onopen = function () { self.xynt_streaming.log("onopen"); @@ -631,15 +640,24 @@ xynt_streaming.prototype = { transp_port = this.transp_port; } - if (transp_type == "websocket") { - var end_proto, first_slash; + if (transp_type == "websocket" || transp_type == "websocketsec") { + var end_proto, first_slash, newpage; // change protocol this.log("precha ["+this.page+"]"); - end_proto = this.page.indexOf("://"); - first_slash = this.page.substring(end_proto+3).indexOf("/"); + if (transp_type == "websocketsec") { + newpage = this.page.replace(/\.php$/g, "_wss.php").replace(/\.php\?/g, "_wss.php?"); + } + else { + newpage = this.page; + } + end_proto = newpage.indexOf("://"); + first_slash = newpage.substring(end_proto+3).indexOf("/"); - page = "ws://" + this.page.substring(end_proto+3, end_proto+3+first_slash) + ":" + transp_port + this.page.substring(end_proto+3 + first_slash); + page = (transp_type == "websocketsec" ? "wss://" : "ws://") + + newpage.substring(end_proto+3, end_proto+3 + first_slash) + ":" + + transp_port + newpage.substring(end_proto+3 + first_slash); + // this.log("MOP WS: " + page); } else { page = this.page; @@ -652,7 +670,11 @@ xynt_streaming.prototype = { try { // transport instantiation - if (transp_type == "websocket") { + if (transp_type == "websocketsec") { + page = url_append_args(page, "transp", "websocketsec"); + this.transp = new transport_ws(this.doc, this, page, true); + } + else if (transp_type == "websocket") { page = url_append_args(page, "transp", "websocket"); this.transp = new transport_ws(this.doc, this, page); } diff --git a/web/xynt_test01.php b/web/xynt_test01.php index 01adb53..4c1c43b 100644 --- a/web/xynt_test01.php +++ b/web/xynt_test01.php @@ -1,4 +1,10 @@ 80, "websocket"=>80, "websocketsec"=>443, "xhr"=>80, "htmlfile"=>80 ); if (!isset($f_trans)) $f_trans = $transs[0]; @@ -70,7 +77,7 @@ if (!isset($f_test)) $f_test = 1; if (!isset($f_port)) - $f_port = 80; + $f_port = NULL; if (!isset($f_fback)) $f_fback = 0; @@ -102,12 +109,14 @@ function xcape($s) } if (isset($isstream) && $isstream == "true") { - require_once("Obj/transports.phh"); if (isset($transp) && $transp == "websocket") { $trobj = new Transport_websocket(); } + else if (isset($transp) && $transp == "websocketsec") { + $trobj = new Transport_websocket(TRUE); + } else if (isset($transp) && $transp == "xhr") { $trobj = new Transport_xhr(); } @@ -127,6 +136,7 @@ if (isset($isstream) && $isstream == "true") { $init_string .= chr(mt_rand(65, 90)); } $headers_in = getallheaders(); + // error_log(print_r($headers_in, TRUE)); $headers = array(); foreach ($headers_in as $header_in => $value) { $headers[mb_convert_case($header_in, MB_CASE_TITLE, 'UTF-8')] = $value; @@ -149,7 +159,7 @@ if (isset($isstream) && $isstream == "true") { fclose($fp); } - if (isset($transp) && $transp == "websocket") { + if (isset($transp) && ($transp == "websocket" || $transp == "websocketsec")) { header_remove('Connection'); header_remove('Content-Encoding'); header_remove('Content-Type'); @@ -290,7 +300,8 @@ if (isset($isstream) && $isstream == "true") { var gst = new globst(); window.onload = function() { - xstm = new xynt_streaming(window, "", , , console, gst, 'xynt_test01_php', 'sess', sess, null, 'xynt_test01.php?isstream=true&f_test=', function(com){eval(com);}); + xstm = new xynt_streaming(window, "", , , console, gst, 'xynt_test01_php', 'sess', sess, null, 'xynt_test01.php?isstream=true&f_test=', function(com){eval(com);}); xstm.hbit_set(heartbit); xstm.start(); } @@ -301,13 +312,13 @@ if (isset($isstream) && $isstream == "true") {
"); for ($test = 1 ; $test <= count($desc) ; $test++) { printf(""); foreach ($transs as $trans) { - printf("Test %s %02d (port %d (fb %d))", $trans, $test, $f_port, $f_fback, $trans, $test, $f_port, $f_fback); + printf("Test %s %02d (port %d (fb %d))", $trans, $test, + ($f_port == NULL ? "" : sprintf("&f_port=%d", $f_port)), $f_fback, $trans, $test, + ($f_port == NULL ? $trans_ports[$trans] : $f_port ), $f_fback); } printf("\n"); }