Last active
August 29, 2015 14:07
-
-
Save matbee-eth/c7e57c84a94c3192ce63 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ab2str(buf) { | |
return String.fromCharCode.apply(null, new Uint8Array(buf)); | |
} | |
function str2ab(str) { | |
var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char | |
var bufView = new Uint8Array(buf); | |
for (var i=0, strLen=str.length; i<strLen; i++) { | |
bufView[i] = str.charCodeAt(i); | |
} | |
return buf; | |
} | |
String.prototype.getBytes = function () { | |
var bytes = []; | |
for (var i = 0; i < this.length; ++i) { | |
bytes.push(this.charCodeAt(i)); | |
} | |
return bytes; | |
}; | |
var client = forge.tls.createConnection({ | |
server: false, | |
verify: function(connection, verified, depth, certs) { | |
// skip verification for testing | |
console.log('[tls] server certificate verified'); | |
return true; | |
}, | |
connected: function(connection) { | |
console.log('[tls] connected'); | |
// prepare some data to send (note that the string is interpreted as | |
// 'binary' encoded, which works for HTTP which only uses ASCII, use | |
// forge.util.encodeUtf8(str) otherwise | |
// client.prepare('GET / HTTP/1.0\r\n\r\n'); | |
}, | |
tlsDataReady: function(connection) { | |
// encrypted data is ready to be sent to the server | |
var data = connection.tlsData.getBytes(); | |
var datab = str2ab(data); | |
console.log("tlsDataReady", data, datab); | |
socket.send(datab, function(writeInfo) { | |
}); | |
}, | |
dataReady: function(connection) { | |
// clear data from the server is ready | |
var data = connection.data.getBytes(); | |
console.log('[tls] data received from the server: ', data, ab2str(data)); | |
}, | |
closed: function() { | |
console.log('[tls] disconnected'); | |
}, | |
error: function(connection, error) { | |
console.log('[tls] error', error); | |
} | |
}); | |
var HOST = "192.168.1.111"; | |
var PORT = 8009; | |
// var HOST = "google.com"; | |
// var PORT = 443; | |
var socket; | |
chrome.socketsExtended.tcp.open({}, function (_socket) { | |
socket = _socket; | |
socket.on("receive", function (msg) { | |
console.log("receive", msg); | |
client.process(ab2str(msg.data)); | |
}); | |
socket.connect(HOST, PORT, function(result) { | |
if (result < 0) { | |
console.log('[socket] disconnected'); | |
} else { | |
console.log('[socket] connected'); | |
client.handshake(); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
You don't need 2 bytes for each char. The string's internal implementation will use two bytes to store each character in a binary string, but each character code is < 256 and when converting to a Uint8Array the number of bytes will match the number of characters.