[connect] use haxe-crypto in ws connection

This commit is contained in:
2020-03-28 17:17:40 +03:00
parent ccfa8c05f8
commit 88b4459023
3 changed files with 17 additions and 29 deletions

View File

@@ -17,6 +17,7 @@
"dependencies": { "dependencies": {
"promhx": "1.1.0", "promhx": "1.1.0",
"protohx": "0.4.6", "protohx": "0.4.6",
"haxe-crypto": "0.0.7",
"yaml": "2.0.0" "yaml": "2.0.0"
} }
} }

View File

@@ -33,13 +33,17 @@ class JsConnection<O:Message, I:Message> extends BaseConnection<O, I> {
override public function connect():Promise<IConnection<O, I>> { override public function connect():Promise<IConnection<O, I>> {
var self = this; 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(); 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(); return connectDeferred.promise();
} }

View File

@@ -1,7 +1,7 @@
package hw.connect.neko; package hw.connect.neko;
import haxe.crypto.BaseCode; import com.hurlant.crypto.hash.SHA1;
import haxe.crypto.Sha1; import com.hurlant.util.Base64;
import haxe.io.Bytes; import haxe.io.Bytes;
import haxe.io.BytesBuffer; import haxe.io.BytesBuffer;
import protohx.Message; import protohx.Message;
@@ -39,33 +39,16 @@ class NekoWSConnection<O:Message, I:Message> extends NekoConnection<O, I> {
} }
} }
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) { 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" var s = "HTTP/1.1 101 Switching Protocols\r\n"
+ "Upgrade: websocket\r\n" + "Upgrade: websocket\r\n"
+ "Connection: Upgrade\r\n" + "Connection: Upgrade\r\n"
+ "Sec-WebSocket-Accept: " + outKey + "\r\n" + "Sec-WebSocket-Accept: " + outKey + "\r\n"
+ "\r\n"; + "\r\n";
socket.output.writeString(s); socket.output.writeString(s);
} }