mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 05:47:35 +00:00
Utilities: Update telws to use the isolated WebSocket service
This commit is contained in:
parent
62ed26164b
commit
d398943c37
2 changed files with 38 additions and 39 deletions
|
@ -42,7 +42,7 @@ target_link_libraries(shot LibGUI)
|
||||||
target_link_libraries(sql LibLine LibSQL)
|
target_link_libraries(sql LibLine LibSQL)
|
||||||
target_link_libraries(su LibCrypt)
|
target_link_libraries(su LibCrypt)
|
||||||
target_link_libraries(tar LibArchive LibCompress)
|
target_link_libraries(tar LibArchive LibCompress)
|
||||||
target_link_libraries(telws LibCrypto LibTLS LibWebSocket LibLine)
|
target_link_libraries(telws LibProtocol LibLine)
|
||||||
target_link_libraries(test-crypto LibCrypto LibTLS LibLine)
|
target_link_libraries(test-crypto LibCrypto LibTLS LibLine)
|
||||||
target_link_libraries(test-fuzz LibCore LibGemini LibGfx LibHTTP LibIPC LibJS LibMarkdown LibShell)
|
target_link_libraries(test-fuzz LibCore LibGemini LibGfx LibHTTP LibIPC LibJS LibMarkdown LibShell)
|
||||||
target_link_libraries(test-js LibJS LibLine LibCore)
|
target_link_libraries(test-js LibJS LibLine LibCore)
|
||||||
|
|
|
@ -12,9 +12,8 @@
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
#include <LibCore/Notifier.h>
|
#include <LibCore/Notifier.h>
|
||||||
#include <LibLine/Editor.h>
|
#include <LibLine/Editor.h>
|
||||||
#include <LibWebSocket/ConnectionInfo.h>
|
#include <LibProtocol/WebSocket.h>
|
||||||
#include <LibWebSocket/Message.h>
|
#include <LibProtocol/WebSocketClient.h>
|
||||||
#include <LibWebSocket/WebSocket.h>
|
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
|
@ -43,6 +42,33 @@ int main(int argc, char** argv)
|
||||||
Core::EventLoop loop;
|
Core::EventLoop loop;
|
||||||
RefPtr<Line::Editor> editor = Line::Editor::construct();
|
RefPtr<Line::Editor> editor = Line::Editor::construct();
|
||||||
bool should_quit = false;
|
bool should_quit = false;
|
||||||
|
auto websocket_client = Protocol::WebSocketClient::construct();
|
||||||
|
auto socket = websocket_client->connect(url, origin);
|
||||||
|
if (!socket) {
|
||||||
|
warnln("Failed to start socket for '{}'\n", url);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
socket->on_open = [&]() {
|
||||||
|
outln("[WebSocket opened]"sv);
|
||||||
|
};
|
||||||
|
socket->on_error = [&](auto error) {
|
||||||
|
outln("[WebSocket Error : {}]", (unsigned)error);
|
||||||
|
};
|
||||||
|
socket->on_message = [&](auto message) {
|
||||||
|
if (!message.is_text) {
|
||||||
|
outln("[Received binary data : {} bytes]", message.data.size());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
outln("[Received utf8 text] {}", String(ReadonlyBytes(message.data)));
|
||||||
|
};
|
||||||
|
socket->on_close = [&](auto code, auto message, bool was_clean) {
|
||||||
|
outln("[Server {} closed connection : '{}' (code {})]",
|
||||||
|
was_clean ? "cleanly" : "dirtily",
|
||||||
|
message,
|
||||||
|
code);
|
||||||
|
should_quit = true;
|
||||||
|
Core::EventLoop::current().quit(0);
|
||||||
|
};
|
||||||
|
|
||||||
if (pledge("stdio unix inet accept rpath wpath tty sigaction", nullptr) < 0) {
|
if (pledge("stdio unix inet accept rpath wpath tty sigaction", nullptr) < 0) {
|
||||||
perror("pledge");
|
perror("pledge");
|
||||||
|
@ -54,33 +80,6 @@ int main(int argc, char** argv)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
WebSocket::ConnectionInfo connection_info(url);
|
|
||||||
connection_info.set_origin(origin);
|
|
||||||
|
|
||||||
auto socket = WebSocket::WebSocket::create(connection_info);
|
|
||||||
socket->on_open = [&]() {
|
|
||||||
outln("[WebSocket opened]"sv);
|
|
||||||
};
|
|
||||||
socket->on_error = [&](auto error) {
|
|
||||||
outln("[WebSocket Error : {}]", (unsigned)error);
|
|
||||||
};
|
|
||||||
socket->on_message = [&](auto message) {
|
|
||||||
if (!message.is_text()) {
|
|
||||||
outln("[Received binary data : {} bytes]", message.data().size());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
outln("[Received utf8 text] {}", String(ReadonlyBytes(message.data())));
|
|
||||||
};
|
|
||||||
socket->on_close = [&](auto code, auto message, bool was_clean) {
|
|
||||||
outln("[Server {} closed connection : '{}' (code {})]",
|
|
||||||
was_clean ? "cleanly" : "dirtily",
|
|
||||||
message,
|
|
||||||
code);
|
|
||||||
should_quit = true;
|
|
||||||
Core::EventLoop::current().quit(0);
|
|
||||||
};
|
|
||||||
socket->start();
|
|
||||||
|
|
||||||
outln("Started server. Commands :");
|
outln("Started server. Commands :");
|
||||||
outln("- '<text>' send the text as message");
|
outln("- '<text>' send the text as message");
|
||||||
outln("- '.text <data>' send the text as message");
|
outln("- '.text <data>' send the text as message");
|
||||||
|
@ -99,27 +98,27 @@ int main(int argc, char** argv)
|
||||||
if (line.starts_with(".")) {
|
if (line.starts_with(".")) {
|
||||||
if (line.starts_with(".text ")) {
|
if (line.starts_with(".text ")) {
|
||||||
editor->add_to_history(line);
|
editor->add_to_history(line);
|
||||||
if (socket->ready_state() != WebSocket::ReadyState::Open) {
|
if (socket->ready_state() != Protocol::WebSocket::ReadyState::Open) {
|
||||||
outln("Could not send message : socket is not open.");
|
outln("Could not send message : socket is not open.");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
socket->send(WebSocket::Message(line.substring(6)));
|
socket->send(line.substring(6));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (line.starts_with(".base64 ")) {
|
if (line.starts_with(".base64 ")) {
|
||||||
editor->add_to_history(line);
|
editor->add_to_history(line);
|
||||||
if (socket->ready_state() != WebSocket::ReadyState::Open) {
|
if (socket->ready_state() != Protocol::WebSocket::ReadyState::Open) {
|
||||||
outln("Could not send message : socket is not open.");
|
outln("Could not send message : socket is not open.");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
auto base64_data = line.substring(8);
|
auto base64_data = line.substring(8);
|
||||||
auto buffer = decode_base64(base64_data);
|
auto buffer = decode_base64(base64_data);
|
||||||
socket->send(WebSocket::Message(buffer, false));
|
socket->send(buffer, false);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (line == ".exit") {
|
if (line == ".exit") {
|
||||||
editor->add_to_history(line);
|
editor->add_to_history(line);
|
||||||
if (socket->ready_state() != WebSocket::ReadyState::Open) {
|
if (socket->ready_state() != Protocol::WebSocket::ReadyState::Open) {
|
||||||
outln("Socket is not open. Exiting.");
|
outln("Socket is not open. Exiting.");
|
||||||
should_quit = true;
|
should_quit = true;
|
||||||
continue;
|
continue;
|
||||||
|
@ -129,7 +128,7 @@ int main(int argc, char** argv)
|
||||||
}
|
}
|
||||||
if (line == ".forceexit") {
|
if (line == ".forceexit") {
|
||||||
editor->add_to_history(line);
|
editor->add_to_history(line);
|
||||||
if (socket->ready_state() == WebSocket::ReadyState::Open)
|
if (socket->ready_state() == Protocol::WebSocket::ReadyState::Open)
|
||||||
socket->close();
|
socket->close();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -137,11 +136,11 @@ int main(int argc, char** argv)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
editor->add_to_history(line);
|
editor->add_to_history(line);
|
||||||
if (socket->ready_state() != WebSocket::ReadyState::Open) {
|
if (socket->ready_state() != Protocol::WebSocket::ReadyState::Open) {
|
||||||
outln("Could not send message : socket is not open.");
|
outln("Could not send message : socket is not open.");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
socket->send(WebSocket::Message(line));
|
socket->send(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue