Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set up wifi using credentials from a JSON file #405

Merged
merged 3 commits into from
Mar 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/cli/serial.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const unindent = require('../lib/unindent');

export default ({ commandProcessor, root }) => {
const serial = commandProcessor.createCategory(root, 'serial', 'Simple serial interface to your devices');

Expand Down Expand Up @@ -36,11 +38,30 @@ export default ({ commandProcessor, root }) => {
});

commandProcessor.createCommand(serial, 'wifi', 'Configure Wi-Fi credentials over serial', {
options: portOption,
options: Object.assign({
'file': {
description: 'Take the credentials from a JSON file instead of prompting for them'
}
}, portOption),
handler: (args) => {
const CloudCommands = require('../cmd/serial');
return new CloudCommands(args).configureWifi();
}
},
examples: {
'$0 $command': 'Prompt for Wi-Fi credentials and send them to a device over serial',
'$0 $command --file credentials.json': 'Read Wi-Fi credentials from credentials.json and send them to a device over serial'
},
epilogue: unindent(`
The JSON file for passing Wi-Fi credentials should look like this:
{
"network": "my_ssid",
"security": "WPA2_AES",
"password": "my_password"
}

The security property can be NONE, WEP, WPA2_AES, WPA2_TKIP, WPA2_AES+TKIP, WPA_AES, WPA_TKIP, WPA_AES+TKIP.
For enterprise Wi-Fi, set security to WPA_802.1x or WPA2_802.1x and provide the eap, username, outer_identity, client_certificate, private_key and root_ca properties.
`)
});

