Browse files

init

  • Loading branch information...
0 parents commit 1778c0646edd74116069dd2f711da9b64013b18a @swdyh committed Aug 9, 2012
Showing with 311 additions and 0 deletions.
  1. +32 −0 README.md
  2. +63 −0 lib/linux_wifi.txt
  3. +25 −0 lib/osx_wifi.txt
  4. +172 −0 lib/wifi_location.js
  5. +19 −0 package.json
32 README.md
@@ -0,0 +1,32 @@
+# Node Wifi Location
+
+Find a current location by surrounding WiFi and Google Map API.
+A port of wifi_location(https://github.com/shokai/wifi_location) to Node.js.
+
+## Install
+
+ npm install wifi_location
+
+## Sample code
+
+ var wifiLocation = require('wifi_location')
+
+ wifiLocation.wifiTowers(function(err, val) {
+ console.log(err, val)
+ })
+
+ wifiLocation.location(function(err, val) {
+ console.log(err, val)
+ })
+
+## License
+
+Apache License Version 2.0
+
+## See more
+
+https://github.com/shokai/wifi_location
+http://shokai.org/blog/archives/6399
+http://blog.64p.org/entry/2012/07/21/082209
+http://unknownplace.org/memo/2012/07/21/1/
+
63 lib/linux_wifi.txt
@@ -0,0 +1,63 @@
+eth1 Scan completed :
+ Cell 02 - Address: c0:8a:de:c3:6d:6c
+ ESSID:"xxx"
+ Mode:Managed
+ Frequency:2.452 GHz (Channel 9)
+ Quality:1/5 Signal level:-86 dBm Noise level:-92 dBm
+ IE: IEEE 802.11i/WPA2 Version 1
+ Group Cipher : TKIP
+ Pairwise Ciphers (2) : CCMP TKIP
+ Authentication Suites (1) : PSK
+ IE: WPA Version 1
+ Group Cipher : TKIP
+ Pairwise Ciphers (2) : CCMP TKIP
+ Authentication Suites (1) : PSK
+ Encryption key:on
+ Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 18 Mb/s
+ 24 Mb/s; 36 Mb/s; 54 Mb/s; 6 Mb/s; 9 Mb/s
+ 12 Mb/s; 48 Mb/s
+ Cell 03 - Address: c0:8a:de:83:6d:6c
+ ESSID:"yyy"
+ Mode:Managed
+ Frequency:2.462 GHz (Channel 11)
+ Quality:1/5 Signal level:-86 dBm Noise level:-92 dBm
+ Encryption key:on
+ Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 18 Mb/s
+ 24 Mb/s; 36 Mb/s; 54 Mb/s; 6 Mb/s; 9 Mb/s
+ 12 Mb/s; 48 Mb/s
+ Cell 04 - Address: c0:8a:de:43:6d:6c
+ ESSID:"RBP"
+ Mode:Managed
+ Frequency:2.462 GHz (Channel 11)
+ Quality:1/5 Signal level:-80 dBm Noise level:-92 dBm
+ IE: IEEE 802.11i/WPA2 Version 1
+ Group Cipher : TKIP
+ Pairwise Ciphers (2) : TKIP CCMP
+ Authentication Suites (1) : PSK
+ IE: WPA Version 1
+ Group Cipher : TKIP
+ Pairwise Ciphers (2) : TKIP CCMP
+ Authentication Suites (1) : PSK
+ Encryption key:on
+ Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 9 Mb/s
+ 18 Mb/s; 36 Mb/s; 54 Mb/s; 6 Mb/s; 12 Mb/s
+ 24 Mb/s; 48 Mb/s
+ Cell 05 - Address: c0:8a:de:03:6d:6c
+ ESSID:"zzz"
+ Mode:Managed
+ Frequency:2.412 GHz (Channel 1)
+ Quality:1/5 Signal level:-86 dBm Noise level:-91 dBm
+ Encryption key:on
+ Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 18 Mb/s
+ 24 Mb/s; 36 Mb/s; 54 Mb/s; 6 Mb/s; 9 Mb/s
+ 12 Mb/s; 48 Mb/s
+ Cell 06 - Address: 50:a7:33:b5:7e:bc
+ ESSID:"aaa"
+ Mode:Managed
+ Frequency:2.437 GHz (Channel 6)
+ Quality:2/5 Signal level:-74 dBm Noise level:-92 dBm
+ Encryption key:on
+ Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 18 Mb/s
+ 24 Mb/s; 36 Mb/s; 54 Mb/s; 6 Mb/s; 9 Mb/s
+ 12 Mb/s; 48 Mb/s
+
25 lib/osx_wifi.txt
@@ -0,0 +1,25 @@
+ SSID BSSID RSSI CHANNEL HT CC SECURITY (auth/unicast/group)
+ at_STARBUCKS_Wi2 c0:8a:de:c3:6d:6c -56 124,+1 Y JP NONE
+ au_Wi-Fi c0:8a:de:83:6d:6c -57 124,+1 Y JP WPA2(PSK/AES/AES)
+ Wi2_club c0:8a:de:43:6d:6c -57 124,+1 Y JP WPA(PSK/AES,TKIP/TKIP) WPA2(PSK/AES,TKIP/TKIP)
+ Wi2 c0:8a:de:03:6d:6c -57 124,+1 Y JP NONE
+ Wi2premium_club 50:a7:33:b5:7e:bc -76 100,+1 Y JP WPA(PSK/AES,TKIP/TKIP) WPA2(PSK/AES,TKIP/TKIP)
+ Wi2premium 50:a7:33:75:7e:bc -76 100,+1 Y JP NONE
+ au_Wi-Fi 50:a7:33:35:7e:bc -76 100,+1 Y JP WPA2(PSK/AES/AES)
+ NTT-SPOT 00:0f:61:b4:dd:c4 -70 44 N -- WEP
+ wifine 00:0f:61:b4:dd:c3 -71 44 N -- NONE
+ docomo 00:0f:61:b4:dd:c0 -70 44 N -- WEP
+ NTT-SPOT 00:0f:61:5a:31:56 -59 1 N -- WEP
+ wifine 00:0f:61:5a:31:55 -58 1 N -- NONE
+ FLETS-SPOT 00:0f:61:5a:31:51 -58 1 N -- WEP
+ docomo 00:0f:61:5a:31:50 -58 1 N -- WEP
+ 0001softbank c2:f8:da:ce:ad:59 -60 13 Y JP NONE
+ FON c0:f8:da:ce:ad:59 -60 13 Y JP NONE
+ Wi2premium_club 50:a7:33:b5:7e:b8 -69 11 Y -- WPA(PSK/AES,TKIP/TKIP) WPA2(PSK/AES,TKIP/TKIP)
+ Wi2premium 50:a7:33:75:7e:b8 -70 11 Y -- NONE
+ au_Wi-Fi 50:a7:33:35:7e:b8 -71 11 Y -- WPA2(PSK/AES/AES)
+ GP01-202BC141FF3F 20:2b:c1:41:ff:3f -74 11 N JP WEP
+ at_STARBUCKS_Wi2 c0:8a:de:c3:6d:68 -55 6 Y -- NONE
+ au_Wi-Fi c0:8a:de:83:6d:68 -56 6 Y -- WPA2(PSK/AES/AES)
+ Wi2_club c0:8a:de:43:6d:68 -56 6 Y -- WPA(PSK/AES,TKIP/TKIP) WPA2(PSK/AES,TKIP/TKIP)
+ Wi2 c0:8a:de:03:6d:68 -55 6 Y -- NONE
172 lib/wifi_location.js
@@ -0,0 +1,172 @@
+var childProcess = require('child_process')
+var request = require('request')
+
+function wifiTowers(callback) {
+ try {
+ switch (process.platform) {
+ case 'darwin':
+ return osxWifiTowers(function(err, val) {
+ callback(err, osxParse(val))
+ })
+ case 'linux':
+ return linuxWifiTowers(function(err, val) {
+ callback(err, linuxParse(val))
+ })
+ default:
+ throw new Error('not supported')
+ }
+ }
+ catch(err) {
+ callback(err)
+ }
+}
+
+function osxWifiTowers(callback) {
+ var cmd = '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -s'
+ childProcess.exec(cmd, callback)
+}
+
+function osxParse(text) {
+ var list = text.trim().split('\n').map(function(i) {
+ var tmp = i.trim().split(/\s+/)
+ if (tmp.length == 8) {
+ tmp[tmp.length - 2] = tmp.slice(-2).join(' ')
+ return tmp.slice(0, -1)
+ }
+ return tmp
+ })
+ return list.slice(1).map(function(i) {
+ return [{}].concat(i).reduce(function(r, i, index) {
+ r[list[0][index - 1]] = i
+ return r
+ })
+ })
+}
+
+function linuxWifiTowers(callback) {
+ var cmd = 'iwlist wlan0 scan'
+ childProcess.exec(cmd, callback)
+}
+
+function linuxParse(text) {
+ var cells = text.split(/\n\s+Cell \d\d/g).slice(1)
+ return cells.map(function(i) {
+ var r = {}
+ var k = null
+ i.split('\n').forEach(function(j) {
+ var rr = j.match(/Address: (.+)/)
+ var rrSignal = j.match(/Quality:(.+) Signal level:(.+) Noise level:(.+)/)
+ if (rr) {
+ r.Address = rr[1].trim()
+ }
+ else if (rrSignal) {
+ r.Quality = rrSignal[1].trim()
+ r['Signal level'] = rrSignal[2].trim()
+ r['Noise level'] = rrSignal[3].trim()
+ }
+ else {
+ var tmp = j.split(':')
+ if (tmp.length == 2) {
+ k = tmp[0].trim()
+ r[k] = tmp[1].trim().replace(/^"/, '').replace(/"$/, '')
+ }
+ else if (k == 'Bit Rates') {
+ r[k] += ' ' + j.trim()
+ }
+ else {
+ // skip
+ // console.log('skip', j)
+ }
+ }
+ })
+ return r
+ })
+}
+
+function filterWifiTowers(wifiTowers) {
+ return wifiTowers.map(function(i) {
+ return {
+ mac_address: (i.mac_address || i.BSSID || i.Address),
+ signal_strength: parseInt(i.signal_strength || i.RSSI || i['Signal level'] || -50, 10),
+ age: 0
+ }
+ })
+}
+
+function location(opt, callback) {
+ if (typeof opt == 'function' && typeof callback == 'undefined') {
+ callback = opt
+ opt = {}
+ }
+ var lang = opt.lang || (process.env['LANG'] ? process.env['LANG'].split('.')[0] : 'en_US')
+ var f = opt.wifiTowers ? function(cb) { cb(null, opt.wifiTowers) } : wifiTowers
+ f(function(err, val) {
+ var param = {
+ version: '1.1.0',
+ host: 'maps.google.com',
+ request_address: true,
+ address_language: lang,
+ wifi_towers: filterWifiTowers(val)
+ }
+ request.post({
+ uri: 'http://www.google.com/loc/json',
+ json: param
+ }, function(err, res, val) {
+ callback(err, val && val.location)
+ })
+ })
+}
+
+function parseTest() {
+ var fs = require('fs')
+ var path = require('path')
+ var assert = require('assert')
+
+ var t_osx = fs.readFileSync(path.join(__dirname, 'osx_wifi.txt')).toString()
+ var d_osx = osxParse(t_osx)
+ assert.equal(d_osx.length, 24)
+ assert.ok(d_osx[0].SSID)
+ assert.ok(d_osx[0].BSSID)
+ assert.ok(d_osx[0].RSSI)
+
+ var t_linux = fs.readFileSync(path.join(__dirname, 'linux_wifi.txt')).toString()
+ var d_linux = linuxParse(t_linux)
+ assert.equal(d_linux.length, 5)
+ assert.ok(d_linux[0].Address)
+ assert.ok(d_linux[0].ESSID)
+ assert.ok(d_linux[0]['Signal level'])
+}
+
+exports.wifiTowers = wifiTowers
+exports.osxWifiTowers = osxWifiTowers
+exports.osxParse = osxParse
+exports.linuxWifiTowers = linuxWifiTowers
+exports.linuxParse = linuxParse
+exports.filterWifiTowers = filterWifiTowers
+exports.location = location
+exports.parseTest = parseTest
+
+if (process.argv[1] == __filename) {
+ if (/(-t|--test)/.test(process.argv[2])) {
+ parseTest()
+ }
+ else if (/(-w|--wifi)/.test(process.argv[2])) {
+ wifiTowers(function(err, val) {
+ if (err) {
+ return console.log(err)
+ }
+ console.log(val)
+ })
+ }
+ else if (/(-l|--location)/.test(process.argv[2])) {
+ location(function(err, val) {
+ if (err) {
+ return console.log(err)
+ }
+ console.log(val)
+ })
+ }
+}
+
+
+
19 package.json
@@ -0,0 +1,19 @@
+{
+ "name": "wifi_location",
+ "version": "0.0.1",
+ "description": "Find a current location by surrounding WiFi and Google Map API. A port of wifi_location(https://github.com/shokai/wifi_location) to Node.js.",
+ "main": "lib/wifi_location.js",
+ "scripts": {
+ "test": "node lib/wifi_location.js -t"
+ },
+ "repository": "https://github.com/swdyh/node_wifi_location",
+ "keywords": [
+ "wifi",
+ "location"
+ ],
+ "dependencies": {
+ "request": "*"
+ },
+ "author": "swdyh",
+ "license": "Apache License Version 2.0"
+}

0 comments on commit 1778c06

Please sign in to comment.