From ba7e3cd9a18d6c74bc1f3819ec7485e25daf3472 Mon Sep 17 00:00:00 2001 From: vaxerski <43317083+vaxerski@users.noreply.github.com> Date: Tue, 24 May 2022 19:42:43 +0200 Subject: Major IPC Rework + Added Socket2 IPC is now done with UNIX sockets instead of TCP ones (security!), and added Socket2, see Wiki. --- src/debug/HyprCtl.cpp | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) (limited to 'src/debug/HyprCtl.cpp') diff --git a/src/debug/HyprCtl.cpp b/src/debug/HyprCtl.cpp index 5c4f42c1..2920045b 100644 --- a/src/debug/HyprCtl.cpp +++ b/src/debug/HyprCtl.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -238,28 +239,20 @@ std::string getRequestFromThread(std::string rq) { void HyprCtl::startHyprCtlSocket() { std::thread([&]() { - uint16_t connectPort = 9187; - - const auto SOCKET = socket(AF_INET, SOCK_STREAM, 0); + const auto SOCKET = socket(AF_UNIX, SOCK_STREAM, 0); if (SOCKET < 0) { Debug::log(ERR, "Couldn't start the Hyprland Socket. (1) IPC will not work."); return; } - sockaddr_in SERVERADDRESS = {.sin_family = AF_INET, .sin_port = connectPort, .sin_addr = (in_addr)INADDR_ANY}; + unlink("/tmp/hypr/.socket.sock"); - while(connectPort < 11000) { - if (bind(SOCKET, (sockaddr*)&SERVERADDRESS, sizeof(SERVERADDRESS)) < 0) { - Debug::log(LOG, "IPC: Port %d failed with an error: %s", connectPort, strerror(errno)); - } else { - break; - } + sockaddr_un SERVERADDRESS = {.sun_family = AF_UNIX}; - connectPort++; - SERVERADDRESS.sin_port = connectPort; - } - + strcpy(SERVERADDRESS.sun_path, "/tmp/hypr/.socket.sock"); + + bind(SOCKET, (sockaddr*)&SERVERADDRESS, SUN_LEN(&SERVERADDRESS)); // 10 max queued. listen(SOCKET, 10); @@ -269,12 +262,7 @@ void HyprCtl::startHyprCtlSocket() { char readBuffer[1024] = {0}; - Debug::log(LOG, "Hypr socket started on port %i", connectPort); - - std::string cmd = "rm -f /tmp/hypr/.socket"; - system(cmd.c_str()); // forgive me for using system() but it works and it doesnt matter here that much - cmd = "echo \"" + std::to_string(connectPort) + "\" > /tmp/hypr/.socket"; - system(cmd.c_str()); + Debug::log(LOG, "Hypr socket started."); while(1) { const auto ACCEPTEDCONNECTION = accept(SOCKET, (sockaddr*)&clientAddress, &clientSize); @@ -285,7 +273,7 @@ void HyprCtl::startHyprCtlSocket() { } auto messageSize = read(ACCEPTEDCONNECTION, readBuffer, 1024); - readBuffer[messageSize == 1024 ? 1024 : messageSize] = '\0'; + readBuffer[messageSize == 1024 ? 1023 : messageSize] = '\0'; std::string request(readBuffer); -- cgit v1.2.3