Skip to main content

geph5_misc_rpc/
client_config.rs

1//! Plain-data types describing how to **configure** a geph5-client engine: its
2//! [`Config`] plus the broker-source descriptors its config embeds.
3//!
4//! These live here, alongside the engine's control protocol
5//! ([`crate::client_control`]), so that tools which merely *drive or configure* an
6//! engine (the `geph` daemon/CLI) can depend on this lightweight crate instead of
7//! the entire engine. The behavior that turns these descriptors into live
8//! transports stays in `geph5-client` (as extension traits).
9
10use std::{collections::BTreeMap, net::SocketAddr, path::PathBuf};
11
12use geph5_broker_protocol::{Credential, ExitConstraint};
13use serde::{Deserialize, Serialize};
14
15#[derive(Serialize, Deserialize, Clone)]
16// Reject unknown fields so a stale config from before the VPN refactor — which
17// carried `vpn`/`vpn_fd` keys that no longer exist — fails loudly instead of
18// silently parsing and running proxy-only (the old fields were ignored, so a
19// user who expected a system tunnel would have leaked outside it).
20#[serde(deny_unknown_fields)]
21pub struct Config {
22    pub socks5_listen: Option<SocketAddr>,
23    pub http_proxy_listen: Option<SocketAddr>,
24    pub pac_listen: Option<SocketAddr>,
25
26    pub control_listen: Option<SocketAddr>,
27    #[serde(default)]
28    pub control_listen_unix: Option<PathBuf>,
29    /// Windows named pipe (e.g. `\\.\pipe\geph-engine-control`) to serve the
30    /// control protocol on. The Windows analogue of `control_listen_unix`.
31    #[serde(default)]
32    pub control_listen_pipe: Option<String>,
33    pub exit_constraint: ExitConstraint,
34    #[serde(default)]
35    pub allow_direct: bool,
36
37    pub cache: Option<PathBuf>,
38
39    pub broker: Option<BrokerSource>,
40    #[serde(alias = "tunneled_broker_source")]
41    pub tunneled_broker: Option<TunneledBrokerSource>,
42    pub broker_keys: Option<BrokerKeys>,
43
44    #[serde(default)]
45    pub port_forward: Vec<PortForwardCfg>,
46
47    #[serde(default)]
48    pub spoof_dns: bool,
49    #[serde(default)]
50    pub passthrough_china: bool,
51    /// Whether to let connections to private/LAN addresses bypass the tunnel
52    /// (connect directly). Defaults to true. Set false for a strict full tunnel.
53    #[serde(default = "default_allow_lan")]
54    pub allow_lan: bool,
55    #[serde(default)]
56    pub dry_run: bool,
57    #[serde(default)]
58    pub credentials: Credential,
59
60    #[serde(default)]
61    pub sess_metadata: serde_json::Value,
62    pub task_limit: Option<u32>,
63}
64
65fn default_allow_lan() -> bool {
66    true
67}
68
69impl Config {
70    /// Create an "inert" version of this config that does not start any processes.
71    pub fn inert(&self) -> Self {
72        let mut this = self.clone();
73        this.dry_run = true;
74        this.socks5_listen = None;
75        this.http_proxy_listen = None;
76        this.pac_listen = None;
77        this.control_listen = None;
78        this.control_listen_unix = None;
79        this.control_listen_pipe = None;
80        this
81    }
82}
83
84#[derive(Serialize, Deserialize, Clone)]
85pub struct PortForwardCfg {
86    pub listen: SocketAddr,
87    pub connect: String,
88}
89
90#[derive(Serialize, Deserialize, Clone)]
91/// Broker keys, in hexadecimal format.
92pub struct BrokerKeys {
93    pub master: String,
94    pub mizaru_free: String,
95    pub mizaru_plus: String,
96    pub mizaru_bw: String,
97}
98
99#[derive(Serialize, Deserialize, Clone)]
100#[serde(rename_all = "snake_case")]
101pub enum BrokerSource {
102    Direct(String),
103    Fronted {
104        front: String,
105        host: String,
106        #[serde(default)]
107        override_dns: Option<Vec<SocketAddr>>,
108    },
109    DirectTcp(SocketAddr),
110    AwsLambda {
111        function_name: String,
112        region: String,
113        obfs_key: String,
114    },
115    Race(Vec<BrokerSource>),
116    PriorityRace(BTreeMap<u64, BrokerSource>),
117}
118
119#[derive(Serialize, Deserialize, Clone)]
120#[serde(rename_all = "snake_case")]
121pub enum TunneledBrokerSource {
122    Direct(String),
123}