Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegPhenomenon committed Oct 18, 2024
1 parent 65634a0 commit d6244d3
Showing 1 changed file with 49 additions and 28 deletions.
77 changes: 49 additions & 28 deletions lib/whois_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,45 +36,66 @@ def connection

def receive_data(data)
connection
begin
ip = Socket.unpack_sockaddr_in(get_peername)
rescue StandardError::TypeError => e
logger.error("uncaught #{e} exception while handling connection: #{e.message}")
close_connection
end
ip = get_client_ip
return if ip.nil?

validator = UnicodeValidator.new(data)
invalid_data = !validator.valid?
name = sanitize_domain_name(data)
return if name.nil?

process_whois_request(name, ip, data)
close_connection_after_writing
end

if invalid_data
private

def get_client_ip
Socket.unpack_sockaddr_in(get_peername)
rescue StandardError::TypeError => e
logger.error("uncaught #{e} exception while handling connection: #{e.message}")
close_connection
nil
end

def sanitize_domain_name(data)
validator = UnicodeValidator.new(data)
if !validator.valid?
logger.info "#{ip}: requested domain name is not in utf-8"
send_data(invalid_encoding_msg)
close_connection_after_writing
return
return nil
end

name = data.strip.downcase
name = SimpleIDN.to_unicode(name)

# Add special handling for .ee second-level domains
if %w[pri.ee fie.ee med.ee com.ee].include?(name)
logger.info "#{ip}: requested: #{data} [searched by: #{name}; Special .ee second-level domain]"
send_data special_ee_domain_msg(name)
SimpleIDN.to_unicode(data.strip.downcase)
end

def process_whois_request(name, ip, original_data)
if special_ee_domain?(name)
handle_special_ee_domain(name, ip, original_data)
else
whois_record = WhoisRecord.find_by(name: name)

if whois_record
logger.info "#{ip}: requested: #{data} [searched by: #{name}; Record found with id: #{whois_record.try(:id)}]"
send_data whois_record.unix_body
else
logger.info "#{ip}: requested: #{data} [searched by: #{name}; No record found]"
provide_data_body(name)
end
handle_regular_domain(name, ip, original_data)
end
close_connection_after_writing
end

private
def special_ee_domain?(name)
%w[pri.ee fie.ee med.ee com.ee].include?(name)
end

def handle_special_ee_domain(name, ip, original_data)
logger.info "#{ip}: requested: #{original_data} [searched by: #{name}; Special .ee second-level domain]"
send_data special_ee_domain_msg(name)
end

def handle_regular_domain(name, ip, original_data)
whois_record = WhoisRecord.find_by(name: name)

if whois_record
logger.info "#{ip}: requested: #{original_data} [searched by: #{name}; Record found with id: #{whois_record.try(:id)}]"
send_data whois_record.unix_body
else
logger.info "#{ip}: requested: #{original_data} [searched by: #{name}; No record found]"
provide_data_body(name)
end
end

def provide_data_body(domain_name)
return send_data(no_entries_msg) if domain_valid_format?(domain_name)
Expand Down

0 comments on commit d6244d3

Please sign in to comment.