Просмотр исходного кода

remove os_info, use whoami, refactor net lib, remove util

master
Jonathan Cobb 4 лет назад
Родитель
Сommit
33dc86e808
5 измененных файлов: 120 добавлений и 124 удалений
  1. +0
    -11
      Cargo.lock
  2. +0
    -1
      Cargo.toml
  3. +0
    -1
      src/lib.rs
  4. +120
    -95
      src/net.rs
  5. +0
    -16
      src/util.rs

+ 0
- 11
Cargo.lock Просмотреть файл

@@ -112,7 +112,6 @@ dependencies = [
"hyper",
"hyper-tls",
"lru",
"os_info",
"pnet",
"tokio",
"tower",
@@ -754,16 +753,6 @@ dependencies = [
"vcpkg",
]

[[package]]
name = "os_info"
version = "2.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2cc1b4330bb29087e791ae2a5cf56be64fb8946a4ff5aec2ba11c6ca51f5d60"
dependencies = [
"log 0.4.11",
"winapi 0.3.9",
]

[[package]]
name = "percent-encoding"
version = "2.1.0"


+ 0
- 1
Cargo.toml Просмотреть файл

@@ -15,7 +15,6 @@ http = "0.2.1"
hyper = { version = "0.13.7", features = ["stream"] }
hyper-tls = "0.4.3"
lru = "0.6.0"
os_info = { version = "2.0.8", default-features = false }
pnet = "0.26.0"
tokio = { version = "0.2.22", features = ["full"] }
tower = "0.3.1"


+ 0
- 1
src/lib.rs Просмотреть файл

@@ -4,7 +4,6 @@
* For personal (non-commercial) use, see license: https://getbubblenow.com/bubble-license/
*/

pub mod util;
pub mod pass;
pub mod hyper_util;
pub mod net;


+ 120
- 95
src/net.rs Просмотреть файл

@@ -4,68 +4,80 @@
* For personal (non-commercial) use, see license: https://getbubblenow.com/bubble-license/
*/

use std::process::{Command, Stdio};
use std::process::{exit, Command, Stdio};

use os_info::{Info, Type};

use crate::util::chop_newline;
use whoami::{platform, Platform};

pub fn ip_gateway() -> String {
let info: Info = os_info::get();
let ostype: Type = info.os_type();
return if ostype == Type::Windows {
let output = Command::new("C:\\Windows\\System32\\cmd.exe")
.stdin(Stdio::null())
.arg("/c")
.arg("route").arg("print").arg("0.0.0.0")
.arg("|").arg("findstr").arg("/L").arg("/C:0.0.0.0")
.output().unwrap().stdout;
let data = String::from_utf8(output).unwrap();
let mut parts = data.split_ascii_whitespace();
parts.next();
parts.next();
chop_newline(String::from(parts.next().unwrap()))
} else if ostype == Type::Macos {
let output = Command::new("/bin/sh")
.stdin(Stdio::null())
.arg("-c")
.arg("netstat -rn | grep -m 1 default | cut -d' ' -f2")
.output().unwrap().stdout;
chop_newline(String::from_utf8(output).unwrap())
} else {
let output = Command::new("/bin/sh")
.stdin(Stdio::null())
.arg("-c")
.arg("ip route show | grep -m 1 default | cut -d' ' -f3")
.output().unwrap().stdout;
chop_newline(String::from_utf8(output).unwrap())
let platform: Platform = platform();
return match platform {
Platform::Windows => {
let output = Command::new("C:\\Windows\\System32\\cmd.exe")
.stdin(Stdio::null())
.arg("/c")
.arg("route").arg("print").arg("0.0.0.0")
.arg("|").arg("findstr").arg("/L").arg("/C:0.0.0.0")
.output().unwrap().stdout;
let data = String::from_utf8(output).unwrap();
let mut parts = data.split_ascii_whitespace();
parts.next();
parts.next();
String::from(parts.next().unwrap().trim())
}
Platform::MacOS => {
let output = Command::new("/bin/sh")
.stdin(Stdio::null())
.arg("-c")
.arg("netstat -rn | grep -m 1 default | cut -d' ' -f2")
.output().unwrap().stdout;
String::from(String::from_utf8(output).unwrap().trim())
}
Platform::Linux => {
let output = Command::new("/bin/sh")
.stdin(Stdio::null())
.arg("-c")
.arg("ip route show | grep -m 1 default | cut -d' ' -f3")
.output().unwrap().stdout;
String::from(String::from_utf8(output).unwrap().trim())
}
_ => {
eprintln!("ERROR: Unsupported platform: {:?}", platform);
exit(2);
}
}
}

pub fn needs_static_route(ip_string: &String) -> bool {
println!("needs_static_route: checking ip={:?}", ip_string);
let info: Info = os_info::get();
let ostype: Type = info.os_type();
let output = if ostype == Type::Windows {
Command::new("C:\\Windows\\System32\\cmd.exe")
.stdin(Stdio::null())
.arg("/c")
.arg("route").arg("print").arg(ip_string)
.arg("|")
.arg("findstr").arg("/L").arg("/C:\"Network Destination\"")
.output().unwrap().stdout
} else if ostype == Type::Macos {
Command::new("/bin/sh")
.stdin(Stdio::null())
.arg("-c")
.arg(format!("netstat -rn | egrep -m 1 \"^{}\"", ip_string))
.output().unwrap().stdout
} else {
Command::new("/bin/sh")
.stdin(Stdio::null())
.arg("-c")
.arg(format!("ip route show | egrep -m 1 \"^{}\" | cut -d' ' -f3", ip_string))
.output().unwrap().stdout
let platform: Platform = platform();
let output = match platform {
Platform::Windows => {
Command::new("C:\\Windows\\System32\\cmd.exe")
.stdin(Stdio::null())
.arg("/c")
.arg("route").arg("print").arg(ip_string)
.arg("|")
.arg("findstr").arg("/L").arg("/C:\"Network Destination\"")
.output().unwrap().stdout
}
Platform::MacOS => {
Command::new("/bin/sh")
.stdin(Stdio::null())
.arg("-c")
.arg(format!("netstat -rn | egrep -m 1 \"^{}\"", ip_string))
.output().unwrap().stdout
}
Platform::Linux => {
Command::new("/bin/sh")
.stdin(Stdio::null())
.arg("-c")
.arg(format!("ip route show | egrep -m 1 \"^{}\" | cut -d' ' -f3", ip_string))
.output().unwrap().stdout
}
_ => {
eprintln!("ERROR: Unsupported platform: {:?}", platform);
exit(2);
}
};
let data = String::from_utf8(output).unwrap();
let mut parts = data.split_ascii_whitespace();
@@ -75,26 +87,33 @@ pub fn needs_static_route(ip_string: &String) -> bool {

pub fn create_static_route(gateway: &String, ip_string: &String) -> bool {
println!("create_static_route: creating: gateway={}, ip={}", gateway, ip_string);
let info: Info = os_info::get();
let ostype: Type = info.os_type();
let output = if ostype == Type::Windows {
Command::new("C:\\Windows\\System32\\cmd.exe")
.stdin(Stdio::null())
.arg("/c")
.arg("route").arg("add").arg(ip_string).arg(gateway)
.output().unwrap().stderr
} else if ostype == Type::Macos {
Command::new("/bin/sh")
.stdin(Stdio::null())
.arg("-c")
.arg(format!("sudo route -n add {} {}", ip_string, gateway))
.output().unwrap().stderr
} else {
Command::new("/bin/sh")
.stdin(Stdio::null())
.arg("-c")
.arg(format!("sudo ip route add {} via {}", ip_string, gateway))
.output().unwrap().stderr
let platform: Platform = platform();
let output = match platform {
Platform::Windows => {
Command::new("C:\\Windows\\System32\\cmd.exe")
.stdin(Stdio::null())
.arg("/c")
.arg("route").arg("add").arg(ip_string).arg(gateway)
.output().unwrap().stderr
}
Platform::MacOS => {
Command::new("/bin/sh")
.stdin(Stdio::null())
.arg("-c")
.arg(format!("sudo route -n add {} {}", ip_string, gateway))
.output().unwrap().stderr
}
Platform::Linux => {
Command::new("/bin/sh")
.stdin(Stdio::null())
.arg("-c")
.arg(format!("sudo ip route add {} via {}", ip_string, gateway))
.output().unwrap().stderr
}
_ => {
eprintln!("ERROR: Unsupported platform: {:?}", platform);
exit(2);
}
};
let data = String::from_utf8(output).unwrap();
let mut parts = data.split_ascii_whitespace();
@@ -108,33 +127,39 @@ pub fn create_static_route(gateway: &String, ip_string: &String) -> bool {

pub fn remove_static_route(ip_string: &String) -> bool {
println!("remove_static_route: removing ip={}", ip_string);
let info: Info = os_info::get();
let ostype: Type = info.os_type();
let output = if ostype == Type::Windows {
Command::new("C:\\Windows\\System32\\cmd.exe")
.stdin(Stdio::null())
.arg("/c")
.arg("route").arg("del").arg(ip_string)
.output().unwrap().stderr
} else if ostype == Type::Macos {
Command::new("/bin/sh")
.stdin(Stdio::null())
.arg("-c")
.arg(format!("sudo route -n del {}", ip_string))
.output().unwrap().stderr
} else {
Command::new("/bin/sh")
.stdin(Stdio::null())
.arg("-c")
.arg(format!("sudo ip route del {}", ip_string))
.output().unwrap().stderr
let platform: Platform = platform();
let output = match platform {
Platform::Windows => {
Command::new("C:\\Windows\\System32\\cmd.exe")
.stdin(Stdio::null())
.arg("/c")
.arg("route").arg("del").arg(ip_string)
.output().unwrap().stderr
}
Platform::MacOS => {
Command::new("/bin/sh")
.stdin(Stdio::null())
.arg("-c")
.arg(format!("sudo route -n del {}", ip_string))
.output().unwrap().stderr
} Platform::Linux => {
Command::new("/bin/sh")
.stdin(Stdio::null())
.arg("-c")
.arg(format!("sudo ip route del {}", ip_string))
.output().unwrap().stderr
}
_ => {
eprintln!("ERROR: Unsupported platform: {:?}", platform);
exit(2);
}
};
let data = String::from_utf8(output).unwrap();
let mut parts = data.split_ascii_whitespace();
let first_part = parts.next();
let ok = first_part.is_none() || first_part.unwrap().len() == 0;
if !ok {
println!("remove_static_route: error removing route to {}: {}", ip_string, data);
eprintln!("ERROR: remove_static_route: error removing route to {}: {}", ip_string, data);
}
ok
}

+ 0
- 16
src/util.rs Просмотреть файл

@@ -1,16 +0,0 @@
#![deny(warnings)]
/**
* Copyright (c) 2020 Bubble, Inc. All rights reserved.
* For personal (non-commercial) use, see license: https://getbubblenow.com/bubble-license/
*/

pub fn chop_newline(output: String) -> String {
let mut data: String = output.clone();
let newline = data.find("\n");
return if newline.is_some() {
data.truncate(newline.unwrap());
data
} else {
data
}
}

Загрузка…
Отмена
Сохранить