Work in progress...

pull/206/merge^2
dannybackx 8 years ago
parent c402bd62b9
commit a33dc84c05
  1. 97
      esp-link/cgiwifi.c
  2. 19
      html/ui.js
  3. 64
      html/wifi/wifiSta.html
  4. 62
      html/wifi/wifiSta.js

@ -34,7 +34,7 @@ bool mdns_started = false;
// ===== wifi status change callbacks // ===== wifi status change callbacks
static WifiStateChangeCb wifi_state_change_cb[4]; static WifiStateChangeCb wifi_state_change_cb[4];
// Temp store for new staion config // Temp store for new station config
struct station_config stconf; struct station_config stconf;
// Temp store for new ap config // Temp store for new ap config
@ -54,6 +54,8 @@ static char *wifiReasons[] = {
static char *wifiMode[] = { 0, "STA", "AP", "AP+STA" }; static char *wifiMode[] = { 0, "STA", "AP", "AP+STA" };
static char *wifiPhy[] = { 0, "11b", "11g", "11n" }; static char *wifiPhy[] = { 0, "11b", "11g", "11n" };
static char *portMode[] = { "unsecure", "disabled", "secure" };
void (*wifiStatusCb)(uint8_t); // callback when wifi status changes void (*wifiStatusCb)(uint8_t); // callback when wifi status changes
static char* ICACHE_FLASH_ATTR wifiGetReason(void) { static char* ICACHE_FLASH_ATTR wifiGetReason(void) {
@ -824,6 +826,99 @@ int ICACHE_FLASH_ATTR cgiWifiInfo(HttpdConnData *connData) {
return HTTPD_CGI_DONE; return HTTPD_CGI_DONE;
} }
static char *portMode2string(int8_t m) {
if (m < 0 || m > 2) return "?";
return portMode[m];
}
// print various Wifi information into json buffer
int ICACHE_FLASH_ATTR printWiFiSecurity(char *buff) {
int len;
len = os_sprintf(buff,
"\"port1mode\" : \"%s\", \"port1portnumber\" : \"%d\", \"port1pwd\" : \"%s\", "
"\"port2mode\" : \"%s\", \"port2portnumber\" : \"%d\", \"port2pwd\" : \"%s\" ",
portMode2string(flashConfig.port1_mode), flashConfig.port1_portnumber, "",
portMode2string(flashConfig.port2_mode), flashConfig.port2_portnumber, ""
);
return len;
}
// Cgi to return various Wifi information
int ICACHE_FLASH_ATTR jsonWiFiSecurity(HttpdConnData *connData) {
char buff[1024];
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
os_strcpy(buff, "{");
printWiFiSecurity(buff+1);
os_strcat(buff, "}");
jsonHeader(connData, 200);
httpdSend(connData, buff, -1);
return HTTPD_CGI_DONE;
}
// Change security settings
int ICACHE_FLASH_ATTR cgiWiFiSecurity(HttpdConnData *connData) {
char dhcp[8];
char staticip[20];
char netmask[20];
char gateway[20];
if (connData->conn==NULL) return HTTPD_CGI_DONE;
// get args and their string lengths
int dl = httpdFindArg(connData->getArgs, "dhcp", dhcp, sizeof(dhcp));
int sl = httpdFindArg(connData->getArgs, "staticip", staticip, sizeof(staticip));
int nl = httpdFindArg(connData->getArgs, "netmask", netmask, sizeof(netmask));
int gl = httpdFindArg(connData->getArgs, "gateway", gateway, sizeof(gateway));
if (!(dl > 0 && sl >= 0 && nl >= 0 && gl >= 0)) {
jsonHeader(connData, 400);
httpdSend(connData, "Request is missing fields", -1);
return HTTPD_CGI_DONE;
}
char url[64]; // redirect URL
if (os_strcmp(dhcp, "off") == 0) {
// parse static IP params
struct ip_info ipi;
bool ok = parse_ip(staticip, &ipi.ip);
if (nl > 0) ok = ok && parse_ip(netmask, &ipi.netmask);
else IP4_ADDR(&ipi.netmask, 255, 255, 255, 0);
if (gl > 0) ok = ok && parse_ip(gateway, &ipi.gw);
else ipi.gw.addr = 0;
if (!ok) {
jsonHeader(connData, 400);
httpdSend(connData, "Cannot parse static IP config", -1);
return HTTPD_CGI_DONE;
}
// save the params in flash
flashConfig.staticip = ipi.ip.addr;
flashConfig.netmask = ipi.netmask.addr;
flashConfig.gateway = ipi.gw.addr;
// construct redirect URL
os_sprintf(url, "{\"url\": \"http://%d.%d.%d.%d\"}", IP2STR(&ipi.ip));
} else {
// dynamic IP
flashConfig.staticip = 0;
os_sprintf(url, "{\"url\": \"http://%s\"}", flashConfig.hostname);
}
configSave(); // ignore error...
// schedule change-over
os_timer_disarm(&reassTimer);
os_timer_setfn(&reassTimer, configWifiIP, NULL);
os_timer_arm(&reassTimer, 1000, 0); // 1 second for the response of this request to make it
// return redirect info
jsonHeader(connData, 200);
httpdSend(connData, url, -1);
return HTTPD_CGI_DONE;
}
// Check string againt invalid characters // Check string againt invalid characters
int ICACHE_FLASH_ATTR checkString(char *str){ int ICACHE_FLASH_ATTR checkString(char *str){
for(int i=0; i<os_strlen(str); i++) { for(int i=0; i<os_strlen(str); i++) {

@ -302,6 +302,25 @@ function getWifiInfo() {
function(s, st) { window.setTimeout(getWifiInfo, 1000); }); function(s, st) { window.setTimeout(getWifiInfo, 1000); });
} }
function showWifiSecurity(data) {
Object.keys(data).forEach(function(v) {
el = $("#secure-" + v);
if (el != null) {
if (el.nodeName === "INPUT") el.value = data[v];
else el.innerHTML = data[v];
}
});
var p1 = $('#secure-port1-r'+data.port1mode);
if (p1) p1.click();
var p2 = $('#secure-port2-r'+data.port1mode);
if (p2) p2.click();
}
function getSecurityInfo() {
ajaxJson('GET', "/wifi/security", showWifiSecurity,
function(s, st) { window.setTimeout(getSecurityInfo, 1000); });
}
//===== System info //===== System info
function showSystemInfo(data) { function showSystemInfo(data) {

@ -65,7 +65,60 @@
<button id="special-button" type="submit" class="pure-button button-primary">Change!</button> <button id="special-button" type="submit" class="pure-button button-primary">Change!</button>
</form> </form>
</div></div> </div></div>
<div class="pure-u-1 pure-u-md-1-2"><div class="card">
<h1>Security Settings</h1>
<form action="#" id="securform" class="pure-form">
<legend>Disable or protect station access</legend>
<div class="form-horizontal">
Port 1
<label for="port1-ron" style="margin-right:1em">
<input type="radio" name="port1" value="on" id="secure-port1-ron"/>
Unsecure</label>
<label for="port1-roff" style="margin-right:1em">
<input type="radio" name="port1" value="off" id="secure-port1-roff"/>
Disabled</label>
<label for="port1-rpwd">
<input type="radio" name="port1" value="pwd" id="secure-port1-rpwd"/>
Secure</label>
</div>
<div id="port1-off" class="pure-form-stacked"></div>
<div id="port1-pn" class="pure-form-stacked">
<label>TCP port number</label>
<input id="secure-port1portnumber" type="text" name="port1number"/>
</div>
<div id="port1-pwd" class="pure-form-stacked">
<label>Password</label>
<input id="secure-port1password" type="password" name="port1password"/>
</div>
<div class="form-horizontal">
Port 2
<label for="port2-ron" style="margin-right:1em">
<input type="radio" name="port2" value="on" id="secure-port2-ron"/>
Unsecure</label>
<label for="port2-roff" style="margin-right:1em">
<input type="radio" name="port2" value="off" id="secure-port2-roff"/>
Disabled</label>
<label for="port2-rpwd">
<input type="radio" name="port2" value="pwd" id="secure-port2-rpwd"/>
Secured</label>
</div>
<div id="port2-off" class="pure-form-stacked"></div>
<div id="port2-pn" class="pure-form-stacked">
<label>TCP port number</label>
<input id="secure-port2portnumber" type="text" name="port2number"/>
</div>
<div id="port2-pwd" class="pure-form-stacked">
<label>Password</label>
<input id="secure-port2password" type="password" name="port2password"/>
</div>
<button id="secure-button" type="submit" class="pure-button button-primary">Change!</button>
</form>
</div></div>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
@ -78,6 +131,17 @@ onLoad(function() {
getWifiInfo(); getWifiInfo();
bnd($("#wifiform"), "submit", changeWifiAp); bnd($("#wifiform"), "submit", changeWifiAp);
bnd($("#specform"), "submit", changeSpecial); bnd($("#specform"), "submit", changeSpecial);
getSecurityInfo();
bnd($("#securform"), "submit", changeSecurity);
bnd($("#secure-port1-roff"), "click", doPort1Off);
bnd($("#secure-port1-ron"), "click", doPort1Unsecure);
bnd($("#secure-port1-rpwd"), "click", doPort1Secure);
bnd($("#secure-port2-roff"), "click", doPort2Off);
bnd($("#secure-port2-ron"), "click", doPort2Unsecure);
bnd($("#secure-port2-rpwd"), "click", doPort2Secure);
bnd($("#dhcp-ron"), "click", doDhcp); bnd($("#dhcp-ron"), "click", doDhcp);
bnd($("#dhcp-roff"), "click", doStatic); bnd($("#dhcp-roff"), "click", doStatic);
scanTimeout = window.setTimeout(scanAPs, 500); scanTimeout = window.setTimeout(scanAPs, 500);

@ -192,6 +192,68 @@ function changeSpecial(e) {
}); });
} }
function changeSecurity(e) {
e.preventDefault();
var url = "security";
url += "?port1=" + document.querySelector('input[name="port1"]:checked').value;
url += "&portnumber=" + encodeURIComponent($("#port1-portnumber").value);
url += "&password=" + encodeURIComponent($("#port1-password").value);
url += "?port2=" + document.querySelector('input[name="port2"]:checked').value;
url += "&portnumber=" + encodeURIComponent($("#port2-portnumber").value);
url += "&password=" + encodeURIComponent($("#port2-password").value);
hideWarning();
var cb = $("#secure-button");
addClass(cb, 'pure-button-disabled');
ajaxSpin("POST", url, function(resp) {
removeClass(cb, 'pure-button-disabled');
getSecurityInfo();
//getWifiInfo(); // it takes 1 second for new settings to be applied
}, function(s, st) {
showWarning("Error: "+st);
removeClass(cb, 'pure-button-disabled');
getSecurityInfo();
});
}
function doPort1Secure() {
$('#port1-off').setAttribute('hidden', '');
$('#port1-pn').removeAttribute('hidden');
$('#port1-pwd').removeAttribute('hidden');
}
function doPort1Off() {
$('#port1-off').removeAttribute('hidden');
$('#port1-pn').setAttribute('hidden', '');
$('#port1-pwd').setAttribute('hidden', '');
}
function doPort1Unsecure() {
$('#port1-off').setAttribute('hidden', '');
$('#port1-pn').removeAttribute('hidden');
$('#port1-pwd').setAttribute('hidden', '');
}
function doPort2Secure() {
$('#port2-off').setAttribute('hidden', '');
$('#port2-pn').removeAttribute('hidden');
$('#port2-pwd').removeAttribute('hidden');
}
function doPort2Off() {
$('#port2-off').removeAttribute('hidden');
$('#port2-pn').setAttribute('hidden', '');
$('#port2-pwd').setAttribute('hidden', '');
}
function doPort2Unsecure() {
$('#port2-off').setAttribute('hidden', '');
$('#port2-pn').removeAttribute('hidden');
$('#port2-pwd').setAttribute('hidden', '');
}
function doDhcp() { function doDhcp() {
$('#dhcp-on').removeAttribute('hidden'); $('#dhcp-on').removeAttribute('hidden');
$('#dhcp-off').setAttribute('hidden', ''); $('#dhcp-off').setAttribute('hidden', '');

Loading…
Cancel
Save