forked from me-no-dev/ESPAsyncWebServer
-
Notifications
You must be signed in to change notification settings - Fork 102
Tunnel #171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+211
−0
Merged
Tunnel #171
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| // SPDX-License-Identifier: LGPL-3.0-or-later | ||
| // Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov | ||
|
|
||
| // | ||
| // Shows how to trigger an async client request from a browser request and send the client response back to the browser through websocket | ||
| // | ||
|
|
||
| #include <Arduino.h> | ||
| #ifdef ESP32 | ||
| #include <AsyncTCP.h> | ||
| #include <WiFi.h> | ||
| #elif defined(ESP8266) | ||
| #include <ESP8266WiFi.h> | ||
| #include <ESPAsyncTCP.h> | ||
| #elif defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350) | ||
| #include <RPAsyncTCP.h> | ||
| #include <WiFi.h> | ||
| #endif | ||
|
|
||
| #include <ESPAsyncWebServer.h> | ||
|
|
||
| #define WIFI_SSID "IoT" | ||
| #define WIFI_PASSWORD "" | ||
|
|
||
| static AsyncWebServer server(80); | ||
| static AsyncWebSocketMessageHandler wsHandler; | ||
| static AsyncWebSocket ws("/ws", wsHandler.eventHandler()); | ||
|
|
||
| static const char *htmlContent PROGMEM = R"( | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>WebSocket Tunnel Example</title> | ||
| </head> | ||
| <body> | ||
| <h1>WebSocket Tunnel Example</h1> | ||
| <div><input type="text" id="url" value="http://www.google.com" /></div> | ||
| <div><button onclick='fetch()'>Fetch</button></div> | ||
| <div><pre id="response"></pre></div> | ||
| <script> | ||
| var ws = new WebSocket('/ws'); | ||
| ws.binaryType = "arraybuffer"; | ||
| ws.onopen = function() { | ||
| console.log("WebSocket connected"); | ||
| }; | ||
| ws.onmessage = function(event) { | ||
| let uint8array = new Uint8Array(event.data); | ||
| let string = new TextDecoder().decode(uint8array); | ||
| console.log("WebSocket message: " + string); | ||
| document.getElementById("response").innerText += string; | ||
| }; | ||
| ws.onclose = function() { | ||
| console.log("WebSocket closed"); | ||
| }; | ||
| ws.onerror = function(error) { | ||
| console.log("WebSocket error: " + error); | ||
| }; | ||
| function fetch() { | ||
| document.getElementById("response").innerText = ""; | ||
| var url = document.getElementById("url").value; | ||
| ws.send(url); | ||
| console.log("WebSocket sent: " + url); | ||
| } | ||
| </script> | ||
| </body> | ||
| </html> | ||
| )"; | ||
| static const size_t htmlContentLength = strlen_P(htmlContent); | ||
|
|
||
| void setup() { | ||
| Serial.begin(115200); | ||
|
|
||
| #ifndef CONFIG_IDF_TARGET_ESP32H2 | ||
| WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | ||
| while (WiFi.status() != WL_CONNECTED) { | ||
| delay(500); | ||
| } | ||
| Serial.println("Connected to WiFi!"); | ||
| Serial.println(WiFi.localIP()); | ||
| #endif | ||
|
|
||
| server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { | ||
| request->send(200, "text/html", (const uint8_t *)htmlContent, htmlContentLength); | ||
| }); | ||
|
|
||
| wsHandler.onMessage([](AsyncWebSocket *server, AsyncWebSocketClient *wsClient, const uint8_t *data, size_t len) { | ||
| String url; | ||
| String host; | ||
| String port; | ||
| String path; | ||
|
|
||
| url.concat((const char *)data, len); | ||
|
|
||
| if (!url.startsWith("http://")) { | ||
| return; | ||
| } | ||
|
|
||
| if (!url.endsWith("/")) { | ||
| url += "/"; | ||
| } | ||
|
|
||
| // Parse the URL to extract the host and port | ||
| int start = url.indexOf("://") + 3; | ||
| int end = url.indexOf("/", start); | ||
| if (end == -1) { | ||
| end = url.length(); | ||
| } | ||
| String hostPort = url.substring(start, end); | ||
| int colonIndex = hostPort.indexOf(":"); | ||
| if (colonIndex != -1) { | ||
| host = hostPort.substring(0, colonIndex); | ||
| port = hostPort.substring(colonIndex + 1); | ||
| } else { | ||
| host = hostPort; | ||
| port = "80"; // Default HTTP port | ||
| } | ||
| path = url.substring(end); | ||
|
|
||
| Serial.printf("Host: %s\n", host.c_str()); | ||
| Serial.printf("Port: %s\n", port.c_str()); | ||
| Serial.printf("Path: %s\n", path.c_str()); | ||
|
|
||
| // Ensure client does not get deleted while the websocket holds a reference to it | ||
| std::shared_ptr<AsyncClient> *safeAsyncClient = new std::shared_ptr<AsyncClient>(std::make_shared<AsyncClient>()); | ||
| AsyncClient *asyncClient = safeAsyncClient->get(); | ||
|
|
||
| asyncClient->onDisconnect([safeAsyncClient](void *arg, AsyncClient *client) { | ||
| Serial.printf("Tunnel disconnected!\n"); | ||
| delete safeAsyncClient; | ||
| }); | ||
|
|
||
| // register a callback when an error occurs | ||
| // note: onDisconnect also called on error | ||
| asyncClient->onError([](void *arg, AsyncClient *client, int8_t error) { | ||
| Serial.printf("Tunnel error: %s\n", client->errorToString(error)); | ||
| }); | ||
|
|
||
| // register a callback when data arrives, to accumulate it | ||
| asyncClient->onPacket( | ||
| [safeAsyncClient](void *arg, AsyncClient *, struct pbuf *pb) { | ||
| std::shared_ptr<AsyncClient> safeAsyncClientRef = *safeAsyncClient; // add a reference | ||
| AsyncWebSocketClient *wsClient = (AsyncWebSocketClient *)arg; | ||
| Serial.printf("Tunnel received %u bytes\n", pb->len); | ||
| AsyncWebSocketSharedBuffer wsBuffer = | ||
| AsyncWebSocketSharedBuffer(new std::vector<uint8_t>((uint8_t *)pb->payload, (uint8_t *)pb->payload + pb->len), [=](std::vector<uint8_t> *bufptr) { | ||
| delete bufptr; | ||
| Serial.printf("ACK %u bytes\n", pb->len); | ||
| safeAsyncClientRef->ackPacket(pb); | ||
| }); | ||
| Serial.printf("Tunnel sending %u bytes\n", wsBuffer->size()); | ||
| Serial.printf("%.*s\n", (int)wsBuffer->size(), wsBuffer->data()); | ||
| wsClient->binary(std::move(wsBuffer)); | ||
| }, | ||
| wsClient | ||
| ); | ||
|
|
||
| asyncClient->onConnect([=](void *arg, AsyncClient *client) { | ||
| Serial.printf("Tunnel connected!\n"); | ||
|
|
||
| client->write("GET "); | ||
| client->write(path.c_str()); | ||
| client->write(" HTTP/1.1\r\n"); | ||
| client->write("Host: "); | ||
| client->write(host.c_str()); | ||
| client->write(":"); | ||
| client->write(port.c_str()); | ||
| client->write("\r\n"); | ||
| client->write("User-Agent: ESP32\r\n"); | ||
| client->write("Accept: */*\r\n"); | ||
| client->write("Connection: close\r\n"); | ||
| client->write("\r\n"); | ||
| }); | ||
|
|
||
| Serial.printf("Fetching: http://%s:%s%s\n", host.c_str(), port.c_str(), path.c_str()); | ||
|
|
||
| if (!asyncClient->connect(host.c_str(), port.toInt())) { | ||
| Serial.printf("Failed to open tunnel!\n"); | ||
| delete safeAsyncClient; | ||
| } | ||
| }); | ||
|
|
||
| wsHandler.onConnect([](AsyncWebSocket *server, AsyncWebSocketClient *client) { | ||
| Serial.printf("Client %" PRIu32 " connected\n", client->id()); | ||
| client->binary("WebSocket connected!"); | ||
| }); | ||
|
|
||
| wsHandler.onDisconnect([](AsyncWebSocket *server, uint32_t clientId) { | ||
| Serial.printf("Client %" PRIu32 " disconnected\n", clientId); | ||
| }); | ||
|
|
||
| server.addHandler(&ws); | ||
| server.begin(); | ||
| Serial.println("Server started!"); | ||
| } | ||
|
|
||
| static uint32_t lastHeap = 0; | ||
|
|
||
| void loop() { | ||
| ws.cleanupClients(2); | ||
|
|
||
| #ifdef ESP32 | ||
| uint32_t now = millis(); | ||
| if (now - lastHeap >= 2000) { | ||
| Serial.printf("Uptime: %3lu s, Free heap: %" PRIu32 "\n", millis() / 1000, ESP.getFreeHeap()); | ||
| lastHeap = now; | ||
| } | ||
| #endif | ||
|
|
||
| delay(500); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.