acid-drop- Hacking the planet from a LilyGo T-Deck using custom firmware |
git clone git://git.acid.vegas/acid-drop.git |
Log | Files | Refs | Archive | README | LICENSE |
Network.cpp (7597B)
1 #include "Network.h" 2 3 4 std::vector<WiFiNetwork> wifiNetworks; 5 int selectedNetworkIndex = 0; 6 7 WireGuard wg; 8 9 10 void connectToWiFi(String ssid, String password) { 11 wifiNetworks.clear(); 12 Serial.println("Connecting to WiFi network: " + ssid); 13 WiFi.begin(ssid.c_str(), password.c_str()); 14 15 int attempts = 0; 16 while (WiFi.status() != WL_CONNECTED && attempts < 20) { 17 delay(500); 18 displayCenteredText("CONNECTING TO " + ssid); 19 attempts++; 20 } 21 22 if (WiFi.status() == WL_CONNECTED) { 23 displayCenteredText("CONNECTED TO " + ssid); 24 25 updateTimeFromNTP(); 26 27 preferences.begin("config", false); 28 preferences.putString("wifi_ssid", ssid); 29 preferences.putString("wifi_password", password); 30 preferences.end(); 31 Serial.println("Stored WiFi credentials updated"); 32 33 wifi_ssid = ssid; 34 wifi_password = password; 35 } else { 36 Serial.println("Failed to connect to WiFi network: " + ssid); 37 displayCenteredText("WIFI CONNECTION FAILED"); 38 39 preferences.begin("config", false); 40 preferences.putString("wifi_ssid", ""); 41 preferences.putString("wifi_password", ""); 42 preferences.end(); 43 Serial.println("Stored WiFi credentials removed"); 44 45 wifi_ssid = ""; 46 wifi_password = ""; 47 48 scanWiFiNetworks(); 49 } 50 } 51 52 53 void displayPasswordInputLine() { 54 tft.fillRect(0, SCREEN_HEIGHT - INPUT_LINE_HEIGHT, SCREEN_WIDTH, INPUT_LINE_HEIGHT, TFT_BLACK); 55 tft.setCursor(0, SCREEN_HEIGHT - INPUT_LINE_HEIGHT); 56 tft.setTextColor(TFT_WHITE); 57 tft.setTextSize(1); 58 tft.print("> " + inputBuffer); 59 } 60 61 62 void displayWiFiNetworks() { 63 tft.fillRect(0, STATUS_BAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - STATUS_BAR_HEIGHT, TFT_BLACK); 64 65 tft.setTextSize(1); 66 tft.setTextColor(TFT_CYAN); 67 tft.setCursor(0, STATUS_BAR_HEIGHT); 68 tft.printf("# CH RSSI ENC SSID\n"); 69 tft.setTextColor(TFT_WHITE); 70 71 int maxDisplayedLines = (SCREEN_HEIGHT - STATUS_BAR_HEIGHT - CHAR_HEIGHT) / (CHAR_HEIGHT + LINE_SPACING); 72 int startIdx = selectedNetworkIndex >= maxDisplayedLines ? selectedNetworkIndex - maxDisplayedLines + 1 : 0; 73 74 for (int i = startIdx; i < wifiNetworks.size() && i < startIdx + maxDisplayedLines; ++i) { 75 displayWiFiNetwork(i, i - startIdx + 1); 76 } 77 } 78 79 80 void displayWiFiNetwork(int index, int displayIndex) { 81 int y = STATUS_BAR_HEIGHT + displayIndex * (CHAR_HEIGHT + LINE_SPACING); 82 tft.setCursor(0, y); 83 84 if (index == selectedNetworkIndex) 85 tft.setTextColor(TFT_GREEN, TFT_BLACK); 86 else 87 tft.setTextColor(TFT_WHITE, TFT_BLACK); 88 89 WiFiNetwork net = wifiNetworks[index]; 90 91 uint16_t rssiColor = getColorFromPercentage((int32_t)net.rssi); 92 tft.printf("%-2d %-3d ", net.index, net.channel); 93 94 tft.setTextColor(rssiColor, TFT_BLACK); 95 tft.printf("%-5d ", net.rssi); 96 97 tft.setTextColor(index == selectedNetworkIndex ? TFT_GREEN : TFT_WHITE, TFT_BLACK); 98 tft.printf("%-8s %s", net.encryption.c_str(), net.ssid.c_str()); 99 } 100 101 102 String getEncryptionType(wifi_auth_mode_t encryptionType) { 103 switch (encryptionType) { 104 case (WIFI_AUTH_OPEN): 105 return "Open"; 106 case (WIFI_AUTH_WEP): 107 return "WEP"; 108 case (WIFI_AUTH_WPA_PSK): 109 return "WPA_PSK"; 110 case (WIFI_AUTH_WPA2_PSK): 111 return "WPA2_PSK"; 112 case (WIFI_AUTH_WPA_WPA2_PSK): 113 return "WPA_WPA2_PSK"; 114 case (WIFI_AUTH_WPA2_ENTERPRISE): 115 return "WPA2_ENTERPRISE"; 116 default: 117 return "Unknown"; 118 } 119 } 120 121 122 void handlePasswordInput(char key) { 123 if (key == '\n' || key == '\r') { 124 wifi_password = inputBuffer; 125 inputBuffer = ""; 126 connectToWiFi(wifi_ssid, wifi_password); 127 } else if (key == '\b') { 128 if (inputBuffer.length() > 0) { 129 inputBuffer.remove(inputBuffer.length() - 1); 130 displayPasswordInputLine(); 131 } 132 } else if (inputBuffer.length() < 63) { 133 inputBuffer += key; 134 displayPasswordInputLine(); 135 } 136 } 137 138 139 void handleWiFiSelection(char key) { 140 if (key == 'u') { 141 updateSelectedNetwork(-1); 142 } else if (key == 'd') { 143 updateSelectedNetwork(1); 144 } else if (key == '\n' || key == '\r') { 145 wifi_ssid = wifiNetworks[selectedNetworkIndex].ssid; 146 if (wifiNetworks[selectedNetworkIndex].encryption == "Secured") { 147 inputBuffer = ""; 148 tft.fillScreen(TFT_BLACK); 149 tft.setTextSize(1); 150 tft.setTextColor(TFT_WHITE); 151 tft.setCursor(0, STATUS_BAR_HEIGHT); 152 tft.println("ENTER PASSWORD:"); 153 displayPasswordInputLine(); 154 155 while (true) { 156 char incoming = getKeyboardInput(); 157 if (incoming != 0) { 158 handlePasswordInput(incoming); 159 if (incoming == '\n' || incoming == '\r') 160 break; 161 } 162 } 163 } else { 164 wifi_password = ""; 165 connectToWiFi(wifi_ssid, wifi_password); 166 } 167 } 168 } 169 170 171 void initializeNetwork() { 172 WiFi.mode(WIFI_STA); 173 WiFi.setHostname("acid-drop"); // Turn into a preference 174 WiFi.onEvent(WiFiEvent); 175 randomizeMacAddress(); 176 } 177 178 179 void randomizeMacAddress() { 180 Serial.println("Current MAC Address: " + WiFi.macAddress()); 181 182 uint8_t new_mac[6]; 183 184 for (int i = 0; i < 6; ++i) 185 new_mac[i] = random(0x00, 0xFF); 186 187 if (esp_wifi_set_mac(WIFI_IF_STA, new_mac) == ESP_OK) 188 Serial.println("New MAC Address: " + WiFi.macAddress()); 189 else 190 Serial.println("Failed to set new MAC Address"); 191 } 192 193 194 void scanWiFiNetworks() { 195 Serial.println("Scanning for WiFi networks..."); 196 displayCenteredText("SCANNING WIFI"); 197 delay(1000); 198 199 wifiNetworks.clear(); 200 201 int n = WiFi.scanNetworks(); 202 203 if (n == 0) { 204 Serial.println("No WiFi networks found"); 205 displayCenteredText("NO NETWORKS FOUND"); 206 delay(2000); 207 scanWiFiNetworks(); 208 return; 209 } 210 211 Serial.print("Total number of networks found: "); 212 Serial.println(n); 213 214 for (int i = 0; i < n && i < 100; i++) { 215 WiFiNetwork net; 216 net.index = i + 1; 217 net.channel = WiFi.channel(i); 218 net.rssi = WiFi.RSSI(i); 219 net.encryption = (WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? "Open" : "Secured"; 220 String ssid = WiFi.SSID(i).substring(0, 32); 221 net.ssid = ssid; 222 wifiNetworks.push_back(net); 223 } 224 225 displayWiFiNetworks(); 226 } 227 228 229 void updateSelectedNetwork(int delta) { 230 int newIndex = selectedNetworkIndex + delta; 231 232 if (newIndex >= 0 && newIndex < wifiNetworks.size()) { 233 selectedNetworkIndex = newIndex; 234 displayWiFiNetworks(); 235 } 236 } 237 238 239 void wgConnect(const IPAddress& localIp, const char* privateKey, const char* endpointAddress, const char* publicKey, uint16_t endpointPort) { 240 wg.begin(localIp, privateKey, endpointAddress, publicKey, endpointPort); 241 } 242 243 244 void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) { 245 switch (event) { 246 case SYSTEM_EVENT_STA_CONNECTED: 247 Serial.println("WiFi connected"); 248 break; 249 case SYSTEM_EVENT_STA_DISCONNECTED: 250 Serial.println("WiFi disconnected"); 251 break; 252 case SYSTEM_EVENT_STA_GOT_IP: 253 Serial.println("WiFi got IP address: " + WiFi.localIP().toString()); 254 break; 255 case SYSTEM_EVENT_STA_LOST_IP: 256 Serial.println("WiFi lost IP address"); 257 break; 258 default: 259 break; 260 } 261 }