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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
- name: Install build and isolated-test dependencies
run: |
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y cmake ninja-build g++ pkg-config libpq-dev libcurl4-openssl-dev libssl-dev libxml2-dev nlohmann-json3-dev libcpp-httplib-dev python3 postgresql openssl nodejs
DEBIAN_FRONTEND=noninteractive apt-get install -y cmake ninja-build g++ pkg-config libpq-dev libcurl4-openssl-dev libssl-dev libxml2-dev nlohmann-json3-dev libcpp-httplib-dev python3 postgresql openssl nodejs libgtk-3-dev xvfb xauth
- name: Build and numerical checks
run: |
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release
Expand Down
20 changes: 20 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,23 @@ add_test(NAME core COMMAND core_tests)
add_executable(release_tests tests/release_tests.cpp)
target_link_libraries(release_tests PRIVATE nlohmann_json::nlohmann_json OpenSSL::Crypto LibXml2::LibXml2 CURL::libcurl)
add_test(NAME release COMMAND release_tests)
option(REACH_BUILD_DESKTOP "Build the native GTK desktop frontend" ON)
if(REACH_BUILD_DESKTOP)
set(REACH_PREV_PKG_SYSROOT "$ENV{PKG_CONFIG_SYSROOT_DIR}")
set(REACH_PREV_PKG_PATH "$ENV{PKG_CONFIG_PATH}")
set(ENV{PKG_CONFIG_SYSROOT_DIR} "")
set(ENV{PKG_CONFIG_PATH} "")
pkg_check_modules(GTK3 QUIET IMPORTED_TARGET gtk+-3.0>=3.24)
set(ENV{PKG_CONFIG_SYSROOT_DIR} "${REACH_PREV_PKG_SYSROOT}")
set(ENV{PKG_CONFIG_PATH} "${REACH_PREV_PKG_PATH}")
if(GTK3_FOUND)
add_executable(reachterminal-desktop desktop/main.cpp desktop/pages.cpp)
target_link_libraries(reachterminal-desktop PRIVATE PkgConfig::GTK3 CURL::libcurl nlohmann_json::nlohmann_json Threads::Threads)
target_compile_options(reachterminal-desktop PRIVATE -Wall -Wextra)
add_executable(native_tests desktop/unit_tests.cpp)
target_link_libraries(native_tests PRIVATE PkgConfig::GTK3 CURL::libcurl nlohmann_json::nlohmann_json Threads::Threads)
add_test(NAME native-adapters COMMAND native_tests)
else()
message(STATUS "GTK 3.24 development files unavailable; install libgtk-3-dev for native frontend")
endif()
endif()
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ReachTerminal

默认使用 **C++ / GTK 原生桌面前端**,不需要浏览器或 WebView。编译后运行 `./scripts/desktop.sh`;参见 [原生桌面说明](docs/native-desktop.md)。IBKR 官方网关登录与双重验证仍使用其官方外部登录流程。

An open-source, self-hosted investment research terminal. Browser frontend,
C++20 backend, PostgreSQL database. No broker order submission code.

