forked from darrenhinderer/telnet-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telnet_server.rb
92 lines (74 loc) · 1.9 KB
/
telnet_server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
$: << File.dirname(__FILE__) unless $:.include? File.dirname(__FILE__)
require 'rubygems'
require 'bundler'
Bundler.setup
require 'eventmachine'
require 'socket'
require 'telnet_auth'
require 'telnet_shell'
require 'ansi'
require 'connection_manager'
class TelnetServer < EventMachine::Connection
#begin connection callbacks
def post_init
send_line("Please login, to signup type \"new\" at the prompt.", :green)
@telnet_auth = TelnetAuth.new(self)
@shell = TelnetShell.new(self)
end
def receive_data(data)
data = remove_newlines(data)
if (@telnet_auth.state != :logged_in)
@telnet_auth.process_response(data)
else
@shell.process_response(data)
end
end
def disconnect
ConnectionManager.instance.remove_connection(self)
send_line("Goodbye.", :green)
close_connection_after_writing
end
def unbind
#do any kind of cleanup here
end
#end callbacks
def send_line(data, color=nil)
send_data(data, color, true)
end
def send_prompt(data, color=nil)
send_data(data, color, false)
end
def start_shell
ConnectionManager.instance.add_connection(self)
@shell.start
end
def to_s
str = @telnet_auth.username
str += " (" + ::Socket.unpack_sockaddr_in(get_peername)[1].to_s + ")"
end
private
def send_data(data, color=nil, newline=false)
if newline
data = data + "\r\n"
end
case color
when :red
super(ANSI.red(data))
when :green
super(ANSI.green(data))
else
super(data)
end
end
def remove_newlines(str)
str.strip.downcase.gsub('\r\n' , '')
end
end
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => "telnet.db"
)
EventMachine::run {
EventMachine::start_server("0.0.0.0", 8090, TelnetServer)
puts "Telnet Server Started"
}