diff --git a/haxelib.json b/haxelib.json index c24d402..b1cba17 100755 --- a/haxelib.json +++ b/haxelib.json @@ -17,6 +17,7 @@ "dependencies": { "promhx": "1.1.0", "protohx": "0.4.6", + "haxe-crypto": "0.0.7", "yaml": "2.0.0" } } diff --git a/src/main/hw/connect/js/JsConnection.hx b/src/main/hw/connect/js/JsConnection.hx index d2dd598..866c2d2 100644 --- a/src/main/hw/connect/js/JsConnection.hx +++ b/src/main/hw/connect/js/JsConnection.hx @@ -33,13 +33,17 @@ class JsConnection extends BaseConnection { override public function connect():Promise> { var self = this; - socket = buildSocket(host, port); - socket.binaryType = BinaryType.ARRAYBUFFER; - socket.onopen = this.onConnect; - socket.onclose = this.onClose; - socket.onerror = this.onError; - socket.onmessage = this.onSocketData; connectDeferred = new Deferred(); + try { + socket = buildSocket(host, port); + socket.binaryType = BinaryType.ARRAYBUFFER; + socket.onopen = this.onConnect; + socket.onclose = this.onClose; + socket.onerror = this.onError; + socket.onmessage = this.onSocketData; + } catch (error:Dynamic) { + connectDeferred.throwError(error); + } return connectDeferred.promise(); } diff --git a/src/main/hw/connect/neko/NekoWSConnection.hx b/src/main/hw/connect/neko/NekoWSConnection.hx index 76885f1..733f429 100644 --- a/src/main/hw/connect/neko/NekoWSConnection.hx +++ b/src/main/hw/connect/neko/NekoWSConnection.hx @@ -1,7 +1,7 @@ package hw.connect.neko; -import haxe.crypto.BaseCode; -import haxe.crypto.Sha1; +import com.hurlant.crypto.hash.SHA1; +import com.hurlant.util.Base64; import haxe.io.Bytes; import haxe.io.BytesBuffer; import protohx.Message; @@ -39,33 +39,16 @@ class NekoWSConnection extends NekoConnection { } } - private function encodeBase64(content:String):String { - var suffix = switch (content.length % 3) - { - case 2: "="; - case 1: "=="; - default: ""; - }; - return BaseCode.encode(content, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/") + suffix; - } - - private function hex2data(hex:String):String { - var data = ""; - for (i in 0...Std.int(hex.length / 2)) { - data += String.fromCharCode(Std.parseInt("0x" + hex.substr(i * 2, 2))); - } - return data; - } - private function sendServerHandShake(socket:sys.net.Socket, inpKey:String) { - var outKey = encodeBase64(hex2data(Sha1.encode(StringTools.trim(inpKey) + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"))); - + var magicKey = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; + var value:String = StringTools.trim(inpKey) + magicKey; + var hash = new SHA1().hash(Bytes.ofString(value)); + var outKey = Base64.encodeByteArray(hash); var s = "HTTP/1.1 101 Switching Protocols\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + "Sec-WebSocket-Accept: " + outKey + "\r\n" + "\r\n"; - socket.output.writeString(s); }