Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
104 lines (89 sloc) 3.21 KB
require 'geoip'
require 'net/http'
require 'json'
$useragents = {}
class Site
def initialize
@default = Array.new(20, "unknown")
end
def getinfo(ip)
g = GeoIP.new(File.join(File.dirname(__FILE__), 'GeoLiteCity.dat'))
tmp = g.country(ip)
tmp ||= @default
data = {
:raw => tmp,
:city => tmp[7] || "unknown",
:region => tmp[6] || "unknown",
:country => tmp[4] || "unknown",
:area_code => tmp[12] || "unknown",
:time_zone => tmp[13] || "unknown"
}
data[:time_zone].gsub!('_',' ')
data
end
def getua(raw_useragent)
return $useragents[raw_useragent] unless $useragents[raw_useragent].nil?
$useragents[raw_useragent] = parse_useragent(raw_useragent)
end
def call(env)
ip = env['HTTP_X_FORWARDED_FOR'] # Reverse-proxy
raw_ua = env['HTTP_USER_AGENT']
referer_link = env['HTTP_REFERER']
data = getinfo(ip)
ua = getua(raw_ua)
os = "#{ua['os_producer']} #{ua['os_name']} #{ua['os_versionName']} #{ua['os_versionNumber']}".strip
language = ua['agent_language']
language = language.split(' - ')[0] # Remove the ' - United States'/' - United Kingdom' and similar
if language.nil? || language.empty?
browser_language = ''
else
browser_language = "the <em>#{language}</em> version of"
end
if referer_link.nil?
referer = ''
else
referer = "<p>You seem to have come from <a href=\"#{referer_link}\">#{referer_link}</a>.</p>"
end
str = <<EOF
<html>
<head>
<title>You're near #{data[:city]}, #{data[:region]}, #{data[:country]}</title>
<style type="text/css">
iframe {
height: 30em;
width: 49%;
}
</style>
</head>
<body>
<p>DISCLAIMER: No personally-identifiable data is stored. No data related to your location is stored. Some information about your browser is stored to boost the speed of repeat requests, but there is nothing linking it to a particular person. This site will more than likely not work on mobile devices. Accuracy varies.</p>
<!-- Location info -->
<p>You are appear to be near #{data[:city]}, #{data[:region]}, #{data[:country]}. Your area code is likely #{data[:area_code]}. Your time zone is likely #{data[:time_zone]}.</p>
<!-- Browser/OS info -->
<p>You are probably using #{browser_language} <em>#{ua['agent_name']} #{ua['agent_version']}</em> on <em>#{os}</em>.</p>
<!-- Last-viewed page -->
#{referer}
<div>
<iframe style="float:left;" src="http://maps.google.com/maps?q=#{data[:raw][9..10].join(',')}&amp;t=h&amp;output=embed"></iframe>
<iframe style="float:right;" src="http://maps.google.com/maps?q=#{data[:raw][7]},+#{data[:raw][6]},+#{data[:raw][4]}&amp;t=h&amp;output=embed"></iframe>
</div>
<!--
#{ua.inspect}
-->
</body>
</html>
EOF
[200, { "Content-Type" => "text/html" }, [str]]
rescue => e
[500, { "Content-Type" => "text/plain" }, ["#{e.inspect}\n#{e.backtrace.join("\n")}\n\n#{@str.inspect}\n\n#{env.inspect}\n\n#{data.inspect}"]]
end
def parse_useragent(ua)
url = URI.parse("http://www.useragentstring.com?uas=#{URI.escape(ua)}&getJSON=all")
res = Net::HTTP.start(url.host, url.port) do |http|
http.get(url.request_uri)
end
json = JSON.parse(res.body)
json['os_versionNumber'].gsub!('_', '.')
json
end
end