gpt4 book ai didi

ruby - 如何在不回显的情况下从套接字获取输入?

转载 作者:数据小太阳 更新时间:2023-10-29 08:42:41 24 4
gpt4 key购买 nike

我正在用 Ruby 创建一个 TCP 服务器,需要从用户那里获取密码。此密码不应回显。不幸的是,当我远程登录到端口 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 时,我可以在屏幕上看到我的密码。

最佳答案

您必须向客户端发送正确的字节序列以告诉它抑制本地回显。这些在 RFC857 中有描述。 .

我浏览了 a lot of resources on how to accomplish this and it's not very直截了当。不过,这是最终起作用的:

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 应用程序。不能保证客户会兑现。

关于ruby - 如何在不回显的情况下从套接字获取输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53095631/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com