Class: MailDiode::Server
- Inherits:
-
GServer
- Object
- GServer
- MailDiode::Server
- Defined in:
- lib/server.rb
Constant Summary collapse
- DEFAULT_PORT =
10025
- DEFAULT_MAX_CONNECTIONS =
20
- DEFAULT_MAX_RECIPIENTS =
5
- DEFAULT_TIMEOUT_SECONDS =
djb recommends 1200
120
Instance Attribute Summary collapse
-
#ip ⇒ Object
readonly
Returns the value of attribute ip.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
- #client_id(client) ⇒ Object
- #connecting(client) ⇒ Object
- #consume_all_input(input) ⇒ Object
- #discard_early_input(client) ⇒ Object
- #disconnecting(client_port) ⇒ Object
- #get_line(input, timeout_seconds) ⇒ Object
-
#initialize(engine_factory, settings) ⇒ Server
constructor
A new instance of Server.
- #load_settings(settings) ⇒ Object
- #response(text) ⇒ Object
- #serve(client) ⇒ Object
Constructor Details
#initialize(engine_factory, settings) ⇒ Server
Returns a new instance of Server.
26 27 28 29 30 31 32 |
# File 'lib/server.rb', line 26 def initialize(engine_factory, settings) load_settings(settings) @factory = engine_factory super(@port, @ip, @max_connections) @audit = true end |
Instance Attribute Details
#ip ⇒ Object (readonly)
Returns the value of attribute ip.
23 24 25 |
# File 'lib/server.rb', line 23 def ip @ip end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
24 25 26 |
# File 'lib/server.rb', line 24 def port @port end |
Instance Method Details
#client_id(client) ⇒ Object
120 121 122 123 |
# File 'lib/server.rb', line 120 def client_id(client) addr = client.peeraddr return "#{addr[1]}" end |
#connecting(client) ⇒ Object
78 79 80 81 |
# File 'lib/server.rb', line 78 def connecting(client) MailDiode::log_debug("##{client_id(client)}: connecting #{client.peeraddr[2]} -> #{client.peeraddr[3]}") true end |
#consume_all_input(input) ⇒ Object
99 100 101 102 103 |
# File 'lib/server.rb', line 99 def consume_all_input(input) while(input.ready?) input.getc end end |
#discard_early_input(client) ⇒ Object
94 95 96 97 |
# File 'lib/server.rb', line 94 def discard_early_input(client) t = Thread.new { consume_all_input(client) } t.join(@timeout_seconds) end |
#disconnecting(client_port) ⇒ Object
83 84 85 |
# File 'lib/server.rb', line 83 def disconnecting(client_port) MailDiode::log_debug("##{client_port}: disconnecting (#{connections})") end |
#get_line(input, timeout_seconds) ⇒ Object
87 88 89 90 91 92 |
# File 'lib/server.rb', line 87 def get_line(input, timeout_seconds) line = nil t = Thread.new { line = input.gets(NEWLINE) } t.join(timeout_seconds) return line end |
#load_settings(settings) ⇒ Object
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/server.rb', line 109 def load_settings(settings) @hostname = settings.get_setting('server', 'hostname') @ip = settings.get_setting('server', 'ip') @ip.untaint @port = settings.get_int('server', 'port', DEFAULT_PORT) @port.untaint @max_connections = settings.get_int('server', 'maxconnections', DEFAULT_MAX_CONNECTIONS) @max_recipients = settings.get_int('server', 'maxrecipients', DEFAULT_MAX_RECIPIENTS) @timeout_seconds = settings.get_int('server', 'timeoutseconds', DEFAULT_TIMEOUT_SECONDS) end |
#response(text) ⇒ Object
105 106 107 |
# File 'lib/server.rb', line 105 def response(text) return "#{text}#{NEWLINE}" end |
#serve(client) ⇒ Object
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 |
# File 'lib/server.rb', line 34 def serve(client) begin MailDiode::log_debug("##{client_id(client)}: serve") client.binmode engine = @factory.create_engine(@hostname, @max_recipients) family, port, hostname, ip = client.addr greeting = engine.start(ip) MailDiode::log_debug("Greeting: #{greeting}") client << response(greeting) while !engine.terminate? MailDiode::log_debug("##{client_id(client)}: calling get_line") line = get_line(client, @timeout_seconds) MailDiode::log_debug("##{client_id(client)}: returned from get_line") if(!line) break end line.chomp!(NEWLINE) MailDiode::log_debug("##{client_id(client)}: calling process_line") result = engine.process_line(line) MailDiode::log_debug("##{client_id(client)}: returned from process_line") if(result) MailDiode::log_debug("##{client_id(client)}: calling discard_early_input") discard_early_input(client) MailDiode::log_debug("##{client_id(client)}: returned from discard_early_input") client << response(result) end end MailDiode::log_debug "##{client_id(client)}: engine terminated" rescue Errno::ECONNRESET MailDiode::log_info "##{client_id(client)}: Client reset connection" rescue Errno::ETIMEDOUT MailDiode::log_info "##{client_id(client)}: Client input timeout" rescue Errno::ENOTCONN MailDiode::log_info "##{client_id(client)}: Client dropped connection" rescue Exception => e MailDiode::log_error("##{client_id(client)}: Exception #{e.class}: #{e}") e.backtrace.each do | line | MailDiode::log_error(line) end client << response("451 Internal Server Error") end MailDiode::log_debug "##{client_id(client)}: serve exiting" end |