Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 8 additions & 7 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
10 changes: 10 additions & 0 deletions candy-cli/src/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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("<int>")
.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)")
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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; }},
};
Expand Down
1 change: 1 addition & 0 deletions candy-cli/src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions candy-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions candy-service/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
39 changes: 36 additions & 3 deletions candy-service/src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,42 @@ class RunHandler : public BaseJSONHandler {
return;
}

auto json = readRequest(request);
auto id = json->getValue<std::string>("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<std::string>("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<int>(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);
Expand Down
4 changes: 4 additions & 0 deletions candy.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down
1 change: 1 addition & 0 deletions candy/src/candy/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ bool run(const std::string &id, const Poco::JSON::Object &config) {
client->setName(config.getValue<std::string>("name"));
client->setPassword(config.getValue<std::string>("password"));
client->setWebSocket(config.getValue<std::string>("websocket"));
client->setConnections(config.getValue<int>("connections"));
client->setTunAddress(config.getValue<std::string>("tun"));
client->setVirtualMac(config.getValue<std::string>("vmac"));
client->setExptTunAddress(config.getValue<std::string>("expt"));
Expand Down
31 changes: 4 additions & 27 deletions candy/src/core/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,9 @@
#include "core/client.h"
#include "core/message.h"
#include <Poco/String.h>
#include <chrono>

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();
}
Expand Down Expand Up @@ -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);
}
Expand Down
16 changes: 1 addition & 15 deletions candy/src/core/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,18 @@
#include "tun/tun.h"
#include "utils/atomic.h"
#include "websocket/client.h"
#include <condition_variable>
#include <mutex>
#include <queue>
#include <string>

namespace candy {

class MsgQueue {
public:
Msg read();
void write(Msg msg);
void clear();

private:
std::queue<Msg> msgQueue;
std::mutex msgMutex;
std::condition_variable msgCondition;
};

class Client {
public:
~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);
Expand Down
30 changes: 30 additions & 0 deletions candy/src/core/message.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
// SPDX-License-Identifier: MIT
#include "core/message.h"
#include <chrono>

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);
Expand Down
15 changes: 15 additions & 0 deletions candy/src/core/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
#define CANDY_CORE_MESSAGE_H

#include "core/net.h"
#include <condition_variable>
#include <cstring>
#include <mutex>
#include <queue>
#include <string>

namespace candy {
Expand All @@ -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<Msg> msgQueue;
std::mutex msgMutex;
std::condition_variable msgCondition;
};

namespace CoreMsg {

struct PubInfo {
Expand Down
Loading
Loading