Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

should update interest after process_send immediately #364

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
560 changes: 429 additions & 131 deletions relay-rust/Cargo.lock

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions relay-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ chrono = "0.4" # for formatting timestamp in logs
byteorder = "1.3" # for reading/writing binary
rand = "0.7" # for random TCP sequence number
ctrlc = { version = "3.0", features = ["termination"] } # for handling Ctrl+C
tokio = { version = "1.2", features = ["io-util", "macros", "net", "parking_lot", "process", "rt"] }
thiserror = "1.0"
bytes = "1.0"
pin-project = "1.0"
toml = "0.5"
serde = { version = "1.0", features = ["derive"] }
lazy_static = "1.4"
iprange = "0.6"
ipnet = "2.3"
once_cell = "1.4"

[target.'cfg(unix)'.dependencies]
libc = "0.2"

[target.'cfg(windows)'.dependencies]
winapi = "0.2"
ws2_32-sys = "0.2"

[profile.release]
lto = true # link-time optimization
19 changes: 19 additions & 0 deletions relay-rust/proxy.toml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
tcp_read_time_out = 15000
tcp_connect_time_out = 15000

lan_host = [
"10.0.0.0/8",
"172.16.0.0/16",
"192.168.1.0/24"
]

[special_proxy_config]
proxy = { proxy_addr = "119.9.77.88:1080", proxy_type = "socks5", username = "gnirehtet", password = "gnirehtet" }
hosts = [
"8.8.8.8",
"119.29.29.29"
]

[default_proxy_config]
proxy = { proxy_addr = "127.0.0.1:10080", proxy_type = "socks5", username = "", password = "" }
hosts = []
17 changes: 17 additions & 0 deletions relay-rust/src/cli_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub const PARAM_SERIAL: u8 = 1;
pub const PARAM_DNS_SERVERS: u8 = 1 << 1;
pub const PARAM_ROUTES: u8 = 1 << 2;
pub const PARAM_PORT: u8 = 1 << 3;
pub const PARAM_CONF: u8 = 1 << 4;

pub const DEFAULT_PORT: u16 = 31416;

Expand All @@ -27,6 +28,7 @@ pub struct CommandLineArguments {
dns_servers: Option<String>,
routes: Option<String>,
port: u16,
conf: String
}

impl CommandLineArguments {
Expand All @@ -36,6 +38,7 @@ impl CommandLineArguments {
let mut dns_servers = None;
let mut routes = None;
let mut port = 0;
let mut conf = String::from("");

let mut iter = args.into_iter();
while let Some(arg) = iter.next() {
Expand Down Expand Up @@ -70,6 +73,15 @@ impl CommandLineArguments {
} else {
return Err(String::from("Missing -p parameter"));
}
} else if (accepted_parameters & PARAM_CONF) != 0 && "-c" == arg {
if !conf.is_empty() {
return Err(String::from("Conf already set"));
}
if let Some(value) = iter.next() {
conf = value.into();
} else {
return Err(String::from("Missing -c parameter"));
}
} else if (accepted_parameters & PARAM_SERIAL) != 0 && serial.is_none() {
serial = Some(arg);
} else {
Expand All @@ -84,6 +96,7 @@ impl CommandLineArguments {
dns_servers,
routes,
port,
conf
})
}

Expand All @@ -102,6 +115,10 @@ impl CommandLineArguments {
pub fn port(&self) -> u16 {
self.port
}

pub fn conf(&self) -> &str {
&self.conf
}
}

#[cfg(test)]
Expand Down
5 changes: 3 additions & 2 deletions relay-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
* limitations under the License.
*/

#![allow(dead_code)]
mod relay;
pub use crate::relay::byte_buffer;

use crate::relay::Relay;
use std::io;

pub fn relay(port: u16) -> io::Result<()> {
Relay::new(port).run()
pub fn relay(port: u16, conf: String) -> io::Result<()> {
Relay::new(port, conf).run()
}
2 changes: 1 addition & 1 deletion relay-rust/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use log::*;
use std::io::{self, Write};

static LOGGER: SimpleLogger = SimpleLogger;
const THRESHOLD: LevelFilter = LevelFilter::Info;
const THRESHOLD: LevelFilter = LevelFilter::Debug;

pub struct SimpleLogger;

Expand Down
54 changes: 38 additions & 16 deletions relay-rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ impl Command for RunCommand {
| cli_args::PARAM_DNS_SERVERS
| cli_args::PARAM_ROUTES
| cli_args::PARAM_PORT
| cli_args::PARAM_CONF
}

fn description(&self) -> &'static str {
Expand All @@ -171,6 +172,7 @@ impl Command for RunCommand {
args.dns_servers(),
args.routes(),
args.port(),
args.conf(),
)
}
}
Expand All @@ -181,7 +183,7 @@ impl Command for AutorunCommand {
}

