Skip to content

Commit

Permalink
add upload to wigle
Browse files Browse the repository at this point in the history
  • Loading branch information
rennancockles committed Sep 1, 2024
1 parent bf7130f commit fb442da
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/core/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,12 @@ int loopOptions(std::vector<Option>& options, bool bright, bool submenu, String
** Description: Função para manipular o progresso da atualização
** Dependencia: prog_handler =>> 0 - Flash, 1 - LittleFS
***************************************************************************************/
void progressHandler(int progress, size_t total) {
void progressHandler(int progress, size_t total, String message) {
int barWidth = map(progress, 0, total, 0, 200);
if(barWidth <3) {
tft.fillRect(6, 27, WIDTH-12, HEIGHT-33, BGCOLOR);
tft.drawRect(18, HEIGHT - 47, 204, 17, FGCOLOR);
displayRedStripe("Running, Wait", TFT_WHITE, FGCOLOR);
displayRedStripe(message, TFT_WHITE, FGCOLOR);
}
tft.fillRect(20, HEIGHT - 45, barWidth, 13, FGCOLOR);
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void listFiles(int index, String fileList[][3]);

void drawWireguardStatus(int x, int y);

void progressHandler(int progress, size_t total);
void progressHandler(int progress, size_t total, String message = "Running, Wait");

int getBattery();

Expand Down
6 changes: 6 additions & 0 deletions src/core/sd_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "modules/others/audio.h"
#include "modules/rf/rf.h"
#include "modules/ir/TV-B-Gone.h"
#include "modules/wifi/wigle.h"
#include "modules/others/bad_usb.h"
#include "modules/others/qrcode_menu.h"

Expand Down Expand Up @@ -653,6 +654,11 @@ String loopSD(FS &fs, bool filePicker, String allowed_ext) {
delay(200);
txSubFile(&fs, filepath);
}});
if(filepath.endsWith(".csv")) options.insert(options.begin(), {"Wigle Upload", [&]() {
delay(200);
Wigle wigle;
wigle.upload(&fs, filepath);
}});
#if defined(USB_as_HID)
if(filepath.endsWith(".txt")) {
options.push_back({"BadUSB Run", [&]() {
Expand Down
188 changes: 188 additions & 0 deletions src/modules/wifi/wigle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
/**
* @file wigle.cpp
* @author Rennan Cockles - https://github.com/rennancockles
* @brief Wigle connector
* @version 0.1
*/


#include "wigle.h"
#include "core/display.h"
#include "core/mykeyboard.h"
#include "core/wifi_common.h"
#include "core/sd_functions.h"

#define CBUFLEN 1024


Wigle::Wigle() {}

Wigle::~Wigle() {}

bool Wigle::_check_token() {
if (wigleBasicToken == "") {
displayError("Wigle token not found");
delay(1000);
return false;
}

auth_header = "Basic " + wigleBasicToken;

if(!wifiConnected) wifiConnectMenu(false);

return true;
}

bool Wigle::get_user() {
if (!_check_token()) return false;

display_banner();
padprintln("Connecting to Wigle...");

WiFiClientSecure client;
client.setInsecure();
if (!client.connect(host, 443)) return false;

client.println("GET /api/v2/profile/user HTTP/1.0");
client.print("Host: ");
client.println(host);
client.println("Connection: close");
client.println("User-Agent: bruce.wardriving");
client.print("Authorization: ");
client.println(auth_header);
client.println();

while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") break;
}

DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc, client);

client.stop();

if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return false;
}

String userid = doc["userid"];
wigle_user = userid;

delay(500);
return true;
}

void Wigle::display_banner() {
drawMainBorderWithTitle("Wigle Upload");
padprintln("\n");
}

void Wigle::dump_wigle_info() {
display_banner();
padprintln("Logged into Wigle as " + wigle_user);
padprintln("");
}

void Wigle::send_upload_headers(WiFiClientSecure &client, String filename, int filesize, String boundary) {
//Content-Disposition header size.
int cd_header_len = 147-45-8 + filename.length() + 2*(boundary.length()+2);

client.println("POST /api/v2/file/upload HTTP/1.0");
client.print("Host: ");
client.println(host);
client.println("Connection: close");
client.println("User-Agent: bruce.wardriving");
client.print("Authorization: ");
client.println(auth_header);
client.print("Content-Type: multipart/form-data; boundary=");
client.println(boundary);
client.print("Content-Length: ");
client.println(filesize + cd_header_len);
client.println();

//Start content-disposition file header:
client.println("--" + boundary);
client.print("Content-Disposition: form-data; name=\"file\"; filename=\"");
client.print(filename);
client.println("\"");
client.println("Content-Type: text/csv");
client.println();
}

bool Wigle::upload(FS *fs, String filepath) {
display_banner();

if (!fs || !get_user()) return false;

dump_wigle_info();

WiFiClientSecure client;
client.setInsecure();
if (!client.connect(host, 443)){
displayError("Wigle API connection failed");
delay(1000);
return false;
}

File file = fs->open(filepath, FILE_READ);
if (!file) {
displayError("Failed to open Wigle file");
delay(1000);
return false;
}

String filename = file.name();
int filesize = file.size();
String boundary = "BRUCE";
boundary.concat(esp_random());

send_upload_headers(client, filename, filesize, boundary);

byte cbuf[CBUFLEN];
float percent = 0;
while (file.available()){
long bytes_available = file.available();
int toread = CBUFLEN;
if (bytes_available < CBUFLEN) toread = bytes_available;
file.read(cbuf, toread);
client.write(cbuf, toread);
percent = ((float)file.position() / (float)file.size()) * 100;
progressHandler(percent, 100, "Uploading...");
}

client.println();
client.print("--");
client.print(boundary);
client.println("--");
client.println();
client.flush();

Serial.println("File transfer complete");

String serverres = "";

while (client.connected()){
if (client.available()){
char c = client.read();
// Serial.write(c);
serverres.concat(c);
}
if (serverres.length() > 1024) break;
}

client.stop();
file.close();

if (serverres.indexOf("\"success\":true") <= -1){
displayError("File upload error");
delay(1000);
return false;
}

displaySuccess("File upload success");
delay(1000);
return true;
}
40 changes: 40 additions & 0 deletions src/modules/wifi/wigle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @file wigle.h
* @author Rennan Cockles - https://github.com/rennancockles
* @brief Wigle connector
* @version 0.1
*/

#ifndef WIGLE_H
#define WIGLE_H

#include "core/globals.h"
#include <WiFiClientSecure.h>


class Wigle {
public:
/////////////////////////////////////////////////////////////////////////////////////
// Constructor
/////////////////////////////////////////////////////////////////////////////////////
Wigle();
~Wigle();

/////////////////////////////////////////////////////////////////////////////////////
// Operations
/////////////////////////////////////////////////////////////////////////////////////
bool get_user(void);
bool upload(FS *fs, String filepath);
void send_upload_headers(WiFiClientSecure &client, String filename, int filesize, String boundary);
void display_banner(void);
void dump_wigle_info(void);

private:
String wigle_user;
String auth_header;
const char* host = "api.wigle.net";

bool _check_token(void);
};

#endif // WIGLE_H

0 comments on commit fb442da

Please sign in to comment.