CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rex/ui/text/input/socket.rb
Views: 11704
1
# -*- coding: binary -*-
2
3
module Rex
4
module Ui
5
module Text
6
7
###
8
#
9
# This class implements input against a socket.
10
#
11
###
12
class Input::Socket < Rex::Ui::Text::Input
13
14
def initialize(sock)
15
@sock = sock
16
end
17
18
#
19
# Sockets do not currently support readline.
20
#
21
def supports_readline
22
false
23
end
24
25
#
26
# Reads input from the raw socket.
27
#
28
def sysread(len = 1)
29
@sock.sysread(len)
30
end
31
32
#
33
# Wait for a line of input to be read from a socket.
34
#
35
def gets
36
37
# Initialize the line buffer
38
line = ''
39
40
# Read data one byte at a time until we see a LF
41
while (true)
42
43
break if line.include?("\n")
44
45
# Read another character of input
46
char = @sock.getc
47
if char.nil?
48
@sock.close
49
return
50
end
51
52
# Telnet sends 0x04 as EOF
53
if (char == 4)
54
@sock.write("[*] Caught ^D, closing the socket...\n")
55
@sock.close
56
return
57
end
58
59
# Append this character to the string
60
line << char
61
62
# Handle telnet sequences
63
case line
64
when /\xff\xf4\xff\xfd\x06/n
65
@sock.write("[*] Caught ^C, closing the socket...\n")
66
@sock.close
67
return
68
69
when /\xff\xed\xff\xfd\x06/n
70
@sock.write("[*] Caught ^Z\n")
71
return
72
end
73
end
74
75
return line
76
end
77
78
#
79
# Returns whether or not EOF has been reached on stdin.
80
#
81
def eof?
82
@sock.closed?
83
end
84
85
#
86
# Returns the file descriptor associated with a socket.
87
#
88
def fd
89
return @sock
90
end
91
end
92
93
end
94
end
95
end
96
97