Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(new TLSSocket(new net.Socket())).connect() fails silently. #3963

Closed
Havvy opened this issue Nov 22, 2015 · 13 comments
Closed

(new TLSSocket(new net.Socket())).connect() fails silently. #3963

Havvy opened this issue Nov 22, 2015 · 13 comments
Labels
doc Issues and PRs related to the documentations. good first issue Issues that are suitable for first-time contributors. tls Issues and PRs related to the tls subsystem.

Comments

@Havvy
Copy link
Contributor

Havvy commented Nov 22, 2015

(EDIT by @Trott: Turns out this is a documentation bug. Labeling good-first-contribution and doc.)

"use strict";

const NetSocket = require("net").Socket;
const TlsSocket = require("tls").TLSSocket;

const tlsSocket = new TlsSocket(new NetSocket());

tlsSocket.once("connect", function doStartup () {
    console.log("The tls socket connected. Yay!");
});

tlsSocket.connect({port: 6697, host: "irc.freenode.net"});
console.log("Sent connect.");

The program ends immediately after the connect is called, telling me that the connection isn't started.

Note that if we don't wrap the net.Socket in a TLSSocket, then the connect works as expected.

@indutny indutny added the tls Issues and PRs related to the tls subsystem. label Nov 22, 2015
@indutny
Copy link
Member

indutny commented Nov 22, 2015

Thanks for submitting this. I had some time to think since our last conversation, and it looks like this is not the way it should be used. Sorry for not figuring it out in first place!

Could you please try using just new TlsSocket() instead? It seems to be working for me.

@indutny
Copy link
Member

indutny commented Nov 22, 2015

I guess this issue may be treated as a documentation bug, because it is not completely clear how TLSSocket should be used.

@Havvy
Copy link
Contributor Author

Havvy commented Nov 22, 2015

Is the usage for upgrading a net.Socket to a TLSSocket after it's already started?

I'm looking at the code, and it does specifically take a net.Socket and wraps it and uses the wrapped handle.

Trying const tlsSocket = new TlsSocket(); I get an error message:

_tls_wrap.js:314
    handle = options.pipe ? new Pipe() : new TCP();
                    ^

TypeError: Cannot read property 'pipe' of undefined
    at TLSSocket._wrapHandle (_tls_wrap.js:314:21)
    at new TLSSocket (_tls_wrap.js:256:18)
    at Object.<anonymous> (/home/havvy/workspace/test/tls_wrap.js:10:19)
    at Module._compile (module.js:434:26)
    at Object.Module._extensions..js (module.js:452:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:475:10)
    at startup (node.js:118:18)
    at node.js:952:3

Trying const tlsSocket = new TlsSocket(undefined, {isServer: false, host: "irc.freenode.net", port: 6697});, it does succeed at connecting.


As per @mscdex, I also tested const tlsSocket = require("tls").connect({socket: new NetSocket(), isServer: false, host: "irc.freenode.net", port: 6667}) which also fails silently.

That said, const tlsSocket = require("tls").connect({isServer: false, host: "irc.freenode.net", port: 6697}) does work (removing the "socket" property from the config object).

@mscdex
Copy link
Contributor

mscdex commented Nov 22, 2015

@Havvy No, I meant something like this:

var Socket = require('net').Socket;
var tls = require('tls');
var sock = new Socket();
var secureSock = tls.connect({ socket: s }, function() {
  console.log("The tls socket connected. Yay!");
});
sock.connect({port: 6697, host: "irc.freenode.net"});

That's typically how you upgrade an existing socket, but if you're using TLS from the start, then just use tls.connect():

var tls = require('tls');
var secureSock = tls.connect({port: 6697, host: "irc.freenode.net"}, function() {
  console.log("The tls socket connected. Yay!");
  secureSock.write(...);
});

@Havvy
Copy link
Contributor Author

Havvy commented Nov 22, 2015

Ah, okay. So if you pass in a net.Socket, you start the net.Socket.

That's the missing piece of information.

So, based on that...

The TLSSocket constructor documentation should be updated to point that out.

Should calling TLSSocket.connect() throw an error if there's a wrapped net socket?

@tflanagan
Copy link
Contributor

@Havvy, mind submitting a PR for that doc?

@Trott Trott added doc Issues and PRs related to the documentations. good first issue Issues that are suitable for first-time contributors. labels Jun 7, 2016
minervapanda added a commit to minervapanda/node that referenced this issue Oct 11, 2016
minervapanda added a commit to minervapanda/node that referenced this issue Nov 7, 2016
@VerteDinde
Copy link
Contributor

Hi all: Was this resolved in minervapanda's commit? If not, I'm happy to tackle it.

@TimothyGu
Copy link
Member

@VerteDinde no it wasn't. That commit never made it to the official repo.

@VerteDinde
Copy link
Contributor

@TimothyGu Cool, I'll make a PR now. :)

VerteDinde added a commit to VerteDinde/node that referenced this issue Jul 3, 2017
@VerteDinde
Copy link
Contributor

@TimothyGu PR submitted! Please let me know if I need to make any changes and thanks for all of the work that you do. ✨

@nikshepsvn
Copy link

Is this still open?

@joyeecheung
Copy link
Member

@nikshepsvn Judging from #14062 I think this should be closed now.

@gibfahn
Copy link
Member

gibfahn commented Oct 9, 2017

I think this can be closed, following discussion in #14062 (starting at #14062 (review)).

FWIW #14062 (comment) contains a long list of documentation things that are probably good first contributions if they're still applicable. @sam-github might be worth putting that into a separate issue.

EDIT: Didn't see @joyeecheung 's comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. good first issue Issues that are suitable for first-time contributors. tls Issues and PRs related to the tls subsystem.
Projects
None yet
Development

No branches or pull requests

10 participants