added diy files more

This commit is contained in:
user 2020-10-16 19:14:34 +02:00
parent 06806b5877
commit cee616b9e1
7 changed files with 336 additions and 57 deletions

View File

@ -1,57 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="841.89px" height="595.28px" viewBox="0 0 841.89 595.28" enable-background="new 0 0 841.89 595.28" xml:space="preserve">
<path fill="none" stroke="#0000FF" stroke-miterlimit="10" d="M453.551,355.944c0-15.651-12.689-28.346-28.347-28.346h-70.874
l-14.171,13.745H141.732L128,327.599H56.693c-15.656,0-28.349,12.69-28.349,28.346v43.744l11.338,14.155v88.25l-11.337,13.752
v24.346c0,15.654,12.689,28.348,28.349,28.348h368.513c15.653,0,28.346-12.69,28.346-28.348v-24.346l-11.338-13.752v-88.25
l11.336-14.155V355.944z M375.478,366.856c3.133,0,5.669,2.539,5.669,5.67c0,3.134-2.536,5.673-5.669,5.673
c-3.132,0-5.67-2.539-5.67-5.673C369.808,369.396,372.346,366.856,375.478,366.856z M106.42,366.856c3.132,0,5.67,2.539,5.67,5.67
c0,3.134-2.538,5.673-5.67,5.673c-3.131,0-5.669-2.539-5.669-5.673C100.751,369.396,103.289,366.856,106.42,366.856z M55.51,557.649
c-3.131,0-5.669-2.537-5.669-5.668c0-3.133,2.538-5.672,5.669-5.672s5.669,2.539,5.669,5.672
C61.179,555.112,58.641,557.649,55.51,557.649z M55.51,353.654c-3.133,0-5.67-2.536-5.67-5.67c0-3.131,2.537-5.67,5.67-5.67
c3.131,0,5.669,2.539,5.669,5.67C61.179,351.118,58.641,353.654,55.51,353.654z M106.42,529.279c-3.132,0-5.67-2.537-5.67-5.67
c0-3.132,2.538-5.668,5.67-5.668s5.67,2.536,5.67,5.668C112.089,526.742,109.55,529.279,106.42,529.279z M375.477,529.279
c-3.131,0-5.669-2.537-5.669-5.67c0-3.132,2.538-5.668,5.669-5.668c3.133,0,5.669,2.536,5.669,5.668
C381.146,526.742,378.608,529.279,375.477,529.279z M166.885,426.789c0-15.652,13.077-21.795,28.733-21.795h60.634
c15.655,0,30.133,6.688,30.133,22.342l0,0h9.553l-0.137-26.31h57.333l0.038,28.951H376.8l-0.078,67.332v3.25v7.166
c0,0-1.229,4.334-6.062,4.334s-255.336,1.168-255.336,1.168s-4.83,0.166-4.83-5.5c0-5.082,0-32.139,0-37.668
c0-0.639,0-1.004,0-1.004v-73.744l56.558,0.104L166.885,426.789z M432.768,557.649c-3.133,0-5.672-2.537-5.672-5.668
c0-3.133,2.539-5.672,5.672-5.672c3.131,0,5.67,2.539,5.67,5.672C438.438,555.112,435.898,557.649,432.768,557.649z M432.77,353.654
c-3.133,0-5.668-2.536-5.668-5.67c0-3.131,2.535-5.67,5.668-5.67s5.67,2.539,5.67,5.67
C438.439,351.118,435.901,353.654,432.77,353.654z"/>
<path fill="none" stroke="#0000FF" stroke-miterlimit="10" d="M444.715,237.722v-88.25l11.336-14.155V91.573
c0-15.651-12.689-28.346-28.348-28.346H356.83l-14.171,13.745H144.232L130.5,63.227H59.193c-15.656,0-28.349,12.69-28.349,28.346
v43.744l11.338,14.155v88.25l-11.337,13.752v24.346c0,15.654,12.689,28.348,28.349,28.348h368.513
c15.652,0,28.346-12.691,28.346-28.348v-24.346L444.715,237.722z M58.009,293.278c-3.131,0-5.669-2.537-5.669-5.668
c0-3.133,2.538-5.672,5.669-5.672s5.669,2.539,5.669,5.672C63.678,290.741,61.14,293.278,58.009,293.278z M58.009,89.283
c-3.133,0-5.67-2.536-5.67-5.67c0-3.131,2.537-5.67,5.67-5.67c3.131,0,5.669,2.539,5.669,5.67
C63.678,86.747,61.14,89.283,58.009,89.283z M108.919,264.908c-3.132,0-5.67-2.537-5.67-5.67c0-3.132,2.538-5.668,5.67-5.668
s5.67,2.536,5.67,5.668C114.588,262.371,112.049,264.908,108.919,264.908z M108.919,113.828c-3.131,0-5.669-2.539-5.669-5.673
c0-3.131,2.538-5.67,5.669-5.67c3.132,0,5.67,2.539,5.67,5.67C114.589,111.289,112.051,113.828,108.919,113.828z M365,192.699
c-5.137,0-9.302-4.164-9.302-9.302v-16.096c0-5.137,4.165-9.302,9.302-9.302c5.138,0,9.303,4.165,9.303,9.302v16.097
C374.303,188.535,370.138,192.699,365,192.699z M377.976,264.908c-3.131,0-5.669-2.537-5.669-5.67c0-3.132,2.538-5.668,5.669-5.668
c3.133,0,5.669,2.536,5.669,5.668C383.645,262.371,381.108,264.908,377.976,264.908z M377.977,113.828
c-3.132,0-5.67-2.539-5.67-5.673c0-3.131,2.538-5.67,5.67-5.67c3.133,0,5.669,2.539,5.669,5.67
C383.646,111.289,381.11,113.828,377.977,113.828z M435.268,293.278c-3.133,0-5.672-2.537-5.672-5.668
c0-3.133,2.539-5.672,5.672-5.672c3.131,0,5.67,2.539,5.67,5.672C440.938,290.741,438.398,293.278,435.268,293.278z M435.27,89.283
c-3.133,0-5.668-2.536-5.668-5.67c0-3.131,2.535-5.67,5.668-5.67s5.67,2.539,5.67,5.67C440.939,86.747,438.4,89.283,435.27,89.283z"
/>
<path fill="none" stroke="#0000FF" stroke-miterlimit="10" d="M546.607,473.878h88.246l14.155,11.336h43.742
c15.65,0,28.344-12.689,28.344-28.348v-70.873l-13.744-14.171V173.39l13.744-13.732V88.351c0-15.656-12.689-28.349-28.344-28.349
h-43.742L634.854,71.34h-88.246l-13.751-11.337h-24.345c-15.653,0-28.346,12.689-28.346,28.349V456.87
c0,15.652,12.69,28.346,28.346,28.346h24.345L546.607,473.878z M491.056,87.167c0-3.131,2.537-5.669,5.668-5.669
c3.133,0,5.671,2.538,5.671,5.669s-2.538,5.669-5.671,5.669C493.593,92.836,491.056,90.298,491.056,87.167z M695.04,87.167
c0-3.133,2.536-5.67,5.67-5.67c3.131,0,5.67,2.537,5.67,5.67c0,3.131-2.539,5.669-5.67,5.669
C697.576,92.836,695.04,90.298,695.04,87.167z M519.424,138.077c0-3.132,2.537-5.67,5.669-5.67c3.133,0,5.668,2.538,5.668,5.67
s-2.535,5.67-5.668,5.67C521.961,143.746,519.424,141.207,519.424,138.077z M670.496,138.077c0-3.131,2.539-5.669,5.674-5.669
c3.131,0,5.67,2.538,5.67,5.669c0,3.132-2.539,5.67-5.67,5.67C673.035,143.747,670.496,141.209,670.496,138.077z M591.629,394.163
c0-5.137,4.164-9.302,9.302-9.302h16.095c5.136,0,9.301,4.165,9.301,9.302c0,5.138-4.165,9.303-9.301,9.303H600.93
C595.793,403.466,591.629,399.301,591.629,394.163z M519.424,407.14c0-3.131,2.537-5.67,5.669-5.67c3.133,0,5.668,2.539,5.668,5.67
c0,3.133-2.535,5.668-5.668,5.668C521.961,412.808,519.424,410.271,519.424,407.14z M670.496,407.14c0-3.131,2.539-5.67,5.674-5.67
c3.131,0,5.67,2.539,5.67,5.67c0,3.133-2.539,5.67-5.67,5.67C673.035,412.81,670.496,410.272,670.496,407.14z M491.056,464.431
c0-3.133,2.537-5.672,5.668-5.672c3.133,0,5.671,2.539,5.671,5.672c0,3.131-2.538,5.67-5.671,5.67
C493.593,470.101,491.056,467.562,491.056,464.431z M695.04,464.433c0-3.133,2.536-5.668,5.67-5.668c3.131,0,5.67,2.535,5.67,5.668
s-2.539,5.67-5.67,5.67C697.576,470.103,695.04,467.563,695.04,464.433z"/>
</svg>

