A simple Whois server with a simple simple API and a front-end built with AngularJS.

main.rb 2.2KB

    require 'sinatra' require 'whois' require 'haml' require 'json' require 'ostruct' require "sinatra/reloader" if development? require_relative 'json_parser' require_relative 'unirest_parser' require_relative 'analytics' # ruby crimes class Whois::Record def to_h who_dat_hash = {} self.properties.each_pair do |k, v| if v.is_a?(Array) who_dat_hash[k.to_s] = [] v.each do |m| if m.respond_to?(:members) who_dat_hash[k.to_s].push( Hash[m.members.zip(m.to_a)] ) else if m.split(' ').length > 1 who_dat_hash[k.to_s].push( [m.split(' ')].to_h ) else who_dat_hash[k.to_s].push(m) end end end elsif v.respond_to?(:members) who_dat_hash[k.to_s] = Hash[v.members.zip(v.to_a)] else who_dat_hash[k.to_s] = v end end who_dat_hash end end Whois::Server.define :tld, ".network", "whois.donuts.co" set :protection, false helpers do def whois_structs_hash(whois) who_dat_is = whois.clone who_dat_is.properties.each do |p| # if end end def cache_for_day response['Cache-Control'] = 'public, max-age=86400' end def whois_lookup lookup_info = Whois.query(params[:url]) end end set :views, File.dirname(__FILE__) + '/views' set :public_folder, File.dirname(__FILE__) + '/public' get '/' do Analytics.track(request.ip, 'Page View') File.read(File.join('public', 'index.html')) end get '/:url' do Analytics.track(request.ip, 'Domain Lookup', params[:url]) content_type 'application/json' response["Content-Type"] = "application/json" headers 'Access-Control-Allow-Origin' => '*', 'Access-Control-Allow-Methods' => ['OPTIONS', 'GET', 'POST'] begin #cache_for_day #UnirestParser.parse(params[:url]) parser = WhoisParser.new if whois_lookup.class != Array data = whois_lookup.to_h else if whois_lookup.first.class != Array data = whois_lookup.first.to_h else data = "" end end parser.parse(data, whois_lookup, params[:url], params[:raw], params[:dev]) rescue Exception => e @error = e { :error => @error }.to_json end end