如何使用 tun tap 接口发送 HTTP 请求



我正在从事一个网络代理项目,也是这个领域的新手。我想创建一个 tun-tap 接口并通过该接口发送 HTTP 请求。这是我的方法。

use tun_tap::Iface;
use tun_tap::Mode;
use std::process:Command;
fn cmd(cmd: &str, args: &[&str]) {
let ecode = Command::new(cmd)
.args(args)
.spawn()
.unwrap()
.wait()
.unwrap();
assert!(ecode.success(), "Failed to execte {}", cmd);
}
fn main() {
let iface = Iface::new("tun1",Mode::Tun).unwrap();
cmd("ip", &["addr", "add", "dev", 'tun1', '192.168.0.54/24']);
cmd("ip", &["link", "set", "up", "dev", 'tun1']);    
// 192.168.0.53:8000 is my development server created by python3 -m http.server command
let sent = iface.send(b"GET http://192.168.0.53:8000/foo?bar=898 HTTP/1.1").unwrap();
}

但是我的开发服务器没有收到任何请求。并且不显示任何错误。

TUN 接口发送和接收 IP 数据包。这意味着您提供给iface.send的数据必须是 IP 数据包才能传送。您可以在代码中看到,您没有指示要连接到哪个服务器,因为在此层连接"甚至不存在"。HTTP 请求中的 IP 恰好在那里,因为 HTTP 协议是这样说的,但在发送此信息时,您必须已经连接到服务器。

为了从 tun 接口发送和接收数据,您必须构建一个 IP 数据包。

一旦您可以发送和接收IP数据包,您必须在此基础上实现TCP协议才能打开与HTTP服务器的连接。在这一层(TCP(上,出现了"连接"的概念。

一旦您可以通过TCP连接打开和发送/接收数据,就必须实现HTTP协议才能与HTTP服务器通信,即"GET http://192.168.0.53:8000/foo?bar=898 HTTP/1.1".

最新更新