From 41f420b68ce439960528f2121989fa9e4f9e7c83 Mon Sep 17 00:00:00 2001 From: lanthora Date: Tue, 3 Nov 2026 12:06:24 +0800 Subject: [PATCH 1/2] Add negotiated multi-connection WebSocket relay --- candy-cli/src/config.cc | 10 +++ candy-cli/src/config.h | 1 + candy-service/README.md | 3 + candy-service/README.zh-CN.md | 3 + candy-service/src/main.cc | 39 ++++++++++- candy.cfg | 4 ++ candy/src/candy/client.cc | 1 + candy/src/core/client.cc | 31 ++------- candy/src/core/client.h | 16 +---- candy/src/core/message.cc | 30 ++++++++ candy/src/core/message.h | 15 ++++ candy/src/websocket/client.cc | 97 ++++++++++++++++++++++---- candy/src/websocket/client.h | 10 +++ candy/src/websocket/message.h | 8 +++ candy/src/websocket/relay.cc | 77 +++++++++++++++++++++ candy/src/websocket/relay.h | 25 +++++++ candy/src/websocket/server.cc | 125 ++++++++++++++++++++++++++-------- candy/src/websocket/server.h | 10 ++- 18 files changed, 416 insertions(+), 89 deletions(-) create mode 100644 candy/src/websocket/relay.cc create mode 100644 candy/src/websocket/relay.h diff --git a/candy-cli/src/config.cc b/candy-cli/src/config.cc index 1bd8eb09..56eea0c9 100644 --- a/candy-cli/src/config.cc +++ b/candy-cli/src/config.cc @@ -34,6 +34,7 @@ Poco::JSON::Object arguments::json() { config.set("discovery", this->discovery); config.set("route", this->routeCost); config.set("mtu", this->mtu); + config.set("connections", this->connections); config.set("port", this->port); config.set("vmac", virtualMac(this->name)); config.set("expt", loadTunAddress(this->name)); @@ -94,6 +95,13 @@ int arguments::parse(int argc, char *argv[]) { .default_value(0) .nargs(1); + program.add_argument("--connections") + .help("WebSocket connection count, including the control connection (1-8, default 1). (client only)") + .metavar("") + .scan<'i', int>() + .default_value(1) + .nargs(1); + program.add_argument("--mtu") .help("maximum transmission unit for the TUN device.\n" "Default is 1400. (client only)") @@ -180,6 +188,7 @@ int arguments::parse(int argc, char *argv[]) { program.set_if_used("--localhost", this->localhost); program.set_if_used("--port", this->port); program.set_if_used("--mtu", this->mtu); + program.set_if_used("--connections", this->connections); program.set_if_used("--discovery", this->discovery); program.set_if_used("--route", this->routeCost); @@ -237,6 +246,7 @@ void arguments::parseFile(std::string cfgFile) { {"discovery", [&](const std::string &value) { this->discovery = std::stoi(value); }}, {"route", [&](const std::string &value) { this->routeCost = std::stoi(value); }}, {"port", [&](const std::string &value) { this->port = std::stoi(value); }}, + {"connections", [&](const std::string &value) { this->connections = std::stoi(value); }}, {"mtu", [&](const std::string &value) { this->mtu = std::stoi(value); }}, {"localhost", [&](const std::string &value) { this->localhost = value; }}, }; diff --git a/candy-cli/src/config.h b/candy-cli/src/config.h index dbd5e5b6..7bc5ef1b 100644 --- a/candy-cli/src/config.h +++ b/candy-cli/src/config.h @@ -31,6 +31,7 @@ struct arguments { int discovery = 0; int routeCost = 0; int mtu = 1400; + int connections = 1; }; int saveTunAddress(const std::string &name, const std::string &cidr); diff --git a/candy-service/README.md b/candy-service/README.md index 44c1b17d..d67a846b 100644 --- a/candy-service/README.md +++ b/candy-service/README.md @@ -49,12 +49,15 @@ The meaning of startup parameters is the same as the [configuration file](../can "port": 0, "localhost": "", "mtu": 1400, + "connections": 1, "expt": "", "vmac": "16-char rand str" } } ``` +All client configuration fields shown above are required (except `mode`, which is unused). Missing fields, invalid types, or integers outside the `int` range return HTTP 400 with a `message` identifying the error; no client is started. + Response: ```json diff --git a/candy-service/README.zh-CN.md b/candy-service/README.zh-CN.md index 3819622a..74613149 100644 --- a/candy-service/README.zh-CN.md +++ b/candy-service/README.zh-CN.md @@ -49,12 +49,15 @@ candy-service /help "port": 0, "localhost": "", "mtu": 1400, + "connections": 1, "expt": "", "vmac": "16-char rand str" } } ``` +上例中的客户端配置字段均需传入(未使用的 `mode` 除外)。字段缺失、类型错误或整数超出 `int` 范围时,返回 HTTP 400,`message` 说明错误,不会启动客户端。 + ```json { "id": "test", diff --git a/candy-service/src/main.cc b/candy-service/src/main.cc index dce03b55..9b8c8f02 100644 --- a/candy-service/src/main.cc +++ b/candy-service/src/main.cc @@ -58,9 +58,42 @@ class RunHandler : public BaseJSONHandler { return; } - auto json = readRequest(request); - auto id = json->getValue("id"); - auto config = json->getObject("config"); + Poco::JSON::Object::Ptr json; + Poco::JSON::Object::Ptr config; + std::string id; + try { + json = readRequest(request); + if (!json || !json->get("id").isString()) { + throw Poco::InvalidArgumentException("missing or invalid parameter: id"); + } + id = json->getValue("id"); + config = json->getObject("config"); + if (!config) { + throw Poco::InvalidArgumentException("missing or invalid parameter: config"); + } + for (const char *field : {"name", "password", "websocket", "tun", "vmac", "expt", "stun", "localhost"}) { + if (!config->get(field).isString()) { + throw Poco::InvalidArgumentException(std::string("missing or invalid string parameter: config.") + field); + } + } + for (const char *field : {"connections", "discovery", "route", "mtu", "port"}) { + auto value = config->get(field); + if (!value.isInteger() || value.isBoolean()) { + throw Poco::InvalidArgumentException(std::string("missing or invalid integer parameter: config.") + field); + } + try { + config->getValue(field); + } catch (const Poco::Exception &) { + throw Poco::InvalidArgumentException(std::string("integer parameter out of range: config.") + field); + } + } + } catch (const Poco::Exception &e) { + response.setStatus(Poco::Net::HTTPResponse::HTTP_BAD_REQUEST); + Poco::JSON::Object::Ptr error = new Poco::JSON::Object; + error->set("message", e.displayText()); + sendResponse(response, error); + return; + } json->remove("config"); std::lock_guard lock(threadMutex); diff --git a/candy.cfg b/candy.cfg index 10c161b2..c6872c02 100644 --- a/candy.cfg +++ b/candy.cfg @@ -28,6 +28,10 @@ websocket = "wss://canets.org" #sdwan = "192.168.202.1/32,172.17.0.0/16,192.168.202.2;192.168.202.2/32,172.16.0.0/16,192.168.202.1" ################################# Client Only ################################# +# [Optional] Total WebSocket connections (1-8, default 1). Requires server support. +# Additional connections carry only relayed IP packets; control stays on the first. +#connections = 4 + # [Optional] Network interface name # Used to differentiate networks when running multiple clients. #name = "" diff --git a/candy/src/candy/client.cc b/candy/src/candy/client.cc index 004c712a..f2b928e1 100644 --- a/candy/src/candy/client.cc +++ b/candy/src/candy/client.cc @@ -91,6 +91,7 @@ bool run(const std::string &id, const Poco::JSON::Object &config) { client->setName(config.getValue("name")); client->setPassword(config.getValue("password")); client->setWebSocket(config.getValue("websocket")); + client->setConnections(config.getValue("connections")); client->setTunAddress(config.getValue("tun")); client->setVirtualMac(config.getValue("vmac")); client->setExptTunAddress(config.getValue("expt")); diff --git a/candy/src/core/client.cc b/candy/src/core/client.cc index 5442dc53..e5c96acd 100644 --- a/candy/src/core/client.cc +++ b/candy/src/core/client.cc @@ -2,36 +2,9 @@ #include "core/client.h" #include "core/message.h" #include -#include namespace candy { -Msg MsgQueue::read() { - std::unique_lock lock(msgMutex); - if (!msgCondition.wait_for(lock, std::chrono::seconds(1), [this] { return !msgQueue.empty(); })) { - return Msg(MsgKind::TIMEOUT); - } - - Msg msg = std::move(msgQueue.front()); - msgQueue.pop(); - return msg; -} - -void MsgQueue::write(Msg msg) { - { - std::unique_lock lock(this->msgMutex); - msgQueue.push(std::move(msg)); - } - msgCondition.notify_one(); -} - -void MsgQueue::clear() { - std::unique_lock lock(this->msgMutex); - while (!msgQueue.empty()) { - msgQueue.pop(); - } -} - Client::~Client() { waitForThreads(); } @@ -71,6 +44,10 @@ void Client::setPassword(const std::string &password) { peerManager.setPassword(password); } +void Client::setConnections(int count) { + ws.setConnections(count); +} + void Client::setWebSocket(const std::string &uri) { ws.setWsServerUri(uri); } diff --git a/candy/src/core/client.h b/candy/src/core/client.h index c4004f79..a3c8d531 100644 --- a/candy/src/core/client.h +++ b/candy/src/core/client.h @@ -7,25 +7,10 @@ #include "tun/tun.h" #include "utils/atomic.h" #include "websocket/client.h" -#include -#include -#include #include namespace candy { -class MsgQueue { -public: - Msg read(); - void write(Msg msg); - void clear(); - -private: - std::queue msgQueue; - std::mutex msgMutex; - std::condition_variable msgCondition; -}; - class Client { public: ~Client(); @@ -33,6 +18,7 @@ class Client { void setName(const std::string &name); void setPassword(const std::string &password); void setWebSocket(const std::string &uri); + void setConnections(int count); void setTunAddress(const std::string &cidr); void setStun(const std::string &stun); void setDiscoveryInterval(int interval); diff --git a/candy/src/core/message.cc b/candy/src/core/message.cc index 7bf17404..454cf0de 100644 --- a/candy/src/core/message.cc +++ b/candy/src/core/message.cc @@ -1,8 +1,38 @@ // SPDX-License-Identifier: MIT #include "core/message.h" +#include namespace candy { +Msg MsgQueue::read() { + std::unique_lock lock(msgMutex); + if (!msgCondition.wait_for(lock, std::chrono::seconds(1), [this] { return !msgQueue.empty(); })) { + return Msg(MsgKind::TIMEOUT); + } + + Msg msg = std::move(msgQueue.front()); + msgQueue.pop(); + return msg; +} + +void MsgQueue::write(Msg msg, size_t limit) { + { + std::unique_lock lock(this->msgMutex); + if (limit && msgQueue.size() >= limit) { + return; + } + msgQueue.push(std::move(msg)); + } + msgCondition.notify_one(); +} + +void MsgQueue::clear() { + std::unique_lock lock(this->msgMutex); + while (!msgQueue.empty()) { + msgQueue.pop(); + } +} + Msg::Msg(MsgKind kind, std::string data) { this->kind = kind; this->data = std::move(data); diff --git a/candy/src/core/message.h b/candy/src/core/message.h index 0d3f6fa8..8bf2cb7c 100644 --- a/candy/src/core/message.h +++ b/candy/src/core/message.h @@ -3,7 +3,10 @@ #define CANDY_CORE_MESSAGE_H #include "core/net.h" +#include #include +#include +#include #include namespace candy { @@ -30,6 +33,18 @@ struct Msg { Msg &operator=(Msg &&packet); }; +class MsgQueue { +public: + Msg read(); + void write(Msg msg, size_t limit = 0); + void clear(); + +private: + std::queue msgQueue; + std::mutex msgMutex; + std::condition_variable msgCondition; +}; + namespace CoreMsg { struct PubInfo { diff --git a/candy/src/websocket/client.cc b/candy/src/websocket/client.cc index 8f109f91..9d76d2a5 100644 --- a/candy/src/websocket/client.cc +++ b/candy/src/websocket/client.cc @@ -15,10 +15,15 @@ #include #include #include +#include #include namespace candy { +void WebSocketClient::setConnections(int count) { + this->connections = std::clamp(count, 1, 8); +} + int WebSocketClient::setName(const std::string &name) { this->name = name; return 0; @@ -89,6 +94,7 @@ int WebSocketClient::run(Client *client) { break; } } + relay->running.store(false); getClient().shutdown(); candy::logger().debug("stop thread: websocket client ws"); }); @@ -103,6 +109,11 @@ int WebSocketClient::wait() { if (this->wsThread.joinable()) { this->wsThread.join(); } + relay->running.store(false); + for (auto &thread : dataThreads) { + thread.join(); + } + dataThreads.clear(); return 0; } @@ -127,10 +138,12 @@ void WebSocketClient::handleWsQueue() { } void WebSocketClient::handlePacket(Msg msg) { - IP4Header *header = (IP4Header *)msg.data.data(); - msg.data.insert(0, 1, WsMsgKind::FORWARD); - sendFrame(msg.data); + if (relay->connections > 0) { + relay->queue.write(std::move(msg), 1024); + } else { + sendFrame(msg.data); + } } void WebSocketClient::handlePubInfo(Msg msg) { @@ -213,6 +226,9 @@ int WebSocketClient::handleWsConn() { void WebSocketClient::handleWsMsg(std::string buffer) { uint8_t msgKind = buffer.front(); switch (msgKind) { + case WsMsgKind::MULTI: + handleMultiMsg(std::move(buffer)); + break; case WsMsgKind::FORWARD: handleForwardMsg(std::move(buffer)); break; @@ -320,6 +336,7 @@ void WebSocketClient::sendFrame(const std::string &buffer, int flags) { } void WebSocketClient::sendFrame(const void *buffer, int length, int flags) { + std::unique_lock lock(sendMutex); if (this->ws) { try { this->ws->sendFrame(buffer, length, flags); @@ -350,6 +367,11 @@ void WebSocketClient::sendAuthMsg() { this->client->getTunMsgQueue().write(Msg(MsgKind::TUNADDR, address.toCidr())); this->client->getPeerMsgQueue().write(Msg(MsgKind::TUNADDR, address.toCidr())); sendPingMessage(); + if (connections > 1) { + WsMsg::Multi multi; + multi.count = connections - 1; + sendFrame(&multi, sizeof(multi)); + } } void WebSocketClient::sendDiscoveryMsg(IP4 dst) { @@ -375,13 +397,14 @@ void WebSocketClient::sendPingMessage() { sendFrame(pingMessage, flags); } -int WebSocketClient::connect() { +std::shared_ptr WebSocketClient::open() { + std::shared_ptr socket; std::shared_ptr uri; try { uri = std::make_shared(wsServerUri); } catch (std::exception &e) { candy::logger().fatal(Poco::format("invalid websocket server: %s: %s", wsServerUri, std::string(e.what()))); - return -1; + return nullptr; } try { @@ -392,24 +415,68 @@ int WebSocketClient::connect() { using Poco::Net::Context; Context::Ptr context = new Context(Context::TLS_CLIENT_USE, "", "", "", Context::VERIFY_NONE); Poco::Net::HTTPSClientSession cs(uri->getHost(), uri->getPort(), context); - this->ws = std::make_shared(cs, request, response); + cs.setTimeout(Poco::Timespan(5, 0)); + socket = std::make_shared(cs, request, response); } else if (uri->getScheme() == "ws") { Poco::Net::HTTPClientSession cs(uri->getHost(), uri->getPort()); - this->ws = std::make_shared(cs, request, response); + cs.setTimeout(Poco::Timespan(5, 0)); + socket = std::make_shared(cs, request, response); } else { candy::logger().fatal(Poco::format("invalid websocket scheme: %s", wsServerUri)); - return -1; + return nullptr; } - // Blocking mode may cause receiveFrame to hang and use 100% CPU - this->ws->setBlocking(false); - this->timestamp = bootTime(); - this->pingMessage = "candy::" + std::string(CANDY_SYSTEM) + "::" + std::string(CANDY_VERSION) + "::" + hostName(); - candy::logger().debug(Poco::format("client info: %s", this->pingMessage)); - return 0; + socket->setSendTimeout(Poco::Timespan(3, 0)); + socket->setReceiveTimeout(Poco::Timespan(3, 0)); + return socket; } catch (std::exception &e) { - candy::logger().fatal(Poco::format("websocket connect failed: %s", std::string(e.what()))); + candy::logger().warning(Poco::format("websocket connect failed: %s", std::string(e.what()))); + return nullptr; + } +} + +int WebSocketClient::connect() { + this->ws = open(); + if (!this->ws) { return -1; } + // Blocking mode may cause receiveFrame to hang and use 100% CPU + this->ws->setBlocking(false); + this->timestamp = bootTime(); + this->pingMessage = "candy::" + std::string(CANDY_SYSTEM) + "::" + std::string(CANDY_VERSION) + "::" + hostName(); + candy::logger().debug(Poco::format("client info: %s", this->pingMessage)); + return 0; +} + +void WebSocketClient::handleMultiMsg(std::string buffer) { + if (buffer.size() != sizeof(WsMsg::Multi) || connections == 1 || !dataThreads.empty()) { + return; + } + auto *header = (const WsMsg::Multi *)buffer.data(); + for (int i = 0; i < std::min(header->count, connections - 1); ++i) { + dataThreads.emplace_back([this, buffer] { connectData(buffer); }); + } +} + +void WebSocketClient::connectData(std::string buffer) { + try { + auto socket = open(); + if (!socket || !relay->running.load()) { + return; + } + buffer.front() = WsMsgKind::JOIN; + socket->sendFrame(buffer.data(), buffer.size(), Poco::Net::WebSocket::FRAME_BINARY); + std::string response(buffer.size(), '\0'); + int flags = 0; + int length = socket->receiveFrame(response.data(), response.size(), flags); + if (length != buffer.size() || flags != Poco::Net::WebSocket::FRAME_BINARY || response != buffer) { + return; + } + relay->run( + *socket, [this](std::string packet) { handleForwardMsg(std::move(packet)); }, + [this] { return getClient().isRunning(); }); + } catch (const std::exception &e) { + candy::logger().debug(std::string("data websocket connect failed: ") + e.what()); + } } int WebSocketClient::disconnect() { diff --git a/candy/src/websocket/client.h b/candy/src/websocket/client.h index 8c0c4afc..8924ae50 100644 --- a/candy/src/websocket/client.h +++ b/candy/src/websocket/client.h @@ -4,11 +4,13 @@ #include "core/message.h" #include "core/net.h" +#include "websocket/relay.h" #include #include #include #include #include +#include namespace candy { @@ -19,6 +21,7 @@ class WebSocketClient { int setName(const std::string &name); int setPassword(const std::string &password); int setWsServerUri(const std::string &uri); + void setConnections(int count); int setExptTunAddress(const std::string &cidr); int setAddress(const std::string &cidr); int setVirtualMac(const std::string &vmac); @@ -63,6 +66,13 @@ class WebSocketClient { private: int connect(); + std::shared_ptr open(); + void handleMultiMsg(std::string buffer); + void connectData(std::string buffer); + int connections = 1; + std::shared_ptr relay = std::make_shared(); + std::vector dataThreads; + std::mutex sendMutex; int disconnect(); std::shared_ptr ws; diff --git a/candy/src/websocket/message.h b/candy/src/websocket/message.h index f1a6992b..582c4396 100644 --- a/candy/src/websocket/message.h +++ b/candy/src/websocket/message.h @@ -15,6 +15,8 @@ constexpr uint8_t UDP4CONN = 3; constexpr uint8_t VMAC = 4; constexpr uint8_t DISCOVERY = 5; constexpr uint8_t ROUTE = 6; +constexpr uint8_t MULTI = 7; +constexpr uint8_t JOIN = 8; constexpr uint8_t GENERAL = 255; } // namespace WsMsgKind @@ -24,6 +26,12 @@ constexpr uint8_t LOCALUDP4CONN = 0; namespace WsMsg { +struct Multi { + uint8_t type = WsMsgKind::MULTI; + uint8_t count = 0; // Number of additional data connections (at most 7). + char key[32] = {}; +}; + struct __attribute__((packed)) Auth { uint8_t type; IP4 ip; diff --git a/candy/src/websocket/relay.cc b/candy/src/websocket/relay.cc new file mode 100644 index 00000000..ab311d44 --- /dev/null +++ b/candy/src/websocket/relay.cc @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: MIT +#include "websocket/relay.h" +#include "utils/log.h" +#include "utils/time.h" +#include "websocket/message.h" +#include +#include + +namespace candy { + +void WsRelay::run(Poco::Net::WebSocket &ws, std::function receive, std::function active) { + ws.setSendTimeout(Poco::Timespan(3, 0)); + ws.setReceiveTimeout(Poco::Timespan(3, 0)); + std::mutex sendMutex; + Utils::Atomic connected{true}; + ++connections; + std::thread sender([&] { + try { + while (running.load() && connected.load() && active()) { + Msg msg = queue.read(); + if (msg.kind == MsgKind::PACKET) { + std::unique_lock lock(sendMutex); + ws.sendFrame(msg.data.data(), msg.data.size(), Poco::Net::WebSocket::FRAME_BINARY); + } + } + } catch (const std::exception &e) { + candy::logger().debug(std::string("data websocket send failed: ") + e.what()); + connected.store(false); + } + }); + try { + int64_t timestamp = bootTime(); + int64_t ping = timestamp; + while (running.load() && connected.load() && active()) { + if (bootTime() - timestamp > 30000) { + break; + } + if (bootTime() - ping > 15000) { + std::unique_lock lock(sendMutex); + ws.sendFrame("", 0, Poco::Net::WebSocket::FRAME_FLAG_FIN | Poco::Net::WebSocket::FRAME_OP_PING); + ping = bootTime(); + } + if (!ws.poll(Poco::Timespan(1, 0), Poco::Net::Socket::SELECT_READ)) { + continue; + } + std::string buffer(1500, '\0'); + int flags = 0; + int length = ws.receiveFrame(buffer.data(), buffer.size(), flags); + int op = flags & Poco::Net::WebSocket::FRAME_OP_BITMASK; + if ((length == 0 && flags == 0) || op == Poco::Net::WebSocket::FRAME_OP_CLOSE) { + break; + } + timestamp = bootTime(); + if (op == Poco::Net::WebSocket::FRAME_OP_PING) { + std::unique_lock lock(sendMutex); + ws.sendFrame(buffer.data(), length, Poco::Net::WebSocket::FRAME_FLAG_FIN | Poco::Net::WebSocket::FRAME_OP_PONG); + } else if (op == Poco::Net::WebSocket::FRAME_OP_BINARY) { + if (length < sizeof(WsMsg::Forward) || uint8_t(buffer.front()) != WsMsgKind::FORWARD) { + break; + } + buffer.resize(length); + receive(std::move(buffer)); + } else if (op != Poco::Net::WebSocket::FRAME_OP_PONG) { + break; + } + } + } catch (const std::exception &e) { + candy::logger().debug(std::string("data websocket receive failed: ") + e.what()); + } + connected.store(false); + sender.join(); + if (--connections == 0) { + queue.clear(); + } +} + +} // namespace candy diff --git a/candy/src/websocket/relay.h b/candy/src/websocket/relay.h new file mode 100644 index 00000000..797de668 --- /dev/null +++ b/candy/src/websocket/relay.h @@ -0,0 +1,25 @@ +// SPDX-License-Identifier: MIT +#ifndef CANDY_WEBSOCKET_RELAY_H +#define CANDY_WEBSOCKET_RELAY_H + +#include "core/message.h" +#include "utils/atomic.h" +#include +#include +#include + +namespace candy { + +struct WsRelay { + MsgQueue queue; + Utils::Atomic running{true}; + std::atomic connections{0}; + std::string key; + int limit = 0; + + void run(Poco::Net::WebSocket &ws, std::function receive, std::function active); +}; + +} // namespace candy + +#endif diff --git a/candy/src/websocket/server.cc b/candy/src/websocket/server.cc index f8848865..0911e161 100644 --- a/candy/src/websocket/server.cc +++ b/candy/src/websocket/server.cc @@ -14,10 +14,12 @@ #include #include #include +#include #include #include #include #include +#include #include #include @@ -67,9 +69,18 @@ class HTTPRequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory { namespace candy { void WsCtx::sendFrame(const std::string &frame, int flags) { + std::unique_lock lock(sendMutex); this->ws->sendFrame(frame.data(), frame.size(), flags); } +void WsCtx::sendPacket(const std::string &frame) { + if (relay && relay->connections > 0) { + relay->queue.write(Msg(MsgKind::PACKET, frame), 1024); + } else { + sendFrame(frame); + } +} + int WebSocketServer::setWebSocket(const std::string &uri) { try { Poco::URI parser(uri); @@ -136,7 +147,7 @@ int WebSocketServer::run() { } int WebSocketServer::shutdown() { - this->running = false; + this->running.store(false); if (this->httpServer) { this->httpServer->stopAll(); } @@ -147,6 +158,10 @@ int WebSocketServer::shutdown() { void WebSocketServer::handleMsg(WsCtx &ctx) { uint8_t msgKind = ctx.buffer.front(); switch (msgKind) { + case WsMsgKind::MULTI: + case WsMsgKind::JOIN: + handleMultiMsg(ctx); + break; case WsMsgKind::AUTH: handleAuthMsg(ctx); break; @@ -171,17 +186,56 @@ void WebSocketServer::handleMsg(WsCtx &ctx) { } } +void WebSocketServer::handleMultiMsg(WsCtx &ctx) { + if (ctx.buffer.size() != sizeof(WsMsg::Multi)) { + ctx.status.store(-1); + return; + } + auto *header = (WsMsg::Multi *)ctx.buffer.data(); + std::unique_lock lock(ipCtxMutex); + if (header->type == WsMsgKind::MULTI) { + if (ctx.ip.empty() || ctx.relay) { + return; + } + auto relay = std::make_shared(); + header->count = std::min(header->count, 7); + if (!header->count || RAND_bytes((unsigned char *)header->key, sizeof(header->key)) != 1) { + return; + } + relay->key.assign(header->key, sizeof(header->key)); + relay->limit = header->count; + ctx.relay = relay; + ctx.sendFrame(ctx.buffer); + } else if (ctx.ip.empty()) { + std::string key(header->key, sizeof(header->key)); + for (auto &entry : ipCtxMap) { + auto relay = entry.second->relay; + if (relay && relay->running.load() && relay->key == key && relay->limit > 0) { + ctx.ip = entry.first; + ctx.relay = relay; + ctx.data = true; + --relay->limit; + ctx.sendFrame(ctx.buffer); + return; + } + } + ctx.status.store(-1); + } else { + ctx.status.store(-1); + } +} + void WebSocketServer::handleAuthMsg(WsCtx &ctx) { if (ctx.buffer.length() < sizeof(WsMsg::Auth)) { candy::logger().warning(Poco::format("invalid auth message: len %zu", ctx.buffer.length())); - ctx.status = -1; + ctx.status.store(-1); return; } WsMsg::Auth *header = (WsMsg::Auth *)ctx.buffer.data(); if (!header->check(this->password)) { candy::logger().warning(Poco::format("auth header check failed: buffer %s", to_hex(ctx.buffer))); - ctx.status = -1; + ctx.status.store(-1); return; } @@ -191,7 +245,10 @@ void WebSocketServer::handleAuthMsg(WsCtx &ctx) { std::unique_lock lock(ipCtxMutex); auto it = ipCtxMap.find(header->ip); if (it != ipCtxMap.end()) { - it->second->status = -1; + it->second->status.store(-1); + if (it->second->relay) { + it->second->relay->running.store(false); + } candy::logger().information(Poco::format("reconnect: %s", it->second->ip.toString())); } else { candy::logger().information(Poco::format("connect: %s", ctx.ip.toString())); @@ -205,13 +262,13 @@ void WebSocketServer::handleAuthMsg(WsCtx &ctx) { void WebSocketServer::handleForwardMsg(WsCtx &ctx) { if (ctx.ip.empty()) { candy::logger().debug("unauthorized forward websocket client"); - ctx.status = -1; + ctx.status.store(-1); return; } if (ctx.buffer.length() < sizeof(WsMsg::Forward)) { candy::logger().debug(Poco::format("invalid forawrd message: len %zu", ctx.buffer.length())); - ctx.status = -1; + ctx.status.store(-1); return; } @@ -221,7 +278,7 @@ void WebSocketServer::handleForwardMsg(WsCtx &ctx) { std::shared_lock lock(this->ipCtxMutex); auto it = this->ipCtxMap.find(header->iph.daddr); if (it != this->ipCtxMap.end()) { - it->second->sendFrame(ctx.buffer); + it->second->sendPacket(ctx.buffer); return; } } @@ -254,7 +311,7 @@ void WebSocketServer::handleForwardMsg(WsCtx &ctx) { std::shared_lock lock(this->ipCtxMutex); for (auto c : this->ipCtxMap) { if (c.second->ip != ctx.ip) { - c.second->sendFrame(ctx.buffer); + c.second->sendPacket(ctx.buffer); } } return; @@ -268,24 +325,24 @@ void WebSocketServer::handleForwardMsg(WsCtx &ctx) { void WebSocketServer::handleExptTunMsg(WsCtx &ctx) { if (ctx.buffer.length() < sizeof(WsMsg::ExptTun)) { candy::logger().warning(Poco::format("invalid dynamic address message: len %zu", ctx.buffer.length())); - ctx.status = -1; + ctx.status.store(-1); return; } WsMsg::ExptTun *header = (WsMsg::ExptTun *)ctx.buffer.data(); if (!header->check(this->password)) { candy::logger().warning(Poco::format("dynamic address header check failed: buffer %s", to_hex(ctx.buffer))); - ctx.status = -1; + ctx.status.store(-1); return; } if (this->dhcp.empty()) { candy::logger().warning("unable to allocate dynamic address"); - ctx.status = -1; + ctx.status.store(-1); return; } Address exptTun; if (exptTun.fromCidr(header->cidr)) { candy::logger().warning(Poco::format("dynamic address header cidr invalid: buffer %s", to_hex(ctx.buffer))); - ctx.status = -1; + ctx.status.store(-1); return; } // Check whether the requested address can be used directly @@ -307,7 +364,7 @@ void WebSocketServer::handleExptTunMsg(WsCtx &ctx) { exptTun = exptTun.Next(); if (exptTun.Host() == this->dhcp.Host()) { candy::logger().warning("all addresses in the network are assigned"); - ctx.status = -1; + ctx.status.store(-1); return; } } while (!exptTun.isValid() && this->ipCtxMap.find(exptTun.Host()) != this->ipCtxMap.end()); @@ -322,13 +379,13 @@ void WebSocketServer::handleExptTunMsg(WsCtx &ctx) { void WebSocketServer::handleUdp4ConnMsg(WsCtx &ctx) { if (ctx.ip.empty()) { candy::logger().debug("unauthorized peer websocket client"); - ctx.status = -1; + ctx.status.store(-1); return; } if (ctx.buffer.length() < sizeof(WsMsg::Conn)) { candy::logger().warning(Poco::format("invalid peer conn message: len %zu", ctx.buffer.length())); - ctx.status = -1; + ctx.status.store(-1); return; } @@ -336,7 +393,7 @@ void WebSocketServer::handleUdp4ConnMsg(WsCtx &ctx) { if (ctx.ip != header->src) { candy::logger().debug( Poco::format("peer source address does not match: auth %s source %s", ctx.ip.toString(), header->src.toString())); - ctx.status = -1; + ctx.status.store(-1); return; } std::shared_lock lock(this->ipCtxMutex); @@ -353,14 +410,14 @@ void WebSocketServer::handleUdp4ConnMsg(WsCtx &ctx) { void WebSocketServer::handleVMacMsg(WsCtx &ctx) { if (ctx.buffer.length() < sizeof(WsMsg::VMac)) { candy::logger().warning(Poco::format("invalid vmac message: len %zu", ctx.buffer.length())); - ctx.status = -1; + ctx.status.store(-1); return; } WsMsg::VMac *header = (WsMsg::VMac *)ctx.buffer.data(); if (!header->check(this->password)) { candy::logger().warning(Poco::format("vmac message check failed: buffer %s", to_hex(ctx.buffer))); - ctx.status = -1; + ctx.status.store(-1); return; } @@ -371,13 +428,13 @@ void WebSocketServer::handleVMacMsg(WsCtx &ctx) { void WebSocketServer::handleDiscoveryMsg(WsCtx &ctx) { if (ctx.ip.empty()) { candy::logger().debug("unauthorized discovery websocket client"); - ctx.status = -1; + ctx.status.store(-1); return; } if (ctx.buffer.length() < sizeof(WsMsg::Discovery)) { candy::logger().debug(Poco::format("invalid discovery message: len %zu", ctx.buffer.length())); - ctx.status = -1; + ctx.status.store(-1); return; } @@ -385,7 +442,7 @@ void WebSocketServer::handleDiscoveryMsg(WsCtx &ctx) { if (ctx.ip != header->src) { candy::logger().debug(Poco::format("discovery source address does not match: auth %s source %s", ctx.ip.toString(), header->src.toString())); - ctx.status = -1; + ctx.status.store(-1); return; } @@ -408,13 +465,13 @@ void WebSocketServer::handleDiscoveryMsg(WsCtx &ctx) { void WebSocketServer::HandleGeneralMsg(WsCtx &ctx) { if (ctx.ip.empty()) { candy::logger().debug("unauthorized general websocket client"); - ctx.status = -1; + ctx.status.store(-1); return; } if (ctx.buffer.length() < sizeof(WsMsg::General)) { candy::logger().debug(Poco::format("invalid general message: len %zu", ctx.buffer.length())); - ctx.status = -1; + ctx.status.store(-1); return; } @@ -423,7 +480,7 @@ void WebSocketServer::HandleGeneralMsg(WsCtx &ctx) { if (ctx.ip != header->src) { candy::logger().debug( Poco::format("general source address does not match: auth %s source %s", ctx.ip.toString(), header->src.toString())); - ctx.status = -1; + ctx.status.store(-1); return; } @@ -478,7 +535,7 @@ int WebSocketServer::listen() { Poco::Net::HTTPServerParams *params = new Poco::Net::HTTPServerParams(); params->setMaxThreads(0x00FFFFFF); - this->running = true; + this->running.store(true); WebSocketHandler wsHandler = [this](Poco::Net::WebSocket &ws) { handleWebsocket(ws); }; this->httpServer = std::make_shared(new HTTPRequestHandlerFactory(wsHandler), socket, params); this->httpServer->start(); @@ -492,12 +549,13 @@ int WebSocketServer::listen() { void WebSocketServer::handleWebsocket(Poco::Net::WebSocket &ws) { ws.setReceiveTimeout(Poco::Timespan(1, 0)); + ws.setSendTimeout(Poco::Timespan(3, 0)); WsCtx ctx = {.ws = &ws}; int flags = 0; int length = 0; std::string buffer; - while (this->running && ctx.status == 0) { + while (this->running.load() && ctx.status.load() == 0) { try { buffer.resize(1500); length = ws.receiveFrame(buffer.data(), buffer.size(), flags); @@ -506,7 +564,7 @@ void WebSocketServer::handleWebsocket(Poco::Net::WebSocket &ws) { // Respond to Ping frame if (frameOp == Poco::Net::WebSocket::FRAME_OP_PING) { flags = (int)Poco::Net::WebSocket::FRAME_FLAG_FIN | (int)Poco::Net::WebSocket::FRAME_OP_PONG; - ws.sendFrame(buffer.data(), buffer.size(), flags); + ctx.sendFrame(std::string(buffer.data(), length), flags); continue; } @@ -522,6 +580,16 @@ void WebSocketServer::handleWebsocket(Poco::Net::WebSocket &ws) { // Process client request handleMsg(ctx); + if (ctx.data) { + ctx.relay->run( + ws, + [&](std::string packet) { + ctx.buffer = std::move(packet); + handleForwardMsg(ctx); + }, + [&] { return this->running.load(); }); + return; + } // Reinitialize buffer buffer = std::string(); @@ -538,6 +606,9 @@ void WebSocketServer::handleWebsocket(Poco::Net::WebSocket &ws) { { std::unique_lock lock(ipCtxMutex); + if (ctx.relay && !ctx.data) { + ctx.relay->running.store(false); + } auto it = ipCtxMap.find(ctx.ip); if (it != ipCtxMap.end() && it->second == &ctx) { ipCtxMap.erase(it); diff --git a/candy/src/websocket/server.h b/candy/src/websocket/server.h index 369793b5..04748df9 100644 --- a/candy/src/websocket/server.h +++ b/candy/src/websocket/server.h @@ -3,6 +3,7 @@ #define CANDY_WEBSOCKET_SERVER_H #include "core/net.h" +#include "websocket/relay.h" #include #include #include @@ -17,12 +18,16 @@ struct WsCtx { Poco::Net::WebSocket *ws; std::string buffer; - int status; + Utils::Atomic status{0}; + std::mutex sendMutex; + std::shared_ptr relay; + bool data = false; IP4 ip; std::string vmac; void sendFrame(const std::string &frame, int flags = Poco::Net::WebSocket::FRAME_BINARY); + void sendPacket(const std::string &frame); }; struct SysRoute { @@ -52,6 +57,7 @@ class WebSocketServer { private: void handleMsg(WsCtx &ctx); void handleAuthMsg(WsCtx &ctx); + void handleMultiMsg(WsCtx &ctx); void handleForwardMsg(WsCtx &ctx); void handleExptTunMsg(WsCtx &ctx); void handleUdp4ConnMsg(WsCtx &ctx); @@ -67,7 +73,7 @@ class WebSocketServer { // Lock when accessing the map to keep pointers valid std::shared_mutex ipCtxMutex; - bool running; + Utils::Atomic running{false}; private: // Start listening; new requests will invoke handleWebsocket From 424fc1c1e29e7b50452a3bfc6b0eb0235a30ef6e Mon Sep 17 00:00:00 2001 From: lanthora Date: Tue, 3 Nov 2026 15:00:06 +0800 Subject: [PATCH 2/2] Migrate Windows CI from MINGW64 to UCRT64 --- .github/workflows/check.yaml | 15 ++++++++------- .github/workflows/release.yaml | 15 ++++++++------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 8c0aa93e..fa2b6efc 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -49,21 +49,22 @@ jobs: - name: depends uses: msys2/setup-msys2@v2 with: - msystem: MINGW64 + msystem: UCRT64 update: true install: >- - mingw-w64-x86_64-cmake - mingw-w64-x86_64-ninja - mingw-w64-x86_64-gcc - mingw-w64-x86_64-spdlog - mingw-w64-x86_64-poco + mingw-w64-ucrt-x86_64-cmake + mingw-w64-ucrt-x86_64-pkgconf + mingw-w64-ucrt-x86_64-ninja + mingw-w64-ucrt-x86_64-gcc + mingw-w64-ucrt-x86_64-spdlog + mingw-w64-ucrt-x86_64-poco - name: checkout uses: actions/checkout@v4 - name: cache uses: actions/cache@v4 with: path: build - key: ${{ hashFiles('CMakeLists.txt') }} + key: windows-ucrt64-${{ hashFiles('CMakeLists.txt', '.github/workflows/check.yaml', '.github/workflows/release.yaml') }} - name: build shell: msys2 {0} run: cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo && cmake --build build diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 1df4a0a1..249e4c80 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -53,21 +53,22 @@ jobs: - name: setup msys2 uses: msys2/setup-msys2@v2 with: - msystem: MINGW64 + msystem: UCRT64 update: true install: >- - mingw-w64-x86_64-cmake - mingw-w64-x86_64-ninja - mingw-w64-x86_64-gcc - mingw-w64-x86_64-spdlog - mingw-w64-x86_64-poco + mingw-w64-ucrt-x86_64-cmake + mingw-w64-ucrt-x86_64-pkgconf + mingw-w64-ucrt-x86_64-ninja + mingw-w64-ucrt-x86_64-gcc + mingw-w64-ucrt-x86_64-spdlog + mingw-w64-ucrt-x86_64-poco - name: checkout uses: actions/checkout@v4 - name: cache uses: actions/cache@v4 with: path: build - key: ${{ hashFiles('CMakeLists.txt') }} + key: windows-ucrt64-${{ hashFiles('CMakeLists.txt', '.github/workflows/check.yaml', '.github/workflows/release.yaml') }} - name: build shell: msys2 {0} run: |