little cleanup
parent
15ef940c15
commit
e94e70b720
|
@ -1,34 +0,0 @@
|
|||
{
|
||||
"name": "@uci/wifi",
|
||||
"version": "0.0.2",
|
||||
"description": "Node wrapper for nmcli",
|
||||
"main": "src",
|
||||
"directories": {
|
||||
"test": "test"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "node -r @std/esm ./test/cmd",
|
||||
"testc": "node -r @std/esm ./test/connect",
|
||||
"tests": "node -r @std/esm ./test/scan",
|
||||
"testd": "node -r @std/esm ./test/disconnect"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/xxx.git"
|
||||
},
|
||||
"keywords": [
|
||||
"wifi",
|
||||
"multiplatform"
|
||||
],
|
||||
"author": "Thibault Friedrich",
|
||||
"license": "LGPL-3.0",
|
||||
"bugs": {
|
||||
"url": "https://github.com/friedrith/node-wifi/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"@uci/try-catch": "0.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@std/esm": "^0.26.0"
|
||||
}
|
||||
}
|
|
@ -2,15 +2,15 @@ import cmd from './cmd'
|
|||
import del from './delete'
|
||||
import tc from '@uci/try-catch'
|
||||
|
||||
export default async function (ap) {
|
||||
|
||||
async function connect(ap) {
|
||||
let ret = null
|
||||
let cmdStr = ''
|
||||
|
||||
// check to see if already connected
|
||||
cmdStr = `nmcli --terse --fields UUID c show --active | grep ${ap.uuid}`
|
||||
ret = await tc(cmd)(cmdStr)
|
||||
if (!ret.err && ret.res!=='') return {connect:'already connected', ap:ap}
|
||||
if (!ret.err && ret.res !== '')
|
||||
return { connect: 'already connected', ap: ap }
|
||||
|
||||
// check for existing connection, if some connect if not it's new
|
||||
cmdStr = `nmcli --terse --fields device,name,UUID c show | grep ${ap.uuid}`
|
||||
|
@ -19,14 +19,18 @@ export default async function (ap) {
|
|||
if (!ret.err && ret.res !== '') {
|
||||
console.log('existing nm connection', ap.ssid)
|
||||
if (ap.update) {
|
||||
cmdStr = `nmcli con modify ${ap.uuid} 802-11-wireless-security.psk ${ap.password}`
|
||||
cmdStr = `nmcli con modify ${ap.uuid} 802-11-wireless-security.psk ${
|
||||
ap.password
|
||||
}`
|
||||
ret = await tc(cmd)(cmdStr)
|
||||
if (ret.err) return { error: 'unable to update password', ap: ap }
|
||||
}
|
||||
cmdStr = `nmcli con up uuid ${ap.uuid}`
|
||||
ret = await tc(cmd)(cmdStr)
|
||||
if (ret.err) {
|
||||
if(ap.update) { return { error:'updated password failed', ap:ap} }
|
||||
if (ap.update) {
|
||||
return { error: 'updated password failed', ap: ap }
|
||||
}
|
||||
return { error: ret.err.message }
|
||||
}
|
||||
return { connect: 'reconnected', ap: ap }
|
||||
|
@ -35,7 +39,9 @@ export default async function (ap) {
|
|||
// connect with new connection
|
||||
// first clean out any connections with same or similar name to SSID
|
||||
ret = await tc(del)({ name: ap.ssid })
|
||||
cmdStr = `nmcli -w 10 device wifi connect '${ap.ssid}' password '${ap.password}' ifname ${ap.iface}`
|
||||
cmdStr = `nmcli -w 10 device wifi connect '${ap.ssid}' password '${
|
||||
ap.password
|
||||
}' ifname ${ap.iface}`
|
||||
ret = await tc(cmd)(cmdStr)
|
||||
if (ret.err) {
|
||||
return { error: `new connection failed -- ${ret.err.cmd}` }
|
||||
|
@ -48,5 +54,6 @@ export default async function (ap) {
|
|||
ap.uuid = ret.res.split(/[\n:]+/)[1]
|
||||
console.log(ap.uuid)
|
||||
return { connect: 'new', ap: ap }
|
||||
|
||||
} // end connect
|
||||
|
||||
export default connect
|
||||
|
|
|
@ -1,17 +1,18 @@
|
|||
import cmd from './cmd'
|
||||
import tc from '@uci/try-catch'
|
||||
|
||||
export default async function (con) {
|
||||
|
||||
async function apdelete(con) {
|
||||
let ret = null
|
||||
let filter = con.name || con.uuid
|
||||
if (!filter) return new Error('no delete filter supplied')
|
||||
let cmdStr = `nmcli --terse --fields UUID,name,type c show | grep 802-11-wireless | grep '${filter}'`
|
||||
ret = await tc(cmd)(cmdStr)
|
||||
if (ret.err || ret.res==='') return new Error(`no connections to delete => ${ret.err.cmd}`)
|
||||
if (ret.err || ret.res === '')
|
||||
return new Error(`no connections to delete => ${ret.err.cmd}`)
|
||||
let connections = ret.res.split('\n')
|
||||
await connections.forEach(async acon => {
|
||||
if (acon != '') { // console returns an extra blank line
|
||||
if (acon != '') {
|
||||
// console returns an extra blank line
|
||||
let fields = acon.replace(/\\:/g, '&&').split(':')
|
||||
// console.log('deleting',fields[0],acon)
|
||||
let cmdStr = `nmcli con delete uuid ${fields[0]}`
|
||||
|
@ -20,6 +21,8 @@ export default async function (con) {
|
|||
})
|
||||
cmdStr = `nmcli --terse --fields uuid,name,type c show | grep 802-11-wireless | grep '${filter}'`
|
||||
ret = await tc(cmd)(cmdStr)
|
||||
if (ret.err || ret.res==='') return {res:'success all connections deleted', connnections:connections}
|
||||
|
||||
if (ret.err || ret.res === '')
|
||||
return { res: 'success all connections deleted', connnections: connections }
|
||||
}
|
||||
|
||||
export default apdelete
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
import cmd from './cmd'
|
||||
import tc from '@uci/try-catch'
|
||||
|
||||
export default async function (iface) {
|
||||
|
||||
async function disconnect(iface) {
|
||||
if (!iface) return { error: 'can not disconnect - no interface given' }
|
||||
let cmdStr = `nmcli device disconnect ${iface}`
|
||||
let ret = await tc(cmd)(cmdStr)
|
||||
if (ret.err) return {error: 'disconnect failed', cmd:ret.err.cmd, message:ret.err}
|
||||
if (ret.err)
|
||||
return { error: 'disconnect failed', cmd: ret.err.cmd, message: ret.err }
|
||||
return { disconnect: 'success', iface: iface }
|
||||
|
||||
} // end disconnect
|
||||
|
||||
export default disconnect
|
||||
|
|
17
src/scan.js
17
src/scan.js
|
@ -1,10 +1,10 @@
|
|||
import cmd from './cmd'
|
||||
import tc from '@uci/try-catch'
|
||||
|
||||
export default async function (interf) {
|
||||
|
||||
async function scan(interf) {
|
||||
let ret = null
|
||||
let cmdStr = 'nmcli --terse --fields active,ssid,bssid,mode,chan,freq,signal,security,wpa-flags,rsn-flags,device device wifi list'
|
||||
let cmdStr =
|
||||
'nmcli --terse --fields active,ssid,bssid,mode,chan,freq,signal,security,wpa-flags,rsn-flags,device device wifi list'
|
||||
ret = await tc(cmd)(cmdStr)
|
||||
if (ret.err) return { error: `scan failed -- ${ret.err.cmd}` }
|
||||
let lines = ret.res.split('\n')
|
||||
|
@ -15,7 +15,7 @@ export default async function (interf) {
|
|||
let fields = lines[i].replace(/\\:/g, '&&').split(':')
|
||||
let ssid = fields[1].replace(/&&/g, ':')
|
||||
let iface = fields[10].replace(/&&/g, ':')
|
||||
if (ssid !== '--' && (interf ? (interf === iface) : true)) {
|
||||
if (ssid !== '--' && (interf ? interf === iface : true)) {
|
||||
// if (ssid !== '--' ) {
|
||||
networks.push({
|
||||
active: fields[0].replace(/&&/g, ':'),
|
||||
|
@ -27,10 +27,13 @@ export default async function (interf) {
|
|||
channel: parseInt(fields[4].replace(/&&/g, ':')),
|
||||
frequency: parseInt(fields[5].replace(/&&/g, ':')),
|
||||
signal_quality: Number(fields[6].replace(/&&/g, ':')),
|
||||
security: fields[7].replace(/&&/g, ':') != '(none)' ? fields[7].replace(/&&/g, ':') : 'none',
|
||||
security:
|
||||
fields[7].replace(/&&/g, ':') != '(none)'
|
||||
? fields[7].replace(/&&/g, ':')
|
||||
: 'none',
|
||||
security_flags: {
|
||||
wpa: fields[8].replace(/&&/g, ':'),
|
||||
rsn: fields[9].replace(/&&/g, ':'),
|
||||
rsn: fields[9].replace(/&&/g, ':')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -38,3 +41,5 @@ export default async function (interf) {
|
|||
}
|
||||
return networks
|
||||
}
|
||||
|
||||
export default scan
|
||||
|
|
Loading…
Reference in New Issue