This commit is contained in:
2025-08-14 20:34:21 +02:00
parent 855af93974
commit ced153a180
3 changed files with 37 additions and 37 deletions
+2
View File
@@ -1 +1,3 @@
pub mod tls;
pub fn request(gateway: &str) {} pub fn request(gateway: &str) {}
+9 -36
View File
@@ -1,43 +1,16 @@
// Cargo.toml dependencies: use std::io::{Read, Write};
// rustls = "0.31"
// rustls-native-certs = "0.8"
use rustls::ClientConfig; use ghostnet_rs::tls;
use rustls::Stream; use rustls::Stream;
use rustls::client::ClientConnection;
use rustls::server::CertificateType;
use rustls_native_certs;
use std::{
io::{Read, Write},
net::TcpStream,
sync::Arc,
};
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() {
// Load native certificates let (mut conn, mut tcp) = tls::mask_tls("github.com").unwrap();
let certs = rustls_native_certs::load_native_certs()
.expect("could not load platform certificate store");
let mut root_store = rustls::RootCertStore::empty();
for cert in certs {
root_store.add(cert).unwrap();
}
// Build TLS client config
let config = ClientConfig::builder()
// .with_safe_defaults()
.with_root_certificates(root_store)
.with_no_client_auth();
let arc_cfg = Arc::new(config);
let server_name = "example.com".try_into()?;
let mut conn = ClientConnection::new(arc_cfg, server_name)?;
let mut tcp = TcpStream::connect("example.com:443")?;
let mut tls = Stream::new(&mut conn, &mut tcp); let mut tls = Stream::new(&mut conn, &mut tcp);
tls.write_all(b"GET / HTTP/1.0\r\nHost: example.com\r\n\r\n")?; tls.write_all("GET / HTTP/1.0\r\nHost: github.com\r\n\r\n".as_bytes())
let mut resp = Vec::new(); .unwrap();
tls.read_to_end(&mut resp)?;
println!("{}", String::from_utf8_lossy(&resp));
Ok(()) let mut resp = Vec::new();
tls.read_to_end(&mut resp).unwrap();
println!("{}", String::from_utf8_lossy(&resp));
} }
+25
View File
@@ -0,0 +1,25 @@
use rustls::ClientConfig;
use rustls::client::ClientConnection;
use rustls_native_certs;
use std::{net::TcpStream, sync::Arc};
pub fn mask_tls<'a>(
addr: &str,
) -> Result<(ClientConnection, TcpStream), Box<dyn std::error::Error>> {
let certs = rustls_native_certs::load_native_certs()
.expect("could not load platform certificate store");
let mut root_store = rustls::RootCertStore::empty();
for cert in certs {
root_store.add(cert).unwrap();
}
// Build TLS client config
let config = ClientConfig::builder()
.with_root_certificates(root_store)
.with_no_client_auth();
let arc_cfg = Arc::new(config);
let conn = ClientConnection::new(arc_cfg, addr.to_owned().try_into()?)?;
let tcp = TcpStream::connect(addr.to_owned() + ":443")?;
Ok((conn, tcp))
}