Skip to content

Commit

Permalink
add tcp wg support
Browse files Browse the repository at this point in the history
  • Loading branch information
PinkD committed Sep 1, 2024
1 parent 9fe91c0 commit 445c4c5
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[submodule "libwg/wireguard-go"]
path = libwg/wireguard-go
url = https://github.com/PinkD/wireguard-go
branch = libwg
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion libwg/wireguard-go
21 changes: 15 additions & 6 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl Client {
c.save_json(&mut file).unwrap();
}

async fn request<T: DeserializeOwned>(
async fn request<T: DeserializeOwned+fmt::Debug>(
&mut self,
api: ApiName,
body: Option<Map<String, Value>>,
Expand Down Expand Up @@ -226,7 +226,9 @@ impl Client {
if let Err(err) = resp {
return Err(Error::ReqwestError(err));
}
Ok(resp.unwrap())
let resp = resp.unwrap();
log::debug!("api {:#?} resp: {:#?}", api, resp);
Ok(resp)
}

fn parse_time_offset_from_date_header(&mut self, resp: &Response) {
Expand Down Expand Up @@ -713,6 +715,7 @@ impl Client {
};
match mode {
"udp" => true,
"tcp" => true,
_ => {
log::info!(
"server name {} is not support {} wg for now",
Expand All @@ -734,13 +737,14 @@ impl Client {
None => self.get_first_available_vpn(filtered_vpn).await,
};

let vpn_addr = match vpn {
Some(ref vpn) => format!("{}:{}", vpn.ip, vpn.vpn_port),
let vpn = match vpn {
Some(ref vpn) => vpn,
None => return Err(Error::Error("no vpn available".to_string())),
};
let vpn_addr = format!("{}:{}", vpn.ip, vpn.vpn_port);
log::info!(
"try connect to {}, address {}",
vpn.unwrap().en_name,
vpn.en_name,
vpn_addr
);

Expand All @@ -765,7 +769,12 @@ impl Client {
peer_key,
route,
dns,
protocol: 0,
protocol: match vpn.protocol_mode {
// tcp
1 => 1,
// udp
_ => 0,
},
};
Ok(wg_conf)
}
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod wg;
#[cfg(windows)]
use is_elevated;

use env_logger::{Builder, Env, Target};
use env_logger;
use std::env;
use std::process::exit;

Expand Down Expand Up @@ -56,9 +56,9 @@ pub const ETIMEDOUT: i32 = 110;

#[tokio::main]
async fn main() {
Builder::from_env(Env::default().default_filter_or("info"))
.target(Target::Stdout)
.init();
// NOTE: If you want to debug, you should set `RUST_LOG` env to `debug` and run corplink-rs in root
// because `check_previlige` will call sudo and drop env if you're not root
env_logger::init();

print_version();
check_previlige();
Expand Down
20 changes: 10 additions & 10 deletions src/resp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(serde::Deserialize)]
#[derive(serde::Deserialize, Debug)]
pub struct Resp<T> {
pub code: i32,
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -10,7 +10,7 @@ pub struct Resp<T> {
pub action: Option<String>,
}

#[derive(serde::Deserialize)]
#[derive(serde::Deserialize, Debug)]
pub struct RespCompany {
pub name: String,
pub zh_name: String,
Expand All @@ -22,39 +22,39 @@ pub struct RespCompany {
pub public_key: String,
}

#[derive(serde::Deserialize)]
#[derive(serde::Deserialize, Debug)]
pub struct RespLoginMethod {
pub login_enable_ldap: bool,
pub login_enable: bool,
pub login_orders: Vec<String>,
}

#[derive(serde::Deserialize)]
#[derive(serde::Deserialize, Debug)]
pub struct RespTpsLoginMethod {
pub alias: String,
pub login_url: String,
pub token: String,
}

#[derive(serde::Deserialize)]
#[derive(serde::Deserialize, Debug)]
pub struct RespCorplinkLoginMethod {
pub mfa: bool,
pub auth: Vec<String>,
}

#[derive(serde::Deserialize)]
#[derive(serde::Deserialize, Debug)]
pub struct RespLogin {
#[serde(default)]
pub url: String,
}

#[derive(serde::Deserialize)]
#[derive(serde::Deserialize, Debug)]
pub struct RespOtp {
pub url: String,
pub code: String,
}

#[derive(serde::Deserialize)]
#[derive(serde::Deserialize, Debug)]
pub struct RespVpnInfo {
pub api_port: u16,
pub vpn_port: u16,
Expand All @@ -69,7 +69,7 @@ pub struct RespVpnInfo {
pub timeout: i32,
}

#[derive(serde::Deserialize)]
#[derive(serde::Deserialize, Debug)]
pub struct RespWgExtraInfo {
pub vpn_mtu: u32,
pub vpn_dns: String,
Expand All @@ -79,7 +79,7 @@ pub struct RespWgExtraInfo {
pub vpn_route_split: Vec<String>,
}

#[derive(serde::Deserialize)]
#[derive(serde::Deserialize, Debug)]
pub struct RespWgInfo {
pub ip: String,
pub ipv6: String,
Expand Down
16 changes: 6 additions & 10 deletions src/wg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ mod libwg {
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}

fn start_wg(log_level: i32, interface_name: &str) -> i32 {
fn start_wg(log_level: i32, protocol: i32, interface_name: &str) -> i32 {
let name = interface_name.as_bytes();
unsafe { libwg::startWg(log_level, to_c_char_array(name)) }
unsafe { libwg::startWg(log_level, protocol, to_c_char_array(name)) }
}

fn stop_wg() {
Expand All @@ -44,14 +44,12 @@ pub fn stop_wg_go() {
}

pub fn start_wg_go(name: &str, protocol: i32, with_log: bool) -> bool {
// TODO: support tcp tun
_ = protocol;
log::info!("start wg-corplink");
let mut log_level = libwg::LogLevelError;
if with_log {
log_level = libwg::LogLevelVerbose;
}
let ret = start_wg(log_level, name);
let ret = start_wg(log_level, protocol, name);
matches!(ret, 0)
}

Expand Down Expand Up @@ -129,12 +127,10 @@ impl UAPIClient {
if timestamp == 0 {
// do nothing because it's invalid
} else {
let nt = chrono::NaiveDateTime::from_timestamp_opt(timestamp, 0)
.unwrap();
let now = chrono::Utc::now().naive_utc();
let nt = chrono::DateTime::from_timestamp(timestamp, 0).unwrap();
let now = chrono::Utc::now().to_utc();
let t = now - nt;
let tt: chrono::DateTime<chrono::Utc> =
chrono::DateTime::from_utc(nt, chrono::Utc);
let tt = nt.to_utc();
let lt = tt.with_timezone(&chrono::Local);
let elapsed = t.to_std().unwrap().as_secs_f32();
log::info!("last handshake is at {lt}, elapsed time {elapsed}s");
Expand Down

0 comments on commit 445c4c5

Please sign in to comment.