Before

Width:  |  Height:  |  Size: 6.0 KiB

View File

@ -0,0 +1,166 @@
#include "./DNSServer.h"
#include <lwip/def.h>
#include <Arduino.h>
#define DEBUG
#define DEBUG_OUTPUT Serial
DNSServer::DNSServer()
{
_ttl = htonl(60);
_errorReplyCode = DNSReplyCode::NonExistentDomain;
}
bool DNSServer::start(const uint16_t &port, const String &domainName,
const IPAddress &resolvedIP)
{
_port = port;
_domainName = domainName;
_resolvedIP[0] = resolvedIP[0];
_resolvedIP[1] = resolvedIP[1];
_resolvedIP[2] = resolvedIP[2];
_resolvedIP[3] = resolvedIP[3];
downcaseAndRemoveWwwPrefix(_domainName);
return _udp.begin(_port) == 1;
}
void DNSServer::setErrorReplyCode(const DNSReplyCode &replyCode)
{
_errorReplyCode = replyCode;
}
void DNSServer::setTTL(const uint32_t &ttl)
{
_ttl = htonl(ttl);
}
void DNSServer::stop()
{
_udp.stop();
}
void DNSServer::downcaseAndRemoveWwwPrefix(String &domainName)
{
domainName.toLowerCase();
domainName.replace("www.", "");
domainName.replace("https://", "");
}
void DNSServer::processNextRequest()
{
_currentPacketSize = _udp.parsePacket();
if (_currentPacketSize)
{
_buffer = (unsigned char*)malloc(_currentPacketSize * sizeof(char));
_udp.read(_buffer, _currentPacketSize);
_dnsHeader = (DNSHeader*) _buffer;
if (_dnsHeader->QR == DNS_QR_QUERY &&
_dnsHeader->OPCode == DNS_OPCODE_QUERY &&
requestIncludesOnlyOneQuestion() &&
(_domainName == "*" || getDomainNameWithoutWwwPrefix() == _domainName)
)
{
replyWithIP();
}
else if (_dnsHeader->QR == DNS_QR_QUERY)
{
replyWithCustomCode();
}
free(_buffer);
}
}
bool DNSServer::requestIncludesOnlyOneQuestion()
{
return ntohs(_dnsHeader->QDCount) == 1 &&
_dnsHeader->ANCount == 0 &&
_dnsHeader->NSCount == 0 &&
_dnsHeader->ARCount == 0;
}
String DNSServer::getDomainNameWithoutWwwPrefix()
{
String parsedDomainName = "";
unsigned char *start = _buffer + 12;
if (*start == 0)
{
return parsedDomainName;
}
int pos = 0;
while(true)
{
unsigned char labelLength = *(start + pos);
for(int i = 0; i < labelLength; i++)
{
pos++;
parsedDomainName += (char)*(start + pos);
}
pos++;
if (*(start + pos) == 0)
{
downcaseAndRemoveWwwPrefix(parsedDomainName);
return parsedDomainName;
}
else
{
parsedDomainName += ".";
}
}
}
void DNSServer::replyWithIP()
{
_dnsHeader->QR = DNS_QR_RESPONSE;
_dnsHeader->ANCount = _dnsHeader->QDCount;
_dnsHeader->QDCount = _dnsHeader->QDCount;
//_dnsHeader->RA = 1;
_udp.beginPacket(_udp.remoteIP(), _udp.remotePort());
_udp.write(_buffer, _currentPacketSize);
_udp.write((uint8_t)192); // answer name is a pointer
_udp.write((uint8_t)12); // pointer to offset at 0x00c
_udp.write((uint8_t)0); // 0x0001 answer is type A query (host address)
_udp.write((uint8_t)1);
_udp.write((uint8_t)0); //0x0001 answer is class IN (internet address)
_udp.write((uint8_t)1);
_udp.write((unsigned char*)&_ttl, 4);
// Length of RData is 4 bytes (because, in this case, RData is IPv4)
_udp.write((uint8_t)0);
_udp.write((uint8_t)4);
_udp.write(_resolvedIP, sizeof(_resolvedIP));
_udp.endPacket();
#ifdef DEBUG
DEBUG_OUTPUT.print("DNS responds: ");
DEBUG_OUTPUT.print(_resolvedIP[0]);
DEBUG_OUTPUT.print(".");
DEBUG_OUTPUT.print(_resolvedIP[1]);
DEBUG_OUTPUT.print(".");
DEBUG_OUTPUT.print(_resolvedIP[2]);
DEBUG_OUTPUT.print(".");
DEBUG_OUTPUT.print(_resolvedIP[3]);
DEBUG_OUTPUT.print(" for ");
DEBUG_OUTPUT.println(getDomainNameWithoutWwwPrefix());
#endif
}
void DNSServer::replyWithCustomCode()
{
_dnsHeader->QR = DNS_QR_RESPONSE;
_dnsHeader->RCode = (unsigned char)_errorReplyCode;
_dnsHeader->QDCount = 0;
_udp.beginPacket(_udp.remoteIP(), _udp.remotePort());
_udp.write(_buffer, sizeof(DNSHeader));
_udp.endPacket();
}

