forked from zephyrchien/realm
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor file tree & support config file
- Loading branch information
1 parent
d14d72b
commit 352c841
Showing
12 changed files
with
164 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use clap::{Arg, App, SubCommand}; | ||
|
||
pub enum CmdInput { | ||
Config(String), | ||
None, | ||
} | ||
|
||
pub fn scan() -> CmdInput { | ||
let matches = App::new("Realm") | ||
.version("1.3-custom") | ||
.about("A high efficiency proxy tool") | ||
.arg( | ||
Arg::with_name("config") | ||
.short("c") | ||
.long("config") | ||
.value_name("json config file") | ||
.help("specify a config file in json format") | ||
.takes_value(true), | ||
) | ||
.get_matches(); | ||
if let Some(config) = matches.value_of("config") { | ||
return CmdInput::Config(config.to_string()); | ||
} | ||
CmdInput::None | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use serde::{Serialize, Deserialize}; | ||
use trust_dns_resolver::config::LookupIpStrategy; | ||
|
||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)] | ||
pub enum DnsMode { | ||
/// Only query for A (Ipv4) records | ||
Ipv4Only, | ||
/// Only query for AAAA (Ipv6) records | ||
Ipv6Only, | ||
/// Query for A and AAAA in parallel | ||
Ipv4AndIpv6, | ||
/// Query for Ipv4 if that fails, query for Ipv6 (default) | ||
Ipv4thenIpv6, | ||
/// Query for Ipv6 if that fails, query for Ipv4 | ||
Ipv6thenIpv4, | ||
} | ||
|
||
impl Default for DnsMode { | ||
fn default() -> Self { | ||
Self::Ipv4thenIpv6 | ||
} | ||
} | ||
|
||
impl DnsMode { | ||
pub fn to_strategy(self) -> LookupIpStrategy { | ||
match self { | ||
Self::Ipv4Only => LookupIpStrategy::Ipv4Only, | ||
Self::Ipv6Only => LookupIpStrategy::Ipv6Only, | ||
Self::Ipv4AndIpv6 => LookupIpStrategy::Ipv4AndIpv6, | ||
Self::Ipv4thenIpv6 => LookupIpStrategy::Ipv4thenIpv6, | ||
Self::Ipv6thenIpv4 => LookupIpStrategy::Ipv6thenIpv4, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use serde::{Serialize, Deserialize}; | ||
|
||
use crate::relay::Endpoint; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct EndpointConfig { | ||
udp: bool, | ||
local: String, | ||
remote: String, | ||
} | ||
|
||
impl EndpointConfig { | ||
pub fn to_endpoint(self) -> Endpoint { | ||
Endpoint::new(&self.local, &self.remote, self.udp) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use std::fs; | ||
|
||
use serde::{Serialize, Deserialize}; | ||
|
||
mod dns_mode; | ||
mod endpoint_config; | ||
|
||
pub use dns_mode::DnsMode; | ||
pub use endpoint_config::EndpointConfig; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct GlobalConfig { | ||
#[serde(default)] | ||
pub dns_mode: DnsMode, | ||
pub endpoints: Vec<EndpointConfig>, | ||
} | ||
|
||
impl GlobalConfig { | ||
pub fn from_config_file(file: &str) -> Self { | ||
let config = fs::read_to_string(file).expect("invalid file path"); | ||
serde_json::from_str(&config).expect("failed to parse config file") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,34 @@ | ||
mod cmd; | ||
mod relay; | ||
mod utils; | ||
mod config; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let eps = vec![relay::Endpoint::new("127.0.0.1:15000", "localhost:20000")]; | ||
relay::run(eps).await | ||
use cmd::CmdInput; | ||
use config::GlobalConfig; | ||
use relay::Endpoint; | ||
|
||
fn main() { | ||
match cmd::scan() { | ||
CmdInput::Config(c) => start_from_config(c), | ||
CmdInput::None => {} | ||
} | ||
} | ||
|
||
fn start_from_config(c: String) { | ||
let config = GlobalConfig::from_config_file(&c); | ||
relay::init_resolver(config.dns_mode.to_strategy()); | ||
let eps: Vec<Endpoint> = config | ||
.endpoints | ||
.into_iter() | ||
.map(|epc| epc.to_endpoint()) | ||
.collect(); | ||
run_relay(eps); | ||
} | ||
|
||
fn run_relay(eps: Vec<Endpoint>) { | ||
tokio::runtime::Builder::new_multi_thread() | ||
.enable_all() | ||
.build() | ||
.unwrap() | ||
.block_on(relay::run(eps)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
use std::ops::Drop; | ||
use tokio::io; | ||
use super::utils; | ||
use crate::utils; | ||
|
||
pub struct Pipe(pub i32, pub i32); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
use tokio::io; | ||
|
||
pub fn new_io_err(e: &str) -> io::Error { | ||
io::Error::new(io::ErrorKind::Other, e) | ||
} |