E' da un po di tempo che provo a proteggere l'accesso alla shield con una password di protezione.
Sto provando con l' HTTP/1.1 401, ma è un po ostico da utilizzare :S
In pratica ho scritto una pagina per il controllo di alcuni led da remoto, e per poterlo collegare alla rete di casa ho bisogno cmq di un minimo di protezione.
Qualcuno sa come utilizzarla?
il mio codice per la pagina è il seguente:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 220 };
EthernetServer server(80);
void setup(){
for (byte i = 22; i < 38; i++) pinMode(i, OUTPUT);
pinMode(10, OUTPUT); digitalWrite(10, 1); // ethernet
pinMode(4, OUTPUT); digitalWrite(4, 1); // SD dello shield anche se non si usa
Ethernet.begin(mac, ip);
}
void loop(){
EthernetClient client = server.available();
if (client) {
String readString;
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
int8_t c = client.read();
readString += (char)c;
if (c == '\n' && currentLineIsBlank) {
// PAGINA HTML
client.println(F("HTTP/1.1 200 OK\n Content-Type: text/html\n"));
client.print(F("<html><head><title>Web Led FC POWER</title><meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' ></head><body>"));
client.print("<h1><strong> Web Led FC POWER</strong></h1>");
for (byte i = 22; i < 38; i++){
if(readString.indexOf("on_"+String(i)) > 0) digitalWrite(i, 1);
if(readString.indexOf("off_"+String(i)) > 0) digitalWrite(i, 0);
client.print("<table border=\"0\" style=\"width:250px\"><tr><td> RELE "+ String(i));
if (digitalRead(i)) client.print(F("</td> <td><input disabled=\"disabled\" maxLength=\"20\" size=\"2\" value=\"ON\" STYLE=\"background-color:#00FF00\"/></td>"));
else client.print(F("</td> <td><input disabled=\"disabled\" maxLength=\"20\" size=\"2\" value=\"OFF\" STYLE=\"background-color:#C0C0C0\"/></td>"));
client.print("<td><input type=\"button\" style=\"width:35px; height:35px\" value=\"On\" onclick =\" location.href='/?on_"+String(i)+"'\"></td>");
client.print("<td><input type=\"button\" style=\"width:35px; height:35px\" value=\"Off\" onclick =\" location.href='/?off_"+String(i)+"'\"></td></tr></table>");
}
client.println(F("</body></html>"));
readString="";
delay(1);
client.flush();
client.stop();
}
}
}
}
}
mi hanno consigliato di usare questa parte di codice per protezione, qualcuno puo darmi qualche diritta??
client.print(F("HTTP/1.1 401 Authorization Required\r\n"));
client.print("WWW-Authenticate: Basic realm=\"Inserisci nome utente e password\"\r\n");
client.println("\"location.href='index");
if(readString.indexOf("YWRtaW5hZG1pbg==") >0){
Io trovai questa soluzione e la implementai nel mio codice.
Quando accedi ti chiede user e password andando alla routine "SendAuthentificationpage".
Se errati ti da errore, se esatti invece ti manda alla pagina web creata alla routine "SendOKpage"
Sostituisci "user:password" con i tuoi dati ma codificati in base64
void loop() {
EthernetClient client = server.available();
//---------------------------aggiunta------------------------------------------
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
readString.concat(c);
if (c == '\n' && currentLineIsBlank) {
//Serial.print(readString);
//--------------------------------------------------------------------------------
if (client) {
Serial.println("new client");
memset(linebuf,0,sizeof(linebuf));
charcount=0;
authentificated=false;
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
linebuf[charcount]=c;
if (charcount<sizeof(linebuf)-1) charcount++;
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
if (authentificated)
SendOKpage(client);
else
SendAuthentificationpage(client);
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
//user-user
if (strstr(linebuf,"Authorization: Basic")>0 && strstr(linebuf,"user:passwd")>0)//user e passwd devono essere in base64 code
authentificated=true;
memset(linebuf,0,sizeof(linebuf));
charcount=0;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
}
}
}
}
void SendOKpage(EthernetClient &client)
{
// send a standard http response header
client.println(F("HTTP/1.1 200 OK"));
client.println(F("Content-Type: text/html"));
client.println();
client.print(F("<html><head><title>Sito Bertu</title><meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' ></head><body>"));
client.println("<h2> SENSORI CASA BERTU</h2><hr />");
// qui pagina web
client.println("<hr />");
client.println("<hr />");
// chiudo il div
client.println("</div>");
client.print(F("</body></html>"));
readString="";
delay(1);
client.flush();
// add a meta refresh tag, so the browser pulls again every 5 seconds:
client.println("<meta http-equiv=\"refresh\" content=\"30\">");
}
void SendAuthentificationpage(EthernetClient &client)
{
client.println("HTTP/1.1 401 Authorization Required");
client.println("WWW-Authenticate: Basic realm=\"Secure Area\"");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<HTML> <HEAD> <TITLE>Sensori casa bertu</TITLE>");
client.println(" </HEAD> <BODY><H1>401 Unauthorized.</H1></BODY> </HTML>");
}
Ovviamente ti ho messo un estratto del mio sketch e forse qualche cosa si é perso ma quello che devi capire é come funziona ed adattarlo alle tue esigenze.
Forse ti serve dichiarare questo ma tieni presente che io non sono un esperto. La mia specialitá é il copia incolla
char linebuf[80];
int charcount=0;
boolean authentificated=false;