使用 node-forge 从浏览器中的按钮创建 TLS 连接?

问题描述

是否可以使用 node-forge 从浏览器中创建 TLS 连接?我基本上是想测试/验证 TLS 握手,并在浏览器中简单地输出有关它的信息。 Node-forge 似乎是一个 javascript tls 实现,但不确定是否可以在浏览器中执行我想要的操作,因为它主要似乎适用于 node。

var socket = new net.socket();
 
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();
    socket.write(data,'binary'); // encoding should be 'binary'
  },dataReady: function(connection) {
    // clear data from the server is ready
    var data = connection.data.getBytes();
    console.log('[tls] data received from the server: ' + data);
  },closed: function() {
    console.log('[tls] disconnected');
  },error: function(connection,error) {
    console.log('[tls] error',error);
  }
});
 
socket.on('connect',function() {
  console.log('[socket] connected');
  client.handshake();
});
socket.on('data',function(data) {
  client.process(data.toString('binary')); // encoding should be 'binary'
});
socket.on('end',function() {
  console.log('[socket] disconnected');
});
 
// connect to google.com
socket.connect(443,'google.com');
 
// or connect to gmail's imap server (but don't send the HTTP header above)
//socket.connect(993,'imap.gmail.com');

这个示例代码片段使用的是 net.sockets,但它可以在浏览器中使用吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)