2023-09-23 01:53:45 +00:00
|
|
|
#![allow(dead_code)]
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests;
|
|
|
|
|
|
2024-05-18 20:32:42 +08:00
|
|
|
use std::{
|
|
|
|
|
net::{Ipv4Addr, SocketAddr},
|
|
|
|
|
path::PathBuf,
|
|
|
|
|
};
|
2024-03-23 00:56:27 +08:00
|
|
|
|
2024-08-08 00:08:59 +08:00
|
|
|
#[macro_use]
|
|
|
|
|
extern crate rust_i18n;
|
|
|
|
|
|
2024-03-23 00:56:27 +08:00
|
|
|
use anyhow::Context;
|
2023-09-23 01:53:45 +00:00
|
|
|
use clap::Parser;
|
|
|
|
|
|
2024-02-08 16:27:18 +08:00
|
|
|
mod arch;
|
2023-09-23 01:53:45 +00:00
|
|
|
mod common;
|
|
|
|
|
mod connector;
|
|
|
|
|
mod gateway;
|
|
|
|
|
mod instance;
|
2024-02-06 13:29:12 +08:00
|
|
|
mod peer_center;
|
2023-09-23 01:53:45 +00:00
|
|
|
mod peers;
|
2024-02-08 23:44:51 +08:00
|
|
|
mod rpc;
|
2024-04-24 23:12:46 +08:00
|
|
|
mod tunnel;
|
2024-05-07 00:38:05 +08:00
|
|
|
mod utils;
|
2024-03-30 22:15:14 +08:00
|
|
|
mod vpn_portal;
|
2023-09-23 01:53:45 +00:00
|
|
|
|
2024-05-07 00:38:05 +08:00
|
|
|
use common::config::{
|
|
|
|
|
ConsoleLoggerConfig, FileLoggerConfig, NetworkIdentity, PeerConfig, VpnPortalConfig,
|
2024-03-23 00:56:27 +08:00
|
|
|
};
|
|
|
|
|
use instance::instance::Instance;
|
2024-05-16 20:16:09 +08:00
|
|
|
use tokio::net::TcpSocket;
|
2024-08-15 22:05:34 +08:00
|
|
|
use utils::setup_panic_handler;
|
2023-09-23 01:53:45 +00:00
|
|
|
|
2024-05-07 00:38:05 +08:00
|
|
|
use crate::{
|
|
|
|
|
common::{
|
|
|
|
|
config::{ConfigLoader, TomlConfigLoader},
|
|
|
|
|
global_ctx::GlobalCtxEvent,
|
|
|
|
|
},
|
|
|
|
|
utils::init_logger,
|
2024-03-23 00:56:27 +08:00
|
|
|
};
|
|
|
|
|
|
2024-05-03 07:55:00 +08:00
|
|
|
#[cfg(feature = "mimalloc")]
|
2024-04-26 23:02:07 +08:00
|
|
|
use mimalloc_rust::*;
|
|
|
|
|
|
2024-05-03 07:55:00 +08:00
|
|
|
#[cfg(feature = "mimalloc")]
|
2024-04-26 23:02:07 +08:00
|
|
|
#[global_allocator]
|
|
|
|
|
static GLOBAL_MIMALLOC: GlobalMiMalloc = GlobalMiMalloc;
|
|
|
|
|
|
2023-09-23 01:53:45 +00:00
|
|
|
#[derive(Parser, Debug)]
|
2024-05-09 15:06:32 +08:00
|
|
|
#[command(name = "easytier-core", author, version, about, long_about = None)]
|
2023-09-23 01:53:45 +00:00
|
|
|
struct Cli {
|
2024-05-07 00:38:05 +08:00
|
|
|
#[arg(
|
|
|
|
|
short,
|
|
|
|
|
long,
|
2024-08-08 00:08:59 +08:00
|
|
|
help = t!("core_clap.config_file").to_string()
|
2024-05-07 00:38:05 +08:00
|
|
|
)]
|
|
|
|
|
config_file: Option<PathBuf>,
|
|
|
|
|
|
2024-03-23 00:56:27 +08:00
|
|
|
#[arg(
|
|
|
|
|
long,
|
2024-08-08 00:08:59 +08:00
|
|
|
help = t!("core_clap.network_name").to_string(),
|
2024-03-23 16:34:48 +08:00
|
|
|
default_value = "default"
|
2024-03-23 00:56:27 +08:00
|
|
|
)]
|
2024-03-23 16:34:48 +08:00
|
|
|
network_name: String,
|
2024-08-08 00:08:59 +08:00
|
|
|
|
2024-03-23 00:56:27 +08:00
|
|
|
#[arg(
|
|
|
|
|
long,
|
2024-08-08 00:08:59 +08:00
|
|
|
help = t!("core_clap.network_secret").to_string(),
|
2024-03-23 16:34:48 +08:00
|
|
|
default_value = ""
|
2024-03-23 00:56:27 +08:00
|
|
|
)]
|
2024-03-23 16:34:48 +08:00
|
|
|
network_secret: String,
|
2023-09-23 01:53:45 +00:00
|
|
|
|
2024-05-05 16:18:05 +08:00
|
|
|
#[arg(
|
|
|
|
|
short,
|
|
|
|
|
long,
|
2024-08-08 00:08:59 +08:00
|
|
|
help = t!("core_clap.ipv4").to_string()
|
2024-05-05 16:18:05 +08:00
|
|
|
)]
|
2023-09-23 01:53:45 +00:00
|
|
|
ipv4: Option<String>,
|
|
|
|
|
|
2024-05-17 23:16:56 +08:00
|
|
|
#[arg(
|
|
|
|
|
short,
|
|
|
|
|
long,
|
2024-08-08 00:08:59 +08:00
|
|
|
help = t!("core_clap.dhcp").to_string()
|
2024-05-17 23:16:56 +08:00
|
|
|
)]
|
|
|
|
|
dhcp: bool,
|
|
|
|
|
|
2024-08-08 00:08:59 +08:00
|
|
|
#[arg(
|
|
|
|
|
short,
|
|
|
|
|
long,
|
|
|
|
|
help = t!("core_clap.peers").to_string(),
|
|
|
|
|
num_args = 0..
|
|
|
|
|
)]
|
2023-09-23 01:53:45 +00:00
|
|
|
peers: Vec<String>,
|
2024-03-23 00:56:27 +08:00
|
|
|
|
2024-08-08 00:08:59 +08:00
|
|
|
#[arg(
|
|
|
|
|
short,
|
|
|
|
|
long,
|
|
|
|
|
help = t!("core_clap.external_node").to_string()
|
|
|
|
|
)]
|
2024-03-23 00:56:27 +08:00
|
|
|
external_node: Option<String>,
|
|
|
|
|
|
|
|
|
|
#[arg(
|
|
|
|
|
short = 'n',
|
|
|
|
|
long,
|
2024-08-08 00:08:59 +08:00
|
|
|
help = t!("core_clap.proxy_networks").to_string()
|
2024-03-23 00:56:27 +08:00
|
|
|
)]
|
|
|
|
|
proxy_networks: Vec<String>,
|
|
|
|
|
|
|
|
|
|
#[arg(
|
|
|
|
|
short,
|
|
|
|
|
long,
|
2024-08-08 00:08:59 +08:00
|
|
|
help = t!("core_clap.rpc_portal").to_string(),
|
|
|
|
|
default_value = "0"
|
2024-03-23 00:56:27 +08:00
|
|
|
)]
|
2024-05-16 20:16:09 +08:00
|
|
|
rpc_portal: String,
|
|
|
|
|
|
2024-08-08 00:08:59 +08:00
|
|
|
#[arg(
|
|
|
|
|
short,
|
|
|
|
|
long,
|
|
|
|
|
help = t!("core_clap.listeners").to_string(),
|
|
|
|
|
default_values_t = ["11010".to_string()],
|
|
|
|
|
num_args = 0..
|
|
|
|
|
)]
|
2024-03-23 00:56:27 +08:00
|
|
|
listeners: Vec<String>,
|
|
|
|
|
|
2024-05-16 20:16:09 +08:00
|
|
|
#[arg(
|
|
|
|
|
long,
|
2024-08-08 00:08:59 +08:00
|
|
|
help = t!("core_clap.no_listener").to_string(),
|
2024-05-16 20:16:09 +08:00
|
|
|
default_value = "false"
|
|
|
|
|
)]
|
|
|
|
|
no_listener: bool,
|
|
|
|
|
|
2024-08-08 00:08:59 +08:00
|
|
|
#[arg(
|
|
|
|
|
long,
|
|
|
|
|
help = t!("core_clap.console_log_level").to_string()
|
|
|
|
|
)]
|
2024-03-23 00:56:27 +08:00
|
|
|
console_log_level: Option<String>,
|
|
|
|
|
|
2024-08-08 00:08:59 +08:00
|
|
|
#[arg(
|
|
|
|
|
long,
|
|
|
|
|
help = t!("core_clap.file_log_level").to_string()
|
|
|
|
|
)]
|
2024-03-23 00:56:27 +08:00
|
|
|
file_log_level: Option<String>,
|
2024-08-08 00:08:59 +08:00
|
|
|
|
|
|
|
|
#[arg(
|
|
|
|
|
long,
|
|
|
|
|
help = t!("core_clap.file_log_dir").to_string()
|
|
|
|
|
)]
|
2024-03-23 00:56:27 +08:00
|
|
|
file_log_dir: Option<String>,
|
2024-03-23 16:34:48 +08:00
|
|
|
|
2024-08-08 00:08:59 +08:00
|
|
|
#[arg(
|
|
|
|
|
long,
|
|
|
|
|
help = t!("core_clap.hostname").to_string()
|
|
|
|
|
)]
|
2024-05-08 14:47:22 +08:00
|
|
|
hostname: Option<String>,
|
|
|
|
|
|
2024-03-23 16:34:48 +08:00
|
|
|
#[arg(
|
|
|
|
|
short = 'm',
|
|
|
|
|
long,
|
2024-08-08 00:08:59 +08:00
|
|
|
help = t!("core_clap.instance_name").to_string(),
|
|
|
|
|
default_value = "default"
|
2024-03-23 16:34:48 +08:00
|
|
|
)]
|
|
|
|
|
instance_name: String,
|
|
|
|
|
|
2024-03-30 22:15:14 +08:00
|
|
|
#[arg(
|
|
|
|
|
long,
|
2024-08-08 00:08:59 +08:00
|
|
|
help = t!("core_clap.vpn_portal").to_string()
|
2024-03-30 22:15:14 +08:00
|
|
|
)]
|
|
|
|
|
vpn_portal: Option<String>,
|
2024-03-31 21:10:59 +08:00
|
|
|
|
2024-08-08 00:08:59 +08:00
|
|
|
#[arg(
|
|
|
|
|
long,
|
|
|
|
|
help = t!("core_clap.default_protocol").to_string()
|
|
|
|
|
)]
|
2024-03-31 21:10:59 +08:00
|
|
|
default_protocol: Option<String>,
|
2024-04-25 23:25:37 +08:00
|
|
|
|
2024-04-27 13:44:59 +08:00
|
|
|
#[arg(
|
|
|
|
|
short = 'u',
|
|
|
|
|
long,
|
2024-08-08 00:08:59 +08:00
|
|
|
help = t!("core_clap.disable_encryption").to_string(),
|
2024-04-27 13:44:59 +08:00
|
|
|
default_value = "false"
|
|
|
|
|
)]
|
|
|
|
|
disable_encryption: bool,
|
|
|
|
|
|
2024-04-25 23:25:37 +08:00
|
|
|
#[arg(
|
|
|
|
|
long,
|
2024-08-08 00:08:59 +08:00
|
|
|
help = t!("core_clap.multi_thread").to_string(),
|
2024-04-25 23:25:37 +08:00
|
|
|
default_value = "false"
|
|
|
|
|
)]
|
|
|
|
|
multi_thread: bool,
|
2024-04-27 23:10:28 +08:00
|
|
|
|
2024-08-08 00:08:59 +08:00
|
|
|
#[arg(
|
|
|
|
|
long,
|
|
|
|
|
help = t!("core_clap.disable_ipv6").to_string(),
|
|
|
|
|
default_value = "false"
|
|
|
|
|
)]
|
2024-04-27 23:10:28 +08:00
|
|
|
disable_ipv6: bool,
|
2024-05-07 00:38:05 +08:00
|
|
|
|
2024-08-08 00:08:59 +08:00
|
|
|
#[arg(
|
|
|
|
|
long,
|
|
|
|
|
help = t!("core_clap.dev_name").to_string()
|
|
|
|
|
)]
|
2024-07-15 00:03:55 +08:00
|
|
|
dev_name: Option<String>,
|
2024-07-03 15:41:12 +08:00
|
|
|
|
2024-05-07 00:38:05 +08:00
|
|
|
#[arg(
|
|
|
|
|
long,
|
2024-08-08 00:08:59 +08:00
|
|
|
help = t!("core_clap.mtu").to_string()
|
2024-05-07 00:38:05 +08:00
|
|
|
)]
|
|
|
|
|
mtu: Option<u16>,
|
2024-05-13 20:34:43 +08:00
|
|
|
|
|
|
|
|
#[arg(
|
|
|
|
|
long,
|
2024-08-08 00:08:59 +08:00
|
|
|
help = t!("core_clap.latency_first").to_string(),
|
2024-05-13 20:34:43 +08:00
|
|
|
default_value = "false"
|
|
|
|
|
)]
|
|
|
|
|
latency_first: bool,
|
2024-05-18 20:32:42 +08:00
|
|
|
|
|
|
|
|
#[arg(
|
|
|
|
|
long,
|
2024-08-08 00:08:59 +08:00
|
|
|
help = t!("core_clap.exit_nodes").to_string(),
|
2024-05-18 20:32:42 +08:00
|
|
|
num_args = 0..
|
|
|
|
|
)]
|
|
|
|
|
exit_nodes: Vec<Ipv4Addr>,
|
|
|
|
|
|
|
|
|
|
#[arg(
|
|
|
|
|
long,
|
2024-08-08 00:08:59 +08:00
|
|
|
help = t!("core_clap.enable_exit_node").to_string(),
|
2024-05-18 20:32:42 +08:00
|
|
|
default_value = "false"
|
|
|
|
|
)]
|
|
|
|
|
enable_exit_node: bool,
|
2024-06-10 10:27:24 +08:00
|
|
|
|
|
|
|
|
#[arg(
|
|
|
|
|
long,
|
2024-08-08 00:08:59 +08:00
|
|
|
help = t!("core_clap.no_tun").to_string(),
|
2024-06-10 10:27:24 +08:00
|
|
|
default_value = "false"
|
|
|
|
|
)]
|
|
|
|
|
no_tun: bool,
|
|
|
|
|
|
|
|
|
|
#[arg(
|
|
|
|
|
long,
|
2024-08-08 00:08:59 +08:00
|
|
|
help = t!("core_clap.use_smoltcp").to_string(),
|
2024-07-07 22:08:50 +08:00
|
|
|
default_value = "false"
|
2024-06-10 10:27:24 +08:00
|
|
|
)]
|
|
|
|
|
use_smoltcp: bool,
|
2024-07-24 22:45:55 +08:00
|
|
|
|
|
|
|
|
#[arg(
|
|
|
|
|
long,
|
2024-08-08 00:08:59 +08:00
|
|
|
help = t!("core_clap.manual_routes").to_string(),
|
2024-07-24 22:45:55 +08:00
|
|
|
num_args = 0..
|
|
|
|
|
)]
|
|
|
|
|
manual_routes: Option<Vec<String>>,
|
2024-08-03 15:12:54 +08:00
|
|
|
|
|
|
|
|
#[arg(
|
|
|
|
|
long,
|
2024-08-08 00:08:59 +08:00
|
|
|
help = t!("core_clap.relay_network_whitelist").to_string(),
|
|
|
|
|
num_args = 0..
|
2024-08-03 15:12:54 +08:00
|
|
|
)]
|
|
|
|
|
relay_network_whitelist: Option<Vec<String>>,
|
2024-08-10 00:26:54 +08:00
|
|
|
|
|
|
|
|
#[arg(
|
|
|
|
|
long,
|
|
|
|
|
help = t!("core_clap.disable_p2p").to_string(),
|
|
|
|
|
default_value = "false"
|
|
|
|
|
)]
|
|
|
|
|
disable_p2p: bool,
|
|
|
|
|
|
|
|
|
|
#[arg(
|
|
|
|
|
long,
|
|
|
|
|
help = t!("core_clap.relay_all_peer_rpc").to_string(),
|
|
|
|
|
default_value = "false"
|
|
|
|
|
)]
|
|
|
|
|
relay_all_peer_rpc: bool,
|
2024-08-14 23:26:15 +08:00
|
|
|
|
|
|
|
|
#[cfg(feature = "socks5")]
|
|
|
|
|
#[arg(
|
|
|
|
|
long,
|
|
|
|
|
help = t!("core_clap.socks5").to_string()
|
|
|
|
|
)]
|
|
|
|
|
socks5: Option<u16>,
|
2023-09-23 01:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
2024-08-20 00:38:59 +08:00
|
|
|
rust_i18n::i18n!("locales", fallback = "en");
|
2024-08-08 00:08:59 +08:00
|
|
|
|
2024-05-16 20:16:09 +08:00
|
|
|
impl Cli {
|
|
|
|
|
fn parse_listeners(&self) -> Vec<String> {
|
|
|
|
|
println!("parsing listeners: {:?}", self.listeners);
|
|
|
|
|
let proto_port_offset = vec![("tcp", 0), ("udp", 0), ("wg", 1), ("ws", 1), ("wss", 2)];
|
|
|
|
|
|
|
|
|
|
if self.no_listener || self.listeners.is_empty() {
|
|
|
|
|
return vec![];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let origin_listners = self.listeners.clone();
|
|
|
|
|
let mut listeners: Vec<String> = Vec::new();
|
|
|
|
|
if origin_listners.len() == 1 {
|
|
|
|
|
if let Ok(port) = origin_listners[0].parse::<u16>() {
|
|
|
|
|
for (proto, offset) in proto_port_offset {
|
|
|
|
|
listeners.push(format!("{}://0.0.0.0:{}", proto, port + offset));
|
|
|
|
|
}
|
|
|
|
|
return listeners;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for l in &origin_listners {
|
|
|
|
|
let proto_port: Vec<&str> = l.split(':').collect();
|
|
|
|
|
if proto_port.len() > 2 {
|
|
|
|
|
if let Ok(url) = l.parse::<url::Url>() {
|
|
|
|
|
listeners.push(url.to_string());
|
|
|
|
|
} else {
|
|
|
|
|
panic!("failed to parse listener: {}", l);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
let Some((proto, offset)) = proto_port_offset
|
|
|
|
|
.iter()
|
|
|
|
|
.find(|(proto, _)| *proto == proto_port[0])
|
|
|
|
|
else {
|
|
|
|
|
panic!("unknown protocol: {}", proto_port[0]);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let port = if proto_port.len() == 2 {
|
|
|
|
|
proto_port[1].parse::<u16>().unwrap()
|
|
|
|
|
} else {
|
|
|
|
|
11010 + offset
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
listeners.push(format!("{}://0.0.0.0:{}", proto, port));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
listeners
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn check_tcp_available(port: u16) -> Option<SocketAddr> {
|
|
|
|
|
let s = format!("127.0.0.1:{}", port).parse::<SocketAddr>().unwrap();
|
|
|
|
|
TcpSocket::new_v4().unwrap().bind(s).map(|_| s).ok()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_rpc_portal(&self) -> SocketAddr {
|
|
|
|
|
if let Ok(port) = self.rpc_portal.parse::<u16>() {
|
|
|
|
|
if port == 0 {
|
|
|
|
|
// check tcp 15888 first
|
|
|
|
|
for i in 15888..15900 {
|
|
|
|
|
if let Some(s) = Cli::check_tcp_available(i) {
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return "127.0.0.1:0".parse().unwrap();
|
|
|
|
|
}
|
|
|
|
|
return format!("127.0.0.1:{}", port).parse().unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.rpc_portal.parse().unwrap()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-23 00:56:27 +08:00
|
|
|
impl From<Cli> for TomlConfigLoader {
|
|
|
|
|
fn from(cli: Cli) -> Self {
|
2024-05-07 00:38:05 +08:00
|
|
|
if let Some(config_file) = &cli.config_file {
|
|
|
|
|
println!(
|
|
|
|
|
"NOTICE: loading config file: {:?}, will ignore all command line flags\n",
|
|
|
|
|
config_file
|
|
|
|
|
);
|
|
|
|
|
return TomlConfigLoader::new(config_file)
|
|
|
|
|
.with_context(|| format!("failed to load config file: {:?}", cli.config_file))
|
|
|
|
|
.unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-23 00:56:27 +08:00
|
|
|
let cfg = TomlConfigLoader::default();
|
2024-05-07 00:38:05 +08:00
|
|
|
|
2024-03-23 00:56:27 +08:00
|
|
|
cfg.set_inst_name(cli.instance_name.clone());
|
2024-05-08 14:47:22 +08:00
|
|
|
|
2024-05-08 16:06:11 +08:00
|
|
|
cfg.set_hostname(cli.hostname.clone());
|
2024-05-08 14:47:22 +08:00
|
|
|
|
2024-04-27 13:44:59 +08:00
|
|
|
cfg.set_network_identity(NetworkIdentity::new(
|
|
|
|
|
cli.network_name.clone(),
|
|
|
|
|
cli.network_secret.clone(),
|
|
|
|
|
));
|
2024-03-23 00:56:27 +08:00
|
|
|
|
2024-05-17 23:16:56 +08:00
|
|
|
cfg.set_dhcp(cli.dhcp);
|
|
|
|
|
|
2024-08-08 22:03:22 +08:00
|
|
|
if let Some(ipv4) = &cli.ipv4 {
|
|
|
|
|
cfg.set_ipv4(Some(
|
|
|
|
|
ipv4.parse()
|
|
|
|
|
.with_context(|| format!("failed to parse ipv4 address: {}", ipv4))
|
|
|
|
|
.unwrap(),
|
|
|
|
|
))
|
2024-03-23 00:56:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cfg.set_peers(
|
|
|
|
|
cli.peers
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|s| PeerConfig {
|
|
|
|
|
uri: s
|
|
|
|
|
.parse()
|
|
|
|
|
.with_context(|| format!("failed to parse peer uri: {}", s))
|
|
|
|
|
.unwrap(),
|
|
|
|
|
})
|
|
|
|
|
.collect(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
cfg.set_listeners(
|
2024-05-16 20:16:09 +08:00
|
|
|
cli.parse_listeners()
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|s| s.parse().unwrap())
|
2024-03-23 00:56:27 +08:00
|
|
|
.collect(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
for n in cli.proxy_networks.iter() {
|
|
|
|
|
cfg.add_proxy_cidr(
|
|
|
|
|
n.parse()
|
|
|
|
|
.with_context(|| format!("failed to parse proxy network: {}", n))
|
|
|
|
|
.unwrap(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-16 20:16:09 +08:00
|
|
|
cfg.set_rpc_portal(cli.parse_rpc_portal());
|
2024-03-23 00:56:27 +08:00
|
|
|
|
|
|
|
|
if cli.external_node.is_some() {
|
|
|
|
|
let mut old_peers = cfg.get_peers();
|
|
|
|
|
old_peers.push(PeerConfig {
|
|
|
|
|
uri: cli
|
|
|
|
|
.external_node
|
|
|
|
|
.clone()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.parse()
|
|
|
|
|
.with_context(|| {
|
|
|
|
|
format!(
|
|
|
|
|
"failed to parse external node uri: {}",
|
|
|
|
|
cli.external_node.unwrap()
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.unwrap(),
|
|
|
|
|
});
|
|
|
|
|
cfg.set_peers(old_peers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if cli.console_log_level.is_some() {
|
|
|
|
|
cfg.set_console_logger_config(ConsoleLoggerConfig {
|
|
|
|
|
level: cli.console_log_level.clone(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if cli.file_log_dir.is_some() || cli.file_log_level.is_some() {
|
|
|
|
|
cfg.set_file_logger_config(FileLoggerConfig {
|
|
|
|
|
level: cli.file_log_level.clone(),
|
|
|
|
|
dir: cli.file_log_dir.clone(),
|
|
|
|
|
file: Some(format!("easytier-{}", cli.instance_name)),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-30 22:15:14 +08:00
|
|
|
if cli.vpn_portal.is_some() {
|
|
|
|
|
let url: url::Url = cli
|
|
|
|
|
.vpn_portal
|
|
|
|
|
.clone()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.parse()
|
|
|
|
|
.with_context(|| {
|
|
|
|
|
format!(
|
|
|
|
|
"failed to parse vpn portal url: {}",
|
|
|
|
|
cli.vpn_portal.unwrap()
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
|
|
|
|
cfg.set_vpn_portal_config(VpnPortalConfig {
|
|
|
|
|
client_cidr: url.path()[1..]
|
|
|
|
|
.parse()
|
|
|
|
|
.with_context(|| {
|
|
|
|
|
format!("failed to parse vpn portal client cidr: {}", url.path())
|
|
|
|
|
})
|
|
|
|
|
.unwrap(),
|
|
|
|
|
wireguard_listen: format!("{}:{}", url.host_str().unwrap(), url.port().unwrap())
|
|
|
|
|
.parse()
|
|
|
|
|
.with_context(|| {
|
|
|
|
|
format!(
|
|
|
|
|
"failed to parse vpn portal wireguard listen address: {}",
|
|
|
|
|
url.host_str().unwrap()
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.unwrap(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 22:45:55 +08:00
|
|
|
if cli.manual_routes.is_some() {
|
|
|
|
|
cfg.set_routes(Some(
|
|
|
|
|
cli.manual_routes
|
|
|
|
|
.clone()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|s| {
|
|
|
|
|
s.parse()
|
|
|
|
|
.with_context(|| format!("failed to parse route: {}", s))
|
|
|
|
|
.unwrap()
|
|
|
|
|
})
|
|
|
|
|
.collect(),
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-14 23:26:15 +08:00
|
|
|
#[cfg(feature = "socks5")]
|
|
|
|
|
if let Some(socks5_proxy) = cli.socks5 {
|
|
|
|
|
cfg.set_socks5_portal(Some(
|
|
|
|
|
format!("socks5://0.0.0.0:{}", socks5_proxy)
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap(),
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-27 13:44:59 +08:00
|
|
|
let mut f = cfg.get_flags();
|
2024-03-31 21:10:59 +08:00
|
|
|
if cli.default_protocol.is_some() {
|
|
|
|
|
f.default_protocol = cli.default_protocol.as_ref().unwrap().clone();
|
|
|
|
|
}
|
2024-04-27 13:44:59 +08:00
|
|
|
f.enable_encryption = !cli.disable_encryption;
|
2024-04-27 23:10:28 +08:00
|
|
|
f.enable_ipv6 = !cli.disable_ipv6;
|
2024-05-13 20:34:43 +08:00
|
|
|
f.latency_first = cli.latency_first;
|
2024-07-15 00:03:55 +08:00
|
|
|
f.dev_name = cli.dev_name.unwrap_or(Default::default());
|
2024-05-07 00:38:05 +08:00
|
|
|
if let Some(mtu) = cli.mtu {
|
|
|
|
|
f.mtu = mtu;
|
|
|
|
|
}
|
2024-05-18 20:32:42 +08:00
|
|
|
f.enable_exit_node = cli.enable_exit_node;
|
2024-06-11 09:09:32 +08:00
|
|
|
f.no_tun = cli.no_tun || cfg!(not(feature = "tun"));
|
2024-06-10 10:27:24 +08:00
|
|
|
f.use_smoltcp = cli.use_smoltcp;
|
2024-08-03 15:12:54 +08:00
|
|
|
if let Some(wl) = cli.relay_network_whitelist {
|
|
|
|
|
f.foreign_network_whitelist = wl.join(" ");
|
|
|
|
|
}
|
2024-08-10 00:26:54 +08:00
|
|
|
f.disable_p2p = cli.disable_p2p;
|
|
|
|
|
f.relay_all_peer_rpc = cli.relay_all_peer_rpc;
|
2024-04-27 13:44:59 +08:00
|
|
|
cfg.set_flags(f);
|
2024-03-31 21:10:59 +08:00
|
|
|
|
2024-05-18 20:32:42 +08:00
|
|
|
cfg.set_exit_nodes(cli.exit_nodes.clone());
|
|
|
|
|
|
2024-03-23 00:56:27 +08:00
|
|
|
cfg
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn print_event(msg: String) {
|
|
|
|
|
println!(
|
|
|
|
|
"{}: {}",
|
|
|
|
|
chrono::Local::now().format("%Y-%m-%d %H:%M:%S"),
|
|
|
|
|
msg
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn peer_conn_info_to_string(p: crate::rpc::PeerConnInfo) -> String {
|
|
|
|
|
format!(
|
|
|
|
|
"my_peer_id: {}, dst_peer_id: {}, tunnel_info: {:?}",
|
|
|
|
|
p.my_peer_id, p.peer_id, p.tunnel
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-23 01:53:45 +00:00
|
|
|
#[tracing::instrument]
|
2024-04-25 23:25:37 +08:00
|
|
|
pub async fn async_main(cli: Cli) {
|
2024-03-23 00:56:27 +08:00
|
|
|
let cfg: TomlConfigLoader = cli.into();
|
2023-09-23 01:53:45 +00:00
|
|
|
|
2024-05-07 00:38:05 +08:00
|
|
|
init_logger(&cfg, false).unwrap();
|
2024-03-23 00:56:27 +08:00
|
|
|
let mut inst = Instance::new(cfg.clone());
|
2023-09-23 01:53:45 +00:00
|
|
|
|
|
|
|
|
let mut events = inst.get_global_ctx().subscribe();
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
|
while let Ok(e) = events.recv().await {
|
2024-03-23 00:56:27 +08:00
|
|
|
match e {
|
|
|
|
|
GlobalCtxEvent::PeerAdded(p) => {
|
|
|
|
|
print_event(format!("new peer added. peer_id: {}", p));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GlobalCtxEvent::PeerRemoved(p) => {
|
|
|
|
|
print_event(format!("peer removed. peer_id: {}", p));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GlobalCtxEvent::PeerConnAdded(p) => {
|
|
|
|
|
print_event(format!(
|
|
|
|
|
"new peer connection added. conn_info: {}",
|
|
|
|
|
peer_conn_info_to_string(p)
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GlobalCtxEvent::PeerConnRemoved(p) => {
|
|
|
|
|
print_event(format!(
|
|
|
|
|
"peer connection removed. conn_info: {}",
|
|
|
|
|
peer_conn_info_to_string(p)
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-11 22:49:48 +08:00
|
|
|
GlobalCtxEvent::ListenerAddFailed(p, msg) => {
|
|
|
|
|
print_event(format!(
|
|
|
|
|
"listener add failed. listener: {}, msg: {}",
|
|
|
|
|
p, msg
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-03 12:40:08 +08:00
|
|
|
GlobalCtxEvent::ListenerAcceptFailed(p, msg) => {
|
|
|
|
|
print_event(format!(
|
|
|
|
|
"listener accept failed. listener: {}, msg: {}",
|
|
|
|
|
p, msg
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-23 00:56:27 +08:00
|
|
|
GlobalCtxEvent::ListenerAdded(p) => {
|
|
|
|
|
if p.scheme() == "ring" {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
print_event(format!("new listener added. listener: {}", p));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GlobalCtxEvent::ConnectionAccepted(local, remote) => {
|
|
|
|
|
print_event(format!(
|
|
|
|
|
"new connection accepted. local: {}, remote: {}",
|
|
|
|
|
local, remote
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GlobalCtxEvent::ConnectionError(local, remote, err) => {
|
|
|
|
|
print_event(format!(
|
|
|
|
|
"connection error. local: {}, remote: {}, err: {}",
|
|
|
|
|
local, remote, err
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GlobalCtxEvent::TunDeviceReady(dev) => {
|
|
|
|
|
print_event(format!("tun device ready. dev: {}", dev));
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 22:45:55 +08:00
|
|
|
GlobalCtxEvent::TunDeviceError(err) => {
|
|
|
|
|
print_event(format!("tun device error. err: {}", err));
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-23 00:56:27 +08:00
|
|
|
GlobalCtxEvent::Connecting(dst) => {
|
|
|
|
|
print_event(format!("connecting to peer. dst: {}", dst));
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-05 16:18:05 +08:00
|
|
|
GlobalCtxEvent::ConnectError(dst, ip_version, err) => {
|
|
|
|
|
print_event(format!(
|
|
|
|
|
"connect to peer error. dst: {}, ip_version: {}, err: {}",
|
|
|
|
|
dst, ip_version, err
|
|
|
|
|
));
|
2024-03-23 00:56:27 +08:00
|
|
|
}
|
2024-03-30 22:15:14 +08:00
|
|
|
|
|
|
|
|
GlobalCtxEvent::VpnPortalClientConnected(portal, client_addr) => {
|
|
|
|
|
print_event(format!(
|
|
|
|
|
"vpn portal client connected. portal: {}, client_addr: {}",
|
|
|
|
|
portal, client_addr
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GlobalCtxEvent::VpnPortalClientDisconnected(portal, client_addr) => {
|
|
|
|
|
print_event(format!(
|
|
|
|
|
"vpn portal client disconnected. portal: {}, client_addr: {}",
|
|
|
|
|
portal, client_addr
|
|
|
|
|
));
|
|
|
|
|
}
|
2024-05-17 23:16:56 +08:00
|
|
|
|
|
|
|
|
GlobalCtxEvent::DhcpIpv4Changed(old, new) => {
|
|
|
|
|
print_event(format!("dhcp ip changed. old: {:?}, new: {:?}", old, new));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GlobalCtxEvent::DhcpIpv4Conflicted(ip) => {
|
|
|
|
|
print_event(format!("dhcp ip conflict. ip: {:?}", ip));
|
|
|
|
|
}
|
2024-03-23 00:56:27 +08:00
|
|
|
}
|
2023-09-23 01:53:45 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2024-03-23 00:56:27 +08:00
|
|
|
println!("Starting easytier with config:");
|
2024-05-09 22:01:55 +08:00
|
|
|
println!("############### TOML ###############\n");
|
2024-03-23 00:56:27 +08:00
|
|
|
println!("{}", cfg.dump());
|
|
|
|
|
println!("-----------------------------------");
|
2023-09-23 01:53:45 +00:00
|
|
|
|
2024-03-23 00:56:27 +08:00
|
|
|
inst.run().await.unwrap();
|
2023-09-23 01:53:45 +00:00
|
|
|
|
|
|
|
|
inst.wait().await;
|
|
|
|
|
}
|
2024-04-25 23:25:37 +08:00
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
setup_panic_handler();
|
|
|
|
|
|
2024-08-08 00:08:59 +08:00
|
|
|
let locale = sys_locale::get_locale().unwrap_or_else(|| String::from("en-US"));
|
|
|
|
|
rust_i18n::set_locale(&locale);
|
|
|
|
|
|
2024-04-25 23:25:37 +08:00
|
|
|
let cli = Cli::parse();
|
|
|
|
|
tracing::info!(cli = ?cli, "cli args parsed");
|
|
|
|
|
|
|
|
|
|
if cli.multi_thread {
|
|
|
|
|
tokio::runtime::Builder::new_multi_thread()
|
2024-04-26 23:02:07 +08:00
|
|
|
.worker_threads(2)
|
2024-04-25 23:25:37 +08:00
|
|
|
.enable_all()
|
|
|
|
|
.build()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.block_on(async move { async_main(cli).await })
|
|
|
|
|
} else {
|
|
|
|
|
tokio::runtime::Builder::new_current_thread()
|
|
|
|
|
.enable_all()
|
|
|
|
|
.build()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.block_on(async move { async_main(cli).await })
|
|
|
|
|
}
|
|
|
|
|
}
|