From e03c3623025d1f59d73321d9905541a8f2ffbeae Mon Sep 17 00:00:00 2001 From: "xinhui.li" Date: Sat, 4 Mar 2023 20:44:07 -0800 Subject: [PATCH] try if trust-dns-resolver works or not --- examples/http_headers/Cargo.toml | 3 +++ examples/http_headers/src/lib.rs | 46 ++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/examples/http_headers/Cargo.toml b/examples/http_headers/Cargo.toml index 02afd242..91310af1 100644 --- a/examples/http_headers/Cargo.toml +++ b/examples/http_headers/Cargo.toml @@ -13,6 +13,9 @@ crate-type = ["cdylib"] [dependencies] log = "0.4" proxy-wasm = { path = "../../" } +trust-dns-resolver = "0.22.0" +tokio = "1.26.0" +hostname = "^0.3" [profile.release] lto = true diff --git a/examples/http_headers/src/lib.rs b/examples/http_headers/src/lib.rs index 315a7b88..38099de6 100644 --- a/examples/http_headers/src/lib.rs +++ b/examples/http_headers/src/lib.rs @@ -12,6 +12,37 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::net::*; +use trust_dns_resolver::Resolver; +use trust_dns_resolver::config::*; + +use std::{ + net::*, + str::FromStr, + sync::{Arc, Mutex as StdMutex}, +}; + +use tokio::runtime::Runtime; + +use trust_dns_proto::{ + op::{NoopMessageFinalizer, Query}, + rr::{DNSClass, Name, RData, Record, RecordType}, + xfer::{DnsExchange, DnsMultiplexer, DnsResponse}, + TokioTime, +}; +use trust_dns_resolver::{ + caching_client::CachingClient, + config::LookupIpStrategy, + error::ResolveError, + lookup::{Lookup, LookupFuture}, + lookup_ip::LookupIpFuture, + Hosts, +}; +use trust_dns_server::{ + authority::{Authority, Catalog}, + store::in_memory::InMemoryAuthority, +}; + use log::info; use proxy_wasm::traits::*; use proxy_wasm::types::*; @@ -43,10 +74,25 @@ impl Context for HttpHeaders {} impl HttpContext for HttpHeaders { fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action { + + let lookup = LookupFuture::lookup( + vec![Name::from_str("www.example.com.").unwrap()], + RecordType::A, + Default::default(), + CachingClient::new(0, client, false), + ); + + let io_loop = Runtime::new().unwrap(); + let lookup = io_loop.block_on(lookup).unwrap(); + + self.set_http_request_header("wasm-request-header-xh", lookup); + for (name, value) in &self.get_http_request_headers() { info!("#{} -> {}: {}", self.context_id, name, value); } + + match self.get_http_request_header(":path") { Some(path) if path == "/hello" => { self.send_http_response(