Class: NetRat
- Inherits:
-
Object
- Object
- NetRat
- Defined in:
- lib/netrat.rb
Instance Attribute Summary collapse
-
#ip ⇒ Object
readonly
Returns the value of attribute ip.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
Instance Method Summary collapse
-
#initialize(port, ip = nil) ⇒ NetRat
constructor
A new instance of NetRat.
- #listen(options = {}) ⇒ Object (also: #connect)
Constructor Details
#initialize(port, ip = nil) ⇒ NetRat
Returns a new instance of NetRat.
8 9 10 11 12 |
# File 'lib/netrat.rb', line 8 def initialize(port, ip = nil) @ip = ip @port = port @socket = nil end |
Instance Attribute Details
#ip ⇒ Object (readonly)
Returns the value of attribute ip.
4 5 6 |
# File 'lib/netrat.rb', line 4 def ip @ip end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
5 6 7 |
# File 'lib/netrat.rb', line 5 def port @port end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
6 7 8 |
# File 'lib/netrat.rb', line 6 def socket @socket end |
Instance Method Details
#listen(options = {}) ⇒ Object Also known as: connect
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/netrat.rb', line 14 def listen( = {}) [:keepalive] ||= false trap("SIGINT") do puts return end if (@ip.nil?) server = TCPServer.new(@port) server.listen(1) @socket = server.accept else @socket = TCPSocket.open(@ip, @port) end loop do if (IO.select([], [], [@socket, STDIN], 0)) server.close if (server) @socket.close if (@socket) return end begin while(!(data = @socket.recv_nonblock(100)).empty?) STDOUT.write(data); end break if (![:keepalive]) rescue Errno::ECONNRESET break if (![:keepalive]) rescue Errno::EAGAIN end begin while (!(data = STDIN.read_nonblock(100)).empty?) @socket.write(data); end break if (![:keepalive]) rescue Errno::EAGAIN rescue EOFError break if (![:keepalive]) end IO.select( [@socket, STDIN], [@socket, STDIN], [@socket, STDIN] ) end ensure server.close if (server) @socket.close if (@socket) end |