我购买了一个http代理(支持http隧道),但没有rust示例代码,我尝试使用crate surf,但没有找到代理方式,我必须自己实现。以下是我的代码:
use async_std::task::block_on;
use std::error::Error;
use std::result::Result;
use futures::{AsyncWriteExt, AsyncReadExt};
use async_tls::TlsConnector;
use async_std::net::TcpStream;
const PROXY: &str = "200.200.200.200:8000"; // this is proxy / http tunnel / example IP
async fn http_get_with_proxy(url: &str, proxy: &str) -> Result<String, Box<dyn Error>> {
// 1. make proxy to build connection to target host
let mut stream = TcpStream::connect(proxy).await?;
let r = stream.write_all(format!("CONNECT www.domain.com:443 HTTP/1.1\r\n\r\n").as_bytes()).await?;
// 2. start SSL handshake process, resuse TCP stream.
let connector = TlsConnector::default();
let mut tls_stream = connector.connect("www.example.com", stream).await?;
// 3. send data with SSL protocal to proxy, proxy will forward data to target.
let r = tls_stream.write_all(format!("GET https://www.example.com/ HTTP/1.1\r\n\r\n").as_bytes()).await?;
let mut buf = String::new();
// 4. recv data from proxy.
let r = tls_stream.read_to_string(&mut buf).await;
Ok(buf)
}
fn main() {
let r = block_on( http_get_with_proxy("https://www.example.com/", PROXY));
dbg!(r);
}
得到错误:
[src/main.rs:35] r = Err(
Custom {
kind: InvalidData,
error: CorruptMessage,
},
)
我不知道出了什么问题。