View File

@ -0,0 +1,72 @@
#ifndef DNSServer_h
#define DNSServer_h
#include <WiFiUdp.h>
#define DNS_QR_QUERY 0
#define DNS_QR_RESPONSE 1
#define DNS_OPCODE_QUERY 0
enum class DNSReplyCode
{
NoError = 0,
FormError = 1,
ServerFailure = 2,
NonExistentDomain = 3,
NotImplemented = 4,
Refused = 5,
YXDomain = 6,
YXRRSet = 7,
NXRRSet = 8
};
struct DNSHeader
{
uint16_t ID; // identification number
unsigned char RD : 1; // recursion desired
unsigned char TC : 1; // truncated message
unsigned char AA : 1; // authoritive answer
unsigned char OPCode : 4; // message_type
unsigned char QR : 1; // query/response flag
unsigned char RCode : 4; // response code
unsigned char Z : 3; // its z! reserved
unsigned char RA : 1; // recursion available
uint16_t QDCount; // number of question entries
uint16_t ANCount; // number of answer entries
uint16_t NSCount; // number of authority entries
uint16_t ARCount; // number of resource entries
};
class DNSServer
{
public:
DNSServer();
void processNextRequest();
void setErrorReplyCode(const DNSReplyCode &replyCode);
void setTTL(const uint32_t &ttl);
// Returns true if successful, false if there are no sockets available
bool start(const uint16_t &port,
const String &domainName,
const IPAddress &resolvedIP);
// stops the DNS server
void stop();
private:
WiFiUDP _udp;
uint16_t _port;
String _domainName;
unsigned char _resolvedIP[4];
int _currentPacketSize;
unsigned char* _buffer;
DNSHeader* _dnsHeader;
uint32_t _ttl;
DNSReplyCode _errorReplyCode;
void downcaseAndRemoveWwwPrefix(String &domainName);
String getDomainNameWithoutWwwPrefix();
bool requestIncludesOnlyOneQuestion();
void replyWithIP();
void replyWithCustomCode();
};
#endif