fn accepted_parameters(&self) -> u8 {
cli_args::PARAM_DNS_SERVERS | cli_args::PARAM_ROUTES | cli_args::PARAM_PORT
cli_args::PARAM_DNS_SERVERS | cli_args::PARAM_ROUTES | cli_args::PARAM_PORT | cli_args::PARAM_CONF
}

fn description(&self) -> &'static str {
Expand All @@ -191,7 +193,7 @@ impl Command for AutorunCommand {
}

fn execute(&self, args: &CommandLineArguments) -> Result<(), CommandExecutionError> {
cmd_autorun(args.dns_servers(), args.routes(), args.port())
cmd_autorun(args.dns_servers(), args.routes(), args.port(), args.conf())
}
}

Expand All @@ -205,6 +207,7 @@ impl Command for StartCommand {
| cli_args::PARAM_DNS_SERVERS
| cli_args::PARAM_ROUTES
| cli_args::PARAM_PORT
| cli_args::PARAM_CONF
}

fn description(&self) -> &'static str {
Expand All @@ -228,6 +231,7 @@ impl Command for StartCommand {
args.dns_servers(),
args.routes(),
args.port(),
args.conf()
)
}
}
Expand All @@ -238,7 +242,7 @@ impl Command for AutostartCommand {
}

fn accepted_parameters(&self) -> u8 {
cli_args::PARAM_DNS_SERVERS | cli_args::PARAM_ROUTES | cli_args::PARAM_PORT
cli_args::PARAM_DNS_SERVERS | cli_args::PARAM_ROUTES | cli_args::PARAM_PORT | cli_args::PARAM_CONF
}

fn description(&self) -> &'static str {
Expand All @@ -249,7 +253,7 @@ impl Command for AutostartCommand {
}

fn execute(&self, args: &CommandLineArguments) -> Result<(), CommandExecutionError> {
cmd_autostart(args.dns_servers(), args.routes(), args.port())
cmd_autostart(args.dns_servers(), args.routes(), args.port(), args.conf())
}
}

Expand Down Expand Up @@ -283,6 +287,7 @@ impl Command for RestartCommand {
| cli_args::PARAM_DNS_SERVERS
| cli_args::PARAM_ROUTES
| cli_args::PARAM_PORT
| cli_args::PARAM_CONF
}

fn description(&self) -> &'static str {
Expand All @@ -296,6 +301,7 @@ impl Command for RestartCommand {
args.dns_servers(),
args.routes(),
args.port(),
args.conf()
)?;
Ok(())
}
Expand Down Expand Up @@ -328,15 +334,15 @@ impl Command for RelayCommand {
}

fn accepted_parameters(&self) -> u8 {
cli_args::PARAM_NONE | cli_args::PARAM_PORT
cli_args::PARAM_NONE | cli_args::PARAM_PORT | cli_args::PARAM_CONF
}

fn description(&self) -> &'static str {
"Start the relay server in the current terminal."
}