commandProcessor.createCommand(serial, 'mac', 'Ask for and display MAC address via serial', {
Expand Down
73 changes: 45 additions & 28 deletions src/cmd/serial.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ class SerialCommand {

configureWifi() {
const comPort = this.options.port;
const credentialsFile = this.options.file;

// TODO remove once we have verbose flag
settings.verboseOutput = true;
Expand All @@ -496,12 +497,33 @@ class SerialCommand {
return this.error('No serial port identified');
}

this._promptWifiScan(wifi, device);
if (credentialsFile) {
this._configWifiFromFile(wifi, device, credentialsFile);
} else {
this._promptWifiScan(wifi, device);
}
});

return wifi.promise;
}

_configWifiFromFile(wifi, device, filename) {
fs.readFile(filename, 'utf-8', (err, content) => {
if (err) {
return wifi.reject(err);
}

let opts;
try {
opts = JSON.parse(content);
} catch (err) {
return wifi.reject(err);
}

this.serialWifiConfig(device, opts).then(wifi.resolve, wifi.reject);
});
}

_promptWifiScan(wifi, device) {
prompt([
{
Expand Down Expand Up @@ -577,14 +599,14 @@ class SerialCommand {
});
}

const ssid = answers.ap;
const ap = networkMap[ssid];
const network = answers.ap;
const ap = networkMap[network];
const security = answers.detectSecurity && ap && ap.security;
if (security) {
console.log(arrow, 'Detected', security, 'security');
}

this.serialWifiConfig(device, ssid, security).then(wifiInfo.resolve, wifiInfo.reject);
this.serialWifiConfig(device, { network, security }).then(wifiInfo.resolve, wifiInfo.reject);
});

return wifiInfo.promise;
Expand Down Expand Up @@ -897,15 +919,11 @@ class SerialCommand {
}

/* eslint-disable max-statements */
serialWifiConfig(device, ssid, securityType, password, opts) {
serialWifiConfig(device, opts = {}) {
if (!device) {
return when.reject('No serial port available');
}

if (!opts) {
opts = {};
}

let isEnterprise = false;

log.verbose('Attempting to configure Wi-Fi on ' + device.port);
Expand Down Expand Up @@ -943,8 +961,8 @@ class SerialCommand {

st.addTrigger('SSID:', (cb) => {
resetTimeout();
if (ssid) {
return cb(ssid + '\n');
if (opts.network) {
return cb(opts.network + '\n');
}

prompt([{
Expand All @@ -966,23 +984,23 @@ class SerialCommand {
});
});

const parseSecurityType = (ent, cb) => {
const parsesecurity = (ent, cb) => {
resetTimeout();
if (securityType) {
if (opts.security) {
let security = 3;
if (securityType.indexOf('WPA2') >= 0 && securityType.indexOf('802.1x') >= 0) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code looks good and works, this is just a random idea.

Not sure 100% if it works, but it looks kinda cool

if (opts.security) {
  const s = opts.security.reduce((acc, current) => { return Object.assign({}, acc, { [current]: true })}, {});

  security = security || (s['WPA2'] && s['802.1x'] && 5);
  isEnterprise = security || isEnterprise;

  security = security || (s['WPA'] && s['802.1x'] && 4);
  isEnterprise = security || isEnterprise;

  security = security || (s['WEP'] && 3);
  isEnterprise = security || isEnterprise;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not my code. It doesn't have any tests so I'd rather leave it alone.

if (opts.security.indexOf('WPA2') >= 0 && opts.security.indexOf('802.1x') >= 0) {
security = 5;
isEnterprise = true;
} else if (securityType.indexOf('WPA') >= 0 && securityType.indexOf('802.1x') >= 0) {
} else if (opts.security.indexOf('WPA') >= 0 && opts.security.indexOf('802.1x') >= 0) {
security = 4;
isEnterprise = true;
} else if (securityType.indexOf('WPA2') >= 0) {
} else if (opts.security.indexOf('WPA2') >= 0) {
security = 3;
} else if (securityType.indexOf('WPA') >= 0) {
} else if (opts.security.indexOf('WPA') >= 0) {
security = 2;
} else if (securityType.indexOf('WEP') >= 0) {
} else if (opts.security.indexOf('WEP') >= 0) {
security = 1;
} else if (securityType.indexOf('NONE') >= 0) {
} else if (opts.security.indexOf('NONE') >= 0) {
security = 0;
}

Expand Down Expand Up @@ -1014,18 +1032,18 @@ class SerialCommand {
});
};

st.addTrigger('Security 0=unsecured, 1=WEP, 2=WPA, 3=WPA2:', parseSecurityType.bind(null, false));
st.addTrigger('Security 0=unsecured, 1=WEP, 2=WPA, 3=WPA2, 4=WPA Enterprise, 5=WPA2 Enterprise:', parseSecurityType.bind(null, true));
st.addTrigger('Security 0=unsecured, 1=WEP, 2=WPA, 3=WPA2:', parsesecurity.bind(null, false));
st.addTrigger('Security 0=unsecured, 1=WEP, 2=WPA, 3=WPA2, 4=WPA Enterprise, 5=WPA2 Enterprise:', parsesecurity.bind(null, true));

st.addTrigger('Security Cipher 1=AES, 2=TKIP, 3=AES+TKIP:', (cb) => {
resetTimeout();
if (securityType !== undefined) {
if (opts.security !== undefined) {
let cipherType = 1;
if (securityType.indexOf('AES') >= 0 && securityType.indexOf('TKIP') >= 0) {
if (opts.security.indexOf('AES') >= 0 && opts.security.indexOf('TKIP') >= 0) {
cipherType = 3;
} else if (securityType.indexOf('TKIP') >= 0) {
} else if (opts.security.indexOf('TKIP') >= 0) {
cipherType = 2;
} else if (securityType.indexOf('AES') >= 0) {
} else if (opts.security.indexOf('AES') >= 0) {
cipherType = 1;
}

Expand Down Expand Up @@ -1185,9 +1203,8 @@ class SerialCommand {
st.addTrigger('Password:', (cb) => {
resetTimeout();
// Skip password prompt as appropriate
if (password) {
//console.log('Password: ' + password);
cb(password + '\n', startTimeout.bind(this, 15000));
if (opts.password) {
cb(opts.password + '\n', startTimeout.bind(this, 15000));

} else {
prompt([{
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class SetupCommand {
function switchChoice(ans) {
// user wants to logout
if (!ans.switch) {
this.command('cloud').logout(true).then(() => {
return self.command('cloud').logout(true).then(() => {
self.__api.clearToken();
self.__oldapi.clearToken();
accountStatus(false);
Expand Down