Binary file not shown.

View File

@ -0,0 +1,98 @@
// Captive portal with (arduino) OTA + SPIFFS
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h> // Over-the-Air updates
#include <ESP8266WebServer.h>
#include "./DNSServer.h" // Dns server
#include <FS.h> // SPIFFS
DNSServer dnsServer;
const byte DNS_PORT = 53;
ESP8266WebServer server(80);
#ifndef STASSID
#define STASSID "Paracity Gallery v041"
//#define STASPSK "mypassword"
#endif
IPAddress apIP(192, 168, 4, 1);
const char* ssid = STASSID;
//const char* password = STAPSK;
void setup() {
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
WiFi.softAP(ssid);
dnsServer.start(DNS_PORT, "*", apIP); // redirect dns request to AP ip
MDNS.begin("opencoil", WiFi.softAPIP());
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.softAPIP());
//Over-the-Air updates
ArduinoOTA.setHostname("opencoil");
//ArduinoOTA.setPassword("change-me"); //disabled to allow data uploads
ArduinoOTA.begin();
SPIFFS.begin();
//redirect all traffic to index.html
server.onNotFound([]() {
if(!handleFileRead(server.uri())){
const char *metaRefreshStr = "<head><meta http-equiv=\"refresh\" content=\"0; url=http://192.168.4.1/index.html\" /></head><body><p>redirecting...</p></body>";
server.send(200, "text/html", metaRefreshStr);
}
});
server.begin();
}
void loop() {
dnsServer.processNextRequest();
ArduinoOTA.handle();
server.handleClient();
delay(50);
}
String getContentType(String filename){
if(server.hasArg("download")) return "application/octet-stream";
else if(filename.endsWith(".htm")) return "text/html";
else if(filename.endsWith(".html")) return "text/html";
else if(filename.endsWith(".css")) return "text/css";
else if(filename.endsWith(".js")) return "application/javascript";
else if(filename.endsWith(".png")) return "image/png";
else if(filename.endsWith(".gif")) return "image/gif";
else if(filename.endsWith(".jpg")) return "image/jpeg";
else if(filename.endsWith(".ico")) return "image/x-icon";
else if(filename.endsWith(".xml")) return "text/xml";
else if(filename.endsWith(".mp4")) return "video/mp4";
else if(filename.endsWith(".pdf")) return "application/x-pdf";
else if(filename.endsWith(".zip")) return "application/x-zip";
else if(filename.endsWith(".gz")) return "application/x-gzip";
return "text/plain";
}
//Given a file path, look for it in the SPIFFS file storage. Returns true if found, returns false if not found.
bool handleFileRead(String path){
if(path.endsWith("/")) path += "index.html";
String contentType = getContentType(path);
String pathWithGz = path + ".gz";
if(SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)){
if(SPIFFS.exists(pathWithGz))
path += ".gz";
File file = SPIFFS.open(path, "r");
size_t sent = server.streamFile(file, contentType);
file.close();
return true;
}
return false;
}