Skip to content

Commit

Permalink
Add support for mixed_port, close #160
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerwooo committed Jul 31, 2024
1 parent 4911fb1 commit d234b46
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "mihoro"
description = "Mihomo CLI client on Linux."
version = "0.4.0"
version = "0.4.1"
edition = "2021"
readme = "README.md"
license = "MIT"
Expand Down
12 changes: 9 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ pub struct Config {

/// `mihomo` configurations (partial).
///
/// Referenced from https://github.com/Dreamacro/mihomo/wiki/configuration
/// Referenced from https://wiki.metacubex.one/config
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MihomoConfig {
pub port: u16,
pub socks_port: u16,
pub mixed_port: Option<u16>,
pub allow_lan: Option<bool>,
pub bind_address: Option<String>,
mode: MihomoMode,
Expand Down Expand Up @@ -80,8 +81,9 @@ impl Config {

// https://wiki.metacubex.one/config/general
mihomo_config: MihomoConfig {
port: 7890,
socks_port: 7891,
port: 7891,
socks_port: 7892,
mixed_port: Some(7890),
allow_lan: Some(false),
bind_address: Some(String::from("*")),
mode: MihomoMode::Rule,
Expand Down Expand Up @@ -170,6 +172,9 @@ pub struct MihomoYamlConfig {
#[serde(rename = "socks-port")]
socks_port: Option<u16>,

#[serde(rename = "mixed-port", skip_serializing_if = "Option::is_none")]
mixed_port: Option<u16>,

#[serde(rename = "allow-lan", skip_serializing_if = "Option::is_none")]
allow_lan: Option<bool>,

Expand Down Expand Up @@ -230,6 +235,7 @@ pub fn apply_mihomo_override(path: &str, override_config: &MihomoConfig) -> Resu
// Apply config overrides
mihomo_yaml.port = Some(override_config.port);
mihomo_yaml.socks_port = Some(override_config.socks_port);
mihomo_yaml.mixed_port = override_config.mixed_port;
mihomo_yaml.allow_lan = override_config.allow_lan;
mihomo_yaml.bind_address = override_config.bind_address.clone();
mihomo_yaml.mode = Some(override_config.mode.clone());
Expand Down
29 changes: 16 additions & 13 deletions src/mihoro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,23 @@ impl Mihoro {
}

pub fn proxy_commands(&self, proxy: &Option<ProxyCommands>) -> Result<()> {
// `mixed_port` takes precedence over `port` and `socks_port` for proxy export
let port = self
.config
.mihomo_config
.mixed_port
.as_ref()
.unwrap_or(&self.config.mihomo_config.port);
let socks_port = self
.config
.mihomo_config
.mixed_port
.as_ref()
.unwrap_or(&self.config.mihomo_config.socks_port);

match proxy {
Some(ProxyCommands::Export) => {
println!(
"{}",
proxy_export_cmd(
"127.0.0.1",
&self.config.mihomo_config.port,
&self.config.mihomo_config.socks_port
)
)
println!("{}", proxy_export_cmd("127.0.0.1", port, socks_port))
}
Some(ProxyCommands::ExportLan) => {
if !self.config.mihomo_config.allow_lan.unwrap_or(false) {
Expand All @@ -230,11 +237,7 @@ impl Mihoro {

println!(
"{}",
proxy_export_cmd(
&local_ip()?.to_string(),
&self.config.mihomo_config.port,
&self.config.mihomo_config.socks_port
)
proxy_export_cmd(&local_ip()?.to_string(), port, socks_port)
);
}
Some(ProxyCommands::Unset) => {
Expand Down

0 comments on commit d234b46

Please sign in to comment.