Skip to content

Commit aac63e8

Browse files
committed
chore(config): add toml crate and define TOML intermediate structs
Adds the `toml = "0.8"` dependency to Cargo.toml as the first step of the JSON -> TOML config migration. No behaviour changes in this commit -- the existing flat Config struct and all callers are completely untouched. src/config.rs: - Add Serialize to ScriptId derive (required for the migration write path in the next commit) - Append TOML intermediate structs at the bottom of the file: - TomlRelay: maps the [relay] section, covers all relay/batching/ blacklist/timeout fields - TomlNetwork: maps the [network] section with Default impl, includes block_stun field (added in v1.9.28 therealaleph#1115, absent from original brief) - TomlScan: maps the [scan] section with Default impl - TomlLogging: maps the [logging] section with Default impl - TomlConfig: root document struct, composes all sections plus existing ExitNodeConfig and FrontingGroup These structs are only used inside Config::load_toml and the JSON->TOML migration writer added in subsequent commits. Both paths produce a flat Config via From<TomlConfig> so nothing outside config.rs needs to change
1 parent 98e73d7 commit aac63e8

3 files changed

Lines changed: 210 additions & 2 deletions

File tree

Cargo.lock

Lines changed: 55 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ base64 = "0.22"
4444
bytes = "1"
4545
httparse = "1"
4646
rand = "0.8"
47+
toml = "0.8"
4748
h2 = "0.4"
4849
http = "1"
4950
flate2 = "1"

src/config.rs

Lines changed: 154 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl Mode {
4141
}
4242
}
4343

44-
#[derive(Debug, Clone, Deserialize)]
44+
#[derive(Debug, Clone, Deserialize, Serialize)]
4545
#[serde(untagged)]
4646
pub enum ScriptId {
4747
One(String),
@@ -663,6 +663,159 @@ impl Config {
663663
}
664664
}
665665