fn execute(&self, args: &CommandLineArguments) -> Result<(), CommandExecutionError> {
cmd_relay(args.port())?;
cmd_relay(args.port(), args.conf().to_string())?;
Ok(())
}
}
Expand All @@ -362,9 +368,10 @@ fn cmd_run(
dns_servers: Option<&str>,
routes: Option<&str>,
port: u16,
conf: &str,
) -> Result<(), CommandExecutionError> {
// start in parallel so that the relay server is ready when the client connects
async_start(serial, dns_servers, routes, port);
async_start(serial, dns_servers, routes, port, conf);

let ctrlc_serial = serial.map(String::from);
ctrlc::set_handler(move || {
Expand All @@ -379,34 +386,38 @@ fn cmd_run(
})
.expect("Error setting Ctrl-C handler");

cmd_relay(port)
cmd_relay(port, conf.to_string())
}

fn cmd_autorun(
dns_servers: Option<&str>,
routes: Option<&str>,
port: u16,
conf: &str
) -> Result<(), CommandExecutionError> {
{
let autostart_dns_servers = dns_servers.map(String::from);
let autostart_routes = routes.map(String::from);
let autostart_conf = String::from(conf);
thread::spawn(move || {
let dns_servers = autostart_dns_servers.as_ref().map(String::as_ref);
let routes = autostart_routes.as_ref().map(String::as_ref);
if let Err(err) = cmd_autostart(dns_servers, routes, port) {
let conf = &autostart_conf;
if let Err(err) = cmd_autostart(dns_servers, routes, port, conf) {
error!(target: TAG, "Cannot auto start clients: {}", err);
}
});
}

cmd_relay(port)
cmd_relay(port, conf.to_string())
}

fn cmd_start(
serial: Option<&str>,
dns_servers: Option<&str>,
routes: Option<&str>,
port: u16,
conf: &str
) -> Result<(), CommandExecutionError> {
if must_install_client(serial)? {
cmd_install(serial)?;
Expand All @@ -433,20 +444,25 @@ fn cmd_start(
if let Some(routes) = routes {
adb_args.append(&mut vec!["--esa", "routes", routes]);
}
adb_args.append(&mut vec!["--esa", "conf", conf]);

exec_adb(serial, adb_args)
}

fn cmd_autostart(
dns_servers: Option<&str>,
routes: Option<&str>,
port: u16,
conf: &str
) -> Result<(), CommandExecutionError> {
let start_dns_servers = dns_servers.map(String::from);
let start_routes = routes.map(String::from);
let start_conf = String::from(conf);
let mut adb_monitor = AdbMonitor::new(Box::new(move |serial: &str| {
let dns_servers = start_dns_servers.as_ref().map(String::as_ref);
let routes = start_routes.as_ref().map(String::as_ref);
async_start(Some(serial), dns_servers, routes, port)
let conf = &start_conf;
async_start(Some(serial), dns_servers, routes, port, conf)
}));
adb_monitor.monitor();
Ok(())
Expand Down Expand Up @@ -479,21 +495,23 @@ fn cmd_tunnel(serial: Option<&str>, port: u16) -> Result<(), CommandExecutionErr
)
}

fn cmd_relay(port: u16) -> Result<(), CommandExecutionError> {
info!(target: TAG, "Starting relay server on port {}...", port);
relaylib::relay(port)?;
fn cmd_relay(port: u16, conf: String) -> Result<(), CommandExecutionError> {
info!(target: TAG, "Starting relay server on port {} with conf file {}...", port, conf);
relaylib::relay(port, conf)?;
Ok(())
}

fn async_start(serial: Option<&str>, dns_servers: Option<&str>, routes: Option<&str>, port: u16) {
fn async_start(serial: Option<&str>, dns_servers: Option<&str>, routes: Option<&str>, port: u16, conf: &str) {
let start_serial = serial.map(String::from);
let start_dns_servers = dns_servers.map(String::from);
let start_routes = routes.map(String::from);
let start_conf = String::from(conf);
thread::spawn(move || {
let serial = start_serial.as_ref().map(String::as_ref);
let dns_servers = start_dns_servers.as_ref().map(String::as_ref);
let routes = start_routes.as_ref().map(String::as_ref);
if let Err(err) = cmd_start(serial, dns_servers, routes, port) {
let conf = &start_conf;
if let Err(err) = cmd_start(serial, dns_servers, routes, port, conf) {
error!(target: TAG, "Cannot start client: {}", err);
}
});
Expand Down Expand Up @@ -605,6 +623,9 @@ fn append_command_usage(msg: &mut String, command: &dyn Command) {
if (accepted_parameters & cli_args::PARAM_ROUTES) != 0 {
msg.push_str(" [-r ROUTE[,ROUTE2,...]]");
}
if (accepted_parameters & cli_args::PARAM_CONF) != 0 {
msg.push_str(" -c path_to_proxy.toml");
}
msg.push('\n');
for desc_line in command.description().split('\n') {
msg.push_str(" ");
Expand All @@ -623,6 +644,7 @@ fn main() {
logger::init().unwrap();
let mut args = env::args();
// args.nth(1) will consume the two first arguments (the binary name and the command name)

if let Some(command_name) = args.nth(1) {
let command = COMMANDS
.iter()
Expand Down
9 changes: 9 additions & 0 deletions relay-rust/src/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@
*/

pub use self::relay::Relay;
pub use self::socks5_protocol::{Socks5State, Authentication};
pub use self::proxy_config::GNIREHTET_PROXY_CONFIG;
pub use self::proxy_config::CONF_PATH;

pub mod byte_buffer;

mod socks5_protocol;
mod proxy_config;

mod binary;
mod client;
mod close_listener;
Expand All @@ -32,6 +39,7 @@ mod ipv4_packet_buffer;
mod net;
mod packet_source;
mod packetizer;

#[allow(clippy::module_inception)] // relay.rs is in relay/
mod relay;
mod router;
Expand All @@ -43,3 +51,4 @@ mod transport_header;
mod tunnel_server;
mod udp_connection;
mod udp_header;

Loading