little cleanup

This commit is contained in:
David Kebler 2019-01-01 20:13:14 -08:00
parent 15ef940c15
commit e94e70b720
5 changed files with 54 additions and 72 deletions

View file

@ -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"
}
}

View file

@ -2,43 +2,49 @@ 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}`
ret = await tc(cmd)(cmdStr)
console.log('check-exisiting',ap.uuid,ap.ssid)
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}`
console.log('check-exisiting', ap.uuid, ap.ssid)
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
}`
ret = await tc(cmd)(cmdStr)
if (ret.err) return {error:'unable to update password', ap:ap}
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} }
return {error: ret.err.message }
if (ap.update) {
return { error: 'updated password failed', ap: ap }
}
return { error: ret.err.message }
}
return {connect:'reconnected', ap:ap}
return { connect: 'reconnected', ap: ap }
}
console.log('NEW CONNECTION')
// 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}`
ret = await tc(del)({ name: ap.ssid })
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}` }
return { error: `new connection failed -- ${ret.err.cmd}` }
}
// TODO set interface method if other than auto which is default
// `nmcli con modify ${ap.ssid} ipv4.method shared
@ -47,6 +53,7 @@ export default async function (ap) {
console.log(ret.res)
ap.uuid = ret.res.split(/[\n:]+/)[1]
console.log(ap.uuid)
return {connect:'new', ap:ap}
return { connect: 'new', ap: ap }
} // end connect
export default connect

View file

@ -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

View file

@ -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}
return {disconnect:'success', iface:iface}
if (ret.err)
return { error: 'disconnect failed', cmd: ret.err.cmd, message: ret.err }
return { disconnect: 'success', iface: iface }
} // end disconnect
export default disconnect

View file

@ -1,22 +1,22 @@
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}` }
if (ret.err) return { error: `scan failed -- ${ret.err.cmd}` }
let lines = ret.res.split('\n')
let networks = []
for (let i = 0 ; i < lines.length ; i++) {
for (let i = 0; i < lines.length; i++) {
if (lines[i] != '') {
// TODO simply the parsing
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 !== '--' ) {
if (ssid !== '--' && (interf ? interf === iface : true)) {
// if (ssid !== '--' ) {
networks.push({
active: fields[0].replace(/&&/g, ':'),
iface: iface,
@ -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, ':'),
wpa: fields[8].replace(/&&/g, ':'),
rsn: fields[9].replace(/&&/g, ':')
}
})
}
@ -38,3 +41,5 @@ export default async function (interf) {
}
return networks
}
export default scan