little cleanup

master
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 del from './delete'
import tc from '@uci/try-catch' import tc from '@uci/try-catch'
export default async function (ap) { async function connect(ap) {
let ret = null let ret = null
let cmdStr = '' let cmdStr = ''
// check to see if already connected // check to see if already connected
cmdStr = `nmcli --terse --fields UUID c show --active | grep ${ap.uuid}` cmdStr = `nmcli --terse --fields UUID c show --active | grep ${ap.uuid}`
ret = await tc(cmd)(cmdStr) 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 // check for existing connection, if some connect if not it's new
cmdStr = `nmcli --terse --fields device,name,UUID c show | grep ${ap.uuid}` cmdStr = `nmcli --terse --fields device,name,UUID c show | grep ${ap.uuid}`
ret = await tc(cmd)(cmdStr) ret = await tc(cmd)(cmdStr)
console.log('check-exisiting',ap.uuid,ap.ssid) console.log('check-exisiting', ap.uuid, ap.ssid)
if (!ret.err && ret.res!=='') { if (!ret.err && ret.res !== '') {
console.log('existing nm connection',ap.ssid) console.log('existing nm connection', ap.ssid)
if(ap.update) { 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) 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}` cmdStr = `nmcli con up uuid ${ap.uuid}`
ret = await tc(cmd)(cmdStr) ret = await tc(cmd)(cmdStr)
if (ret.err) { if (ret.err) {
if(ap.update) { return { error:'updated password failed', ap:ap} } if (ap.update) {
return {error: ret.err.message } 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') console.log('NEW CONNECTION')
// connect with new connection // connect with new connection
// first clean out any connections with same or similar name to SSID // first clean out any connections with same or similar name to SSID
ret = await tc(del)( {name:ap.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) ret = await tc(cmd)(cmdStr)
if (ret.err) { 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 // TODO set interface method if other than auto which is default
// `nmcli con modify ${ap.ssid} ipv4.method shared // `nmcli con modify ${ap.ssid} ipv4.method shared
@ -47,6 +53,7 @@ export default async function (ap) {
console.log(ret.res) console.log(ret.res)
ap.uuid = ret.res.split(/[\n:]+/)[1] ap.uuid = ret.res.split(/[\n:]+/)[1]
console.log(ap.uuid) console.log(ap.uuid)
return {connect:'new', ap:ap} return { connect: 'new', ap: ap }
} // end connect } // end connect
export default connect

View File

@ -1,17 +1,18 @@
import cmd from './cmd' import cmd from './cmd'
import tc from '@uci/try-catch' import tc from '@uci/try-catch'
export default async function (con) { async function apdelete(con) {
let ret = null let ret = null
let filter = con.name || con.uuid let filter = con.name || con.uuid
if (!filter) return new Error('no delete filter supplied') 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}'` let cmdStr = `nmcli --terse --fields UUID,name,type c show | grep 802-11-wireless | grep '${filter}'`
ret = await tc(cmd)(cmdStr) 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') let connections = ret.res.split('\n')
await connections.forEach(async acon => { 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(':') let fields = acon.replace(/\\:/g, '&&').split(':')
// console.log('deleting',fields[0],acon) // console.log('deleting',fields[0],acon)
let cmdStr = `nmcli con delete uuid ${fields[0]}` 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}'` cmdStr = `nmcli --terse --fields uuid,name,type c show | grep 802-11-wireless | grep '${filter}'`
ret = await tc(cmd)(cmdStr) 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 cmd from './cmd'
import tc from '@uci/try-catch' 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' } if (!iface) return { error: 'can not disconnect - no interface given' }
let cmdStr = `nmcli device disconnect ${iface}` let cmdStr = `nmcli device disconnect ${iface}`
let ret = await tc(cmd)(cmdStr) let ret = await tc(cmd)(cmdStr)
if (ret.err) return {error: 'disconnect failed', cmd:ret.err.cmd, message:ret.err} if (ret.err)
return {disconnect:'success', iface:iface} return { error: 'disconnect failed', cmd: ret.err.cmd, message: ret.err }
return { disconnect: 'success', iface: iface }
} // end disconnect } // end disconnect
export default disconnect

View File

@ -1,22 +1,22 @@
import cmd from './cmd' import cmd from './cmd'
import tc from '@uci/try-catch' import tc from '@uci/try-catch'
export default async function (interf) { async function scan(interf) {
let ret = null 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) 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 lines = ret.res.split('\n')
let networks = [] let networks = []
for (let i = 0 ; i < lines.length ; i++) { for (let i = 0; i < lines.length; i++) {
if (lines[i] != '') { if (lines[i] != '') {
// TODO simply the parsing // TODO simply the parsing
let fields = lines[i].replace(/\\:/g, '&&').split(':') let fields = lines[i].replace(/\\:/g, '&&').split(':')
let ssid = fields[1].replace(/&&/g, ':') let ssid = fields[1].replace(/&&/g, ':')
let iface = fields[10].replace(/&&/g, ':') let iface = fields[10].replace(/&&/g, ':')
if (ssid !== '--' && (interf ? (interf === iface) : true)) { if (ssid !== '--' && (interf ? interf === iface : true)) {
// if (ssid !== '--' ) { // if (ssid !== '--' ) {
networks.push({ networks.push({
active: fields[0].replace(/&&/g, ':'), active: fields[0].replace(/&&/g, ':'),
iface: iface, iface: iface,
@ -27,10 +27,13 @@ export default async function (interf) {
channel: parseInt(fields[4].replace(/&&/g, ':')), channel: parseInt(fields[4].replace(/&&/g, ':')),
frequency: parseInt(fields[5].replace(/&&/g, ':')), frequency: parseInt(fields[5].replace(/&&/g, ':')),
signal_quality: Number(fields[6].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: { security_flags: {
wpa: fields[8].replace(/&&/g, ':'), 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 return networks
} }
export default scan