@@ -7,11 +7,12 @@ module Rex
7
7
module Google
8
8
# @example
9
9
# g = Rex::Google::Geolocation.new
10
+ # g.set_api_key('example')
10
11
# g.add_wlan("00:11:22:33:44:55", "example", -80)
11
12
# g.fetch!
12
13
# puts g, g.google_maps_url
13
14
class Geolocation
14
- GOOGLE_API_URI = "https://maps .googleapis.com/maps/api/browserlocation/json?browser=firefox&sensor=true& "
15
+ GOOGLE_API_URI = "https://www .googleapis.com/geolocation/v1/geolocate?key= "
15
16
16
17
attr_accessor :accuracy
17
18
attr_accessor :latitude
@@ -24,36 +25,39 @@ def initialize
24
25
25
26
# Ask Google's Maps API for the location of a given set of BSSIDs (MAC
26
27
# addresses of access points), ESSIDs (AP names), and signal strengths.
27
- def fetch!
28
- @uri . query << @wlan_list . take ( 10 ) . join ( "&wifi=" )
29
- request = Net ::HTTP ::Get . new ( @uri . request_uri )
28
+ def fetch!
29
+ request = Net ::HTTP ::Post . new ( @uri . request_uri )
30
+ request . body = { 'wifiAccessPoints' => @wlan_list } . to_json
31
+ request [ 'Content-Type' ] = 'application/json'
30
32
http = Net ::HTTP . new ( @uri . host , @uri . port )
31
33
http . use_ssl = true
32
34
response = http . request ( request )
33
35
36
+ msg = "Failure connecting to Google for location lookup."
34
37
if response && response . code == '200'
35
38
results = JSON . parse ( response . body )
36
39
self . latitude = results [ "location" ] [ "lat" ]
37
40
self . longitude = results [ "location" ] [ "lng" ]
38
41
self . accuracy = results [ "accuracy" ]
42
+ elsif response && response . body && response . code != '404' # we can json load and get a good error message
43
+ msg += " Code #{ results [ 'error' ] [ 'code' ] } for query #{ @uri } with error #{ results [ 'error' ] [ 'message' ] } "
44
+ fail msg
39
45
else
40
- msg = "Failure connecting to Google for location lookup."
41
46
msg += " Code #{ response . code } for query #{ @uri } " if response
42
47
fail msg
43
48
end
44
49
end
45
50
46
51
# Add an AP to the list to send to Google when {#fetch!} is called.
47
52
#
48
- # Turns out Google's API doesn't really care about ESSID or signal strength
49
- # as long as you have BSSIDs. Presumably adding them will make it more
50
- # accurate? Who knows.
51
- #
52
53
# @param mac [String] in the form "00:11:22:33:44:55"
53
- # @param ssid [String] ESSID associated with the mac
54
54
# @param signal_strength [String] a thing like
55
- def add_wlan ( mac , ssid = nil , signal_strength = nil )
56
- @wlan_list . push ( URI . encode ( "mac:#{ mac . upcase } |ssid:#{ ssid } |ss=#{ signal_strength . to_i } " ) )
55
+ def add_wlan ( mac , signal_strength )
56
+ @wlan_list . push ( { :macAddress => mac . upcase . to_s , :signalStrength => signal_strength . to_s } )
57
+ end
58
+
59
+ def set_api_key ( key )
60
+ @uri = URI . parse ( URI . encode ( GOOGLE_API_URI + key ) )
57
61
end
58
62
59
63
def google_maps_url
0 commit comments