|
| 1 | +#include "Arduino.h" |
| 2 | + |
| 3 | +#include "Netdump.h" |
| 4 | +#include <ESP8266WiFi.h> |
| 5 | +#include <ESP8266WebServer.h> |
| 6 | +#include <ESP8266mDNS.h> |
| 7 | +//#include <FS.h> |
| 8 | +#include <LittleFS.h> |
| 9 | +#include <map> |
| 10 | + |
| 11 | +using namespace NetCapture; |
| 12 | + |
| 13 | +#ifndef STASSID |
| 14 | +#define STASSID "your-ssid" |
| 15 | +#define STAPSK "your-password" |
| 16 | +#endif |
| 17 | + |
| 18 | +const char* ssid = STASSID; |
| 19 | +const char* password = STAPSK; |
| 20 | + |
| 21 | +Netdump nd; |
| 22 | + |
| 23 | +//FS* filesystem = &SPIFFS; |
| 24 | +FS* filesystem = &LittleFS; |
| 25 | + |
| 26 | +ESP8266WebServer webServer(80); // Used for sending commands |
| 27 | +WiFiServer tcpServer(8000); // Used to show netcat option. |
| 28 | +File tracefile; |
| 29 | + |
| 30 | +std::map<PacketType, int> packetCount; |
| 31 | + |
| 32 | +enum class SerialOption : uint8_t { |
| 33 | + AllFull, |
| 34 | + LocalNone, |
| 35 | + HTTPChar |
| 36 | +}; |
| 37 | + |
| 38 | +void startSerial(SerialOption option) { |
| 39 | + switch (option) { |
| 40 | + case SerialOption::AllFull : //All Packets, show packet summary. |
| 41 | + nd.printDump(Serial, Packet::PacketDetail::FULL); |
| 42 | + break; |
| 43 | + |
| 44 | + case SerialOption::LocalNone : // Only local IP traffic, full details |
| 45 | + nd.printDump(Serial, Packet::PacketDetail::NONE, |
| 46 | + [](Packet n) { |
| 47 | + return (n.hasIP(WiFi.localIP())); |
| 48 | + } |
| 49 | + ); |
| 50 | + break; |
| 51 | + case SerialOption::HTTPChar : // Only HTTP traffic, show packet content as chars |
| 52 | + nd.printDump(Serial, Packet::PacketDetail::CHAR, |
| 53 | + [](Packet n) { |
| 54 | + return (n.isHTTP()); |
| 55 | + } |
| 56 | + ); |
| 57 | + break; |
| 58 | + default : |
| 59 | + Serial.printf("No valid SerialOption provided\r\n"); |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +void startTracefile() { |
| 64 | + // To file all traffic, format pcap file |
| 65 | + tracefile = filesystem->open("/tr.pcap", "w"); |
| 66 | + nd.fileDump(tracefile); |
| 67 | +} |
| 68 | + |
| 69 | +void startTcpDump() { |
| 70 | + // To tcpserver, all traffic. |
| 71 | + tcpServer.begin(); |
| 72 | + nd.tcpDump(tcpServer); |
| 73 | +} |
| 74 | + |
| 75 | +void setup(void) { |
| 76 | + Serial.begin(115200); |
| 77 | + |
| 78 | + WiFi.mode(WIFI_STA); |
| 79 | + WiFi.begin(ssid, password); |
| 80 | + |
| 81 | + if (WiFi.waitForConnectResult() != WL_CONNECTED) { |
| 82 | + Serial.println("WiFi Failed, stopping sketch"); |
| 83 | + while (1) { |
| 84 | + delay(1000); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if (!MDNS.begin("netdumphost")) { |
| 89 | + Serial.println("Error setting up MDNS responder!"); |
| 90 | + } |
| 91 | + |
| 92 | + filesystem->begin(); |
| 93 | + |
| 94 | + webServer.on("/list", |
| 95 | + []() { |
| 96 | + Dir dir = filesystem->openDir("/"); |
| 97 | + String d = "<h1>File list</h1>"; |
| 98 | + while (dir.next()) { |
| 99 | + d.concat("<li>" + dir.fileName() + "</li>"); |
| 100 | + } |
| 101 | + webServer.send(200, "text.html", d); |
| 102 | + } |
| 103 | + ); |
| 104 | + |
| 105 | + webServer.on("/req", |
| 106 | + []() { |
| 107 | + static int rq = 0; |
| 108 | + String a = "<h1>You are connected, Number of requests = " + String(rq++) + "</h1>"; |
| 109 | + webServer.send(200, "text/html", a); |
| 110 | + } |
| 111 | + ); |
| 112 | + |
| 113 | + webServer.on("/reset", |
| 114 | + []() { |
| 115 | + nd.reset(); |
| 116 | + tracefile.close(); |
| 117 | + tcpServer.close(); |
| 118 | + webServer.send(200, "text.html", "<h1>Netdump session reset</h1>"); |
| 119 | + } |
| 120 | + ); |
| 121 | + |
| 122 | + webServer.serveStatic("/", *filesystem, "/"); |
| 123 | + webServer.begin(); |
| 124 | + |
| 125 | + startSerial(SerialOption::AllFull); // Serial output examples, use enum SerialOption for selection |
| 126 | + |
| 127 | + // startTcpDump(); // tcpdump option |
| 128 | + // startTracefile(); // output to SPIFFS or LittleFS |
| 129 | + |
| 130 | + // use a self provide callback, this count network packets |
| 131 | + /* |
| 132 | + nd.setCallback( |
| 133 | + [](Packet p) |
| 134 | + { |
| 135 | + Serial.printf("PKT : %s : ",p.sourceIP().toString().c_str()); |
| 136 | + for ( auto pp : p.allPacketTypes()) |
| 137 | + { |
| 138 | + Serial.printf("%s ",pp.toString().c_str()); |
| 139 | + packetCount[pp]++; |
| 140 | + } |
| 141 | + Serial.printf("\r\n CNT "); |
| 142 | + for (auto pc : packetCount) |
| 143 | + { |
| 144 | + Serial.printf("%s %d ", pc.first.toString().c_str(),pc.second); |
| 145 | + } |
| 146 | + Serial.printf("\r\n"); |
| 147 | + } |
| 148 | + ); |
| 149 | + */ |
| 150 | +} |
| 151 | + |
| 152 | +void loop(void) { |
| 153 | + webServer.handleClient(); |
| 154 | + MDNS.update(); |
| 155 | +} |
| 156 | + |
0 commit comments