我正在Ruby中创建一个TCP Server,需要从用户那里获取密码。此密码不应回显。不幸的是,当我远程登录到8080端口并键入:我的密码已回显。
@server_socket = TCPServer.open(8080, localhost)
conn = @server_socket.accept
conn.write "Password: "
conn.gets.chomp # <-- This should not be visible
当我键入telnet localhost 8080时,我可以在屏幕上看到密码。
答案 0 :(得分:3)
您必须将正确的字节序列发送给客户端,以告诉它抑制本地回显。这些在RFC857中进行了描述。
我浏览了a lot of resources on how to accomplish {{3} } this and it's not很简单。最终,这是可行的:
require 'socket'
server = TCPServer.open(2000)
client = server.accept
# Send the IAC DONT ECHO byte sequence
client.write "#{255.chr}#{254.chr}#{1.chr}"
# Send the IAC WILL ECHO byte sequence
client.write "#{255.chr}#{251.chr}#{1.chr}"
client.write 'Enter any text and it will not echo locally: '
text = client.gets.chomp
client.write "\r\n"
client.write "The text entered while echo was suppressed was #{text}\r\n"
# Send the IAC DO ECHO byte sequence
client.write "#{255.chr}#{253.chr}#{1.chr}"
# Send the IAC WONT ECHO byte sequence
client.write "#{255.chr}#{252.chr}#{1.chr}"
client.write 'Local echo has been re-enabled, enter any text: '
client.gets.chomp
client.close
telnet客户端中的输出为:
telnet localhost 2000
Trying ::1...
Connected to localhost.
Escape character is '^]'.
Enter any text and it will not echo locally:
The text entered while echo was suppressed was foo
Local echo has been re-enabled, enter any text: bar
Connection closed by foreign host.
此解决方案取决于支持这些模式并在请求时尊重它们的客户端telnet应用程序。无法保证客户会尊重他们。