2018-04-05 14:12:03 -07:00
|
|
|
import cmd from './cmd'
|
|
|
|
import del from './delete'
|
|
|
|
import tc from '@uci/try-catch'
|
|
|
|
|
2019-01-01 20:13:14 -08:00
|
|
|
async function connect(ap) {
|
2018-04-05 14:12:03 -07:00
|
|
|
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)
|
2019-01-01 20:13:14 -08:00
|
|
|
if (!ret.err && ret.res !== '')
|
|
|
|
return { connect: 'already connected', ap: ap }
|
2018-04-05 14:12:03 -07:00
|
|
|
|
|
|
|
// 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)
|
2019-01-01 20:13:14 -08:00
|
|
|
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
|
|
|
|
}`
|
2018-04-05 14:12:03 -07:00
|
|
|
ret = await tc(cmd)(cmdStr)
|
2019-01-01 20:13:14 -08:00
|
|
|
if (ret.err) return { error: 'unable to update password', ap: ap }
|
2018-04-05 14:12:03 -07:00
|
|
|
}
|
|
|
|
cmdStr = `nmcli con up uuid ${ap.uuid}`
|
|
|
|
ret = await tc(cmd)(cmdStr)
|
|
|
|
if (ret.err) {
|
2019-01-01 20:13:14 -08:00
|
|
|
if (ap.update) {
|
|
|
|
return { error: 'updated password failed', ap: ap }
|
|
|
|
}
|
|
|
|
return { error: ret.err.message }
|
2018-04-05 14:12:03 -07:00
|
|
|
}
|
2019-01-01 20:13:14 -08:00
|
|
|
return { connect: 'reconnected', ap: ap }
|
2018-04-05 14:12:03 -07:00
|
|
|
}
|
|
|
|
console.log('NEW CONNECTION')
|
|
|
|
// connect with new connection
|
|
|
|
// first clean out any connections with same or similar name to SSID
|
2019-01-01 20:13:14 -08:00
|
|
|
ret = await tc(del)({ name: ap.ssid })
|
|
|
|
cmdStr = `nmcli -w 10 device wifi connect '${ap.ssid}' password '${
|
|
|
|
ap.password
|
|
|
|
}' ifname ${ap.iface}`
|
2018-04-05 14:12:03 -07:00
|
|
|
ret = await tc(cmd)(cmdStr)
|
|
|
|
if (ret.err) {
|
2019-01-01 20:13:14 -08:00
|
|
|
return { error: `new connection failed -- ${ret.err.cmd}` }
|
2018-04-05 14:12:03 -07:00
|
|
|
}
|
|
|
|
// TODO set interface method if other than auto which is default
|
|
|
|
// `nmcli con modify ${ap.ssid} ipv4.method shared
|
2018-04-11 12:41:59 -07:00
|
|
|
cmdStr = `nmcli --terse --fields connection.uuid c show id '${ap.ssid}'`
|
2018-04-05 14:12:03 -07:00
|
|
|
ret = await tc(cmd)(cmdStr)
|
2018-04-11 12:41:59 -07:00
|
|
|
console.log(ret.res)
|
2018-04-05 14:12:03 -07:00
|
|
|
ap.uuid = ret.res.split(/[\n:]+/)[1]
|
2018-04-11 12:41:59 -07:00
|
|
|
console.log(ap.uuid)
|
2019-01-01 20:13:14 -08:00
|
|
|
return { connect: 'new', ap: ap }
|
2018-04-05 14:12:03 -07:00
|
|
|
} // end connect
|
2019-01-01 20:13:14 -08:00
|
|
|
|
|
|
|
export default connect
|