[connect] use haxe-crypto in ws connection
This commit is contained in:
@@ -33,13 +33,17 @@ class JsConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
|
||||
override public function connect():Promise<IConnection<O, I>> {
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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<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) {
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user