feat: [RustHTTP] DNS Host Map

This commit is contained in:
xkeyC 2024-03-12 20:07:06 +08:00
parent 51393317b1
commit 26b58324c4
11 changed files with 262 additions and 40 deletions

View File

@ -15,15 +15,18 @@ class RSHttp {
} }
static Future<RustHttpResponse> get(String url, static Future<RustHttpResponse> get(String url,
{Map<String, String>? headers}) async { {Map<String, String>? headers, String? withIpAddress}) async {
final r = await rust_http.fetch( final r = await rust_http.fetch(
method: MyMethod.gets, url: url, headers: headers); method: MyMethod.gets,
url: url,
headers: headers,
withIpAddress: withIpAddress);
return r; return r;
} }
static Future<String> getText(String url, static Future<String> getText(String url,
{Map<String, String>? headers}) async { {Map<String, String>? headers, String? withIpAddress}) async {
final r = await get(url, headers: headers); final r = await get(url, headers: headers, withIpAddress: withIpAddress);
if (r.data == null) return ""; if (r.data == null) return "";
final str = utf8.decode(r.data!); final str = utf8.decode(r.data!);
return str; return str;
@ -32,13 +35,18 @@ class RSHttp {
static Future<RustHttpResponse> postData(String url, static Future<RustHttpResponse> postData(String url,
{Map<String, String>? headers, {Map<String, String>? headers,
String? contentType, String? contentType,
Uint8List? data}) async { Uint8List? data,
String? withIpAddress}) async {
if (contentType != null) { if (contentType != null) {
headers ??= {}; headers ??= {};
headers["Content-Type"] = contentType; headers["Content-Type"] = contentType;
} }
final r = await rust_http.fetch( final r = await rust_http.fetch(
method: MyMethod.post, url: url, headers: headers, inputData: data); method: MyMethod.post,
url: url,
headers: headers,
inputData: data,
withIpAddress: withIpAddress);
return r; return r;
} }

View File

@ -16,17 +16,22 @@ Future<RustHttpResponse> fetch(
required String url, required String url,
Map<String, String>? headers, Map<String, String>? headers,
Uint8List? inputData, Uint8List? inputData,
String? withIpAddress,
dynamic hint}) => dynamic hint}) =>
RustLib.instance.api.fetch( RustLib.instance.api.fetch(
method: method, method: method,
url: url, url: url,
headers: headers, headers: headers,
inputData: inputData, inputData: inputData,
withIpAddress: withIpAddress,
hint: hint); hint: hint);
Future<List<String>> dnsLookupTxt({required String host, dynamic hint}) => Future<List<String>> dnsLookupTxt({required String host, dynamic hint}) =>
RustLib.instance.api.dnsLookupTxt(host: host, hint: hint); RustLib.instance.api.dnsLookupTxt(host: host, hint: hint);
Future<List<String>> dnsLookupIps({required String host, dynamic hint}) =>
RustLib.instance.api.dnsLookupIps(host: host, hint: hint);
enum MyMethod { enum MyMethod {
options, options,
gets, gets,

View File

@ -64,6 +64,8 @@ class RustLib extends BaseEntrypoint<RustLibApi, RustLibApiImpl, RustLibWire> {
} }
abstract class RustLibApi extends BaseApi { abstract class RustLibApi extends BaseApi {
Future<List<String>> dnsLookupIps({required String host, dynamic hint});
Future<List<String>> dnsLookupTxt({required String host, dynamic hint}); Future<List<String>> dnsLookupTxt({required String host, dynamic hint});
Future<RustHttpResponse> fetch( Future<RustHttpResponse> fetch(
@ -71,6 +73,7 @@ abstract class RustLibApi extends BaseApi {
required String url, required String url,
Map<String, String>? headers, Map<String, String>? headers,
Uint8List? inputData, Uint8List? inputData,
String? withIpAddress,
dynamic hint}); dynamic hint});
Future<void> setDefaultHeader( Future<void> setDefaultHeader(
@ -91,6 +94,31 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
required super.portManager, required super.portManager,
}); });
@override
Future<List<String>> dnsLookupIps({required String host, dynamic hint}) {
return handler.executeNormal(NormalTask(
callFfi: (port_) {
final serializer = SseSerializer(generalizedFrbRustBinding);
sse_encode_String(host, serializer);
pdeCallFfi(generalizedFrbRustBinding, serializer,
funcId: 4, port: port_);
},
codec: SseCodec(
decodeSuccessData: sse_decode_list_String,
decodeErrorData: sse_decode_AnyhowException,
),
constMeta: kDnsLookupIpsConstMeta,
argValues: [host],
apiImpl: this,
hint: hint,
));
}
TaskConstMeta get kDnsLookupIpsConstMeta => const TaskConstMeta(
debugName: "dns_lookup_ips",
argNames: ["host"],
);
@override @override
Future<List<String>> dnsLookupTxt({required String host, dynamic hint}) { Future<List<String>> dnsLookupTxt({required String host, dynamic hint}) {
return handler.executeNormal(NormalTask( return handler.executeNormal(NormalTask(
@ -122,6 +150,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
required String url, required String url,
Map<String, String>? headers, Map<String, String>? headers,
Uint8List? inputData, Uint8List? inputData,
String? withIpAddress,
dynamic hint}) { dynamic hint}) {
return handler.executeNormal(NormalTask( return handler.executeNormal(NormalTask(
callFfi: (port_) { callFfi: (port_) {
@ -130,6 +159,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
sse_encode_String(url, serializer); sse_encode_String(url, serializer);
sse_encode_opt_Map_String_String(headers, serializer); sse_encode_opt_Map_String_String(headers, serializer);
sse_encode_opt_list_prim_u_8_strict(inputData, serializer); sse_encode_opt_list_prim_u_8_strict(inputData, serializer);
sse_encode_opt_String(withIpAddress, serializer);
pdeCallFfi(generalizedFrbRustBinding, serializer, pdeCallFfi(generalizedFrbRustBinding, serializer,
funcId: 2, port: port_); funcId: 2, port: port_);
}, },
@ -138,7 +168,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
decodeErrorData: sse_decode_AnyhowException, decodeErrorData: sse_decode_AnyhowException,
), ),
constMeta: kFetchConstMeta, constMeta: kFetchConstMeta,
argValues: [method, url, headers, inputData], argValues: [method, url, headers, inputData, withIpAddress],
apiImpl: this, apiImpl: this,
hint: hint, hint: hint,
)); ));
@ -146,7 +176,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
TaskConstMeta get kFetchConstMeta => const TaskConstMeta( TaskConstMeta get kFetchConstMeta => const TaskConstMeta(
debugName: "fetch", debugName: "fetch",
argNames: ["method", "url", "headers", "inputData"], argNames: ["method", "url", "headers", "inputData", "withIpAddress"],
); );
@override @override
@ -188,7 +218,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
sse_encode_list_String(arguments, serializer); sse_encode_list_String(arguments, serializer);
sse_encode_String(workingDirectory, serializer); sse_encode_String(workingDirectory, serializer);
pdeCallFfi(generalizedFrbRustBinding, serializer, pdeCallFfi(generalizedFrbRustBinding, serializer,
funcId: 4, port: port_); funcId: 5, port: port_);
}, },
codec: SseCodec( codec: SseCodec(
decodeSuccessData: sse_decode_String, decodeSuccessData: sse_decode_String,
@ -273,6 +303,12 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
return raw == null ? null : dco_decode_Map_String_String(raw); return raw == null ? null : dco_decode_Map_String_String(raw);
} }
@protected
String? dco_decode_opt_String(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
return raw == null ? null : dco_decode_String(raw);
}
@protected @protected
int? dco_decode_opt_box_autoadd_u_64(dynamic raw) { int? dco_decode_opt_box_autoadd_u_64(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs // Codec=Dco (DartCObject based), see doc to use other codecs
@ -431,6 +467,17 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
} }
} }
@protected
String? sse_decode_opt_String(SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
if (sse_decode_bool(deserializer)) {
return (sse_decode_String(deserializer));
} else {
return null;
}
}
@protected @protected
int? sse_decode_opt_box_autoadd_u_64(SseDeserializer deserializer) { int? sse_decode_opt_box_autoadd_u_64(SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs // Codec=Sse (Serialization based), see doc to use other codecs
@ -595,6 +642,16 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
} }
} }
@protected
void sse_encode_opt_String(String? self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
sse_encode_bool(self != null, serializer);
if (self != null) {
sse_encode_String(self, serializer);
}
}
@protected @protected
void sse_encode_opt_box_autoadd_u_64(int? self, SseSerializer serializer) { void sse_encode_opt_box_autoadd_u_64(int? self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs // Codec=Sse (Serialization based), see doc to use other codecs

View File

@ -53,6 +53,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected @protected
Map<String, String>? dco_decode_opt_Map_String_String(dynamic raw); Map<String, String>? dco_decode_opt_Map_String_String(dynamic raw);
@protected
String? dco_decode_opt_String(dynamic raw);
@protected @protected
int? dco_decode_opt_box_autoadd_u_64(dynamic raw); int? dco_decode_opt_box_autoadd_u_64(dynamic raw);
@ -113,6 +116,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
Map<String, String>? sse_decode_opt_Map_String_String( Map<String, String>? sse_decode_opt_Map_String_String(
SseDeserializer deserializer); SseDeserializer deserializer);
@protected
String? sse_decode_opt_String(SseDeserializer deserializer);
@protected @protected
int? sse_decode_opt_box_autoadd_u_64(SseDeserializer deserializer); int? sse_decode_opt_box_autoadd_u_64(SseDeserializer deserializer);
@ -179,6 +185,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
void sse_encode_opt_Map_String_String( void sse_encode_opt_Map_String_String(
Map<String, String>? self, SseSerializer serializer); Map<String, String>? self, SseSerializer serializer);
@protected
void sse_encode_opt_String(String? self, SseSerializer serializer);
@protected @protected
void sse_encode_opt_box_autoadd_u_64(int? self, SseSerializer serializer); void sse_encode_opt_box_autoadd_u_64(int? self, SseSerializer serializer);

View File

@ -52,6 +52,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected @protected
Map<String, String>? dco_decode_opt_Map_String_String(dynamic raw); Map<String, String>? dco_decode_opt_Map_String_String(dynamic raw);
@protected
String? dco_decode_opt_String(dynamic raw);
@protected @protected
int? dco_decode_opt_box_autoadd_u_64(dynamic raw); int? dco_decode_opt_box_autoadd_u_64(dynamic raw);
@ -112,6 +115,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
Map<String, String>? sse_decode_opt_Map_String_String( Map<String, String>? sse_decode_opt_Map_String_String(
SseDeserializer deserializer); SseDeserializer deserializer);
@protected
String? sse_decode_opt_String(SseDeserializer deserializer);
@protected @protected
int? sse_decode_opt_box_autoadd_u_64(SseDeserializer deserializer); int? sse_decode_opt_box_autoadd_u_64(SseDeserializer deserializer);
@ -178,6 +184,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
void sse_encode_opt_Map_String_String( void sse_encode_opt_Map_String_String(
Map<String, String>? self, SseSerializer serializer); Map<String, String>? self, SseSerializer serializer);
@protected
void sse_encode_opt_String(String? self, SseSerializer serializer);
@protected @protected
void sse_encode_opt_box_autoadd_u_64(int? self, SseSerializer serializer); void sse_encode_opt_box_autoadd_u_64(int? self, SseSerializer serializer);

View File

@ -12,6 +12,7 @@ enum MyHttpVersion {
http11, http11,
http2, http2,
http3, http3,
httpUnknown,
} }
class RustHttpResponse { class RustHttpResponse {

View File

@ -22,3 +22,5 @@ reqwest = { version = "0.11", features = ["rustls-tls-native-roots", "cookies",
hickory-resolver = {version = "0.24.0"} hickory-resolver = {version = "0.24.0"}
anyhow = "1.0" anyhow = "1.0"
win32job = "2" win32job = "2"
lazy_static = "1.4.0"
scopeguard = "1.2.0"

View File

@ -36,10 +36,14 @@ pub fn set_default_header(headers: HashMap<String, String>) {
pub async fn fetch(method: MyMethod, pub async fn fetch(method: MyMethod,
url: String, url: String,
headers: Option<HashMap<String, String>>, headers: Option<HashMap<String, String>>,
input_data: Option<Vec<u8>>) -> anyhow::Result<RustHttpResponse> { input_data: Option<Vec<u8>>, with_ip_address: Option<String>) -> anyhow::Result<RustHttpResponse> {
http_package::fetch(_my_method_to_hyper_method(method), url, headers, input_data).await http_package::fetch(_my_method_to_hyper_method(method), url, headers, input_data, with_ip_address).await
} }
pub async fn dns_lookup_txt(host: String) -> anyhow::Result<Vec<String>> { pub async fn dns_lookup_txt(host: String) -> anyhow::Result<Vec<String>> {
http_package::dns_lookup_txt(host).await http_package::dns_lookup_txt(host).await
}
pub async fn dns_lookup_ips(host: String) -> anyhow::Result<Vec<String>> {
http_package::dns_lookup_ips(host).await
} }

View File

@ -38,6 +38,39 @@ flutter_rust_bridge::frb_generated_default_handler!();
// Section: wire_funcs // Section: wire_funcs
fn wire_dns_lookup_ips_impl(
port_: flutter_rust_bridge::for_generated::MessagePort,
ptr_: flutter_rust_bridge::for_generated::PlatformGeneralizedUint8ListPtr,
rust_vec_len_: i32,
data_len_: i32,
) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_async::<flutter_rust_bridge::for_generated::SseCodec, _, _, _>(
flutter_rust_bridge::for_generated::TaskInfo {
debug_name: "dns_lookup_ips",
port: Some(port_),
mode: flutter_rust_bridge::for_generated::FfiCallMode::Normal,
},
move || {
let message = unsafe {
flutter_rust_bridge::for_generated::Dart2RustMessageSse::from_wire(
ptr_,
rust_vec_len_,
data_len_,
)
};
let mut deserializer =
flutter_rust_bridge::for_generated::SseDeserializer::new(message);
let api_host = <String>::sse_decode(&mut deserializer);
deserializer.end();
move |context| async move {
transform_result_sse(
(move || async move { crate::api::http_api::dns_lookup_ips(api_host).await })()
.await,
)
}
},
)
}
fn wire_dns_lookup_txt_impl( fn wire_dns_lookup_txt_impl(
port_: flutter_rust_bridge::for_generated::MessagePort, port_: flutter_rust_bridge::for_generated::MessagePort,
ptr_: flutter_rust_bridge::for_generated::PlatformGeneralizedUint8ListPtr, ptr_: flutter_rust_bridge::for_generated::PlatformGeneralizedUint8ListPtr,
@ -98,6 +131,7 @@ fn wire_fetch_impl(
let api_headers = let api_headers =
<Option<std::collections::HashMap<String, String>>>::sse_decode(&mut deserializer); <Option<std::collections::HashMap<String, String>>>::sse_decode(&mut deserializer);
let api_input_data = <Option<Vec<u8>>>::sse_decode(&mut deserializer); let api_input_data = <Option<Vec<u8>>>::sse_decode(&mut deserializer);
let api_with_ip_address = <Option<String>>::sse_decode(&mut deserializer);
deserializer.end(); deserializer.end();
move |context| async move { move |context| async move {
transform_result_sse( transform_result_sse(
@ -107,6 +141,7 @@ fn wire_fetch_impl(
api_url, api_url,
api_headers, api_headers,
api_input_data, api_input_data,
api_with_ip_address,
) )
.await .await
})() })()
@ -275,6 +310,7 @@ impl SseDecode for crate::http_package::MyHttpVersion {
2 => crate::http_package::MyHttpVersion::HTTP_11, 2 => crate::http_package::MyHttpVersion::HTTP_11,
3 => crate::http_package::MyHttpVersion::HTTP_2, 3 => crate::http_package::MyHttpVersion::HTTP_2,
4 => crate::http_package::MyHttpVersion::HTTP_3, 4 => crate::http_package::MyHttpVersion::HTTP_3,
5 => crate::http_package::MyHttpVersion::HTTP_UNKNOWN,
_ => unreachable!("Invalid variant for MyHttpVersion: {}", inner), _ => unreachable!("Invalid variant for MyHttpVersion: {}", inner),
}; };
} }
@ -312,6 +348,17 @@ impl SseDecode for Option<std::collections::HashMap<String, String>> {
} }
} }
impl SseDecode for Option<String> {
// 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(<String>::sse_decode(deserializer));
} else {
return None;
}
}
}
impl SseDecode for Option<u64> { impl SseDecode for Option<u64> {
// Codec=Sse (Serialization based), see doc to use other codecs // Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
@ -407,10 +454,11 @@ fn pde_ffi_dispatcher_primary_impl(
) { ) {
// Codec=Pde (Serialization + dispatch), see doc to use other codecs // Codec=Pde (Serialization + dispatch), see doc to use other codecs
match func_id { match func_id {
4 => wire_dns_lookup_ips_impl(port, ptr, rust_vec_len, data_len),
3 => wire_dns_lookup_txt_impl(port, ptr, rust_vec_len, data_len), 3 => wire_dns_lookup_txt_impl(port, ptr, rust_vec_len, data_len),
2 => wire_fetch_impl(port, ptr, rust_vec_len, data_len), 2 => wire_fetch_impl(port, ptr, rust_vec_len, data_len),
1 => wire_set_default_header_impl(port, ptr, rust_vec_len, data_len), 1 => wire_set_default_header_impl(port, ptr, rust_vec_len, data_len),
4 => wire_start_process_impl(port, ptr, rust_vec_len, data_len), 5 => wire_start_process_impl(port, ptr, rust_vec_len, data_len),
_ => unreachable!(), _ => unreachable!(),
} }
} }
@ -438,6 +486,7 @@ impl flutter_rust_bridge::IntoDart for crate::http_package::MyHttpVersion {
Self::HTTP_11 => 2.into_dart(), Self::HTTP_11 => 2.into_dart(),
Self::HTTP_2 => 3.into_dart(), Self::HTTP_2 => 3.into_dart(),
Self::HTTP_3 => 4.into_dart(), Self::HTTP_3 => 4.into_dart(),
Self::HTTP_UNKNOWN => 5.into_dart(),
} }
} }
} }
@ -574,6 +623,7 @@ impl SseEncode for crate::http_package::MyHttpVersion {
crate::http_package::MyHttpVersion::HTTP_11 => 2, crate::http_package::MyHttpVersion::HTTP_11 => 2,
crate::http_package::MyHttpVersion::HTTP_2 => 3, crate::http_package::MyHttpVersion::HTTP_2 => 3,
crate::http_package::MyHttpVersion::HTTP_3 => 4, crate::http_package::MyHttpVersion::HTTP_3 => 4,
crate::http_package::MyHttpVersion::HTTP_UNKNOWN => 5,
_ => { _ => {
unimplemented!(""); unimplemented!("");
} }
@ -616,6 +666,16 @@ impl SseEncode for Option<std::collections::HashMap<String, String>> {
} }
} }
impl SseEncode for Option<String> {
// 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 {
<String>::sse_encode(value, serializer);
}
}
}
impl SseEncode for Option<u64> { impl SseEncode for Option<u64> {
// Codec=Sse (Serialization based), see doc to use other codecs // Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {

View File

@ -1,11 +1,17 @@
use std::io; use hickory_resolver::config::{NameServerConfigGroup, ResolverConfig, ResolverOpts};
use hickory_resolver::{lookup_ip::LookupIpIntoIter, TokioAsyncResolver}; use hickory_resolver::{lookup_ip::LookupIpIntoIter, TokioAsyncResolver};
use hyper::client::connect::dns::Name; use hyper::client::connect::dns::Name;
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use reqwest::dns::{Addrs, Resolve, Resolving}; use reqwest::dns::{Addrs, Resolve, Resolving};
use std::collections::HashMap;
use std::io;
use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::sync::Arc; use std::sync::{Arc, RwLock};
use hickory_resolver::config::{NameServerConfigGroup, ResolverConfig, ResolverOpts}; use lazy_static::lazy_static;
lazy_static! {
pub static ref MY_HOSTS_MAP: RwLock<HashMap<String, IpAddr>> = RwLock::from(HashMap::new());
}
/// Wrapper around an `AsyncResolver`, which implements the `Resolve` trait. /// Wrapper around an `AsyncResolver`, which implements the `Resolve` trait.
#[derive(Debug, Default, Clone)] #[derive(Debug, Default, Clone)]
@ -22,6 +28,14 @@ struct SocketAddrs {
impl Resolve for MyHickoryDnsResolver { impl Resolve for MyHickoryDnsResolver {
fn resolve(&self, name: Name) -> Resolving { fn resolve(&self, name: Name) -> Resolving {
let my_hosts = MY_HOSTS_MAP.read().unwrap();
let name_str = name.to_string();
if let Some(ip) = my_hosts.get(name_str.as_str()) {
let addrs: Addrs = Box::new(std::iter::once(SocketAddr::new(*ip, 0)));
println!("using host map === {:?}", name_str);
return Box::pin(async move { Ok(addrs) });
}
let resolver = self.clone(); let resolver = self.clone();
Box::pin(async move { Box::pin(async move {
let resolver = resolver.state.get_or_try_init(new_resolver)?; let resolver = resolver.state.get_or_try_init(new_resolver)?;
@ -38,11 +52,21 @@ impl MyHickoryDnsResolver {
pub(crate) async fn lookup_txt(&self, name: String) -> anyhow::Result<Vec<String>> { pub(crate) async fn lookup_txt(&self, name: String) -> anyhow::Result<Vec<String>> {
let resolver = self.state.get_or_try_init(new_resolver)?; let resolver = self.state.get_or_try_init(new_resolver)?;
let txt = resolver.txt_lookup(name).await?; let txt = resolver.txt_lookup(name).await?;
let t = txt.iter() let t = txt
.iter()
.map(|rdata| rdata.to_string()) .map(|rdata| rdata.to_string())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
Ok(t) Ok(t)
} }
pub(crate) async fn lookup_ips(&self, name: String) -> anyhow::Result<Vec<String>> {
let resolver = self.state.get_or_try_init(new_resolver)?;
let ips = resolver.ipv4_lookup(name).await?;
let t = ips
.iter()
.map(|ip| ip.to_string())
.collect::<Vec<_>>();
Ok(t)
}
} }
impl Iterator for SocketAddrs { impl Iterator for SocketAddrs {
@ -58,14 +82,15 @@ fn new_resolver() -> io::Result<TokioAsyncResolver> {
&[ &[
IpAddr::V4(Ipv4Addr::new(119, 29, 29, 29)), IpAddr::V4(Ipv4Addr::new(119, 29, 29, 29)),
IpAddr::V4(Ipv4Addr::new(223, 6, 6, 6)), IpAddr::V4(Ipv4Addr::new(223, 6, 6, 6)),
IpAddr::V4(Ipv4Addr::new(1, 1, 1, 1)),
IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)),
IpAddr::V4(Ipv4Addr::new(180, 76, 76, 76)), IpAddr::V4(Ipv4Addr::new(180, 76, 76, 76)),
IpAddr::V4(Ipv4Addr::new(1, 2, 4, 8)), IpAddr::V4(Ipv4Addr::new(1, 2, 4, 8)),
IpAddr::V4(Ipv4Addr::new(166, 111, 8, 28)), IpAddr::V4(Ipv4Addr::new(166, 111, 8, 28)),
IpAddr::V4(Ipv4Addr::new(101, 226, 4, 6)), IpAddr::V4(Ipv4Addr::new(101, 226, 4, 6)),
IpAddr::V4(Ipv4Addr::new(114, 114, 114, 114)), IpAddr::V4(Ipv4Addr::new(114, 114, 114, 114)),
IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)), ], 53, true,
IpAddr::V4(Ipv4Addr::new(1, 1, 1, 1)), );
], 53, true);
let cfg = ResolverConfig::from_parts(None, vec![], group); let cfg = ResolverConfig::from_parts(None, vec![], group);
let mut opts = ResolverOpts::default(); let mut opts = ResolverOpts::default();
opts.edns0 = true; opts.edns0 = true;

View File

@ -1,16 +1,27 @@
pub mod dns; pub mod dns;
use scopeguard::defer;
use lazy_static::lazy_static;
use hyper::Uri;
use reqwest::header::{HeaderMap, HeaderName, HeaderValue}; use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
use reqwest::{Method, RequestBuilder}; use reqwest::{Method, RequestBuilder};
use std::collections::HashMap; use std::collections::HashMap;
use std::str::FromStr; use std::str::FromStr;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use std::time::Duration; use std::time::Duration;
use flutter_rust_bridge::for_generated::lazy_static;
#[derive(Debug)]
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
pub enum MyHttpVersion { HTTP_09, HTTP_10, HTTP_11, HTTP_2, HTTP_3 } pub enum MyHttpVersion {
HTTP_09,
HTTP_10,
HTTP_11,
HTTP_2,
HTTP_3,
HTTP_UNKNOWN,
}
#[derive(Debug)]
pub struct RustHttpResponse { pub struct RustHttpResponse {
pub status_code: u16, pub status_code: u16,
pub headers: HashMap<String, String>, pub headers: HashMap<String, String>,
@ -23,28 +34,34 @@ pub struct RustHttpResponse {
fn _hyper_version_to_my_version(v: reqwest::Version) -> MyHttpVersion { fn _hyper_version_to_my_version(v: reqwest::Version) -> MyHttpVersion {
match v { match v {
reqwest::Version::HTTP_09 => { MyHttpVersion::HTTP_09 } reqwest::Version::HTTP_09 => MyHttpVersion::HTTP_09,
reqwest::Version::HTTP_10 => { MyHttpVersion::HTTP_10 } reqwest::Version::HTTP_10 => MyHttpVersion::HTTP_10,
reqwest::Version::HTTP_11 => { MyHttpVersion::HTTP_11 } reqwest::Version::HTTP_11 => MyHttpVersion::HTTP_11,
reqwest::Version::HTTP_2 => { MyHttpVersion::HTTP_2 } reqwest::Version::HTTP_2 => MyHttpVersion::HTTP_2,
reqwest::Version::HTTP_3 => { MyHttpVersion::HTTP_3 } reqwest::Version::HTTP_3 => MyHttpVersion::HTTP_3,
_ => { panic!("Unsupported HTTP version") } _ => MyHttpVersion::HTTP_UNKNOWN,
} }
} }
lazy_static! { lazy_static! {
static ref DEFAULT_HEADER: RwLock<HeaderMap> = RwLock::from(HeaderMap::new()); static ref DEFAULT_HEADER: RwLock<HeaderMap> = RwLock::from(HeaderMap::new());
static ref DNS_CLIENT : Arc<dns::MyHickoryDnsResolver> = Arc::from(dns::MyHickoryDnsResolver::default()); static ref DNS_CLIENT: Arc<dns::MyHickoryDnsResolver> = Arc::from(dns::MyHickoryDnsResolver::default());
static ref HTTP_CLIENT: reqwest::Client = { static ref HTTP_CLIENT: reqwest::Client = new_http_client(true);
reqwest::Client::builder() }
.dns_resolver(DNS_CLIENT.clone())
.use_rustls_tls() fn new_http_client(keep_alive: bool) -> reqwest::Client {
.connect_timeout(Duration::from_secs(10)) let mut c = reqwest::Client::builder()
.gzip(true) .dns_resolver(DNS_CLIENT.clone())
.no_proxy() .use_rustls_tls()
.build() .connect_timeout(Duration::from_secs(10))
.unwrap() .gzip(true)
}; .no_proxy();
if !keep_alive {
c = c.tcp_keepalive(None);
} else {
c = c.tcp_keepalive(Duration::from_secs(120));
}
c.build().unwrap()
} }
pub fn set_default_header(headers: HashMap<String, String>) { pub fn set_default_header(headers: HashMap<String, String>) {
@ -63,13 +80,34 @@ pub async fn fetch(
url: String, url: String,
headers: Option<HashMap<String, String>>, headers: Option<HashMap<String, String>>,
input_data: Option<Vec<u8>>, input_data: Option<Vec<u8>>,
with_ip_address: Option<String>,
) -> anyhow::Result<RustHttpResponse> { ) -> anyhow::Result<RustHttpResponse> {
let mut req = _mix_header(HTTP_CLIENT.request(method, url), headers); 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 mut hosts = dns::MY_HOSTS_MAP.write().unwrap();
let url_host = Uri::from_str(url.as_str()).unwrap().host().unwrap().to_string();
hosts.insert(url_host, addr);
}
defer! {
if address_clone.is_some() {
let mut hosts = dns::MY_HOSTS_MAP.write().unwrap();
hosts.remove(url.clone().as_str());
}
}
let mut req = if address_clone.is_some() {
_mix_header(new_http_client(false).request(method, url_clone), headers)
} else {
_mix_header(HTTP_CLIENT.request(method, url_clone), headers)
};
if input_data.is_some() { if input_data.is_some() {
req = req.body(input_data.unwrap()); req = req.body(input_data.unwrap());
} }
let resp = req.send().await?; let resp = req.send().await?;
let url = resp.url().to_string(); let url = resp.url().to_string();
let status_code = resp.status().as_u16(); let status_code = resp.status().as_u16();
let resp_headers = _reade_resp_header(resp.headers()); let resp_headers = _reade_resp_header(resp.headers());
@ -104,6 +142,10 @@ pub async fn dns_lookup_txt(name: String) -> anyhow::Result<Vec<String>> {
DNS_CLIENT.lookup_txt(name).await DNS_CLIENT.lookup_txt(name).await
} }
pub async fn dns_lookup_ips(name: String) -> anyhow::Result<Vec<String>> {
DNS_CLIENT.lookup_ips(name).await
}
fn _reade_resp_header(r_header: &HeaderMap) -> HashMap<String, String> { fn _reade_resp_header(r_header: &HeaderMap) -> HashMap<String, String> {
let mut resp_headers = HashMap::new(); let mut resp_headers = HashMap::new();
for ele in r_header { for ele in r_header {