Skip to content

Commit c2b0050

Browse files
authored
Add status for nodes (#7)
1 parent d50c49c commit c2b0050

13 files changed

Lines changed: 471 additions & 78 deletions

File tree

dev/dev.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,8 @@ ON inbounds (node_id, tag);
107107
ALTER TABLE users
108108
ALTER COLUMN daily_limit_mb DROP NOT NULL;
109109

110+
111+
ALTER TABLE nodes ADD COLUMN cores INTEGER NOT NULL DEFAULT 1;
112+
ALTER TABLE nodes ADD COLUMN max_bandwidth_bps BIGINT NOT NULL DEFAULT 100000000;
113+
114+

docs/PROTO.md

Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Application Protocol Description
1+
# Application Protocol Design
22

33
## Overview
44

@@ -7,34 +7,80 @@ The application communicates via ZeroMQ Pub/Sub pattern, exchanging binary messa
77
This approach replaces plain JSON messaging with efficient zero-copy serialization, reducing message size and CPU overhead.
88
Supported Actions & Message Structures
99

10+
Check src/bin/utils.rs for debugging binary message sending
11+
1012
Messages are sent as binary blobs representing the following Rust struct:
1113

12-
#[derive(Archive, Serialize, Deserialize, Clone, Debug)]
13-
pub struct Message {
14-
pub conn_id: UuidWrapper,
15-
pub action: Action,
16-
pub tag: ArchivedTagSimple,
17-
pub wg: Option<WgParam>,
18-
pub password: Option<String>,
19-
}
20-
21-
#[derive(Archive, Serialize, Deserialize, Debug, Clone)]
22-
pub enum Action {
23-
Create,
24-
Update,
25-
Delete,
26-
ResetStat,
27-
}
28-
29-
CREATE/UPDATE/DELETE Connection
14+
#[derive(Archive, Serialize, Deserialize, Clone, Debug)]
15+
pub struct Message {
16+
pub conn_id: UuidWrapper,
17+
pub action: Action,
18+
pub tag: ProtoTag,
19+
pub wg: Option<Param>,
20+
pub password: Option<String>,
21+
}
22+
23+
#[derive(Archive, Serialize, Deserialize, Debug, Clone)]
24+
pub enum Action {
25+
Create,
26+
Update,
27+
Delete,
28+
ResetStat,
29+
}
30+
31+
#[derive(
32+
Archive,
33+
Clone,
34+
Debug,
35+
RkyvDeserialize,
36+
RkyvSerialize,
37+
Deserialize,
38+
Serialize,
39+
PartialEq,
40+
Eq,
41+
Hash,
42+
Copy,
43+
ToSql,
44+
FromSql,
45+
)]
46+
#[archive_attr(derive(Clone, Debug))]
47+
#[archive(check_bytes)]
48+
#[postgres(name = "proto", rename_all = "snake_case")]
49+
pub enum ProtoTag {
50+
#[serde(rename = "VlessXtls")]
51+
VlessXtls,
52+
#[serde(rename = "VlessGrpc")]
53+
VlessGrpc,
54+
#[serde(rename = "Vmess")]
55+
Vmess,
56+
#[serde(rename = "Shadowsocks")]
57+
Shadowsocks,
58+
#[serde(rename = "Wireguard")]
59+
Wireguard,
60+
}
61+
62+
#[derive(
63+
Archive, Clone, Debug, Serialize, Deserialize, RkyvDeserialize, RkyvSerialize, PartialEq,
64+
)]
65+
pub struct Param {
66+
pub keys: Keys,
67+
pub address: IpAddrMaskSerializable,
68+
}
69+
70+
71+
72+
Note: password field only for Shadowsocks
73+
wg fileds only for wireguard
74+
75+
## CREATE/UPDATE/DELETE/ResetStat Connection
3076

3177
Connections represent individual tunnels (WireGuard, Shadowsocks, VLESS, etc.) and are managed similarly.
3278

3379
Binary message includes:
3480

3581
conn_id: Connection UUID
3682

37-
action: Create/Update/Delete
83+
action: Create/Update/Delete/ResetStat
3884

3985
tag: Protocol type string ("Wireguard", "Shadowsocks", "VlessXtls", etc.)
4086

@@ -73,6 +119,9 @@ Create
73119
"password": null
74120
}
75121

122+
Display output — binary message printed
123+
17865be5-e18b-40d6-b5af-e1c4d51ff50a | Create | Wireguard | privkey: LY8D/CyB/JT1uiFhK1yVKxBB3VMZeA0DzOAJEvgQw50= pubkey: a4uH3iSdV6Ifc7thKL8IHTM8PkL/yPBUztN9xuoA2Do= address: 10.10.0.24/32 | -
124+
76125
Update
77126

78127
{
@@ -92,6 +141,9 @@ Update
92141
"password": null
93142
}
94143

144+
Display output — binary message printed
145+
17865be5-e18b-40d6-b5af-e1c4d51ff50a | Update | Wireguard | privkey: NEW_PRIVATE_KEY_BASE64 pubkey: NEW_PUBLIC_KEY_BASE64 address: 10.10.0.25/32 | -
146+
95147
Delete
96148

97149
{
@@ -101,3 +153,7 @@ Delete
101153
"wg": null,
102154
"password": null
103155
}
156+
157+
158+
Display output — binary message printed
159+
17865be5-e18b-40d6-b5af-e1c4d51ff50a | Delete | Wireguard | - | -

src/bin/api/core/clickhouse/mod.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use clickhouse::Client as ChClient;
2+
use pony::metrics::metrics::Metric;
3+
use serde::de::DeserializeOwned;
4+
use std::fmt::Debug;
25

36
pub mod query;
7+
pub mod score;
48

59
#[derive(Clone)]
610
pub struct ChContext {
@@ -18,3 +22,22 @@ impl ChContext {
1822
ChClient::default().with_url(&self.url)
1923
}
2024
}
25+
26+
impl ChContext {
27+
async fn execute_metric_query<T>(&self, query: &str) -> Option<Metric<T>>
28+
where
29+
T: DeserializeOwned + Debug + Send + Clone + Sync + 'static,
30+
{
31+
log::debug!("Executing query:\n{}", query);
32+
let client = self.client();
33+
34+
let result = client.query(query).fetch_all::<Metric<T>>().await;
35+
36+
match &result {
37+
Ok(rows) => log::debug!("Fetched {} rows", rows.len()),
38+
Err(e) => log::error!("Query failed: {:?}", e),
39+
}
40+
41+
result.ok().and_then(|mut rows| rows.pop())
42+
}
43+
}

0 commit comments

Comments
 (0)