Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 103 lines (89 sloc) 3.291 kb
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 93 94 95 96 97 98 99 100 101 102 103
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
Something went wrong with that request. Please try again.