1

I'm currently having an issue with replacing a user input without them pressing the "return/enter" key on their keyboard. This is to allow for entering passwords without them directly showing in a terminal window.

Example for the code below, when using telnet to connect to the tcp server, I'm expecting the console to log every each key the user presses and so whenever the user inputs a key, I am able to refresh the console and replace the key with a *, or even for the users chat input to not show in their terminal(only viewable when I print it out).

By receiving user input via character mode, this would allow my server to acknowledge when the user sends in an input.

The issue with the code below is that the socket.on('data') only receives data when the user presses the return/enter key.

Here's a minimal reproducible snippet:

const Net = require('net');

var server = Net.Server()

server.on('connection', async(socket) => {
  socket.on('data', async data => {
    console.log(data)
  });
  socket.write('Password: ')
})

server.listen(3000, function() {
   console.log('server is listening in 127.0.0.1:3000');
});

I've had some possible solutions(and solutions that I have tried) which may help the community in answering my question:

• Doing MODE CHARACTER in telnet, this isn't really what I was looking for.

• Some form of a function allows for user input but disables the client side from displaying the string in the terminal window and replaces the user inputs with *.

• Some form of a function which allows the server to acknowledge when there is a change in the string that the user as entered and clearing the terminal window, while replacing the user string with *.

• Completely disabling the user interface in showing the users input and only allowing the server side to view the inputted string(this wouldn't allow the user to see the length of their password).

Jeffplays2005
  • 172
  • 15

2 Answers2

1

Here are some workarounds that you can do (kindly check them all):

1) Add the keyword await inside of the async function:

  socket.on('data', async data => {
    await console.log(data)
  });

2) Move socket.write to the beginning of the function and add in the end of "Password: " \r\n

server.on('connection', async(socket) => {
  socket.write('Password: \r\n');
  socket.on('data', async data => {
    await console.log(data)
  });
})

3) Last but not least, you're presumably doing something in your implementation that should not be done and which is calling socket.write from inside the socket.on("connection". After doing some research, it turns out that we usually seperate the code in 2 files (or 2 distincts parts): one for the server side and another for the client side. From the server we keep listening to incoming connections (socket.on("connection") and from the client is done the socket.write. For more detailed info about this one, kindly check the answer to this question

underflow
  • 1,545
  • 1
  • 5
  • 19
  • I've tried running method one and can definitely give you an answer where that is not possible to be the solution as the telnet server is only receiving lines rather than characters and therefore awaiting for a log doesn't work. Method 2 would also fail as moving the event doesn't do anything different. Method 3 also doesn't work as refer to why 1) won't work. – Jeffplays2005 Mar 08 '23 at 09:10
  • Just an addition to the 2 linked posts, they both won't work either as those are a client in js connecting to a telnet server in js which is different to my post. – Jeffplays2005 Mar 08 '23 at 09:11
  • Sorry for no bounty as I just think there wasn't too much effort into this answer as there was clearly no link between the answer and my question. The answer is unrelated to the proposed solutions in the question and in the bounty. – Jeffplays2005 Mar 08 '23 at 09:15
  • 1
    Don't worry i'm not seeking any bounty. Are you sure that you tried the `\r\n`? – underflow Mar 08 '23 at 09:48
  • 1
    100% and also that isn't really what I'm looking for as I'm telnetting the TCP server via default MacOS telnet and not through node net. I guess I'll just award you for the effort on posting that comment ^ haha. – Jeffplays2005 Mar 08 '23 at 10:33
  • 1
    This is amazing, thank you! Hope you resolve it the soonest – underflow Mar 08 '23 at 11:40
0

you can receive character mode input by setting the readableEncoding option to utf8 and listening for the 'readable' event.

// Set the readable encoding to utf8

 socket.setEncoding('utf8');
SHresTho12
  • 24
  • 6
  • This doesn't help my original question as by setting the encoding to utf8, it only sends data as utf8 instead of a buffer. – Jeffplays2005 Mar 02 '23 at 11:03