666+
// TOML intermediate structs
667+
//
668+
// The flat `Config` struct and all its callers are unchanged. These structs
669+
// only exist inside Config::load_toml and the JSON->TOML migration writer.
670+
// Both paths produce a flat Config in the end via From<TomlConfig>.
671+
672+
/// [relay] section of config.toml.
673+
#[derive(Debug, Clone, Deserialize, Serialize)]
674+
pub struct TomlRelay {
675+
pub mode: String,
676+
#[serde(default)]
677+
pub script_id: Option<ScriptId>,
678+
#[serde(default)]
679+
pub script_ids: Option<ScriptId>,
680+
#[serde(default)]
681+
pub auth_key: String,
682+
#[serde(default)]
683+
pub parallel_relay: u8,
684+
#[serde(default)]
685+
pub enable_batching: bool,
686+
#[serde(default)]
687+
pub coalesce_step_ms: u16,
688+
#[serde(default)]
689+
pub coalesce_max_ms: u16,
690+
#[serde(default)]
691+
pub youtube_via_relay: bool,
692+
#[serde(default)]
693+
pub normalize_x_graphql: bool,
694+
#[serde(default)]
695+
pub disable_padding: bool,
696+
#[serde(default)]
697+
pub force_http1: bool,
698+
#[serde(default = "default_auto_blacklist_strikes")]
699+
pub auto_blacklist_strikes: u32,
700+
#[serde(default = "default_auto_blacklist_window_secs")]
701+
pub auto_blacklist_window_secs: u64,
702+
#[serde(default = "default_auto_blacklist_cooldown_secs")]
703+
pub auto_blacklist_cooldown_secs: u64,
704+
#[serde(default = "default_request_timeout_secs")]
705+
pub request_timeout_secs: u64,
706+
}
707+
708+
/// [network] section of config.toml.
709+
#[derive(Debug, Clone, Deserialize, Serialize)]
710+
pub struct TomlNetwork {
711+
#[serde(default = "default_google_ip")]
712+
pub google_ip: String,
713+
#[serde(default = "default_front_domain")]
714+
pub front_domain: String,
715+
#[serde(default = "default_listen_host")]
716+
pub listen_host: String,
717+
#[serde(default = "default_listen_port")]
718+
pub listen_port: u16,
719+
#[serde(default)]
720+
pub socks5_port: Option<u16>,
721+
#[serde(default = "default_verify_ssl")]
722+
pub verify_ssl: bool,
723+
#[serde(default)]
724+
pub upstream_socks5: Option<String>,
725+
#[serde(default = "default_block_quic")]
726+
pub block_quic: bool,
727+
#[serde(default = "default_block_stun")]
728+
pub block_stun: bool,
729+
#[serde(default)]
730+
pub sni_hosts: Option<Vec<String>>,
731+
#[serde(default)]
732+
pub passthrough_hosts: Vec<String>,
733+
#[serde(default = "default_tunnel_doh")]
734+
pub tunnel_doh: bool,
735+
#[serde(default = "default_block_doh")]
736+
pub block_doh: bool,
737+
#[serde(default)]
738+
pub bypass_doh_hosts: Vec<String>,
739+
#[serde(default)]
740+
pub hosts: HashMap<String, String>,
741+
}
742+
743+
impl Default for TomlNetwork {
744+
fn default() -> Self {
745+
Self {
746+
google_ip: default_google_ip(),
747+
front_domain: default_front_domain(),
748+
listen_host: default_listen_host(),
749+
listen_port: default_listen_port(),
750+
socks5_port: None,
751+
verify_ssl: default_verify_ssl(),
752+
upstream_socks5: None,
753+
block_quic: default_block_quic(),
754+
block_stun: default_block_stun(),
755+
sni_hosts: None,
756+
passthrough_hosts: Vec::new(),
757+
tunnel_doh: default_tunnel_doh(),
758+
block_doh: default_block_doh(),
759+
bypass_doh_hosts: Vec::new(),
760+
hosts: HashMap::new(),
761+
}
762+
}
763+
}
764+
765+
/// [scan] section of config.toml.
766+
#[derive(Debug, Clone, Deserialize, Serialize)]
767+
pub struct TomlScan {
768+
#[serde(default = "default_fetch_ips_from_api")]
769+
pub fetch_ips_from_api: bool,
770+
#[serde(default = "default_max_ips_to_scan")]
771+
pub max_ips_to_scan: usize,
772+
#[serde(default = "default_scan_batch_size")]
773+
pub scan_batch_size: usize,
774+
#[serde(default = "default_google_ip_validation")]
775+
pub google_ip_validation: bool,
776+
}
777+
778+
impl Default for TomlScan {
779+
fn default() -> Self {
780+
Self {
781+
fetch_ips_from_api: default_fetch_ips_from_api(),
782+
max_ips_to_scan: default_max_ips_to_scan(),
783+
scan_batch_size: default_scan_batch_size(),
784+
google_ip_validation: default_google_ip_validation(),
785+
}
786+
}
787+
}
788+
789+
/// [logging] section of config.toml.
790+
#[derive(Debug, Clone, Deserialize, Serialize)]
791+
pub struct TomlLogging {
792+
#[serde(default = "default_log_level")]
793+
pub log_level: String,
794+
}
795+
796+
impl Default for TomlLogging {
797+
fn default() -> Self {
798+
Self { log_level: default_log_level() }
799+
}
800+
}
801+
802+
/// Root config.toml document. Deserialized first, then flattened into
803+
/// `Config` via `From<TomlConfig>` so the rest of the codebase is untouched.
804+
#[derive(Debug, Deserialize, Serialize)]
805+
pub struct TomlConfig {
806+
pub relay: TomlRelay,
807+
#[serde(default)]
808+
pub network: TomlNetwork,
809+
#[serde(default)]
810+
pub scan: TomlScan,
811+
#[serde(default)]
812+
pub logging: TomlLogging,
813+
#[serde(default)]
814+
pub exit_node: ExitNodeConfig,
815+
#[serde(default)]
816+
pub fronting_groups: Vec<FrontingGroup>,
817+
}
818+
666819
#[cfg(test)]
667820
mod tests {
668821
use super::*;

0 commit comments

Comments
 (0)