Expand Down
123 changes: 123 additions & 0 deletions desktop/api.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#pragma once
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <curl/curl.h>
#include <functional>
#include <mutex>
#include <nlohmann/json.hpp>
#include <queue>
#include <stdexcept>
#include <thread>
using json = nlohmann::json;
class LocalApi {
std::string base, token, expected;
std::atomic<bool> &stopping;
std::chrono::steady_clock::time_point last{};
json transport(const std::string &path, const std::string &body, bool post,
const std::string &type) {
if (!path.starts_with("/api/") || path.find('\r') != std::string::npos ||
path.find('\n') != std::string::npos)
throw std::runtime_error("Invalid local API path");
auto now = std::chrono::steady_clock::now();
if (now - last < std::chrono::milliseconds(300))
std::this_thread::sleep_for(std::chrono::milliseconds(300) - (now - last));
last = std::chrono::steady_clock::now();
auto c = curl_easy_init();
if (!c)
throw std::runtime_error("Cannot initialize local connection");
std::string data, url = base + path;
auto headers = curl_slist_append(nullptr, ("X-Reach-Token: " + token).c_str());
headers = curl_slist_append(headers, ("Content-Type: " + type).c_str());
curl_easy_setopt(c, CURLOPT_URL, url.c_str());
curl_easy_setopt(c, CURLOPT_PROXY, "");
curl_easy_setopt(c, CURLOPT_FOLLOWLOCATION, 0L);
curl_easy_setopt(c, CURLOPT_PROTOCOLS_STR, "http");
curl_easy_setopt(c, CURLOPT_CONNECTTIMEOUT, 2L);
curl_easy_setopt(c, CURLOPT_TIMEOUT, 60L);
curl_easy_setopt(c, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(c, CURLOPT_HTTPHEADER, headers);
if (post) {
curl_easy_setopt(c, CURLOPT_POST, 1L);
curl_easy_setopt(c, CURLOPT_POSTFIELDS, body.data());
curl_easy_setopt(c, CURLOPT_POSTFIELDSIZE_LARGE, static_cast<curl_off_t>(body.size()));
}
curl_easy_setopt(
c, CURLOPT_WRITEFUNCTION, +[](char *p, size_t a, size_t b, void *u) -> size_t {
auto &s = *static_cast<std::string *>(u);
if (b && a > (32 * 1024 * 1024 - s.size()) / b)
return 0;
s.append(p, a * b);
return a * b;
});
curl_easy_setopt(c, CURLOPT_WRITEDATA, &data);
curl_easy_setopt(c, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(
c, CURLOPT_XFERINFOFUNCTION,
+[](void *p, curl_off_t, curl_off_t, curl_off_t, curl_off_t) -> int {
return static_cast<std::atomic<bool> *>(p)->load() ? 1 : 0;
});
curl_easy_setopt(c, CURLOPT_XFERINFODATA, &stopping);
auto code = curl_easy_perform(c);
long status = 0;
curl_easy_getinfo(c, CURLINFO_RESPONSE_CODE, &status);
curl_slist_free_all(headers);
curl_easy_cleanup(c);
if (code != CURLE_OK)
throw std::runtime_error("本地后端连接失败:" + std::string(curl_easy_strerror(code)) +
"。请检查终端后端是否正在运行。");
json v;
try {
v = json::parse(data);
} catch (...) {
throw std::runtime_error("本地后端返回无效数据");
}
if (status < 200 || status >= 300)
throw std::runtime_error(
v.value("error", std::string("本地请求失败 HTTP ") + std::to_string(status)));
return v;
}

public:
LocalApi(int port, const std::string &mode, std::atomic<bool> &stop)
: base("http://127.0.0.1:" + std::to_string(port)), expected(mode), stopping(stop) {
if (port < 1 || port > 65535)
throw std::runtime_error("Invalid local port");
}
void connect() {
auto s = transport("/api/session", "", false, "application/json");
if (s.value("environment", std::string()) != expected)
throw std::runtime_error("工作区环境不匹配,已拒绝连接以保护用户数据");
token = s.at("token");
}
json get(const std::string &p) {
if (token.empty())
connect();
return transport(p, "", false, "application/json");
}
json post(const std::string &p, const json &v = json::object()) {
if (token.empty())
connect();
return transport(p, v.dump(), true, "application/json");
}
json raw(const std::string &p, const std::string &v, const std::string &type) {
if (token.empty())
connect();
return transport(p, v, true, type);
}
static std::string encode(const std::string &s) {
std::string out;
const char *h = "0123456789ABCDEF";
for (unsigned char c : s) {
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') ||
c == '-' || c == '_' || c == '.')
out += static_cast<char>(c);
else {
out += '%';
out += h[c >> 4];
out += h[c & 15];
}
}
return out;
}
};
78 changes: 78 additions & 0 deletions desktop/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "window.hpp"
#include <iostream>
void Terminal::preview() {
if (snapshot.empty())
return;
gtk_container_check_resize(GTK_CONTAINER(window));
GtkAllocation allocation;
gtk_widget_get_allocation(window, &allocation);
gtk_widget_size_allocate(window, &allocation);
while (gtk_events_pending())
gtk_main_iteration();
int width = gtk_widget_get_allocated_width(window),
height = gtk_widget_get_allocated_height(window);
auto surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
auto cr = cairo_create(surface);
gtk_widget_draw(window, cr);
cairo_destroy(cr);
auto result = cairo_surface_write_to_png(surface, snapshot.c_str());
cairo_surface_destroy(surface);
if (result != CAIRO_STATUS_SUCCESS) {
testFailed = true;
std::cerr << "Cannot render native preview\n";
}
}
int main(int argc, char **argv) {
bool test = false;
std::string image;
int port = 8788;
try {
if (auto p = std::getenv("REACH_DESKTOP_PORT"))
port = std::stoi(p);
for (int i = 1; i < argc; ++i) {
std::string a = argv[i];
if (a == "--self-test")
test = true;
else if (a == "--snapshot" && i + 1 < argc)
image = argv[++i];
else
throw std::runtime_error(
"Usage: reachterminal-desktop [--self-test] [--snapshot FILE]");
}
if (port < 1 || port > 65535)
throw std::runtime_error("Invalid local port");
curl_global_init(CURL_GLOBAL_DEFAULT);
int gtkArgc = 1;
if (!gtk_init_check(&gtkArgc, &argv))
throw std::runtime_error("无法连接图形桌面;请在本机桌面会话运行原生终端。");
auto css = gtk_css_provider_new();
gtk_css_provider_load_from_data(css, R"CSS(
* { font-family: "Noto Sans", "DejaVu Sans", sans-serif; font-size: 13px; }
window, viewport {background:#0b121a;color:#d6e2eb;} box {color:#d6e2eb;} .rail {background:#111b27;border-right:1px solid #283746;}
.brand {color:#50ccba;font-size:24px;font-weight:bold;} .eyebrow {color:#53c6b7;font-size:11px;letter-spacing:1px;} .page-title {font-size:27px;font-weight:bold;}
.section-title {font-size:15px;font-weight:bold;color:#e4edf4;} .muted {color:#94a6b8;font-size:12px;} .metric {font-size:23px;font-weight:bold;color:#e4edf4;}
frame {border:1px solid #2a3948;border-radius:7px;background:#131e2a;} frame border {border:none;} label {padding:2px 0;}
button {background:#192836;color:#dbe7ef;border:1px solid #314453;border-radius:5px;padding:9px 13px;box-shadow:none;text-shadow:none;}
button:hover,button.selected {background:#1b4945;color:#77e3d3;border-color:#388d81;} button:disabled {color:#526675;background:#15202a;}
entry,spinbutton,combobox button,textview text {background:#0c1721;color:#dce7ef;border-color:#304252;} entry {padding:7px;} spinbutton entry {padding:7px;}
treeview {background:#111d28;color:#dbe7ef;} treeview:selected {background:#24564f;color:#ecfffa;} treeview header button {background:#182b3c;color:#a7c0d0;}
scrolledwindow {border:none;} .connection {color:#56cdbb;border-left:3px solid #56cdbb;padding:12px;} scrollbar slider {background:#3b5367;min-width:7px;min-height:24px;border-radius:5px;}
)CSS",
-1, nullptr);
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(css),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
g_object_unref(css);
Terminal terminal(port, test, image);
terminal.run();
if (test) {
if (terminal.failed())
return 1;
std::cout << "Native desktop: all 9 workspaces and hedge scenario verified in isolated "
"test environment\n";
}
return 0;
} catch (const std::exception &e) {
std::cerr << "ReachTerminal Desktop: " << e.what() << '\n';
return 1;
}
}
Loading
Loading