Skip to content
Merged
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
255 changes: 168 additions & 87 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ enum_dispatch = "0.3"
thiserror = "2"

compio = "0.16"
cyper = { version = "0.5", default-features = false }
cyper = { version = "0.6", default-features = false }
nyquest = "0.3"
nyquest-preset = "0.3"
winio = { version = "0.9", default-features = false }
flume = "0.11"

chrono = { version = "0.4", default-features = false, features = [
"clock",
Expand All @@ -51,6 +52,7 @@ dirs = "6"
log = "0.4"
base64 = "0.22"

flume = "0.11"
futures-util = "0.3"

keyring = { version = "3", features = [
Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ jobs:
steps:
- script: |
sudo apt-get update
sudo apt-get install qt6-base-dev
sudo apt-get install qt6-base-dev libcurl4-openssl-dev
displayName: 'Install qt6'

- script: cargo install cargo-deb
Expand Down
9 changes: 4 additions & 5 deletions tunet-flutter/native/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::frb_generated::StreamSink;
use anyhow::Result;
use flutter_rust_bridge::{frb, setup_default_user_utils};

pub use netstatus::NetStatus;
Expand Down Expand Up @@ -69,7 +68,7 @@ pub struct Runtime {

impl Runtime {
#[frb(sync)]
pub fn new() -> Result<Runtime> {
pub fn new() -> Runtime {
#[cfg(target_os = "android")]
android_logger::init_once(
android_logger::Config::default()
Expand All @@ -84,12 +83,12 @@ impl Runtime {

let (atx, arx) = flume::unbounded();
let (utx, urx) = flume::unbounded();
let model = Model::new(atx, utx)?;
Ok(Self {
let model = Model::new(atx, utx);
Self {
arx: Arc::new(Mutex::new(Some(arx))),
urx: Arc::new(Mutex::new(Some(urx))),
model: Arc::new(Mutex::new(model)),
})
}
}

#[frb(sync)]
Expand Down
2 changes: 1 addition & 1 deletion tunet-gui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Component for MainModel {

let (action_sender, action_receiver) = flume::unbounded();
let (update_sender, update_receiver) = flume::unbounded();
let model = Model::new(action_sender, update_sender).unwrap();
let model = Model::new(action_sender, update_sender);
{
let sender = sender.clone();
spawn(async move {
Expand Down
11 changes: 6 additions & 5 deletions tunet-helper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repository.workspace = true
[dependencies]
authtea = { workspace = true }
netstatus = { workspace = true }
cyper = { workspace = true, default-features = false, features = ["json"] }
nyquest = { workspace = true, features = ["async"] }
url = "2"
md-5 = "0.10"
sha-1 = "0.10"
Expand All @@ -23,13 +23,14 @@ thiserror = { workspace = true }
allo-isolate = { version = "0.1", features = ["chrono"], optional = true }

[target.'cfg(target_os = "android")'.dependencies]
cyper = { workspace = true, default-features = false, features = ["rustls"] }
cyper = { workspace = true, default-features = false, features = [
"rustls",
"nyquest-async",
] }
rustls = { workspace = true, default-features = false, features = ["ring"] }

[target.'cfg(not(target_os = "android"))'.dependencies]
cyper = { workspace = true, default-features = false, features = [
"native-tls",
] }
nyquest-preset = { workspace = true, features = ["async"] }

[features]
default = []
Expand Down
50 changes: 26 additions & 24 deletions tunet-helper/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ use base64::{
use data_encoding::HEXLOWER;
use hmac::{Hmac, Mac};
use md5::Md5;
use nyquest::Request;
use regex::Regex;
use serde_json::{json, Value as JsonValue};
use sha1::{Digest, Sha1};
use std::borrow::Cow;
use std::marker::PhantomData;
use std::sync::LazyLock;
use url::Url;
Expand Down Expand Up @@ -51,7 +53,7 @@ impl<U: AuthConnectUri + Send + Sync> AuthConnect<U> {
],
)
.unwrap();
let res = self.client.get(uri)?.send().await?;
let res = self.client.request(Request::get(uri.to_string())).await?;
let t = res.text().await?;
let mut json: JsonValue = serde_json::from_str(&t[9..t.len() - 1])?;
Ok(json
Expand All @@ -61,7 +63,7 @@ impl<U: AuthConnectUri + Send + Sync> AuthConnect<U> {
}

async fn get_ac_id(&self) -> Option<i32> {
let res = self.client.get(REDIRECT_URI).ok()?.send().await.ok()?;
let res = self.client.request(Request::get(REDIRECT_URI)).await.ok()?;
let t = res.text().await.ok()?;
let cap = AC_ID_REGEX.captures(&t)?;
cap[1].parse::<i32>().ok()
Expand Down Expand Up @@ -95,22 +97,22 @@ impl<U: AuthConnectUri + Send + Sync> AuthConnect<U> {
sha1.finalize()
};
let params = [
("action", "login"),
("ac_id", &ac_id.to_string()),
("double_stack", "1"),
("n", "200"),
("type", "1"),
("username", u),
("password", &format!("{{MD5}}{password_md5}")),
("info", &info),
("chksum", &HEXLOWER.encode(&chksum)),
("callback", "callback"),
("action", Cow::Borrowed("login")),
("ac_id", Cow::Owned(ac_id.to_string())),
("double_stack", Cow::Borrowed("1")),
("n", Cow::Borrowed("200")),
("type", Cow::Borrowed("1")),
("username", Cow::Owned(u.to_string())),
("password", Cow::Owned(format!("{{MD5}}{password_md5}"))),
("info", Cow::Owned(info)),
("chksum", Cow::Owned(HEXLOWER.encode(&chksum))),
("callback", Cow::Borrowed("callback")),
];
let res = self
.client
.post(U::log_uri())?
.form(&params)?
.send()
.request(Request::post(U::log_uri()).with_body(nyquest::Body::form(
params.map(|(k, v)| (Cow::Borrowed(k), v)),
)))
.await?;
let t = res.text().await?;
Self::parse_response(&t)
Expand Down Expand Up @@ -145,24 +147,24 @@ impl<U: AuthConnectUri + Send + Sync> TUNetHelper for AuthConnect<U> {

async fn logout(&self, u: &str) -> NetHelperResult<String> {
let params = [
("action", "logout"),
("ac_id", "1"),
("double_stack", "1"),
("username", u),
("callback", "callback"),
("action", Cow::Borrowed("logout")),
("ac_id", Cow::Borrowed("1")),
("double_stack", Cow::Borrowed("1")),
("username", Cow::Owned(u.to_string())),
("callback", Cow::Borrowed("callback")),
];
let res = self
.client
.post(U::log_uri())?
.form(&params)?
.send()
.request(Request::post(U::log_uri()).with_body(nyquest::Body::form(
params.map(|(k, v)| (Cow::Borrowed(k), v)),
)))
.await?;
let t = res.text().await?;
Self::parse_response(&t)
}

async fn flux(&self) -> NetHelperResult<NetFlux> {
let res = self.client.get(U::flux_uri())?.send().await?;
let res = self.client.request(Request::get(U::flux_uri())).await?;
res.text().await?.parse()
}
}
Expand Down
28 changes: 24 additions & 4 deletions tunet-helper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

use std::fmt::{Display, Formatter};
use std::future::Future;
use std::sync::Once;
use thiserror::Error;

pub use chrono::{
DateTime, Datelike, Duration as NaiveDuration, FixedOffset, Local, NaiveDate, NaiveDateTime,
Timelike,
};
pub use cyper::Client as HttpClient;
pub use nyquest::AsyncClient as HttpClient;

mod auth;
pub mod suggest;
Expand All @@ -28,7 +29,7 @@ pub enum NetHelperError {
#[error("无法确定登录方式")]
InvalidHost,
#[error("网络请求错误:{0}")]
Reqwest(#[from] cyper::Error),
Nyquest(#[from] nyquest::Error),
#[error("JSON 解析错误:{0}")]
Json(#[from] serde_json::Error),
}
Expand Down Expand Up @@ -234,8 +235,27 @@ impl TUNetConnect {
}
}

pub fn create_http_client() -> HttpClient {
cyper::ClientBuilder::new().build()
pub async fn create_http_client() -> NetHelperResult<HttpClient> {
static ONCE: Once = Once::new();

#[cfg(not(target_os = "android"))]
ONCE.call_once(|| {
nyquest_preset::register();
});

#[cfg(target_os = "android")]
ONCE.call_once(|| {
cyper::nyquest::register();
});

Ok(nyquest::ClientBuilder::default()
.no_caching()
.no_cookies()
.no_proxy()
.no_redirects()
.user_agent(format!("tunet-helper/{}", env!("CARGO_PKG_VERSION")))
.build_async()
.await?)
}

#[cfg(feature = "dart")]
Expand Down
8 changes: 5 additions & 3 deletions tunet-helper/src/suggest/ping.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use nyquest::Request;

use crate::*;

async fn can_connect_impl(client: &HttpClient, uri: &str) -> cyper::Result<()> {
client.head(uri)?.send().await?;
async fn can_connect_impl(client: &HttpClient, uri: &'static str) -> nyquest::Result<()> {
client.request(Request::head(uri)).await?;
Ok(())
}

async fn can_connect(client: &HttpClient, uri: &str) -> bool {
async fn can_connect(client: &HttpClient, uri: &'static str) -> bool {
can_connect_impl(client, uri).await.is_ok()
}

Expand Down
3 changes: 2 additions & 1 deletion tunet-model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ flume = { workspace = true }
tunet-helper = { workspace = true }
netstatus = { workspace = true }
futures-util = { workspace = true }
compio = { workspace = true }
compio = { workspace = true, features = ["time"] }
anyhow = { workspace = true }
drop_guard = "0.3"
async-once-cell = "0.5"
Loading