Skip to content

Commit

Permalink
Improved hostscan and color UI
Browse files Browse the repository at this point in the history
  • Loading branch information
pr3y committed Jun 29, 2024
1 parent a276a0d commit 06e856c
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 60 deletions.
5 changes: 3 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ default_envs =

[common]
build_flags =
-DBRUCE_VERSION='"1.0.2"'
-DBRUCE_VERSION='"1.1.0"'
-DMAXFILES=256
-DEEPROMSIZE=64
-DLH=8
Expand Down Expand Up @@ -164,7 +164,7 @@ lib_deps =

[env:m5stack-cardputer]
platform = espressif32
platform_packages[email protected]
;[email protected]
board = m5stack-stamps3
framework = arduino
board_build.partitions = custom_8Mb.csv
Expand Down Expand Up @@ -197,6 +197,7 @@ build_flags =
-DLED_OFF=LOW

-DUSER_SETUP_LOADED=1
-DUSE_HSPI_PORT=1
-DST7789_2_DRIVER=1
-DTFT_RGB_ORDER=1
-DTFT_WIDTH=135
Expand Down
40 changes: 35 additions & 5 deletions src/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,41 @@ while(1) {
** if the value is not set, it will use the default value
**********************************************************************/
void readFGCOLORFromEEPROM() {
EEPROM.get(5, FGCOLOR);
if(!FGCOLOR) {
FGCOLOR = TFT_PURPLE+0x3000;
EEPROM.put(5, FGCOLOR);
}
int colorEEPROM;

EEPROM.begin(EEPROMSIZE);
EEPROM.get(5, colorEEPROM);

switch(colorEEPROM){
case 0:
FGCOLOR = TFT_PURPLE+0x3000;
break;
case 1:
FGCOLOR = TFT_WHITE;
break;
case 2:
FGCOLOR = TFT_RED;
break;
case 3:
FGCOLOR = TFT_DARKGREEN;
break;
case 4:
FGCOLOR = TFT_BLUE;
break;
case 5:
FGCOLOR = TFT_YELLOW;
break;
case 7:
FGCOLOR = TFT_ORANGE;
break;
default:
FGCOLOR = TFT_PURPLE+0x3000;
EEPROM.put(5, 0);
EEPROM.commit();
break;

}
EEPROM.end(); // Free EEPROM memory
}


Expand Down
155 changes: 113 additions & 42 deletions src/scan_hosts.cpp
Original file line number Diff line number Diff line change
@@ -1,76 +1,147 @@
#include "globals.h"
#include "lwip/etharp.h"
#include "scan_hosts.h"
#include "display.h"
#include "mykeyboard.h"
#include "wifi_common.h"
#include "clients.h"

//thx to 7h30th3r0n3, which made scanHosts faster using ARP

std::vector<IPAddress> hostslist;

void read_arp_table(char * from_ip, int read_from, int read_to, std::vector<IPAddress>& hostslist) {
Serial.printf("Reading ARP table from: %d to %d\n", read_from, read_to);
for (int i = read_from; i <= read_to; i++) {
char test[32];
sprintf(test, "%s%d", from_ip, i);
ip4_addr_t test_ip;
ipaddr_aton(test, (ip_addr_t*)&test_ip);

const ip4_addr_t *ipaddr_ret = NULL;
struct eth_addr *eth_ret = NULL;
if (etharp_find_addr(NULL, &test_ip, &eth_ret, &ipaddr_ret) >= 0) {
IPAddress foundIP;
foundIP.fromString(ipaddr_ntoa((ip_addr_t*)&test_ip));
hostslist.push_back(foundIP);
//tft.println(foundIP.toString());
String result = foundIP.toString().substring(foundIP.toString().lastIndexOf('.') - 1);
options.push_back({result.c_str(), [=](){afterScanOptions(foundIP); }});
Serial.printf("Adding found IP: %s\n", ipaddr_ntoa((ip_addr_t*)&test_ip));
}
}
}

void send_arp(char * from_ip, std::vector<IPAddress>& hostslist) {
Serial.println("Sending ARP requests to the whole network");
const TickType_t xDelay = (10) / portTICK_PERIOD_MS;
void * netif = NULL;
tcpip_adapter_get_netif(TCPIP_ADAPTER_IF_STA, &netif);
struct netif *netif_interface = (struct netif *)netif;

for (char i = 1; i < 254; i++) {
char test[32];
sprintf(test, "%s%d", from_ip, i);
ip4_addr_t test_ip;
ipaddr_aton(test, (ip_addr_t*)&test_ip);

int8_t arp_request_ret = etharp_request(netif_interface, &test_ip);
vTaskDelay(xDelay);
}
// Lire toutes les entrées de la table ARP
read_arp_table(from_ip, 1, 254, hostslist);
}

void logPingResult(IPAddress host, bool responded) {
char buffer[64]; // Tamanho do buffer ajustável conforme necessário
void logARPResult(IPAddress host, bool responded) {
char buffer[64];
if (responded) {
sprintf(buffer, "Host %s respondeu ao ping.", host.toString().c_str());
sprintf(buffer, "Host %s respond to ARP.", host.toString().c_str());
} else {
sprintf(buffer, "Host %s não respondeu ao ping.", host.toString().c_str());
sprintf(buffer, "Host %s did not respond to ARP.", host.toString().c_str());
}
log_d("%s",buffer);
Serial.println(buffer);
}

bool pingHost(IPAddress host) {
int count = Ping.ping(host, 1); // Ping com 1 tentativa
bool responded = (count > 0);
logPingResult(host, responded);
bool arpRequest(IPAddress host) {
char ipStr[16];
sprintf(ipStr, "%s", host.toString().c_str());
ip4_addr_t test_ip;
ipaddr_aton(ipStr, (ip_addr_t*)&test_ip);

struct eth_addr *eth_ret = NULL;
const ip4_addr_t *ipaddr_ret = NULL;
bool responded = etharp_find_addr(NULL, &test_ip, &eth_ret, &ipaddr_ret) >= 0;
logARPResult(host, responded);
return responded;
}


void local_scan_setup() {
if(!wifiConnected) wifiConnectMenu(false);

int lastDot = WiFi.localIP().toString().lastIndexOf('.');
String networkRange = WiFi.localIP().toString().substring(0, lastDot + 1);
char networkRangeChar[12];

networkRange.toCharArray(networkRangeChar, sizeof(networkRangeChar));

send_arp(networkRangeChar, hostslist);

options = {};

IPAddress gatewayIP;
IPAddress subnetMask;
std::vector<IPAddress> responderam;
drawMainBorder();
tft.setTextSize(FP);
tft.setCursor(8,30);
std::vector<IPAddress> hostslist;

gatewayIP = WiFi.gatewayIP();
subnetMask = WiFi.subnetMask();

IPAddress network = WiFi.localIP();
network[3] = 0; // Define o endereço de rede para o primeiro host

int numHosts = 254 - subnetMask[3]; // Calcula o número de hosts possíveis na rede
tft.println("Probing " + String(numHosts) + " hosts (" + String(numHosts/40 + 1).substring(0,4) + " lines)");
tft.fillRect(0,38,WIDTH,LH*(numHosts/40 + 1), BGCOLOR);
tft.drawRect(0,38,WIDTH,LH*(numHosts/40 + 1), FGCOLOR);
tft.fillRect(6,38,WIDTH-12,LH*(numHosts/40 + 1), BGCOLOR);
network[3] = 0;

int numHosts = 254 - subnetMask[3];

displayRedStripe("Probing " + String(numHosts) + " hosts",TFT_WHITE, FGCOLOR);

bool foundHosts;
bool stopScan;

char base_ip[16];
sprintf(base_ip, "%d.%d.%d.", network[0], network[1], network[2]);

send_arp(base_ip, hostslist);

for (int i = 1; i <= numHosts; i++) {
if (stopScan) {
break;
}

IPAddress currentIP = network;
currentIP[3] = i;

if (pingHost(currentIP)) {
tft.print("x");
responderam.push_back(currentIP);
} else tft.print(".");

if(checkEscPress()) i=256; //ends for loop

}
options = {};
log_d("Hosts que responderam ao ping:");
for (IPAddress ip : responderam) {
String txt = "..." + String(ip[2]) + "." + String(ip[3]);
options.push_back({ txt.c_str(), [=](){ afterScanOptions(ip); }});
if (arpRequest(currentIP)) {
hostslist.push_back(currentIP);
foundHosts = true;
}
}

while(!checkEscPress()) {
loopOptions(options);
delay(300);
if (!foundHosts) {
tft.println("No hosts found");
delay(2000);
return;
}


delay(200);
loopOptions(options);
delay(200);




}


void afterScanOptions(IPAddress ip) {
std::vector<std::pair<std::string, std::function<void()>>> option;
option = {
Expand All @@ -87,7 +158,7 @@ void afterScanOptions(IPAddress ip) {
void scanPorts(IPAddress host) {
WiFiClient client;
const int ports[] = {20, 21, 22, 23, 25, 80, 137, 139, 443, 3389, 8080, 8443, 9090};
const int numPorts = sizeof(ports) / sizeof(ports[0]);
const int numPorts = sizeof(ports) / sizeof(ports[0]);
drawMainBorder();
tft.setTextSize(FP);
tft.setCursor(8,30);
Expand All @@ -96,17 +167,17 @@ void scanPorts(IPAddress host) {
tft.print("Ports Opened: ");
//for (int port = start; port <= stop; port++) {
for (int i = 0; i < numPorts; i++) {
int port = ports[i];
if (client.connect(host, port)) {
int port = ports[i];
if (client.connect(host, port)) {
if (tft.getCursorX()>(240-LW*4)) tft.setCursor(7,tft.getCursorY() + LH);
tft.print(port);
tft.print(", ");
tft.print(", ");
client.stop();
} else tft.print(".");
} else tft.print(".");
}
tft.setCursor(8,tft.getCursorY()+16);
tft.print("Done!");

while(checkSelPress()) yield();
while(!checkSelPress()) yield();
}
}
21 changes: 10 additions & 11 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,24 +118,23 @@ NTPClient timeClient(ntpUDP, ntpServer, selectedTimezone, daylightOffset_sec);


void setUIColor(){
//eeprom.begin(EEPROMSIZE);
EEPROM.begin(EEPROMSIZE);
//int color = EEPROM.read(5);
EEPROM.end();

options = {
{"Default", [&]() { FGCOLOR=TFT_PURPLE+0x3000; }},
{"White", [&]() { FGCOLOR=TFT_WHITE; }},
{"Red", [&]() { FGCOLOR=TFT_RED; }},
{"Green", [&]() { FGCOLOR=TFT_DARKGREEN; }},
{"Blue", [&]() { FGCOLOR=TFT_BLUE; }},
{"Yellow", [&]() { FGCOLOR=TFT_YELLOW; }},
{"Magenta", [&]() { FGCOLOR=TFT_MAGENTA; }},
{"Orange", [&]() { FGCOLOR=TFT_ORANGE; }},
{"Default", [&]() { FGCOLOR=TFT_PURPLE+0x3000;EEPROM.write(5,0);EEPROM.commit(); }},
{"White", [&]() { FGCOLOR=TFT_WHITE; EEPROM.write(5,1);EEPROM.commit(); }},
{"Red", [&]() { FGCOLOR=TFT_RED; EEPROM.write(5,2);EEPROM.commit(); }},
{"Green", [&]() { FGCOLOR=TFT_DARKGREEN; EEPROM.write(5,3);EEPROM.commit(); }},
{"Blue", [&]() { FGCOLOR=TFT_BLUE; EEPROM.write(5,4);EEPROM.commit(); }},
{"Yellow", [&]() { FGCOLOR=TFT_YELLOW; EEPROM.write(5,5);EEPROM.commit(); }},
{"Magenta", [&]() { FGCOLOR=TFT_MAGENTA; EEPROM.write(5,6);EEPROM.commit(); }},
{"Orange", [&]() { FGCOLOR=TFT_ORANGE; EEPROM.write(5,7);EEPROM.commit(); }},
};
delay(200);
loopOptions(options);
tft.setTextColor(TFT_BLACK, FGCOLOR);
EEPROM.write(5, FGCOLOR);
EEPROM.end();
}


Expand Down

0 comments on commit 06e856c

Please sign in to comment.