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)]
16pub struct Config {
17    pub socks5_listen: Option<SocketAddr>,
18    pub http_proxy_listen: Option<SocketAddr>,
19    pub pac_listen: Option<SocketAddr>,
20
21    pub control_listen: Option<SocketAddr>,
22    #[serde(default)]
23    pub control_listen_unix: Option<PathBuf>,
24    /// Windows named pipe (e.g. `\\.\pipe\geph-engine-control`) to serve the
25    /// control protocol on. The Windows analogue of `control_listen_unix`.
26    #[serde(default)]
27    pub control_listen_pipe: Option<String>,
28    pub exit_constraint: ExitConstraint,
29    #[serde(default)]
30    pub allow_direct: bool,
31
32    pub cache: Option<PathBuf>,
33
34    pub broker: Option<BrokerSource>,
35    #[serde(alias = "tunneled_broker_source")]
36    pub tunneled_broker: Option<TunneledBrokerSource>,
37    pub broker_keys: Option<BrokerKeys>,
38
39    #[serde(default)]
40    pub port_forward: Vec<PortForwardCfg>,
41
42    #[serde(default)]
43    pub spoof_dns: bool,
44    #[serde(default)]
45    pub passthrough_china: bool,
46    /// Whether to let connections to private/LAN addresses bypass the tunnel
47    /// (connect directly). Defaults to true. Set false for a strict full tunnel.
48    #[serde(default = "default_allow_lan")]
49    pub allow_lan: bool,
50    #[serde(default)]
51    pub dry_run: bool,
52    #[serde(default)]
53    pub credentials: Credential,
54
55    #[serde(default)]
56    pub sess_metadata: serde_json::Value,
57    pub task_limit: Option<u32>,
58}
59
60fn default_allow_lan() -> bool {
61    true
62}
63
64impl Config {
65    /// Create an "inert" version of this config that does not start any processes.
66    pub fn inert(&self) -> Self {
67        let mut this = self.clone();
68        this.dry_run = true;
69        this.socks5_listen = None;
70        this.http_proxy_listen = None;
71        this.pac_listen = None;
72        this.control_listen = None;
73        this.control_listen_unix = None;
74        this.control_listen_pipe = None;
75        this
76    }
77}
78
79#[derive(Serialize, Deserialize, Clone)]
80pub struct PortForwardCfg {
81    pub listen: SocketAddr,
82    pub connect: String,
83}
84
85#[derive(Serialize, Deserialize, Clone)]
86/// Broker keys, in hexadecimal format.
87pub struct BrokerKeys {
88    pub master: String,
89    pub mizaru_free: String,
90    pub mizaru_plus: String,
91    pub mizaru_bw: String,
92}
93
94#[derive(Serialize, Deserialize, Clone)]
95#[serde(rename_all = "snake_case")]
96pub enum BrokerSource {
97    Direct(String),
98    Fronted {
99        front: String,
100        host: String,
101        #[serde(default)]
102        override_dns: Option<Vec<SocketAddr>>,
103    },
104    DirectTcp(SocketAddr),
105    AwsLambda {
106        function_name: String,
107        region: String,
108        obfs_key: String,
109    },
110    Race(Vec<BrokerSource>),
111    PriorityRace(BTreeMap<u64, BrokerSource>),
112}
113
114#[derive(Serialize, Deserialize, Clone)]
115#[serde(rename_all = "snake_case")]
116pub enum TunneledBrokerSource {
117    Direct(String),
118}