feat: rust/rsHttp Option with_custom_dns

This commit is contained in:
2024-11-03 15:57:37 +08:00
parent 7e1e96707c
commit 0c03050f5c
7 changed files with 227 additions and 44 deletions

View File

@ -16,7 +16,7 @@ pub enum MyMethod {
}
fn _my_method_to_hyper_method(m: MyMethod) -> Method {
return match m {
match m {
MyMethod::Options => Method::OPTIONS,
MyMethod::Gets => Method::GET,
MyMethod::Post => Method::POST,
@ -26,7 +26,7 @@ fn _my_method_to_hyper_method(m: MyMethod) -> Method {
MyMethod::Trace => Method::TRACE,
MyMethod::Connect => Method::CONNECT,
MyMethod::Patch => Method::PATCH,
};
}
}
pub fn set_default_header(headers: HashMap<String, String>) {
@ -39,6 +39,7 @@ pub async fn fetch(
headers: Option<HashMap<String, String>>,
input_data: Option<Vec<u8>>,
with_ip_address: Option<String>,
with_custom_dns: Option<bool>,
) -> anyhow::Result<RustHttpResponse> {
http_package::fetch(
_my_method_to_hyper_method(method),
@ -46,6 +47,7 @@ pub async fn fetch(
headers,
input_data,
with_ip_address,
with_custom_dns
)
.await
}

View File

@ -156,6 +156,7 @@ fn wire__crate__api__http_api__fetch_impl(
headers: impl CstDecode<Option<std::collections::HashMap<String, String>>>,
input_data: impl CstDecode<Option<Vec<u8>>>,
with_ip_address: impl CstDecode<Option<String>>,
with_custom_dns: impl CstDecode<Option<bool>>,
) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_async::<flutter_rust_bridge::for_generated::DcoCodec, _, _, _>(
flutter_rust_bridge::for_generated::TaskInfo {
@ -169,6 +170,7 @@ fn wire__crate__api__http_api__fetch_impl(
let api_headers = headers.cst_decode();
let api_input_data = input_data.cst_decode();
let api_with_ip_address = with_ip_address.cst_decode();
let api_with_custom_dns = with_custom_dns.cst_decode();
move |context| async move {
transform_result_dco::<_, _, flutter_rust_bridge::for_generated::anyhow::Error>(
(move || async move {
@ -178,6 +180,7 @@ fn wire__crate__api__http_api__fetch_impl(
api_headers,
api_input_data,
api_with_ip_address,
api_with_custom_dns,
)
.await?;
Ok(output_ok)
@ -567,6 +570,17 @@ impl SseDecode for Option<String> {
}
}
impl SseDecode for Option<bool> {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
if (<bool>::sse_decode(deserializer)) {
return Some(<bool>::sse_decode(deserializer));
} else {
return None;
}
}
}
impl SseDecode for Option<u64> {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
@ -1008,6 +1022,16 @@ impl SseEncode for Option<String> {
}
}
impl SseEncode for Option<bool> {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
<bool>::sse_encode(self.is_some(), serializer);
if let Some(value) = self {
<bool>::sse_encode(value, serializer);
}
}
}
impl SseEncode for Option<u64> {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
@ -1180,6 +1204,12 @@ mod io {
String::from_utf8(vec).unwrap()
}
}
impl CstDecode<bool> for *mut bool {
// Codec=Cst (C-struct based), see doc to use other codecs
fn cst_decode(self) -> bool {
unsafe { *flutter_rust_bridge::for_generated::box_from_leak_ptr(self) }
}
}
impl CstDecode<crate::api::asar_api::RsiLauncherAsarData> for *mut wire_cst_rsi_launcher_asar_data {
// Codec=Cst (C-struct based), see doc to use other codecs
fn cst_decode(self) -> crate::api::asar_api::RsiLauncherAsarData {
@ -1372,6 +1402,7 @@ mod io {
headers: *mut wire_cst_list_record_string_string,
input_data: *mut wire_cst_list_prim_u_8_strict,
with_ip_address: *mut wire_cst_list_prim_u_8_strict,
with_custom_dns: *mut bool,
) {
wire__crate__api__http_api__fetch_impl(
port_,
@ -1380,6 +1411,7 @@ mod io {
headers,
input_data,
with_ip_address,
with_custom_dns,
)
}
@ -1436,6 +1468,11 @@ mod io {
wire__crate__api__win32_api__set_foreground_window_impl(port_, window_name)
}
#[no_mangle]
pub extern "C" fn frbgen_starcitizen_doctor_cst_new_box_autoadd_bool(value: bool) -> *mut bool {
flutter_rust_bridge::for_generated::new_leak_box_ptr(value)
}
#[no_mangle]
pub extern "C" fn frbgen_starcitizen_doctor_cst_new_box_autoadd_rsi_launcher_asar_data(
) -> *mut wire_cst_rsi_launcher_asar_data {

View File

@ -48,15 +48,19 @@ static DEFAULT_HEADER: Lazy<RwLock<HeaderMap>> = Lazy::new(|| RwLock::from(Heade
static DNS_CLIENT: Lazy<Arc<dns::MyHickoryDnsResolver>> =
Lazy::new(|| Arc::from(dns::MyHickoryDnsResolver::default()));
static HTTP_CLIENT: Lazy<reqwest::Client> = Lazy::new(|| new_http_client(true));
static HTTP_CLIENT: Lazy<reqwest::Client> = Lazy::new(|| new_http_client(true,true));
fn new_http_client(keep_alive: bool) -> reqwest::Client {
static HTTP_CLIENT_NO_CUSTOM_DNS: Lazy<reqwest::Client> = Lazy::new(|| new_http_client(true,false));
fn new_http_client(keep_alive: bool,with_custom_dns: bool) -> reqwest::Client {
let mut c = reqwest::Client::builder()
.dns_resolver(DNS_CLIENT.clone())
.use_rustls_tls()
.connect_timeout(Duration::from_secs(10))
.gzip(true)
.no_proxy();
if with_custom_dns {
c = c.dns_resolver(DNS_CLIENT.clone());
}
if !keep_alive {
c = c.tcp_keepalive(None);
} else {
@ -82,15 +86,15 @@ pub async fn fetch(
headers: Option<HashMap<String, String>>,
input_data: Option<Vec<u8>>,
with_ip_address: Option<String>,
with_custom_dns: Option<bool>,
) -> anyhow::Result<RustHttpResponse> {
let address_clone = with_ip_address.clone();
let url_clone = url.clone();
if address_clone.is_some() {
let addr = std::net::IpAddr::from_str(with_ip_address.unwrap().as_str()).unwrap();
let addr = std::net::IpAddr::from_str(with_ip_address.unwrap().as_str())?;
let mut hosts = dns::MY_HOSTS_MAP.write().unwrap();
let url_host = Url::from_str(url.as_str())
.unwrap()
let url_host = Url::from_str(url.as_str())?
.host()
.unwrap()
.to_string();
@ -105,9 +109,13 @@ pub async fn fetch(
}
let mut req = if address_clone.is_some() {
_mix_header(new_http_client(false).request(method, url_clone), headers)
_mix_header(new_http_client(false,with_custom_dns.unwrap_or(false)).request(method, url_clone), headers)
} else {
_mix_header(HTTP_CLIENT.request(method, url_clone), headers)
if with_custom_dns.unwrap_or(false) {
_mix_header(HTTP_CLIENT.request(method, url_clone), headers)
}else {
_mix_header(HTTP_CLIENT_NO_CUSTOM_DNS.request(method, url_clone), headers)
}
};
if input_data.is_some() {
req = req.body(input_data.unwrap());