Class: Rex::Google::Geolocation
- Inherits:
-
Object
- Object
- Rex::Google::Geolocation
- Defined in:
- lib/rex/google/geolocation.rb
Overview
Constant Summary collapse
- GOOGLE_API_URI =
"https://www.googleapis.com/geolocation/v1/geolocate?key="
Instance Attribute Summary collapse
-
#accuracy ⇒ Object
Returns the value of attribute accuracy.
-
#latitude ⇒ Object
Returns the value of attribute latitude.
-
#longitude ⇒ Object
Returns the value of attribute longitude.
Instance Method Summary collapse
-
#add_wlan(mac, signal_strength) ⇒ Object
Add an AP to the list to send to Google when #fetch! is called.
-
#fetch! ⇒ Object
Ask Google’s Maps API for the location of a given set of BSSIDs (MAC addresses of access points), ESSIDs (AP names), and signal strengths.
- #google_maps_url ⇒ Object
-
#initialize ⇒ Geolocation
constructor
A new instance of Geolocation.
- #set_api_key(key) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ Geolocation
Returns a new instance of Geolocation.
21 22 23 24 |
# File 'lib/rex/google/geolocation.rb', line 21 def initialize @uri = URI.parse(URI::DEFAULT_PARSER.escape(GOOGLE_API_URI)) @wlan_list = [] end |
Instance Attribute Details
#accuracy ⇒ Object
Returns the value of attribute accuracy.
17 18 19 |
# File 'lib/rex/google/geolocation.rb', line 17 def accuracy @accuracy end |
#latitude ⇒ Object
Returns the value of attribute latitude.
18 19 20 |
# File 'lib/rex/google/geolocation.rb', line 18 def latitude @latitude end |
#longitude ⇒ Object
Returns the value of attribute longitude.
19 20 21 |
# File 'lib/rex/google/geolocation.rb', line 19 def longitude @longitude end |
Instance Method Details
#add_wlan(mac, signal_strength) ⇒ Object
Add an AP to the list to send to Google when #fetch! is called.
56 57 58 |
# File 'lib/rex/google/geolocation.rb', line 56 def add_wlan(mac, signal_strength) @wlan_list.push({ :macAddress => mac.upcase.to_s, :signalStrength => signal_strength.to_s }) end |
#fetch! ⇒ Object
Ask Google’s Maps API for the location of a given set of BSSIDs (MAC addresses of access points), ESSIDs (AP names), and signal strengths.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rex/google/geolocation.rb', line 28 def fetch! request = Net::HTTP::Post.new(@uri.request_uri) request.body = {'wifiAccessPoints' => @wlan_list}.to_json request['Content-Type'] = 'application/json' http = Net::HTTP.new(@uri.host, @uri.port) http.use_ssl = true response = http.request(request) msg = "Failure connecting to Google for location lookup." if response && response.code == '200' results = JSON.parse(response.body) self.latitude = results["location"]["lat"] self.longitude = results["location"]["lng"] self.accuracy = results["accuracy"] elsif response && response.body && response.code != '404' # we can json load and get a good error message results = JSON.parse(response.body) msg += " Code #{results['error']['code']} for query #{@uri} with error #{results['error']['message']}" fail msg else msg += " Code #{response.code} for query #{@uri}" if response fail msg end end |
#google_maps_url ⇒ Object
64 65 66 |
# File 'lib/rex/google/geolocation.rb', line 64 def google_maps_url "https://maps.google.com/?q=#{latitude},#{longitude}" end |
#set_api_key(key) ⇒ Object
60 61 62 |
# File 'lib/rex/google/geolocation.rb', line 60 def set_api_key(key) @uri = URI.parse(URI::DEFAULT_PARSER.escape(GOOGLE_API_URI + key)) end |
#to_s ⇒ Object
68 69 70 |
# File 'lib/rex/google/geolocation.rb', line 68 def to_s "Google indicates the device is within #{accuracy} meters of #{latitude},#{longitude}." end |