简体   繁体   English

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

[英]How to send a HTTP Request using a tun tap interface

I am working on a network proxy project and a newbie to this field.我正在从事网络代理项目和该领域的新手。 I want to create a tun-tap interface and send an HTTP request through this interface.我想创建一个tun-tap接口并通过这个接口发送一个 HTTP 请求。 Here is my approach.这是我的方法。

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();
}

But my development server is not receiving any request.但是我的开发服务器没有收到任何请求。 And not displaying any error.并且不显示任何错误。

A TUN interface sends and receives IP packets. TUN 接口发送和接收 IP 数据包。 This means that the data you give to iface.send must be an IP packet in order to be delivered.这意味着您提供给iface.send的数据必须是 IP 数据包才能传送。 You can see in your code you're not indicating what server you are connecting to because at this layer connections "don't even exist".你可以在你的代码中看到你没有指出你要连接到哪个服务器,因为在这一层连接“甚至不存在”。 The IP in the HTTP request happens to be there because HTTP protocol says so, but you must already be connected to the server when you send this information. HTTP 请求中的 IP 恰好在那里,因为 HTTP 协议是这样说的,但是当您发送此信息时,您必须已经连接到服务器。

In order to send and receive data from a tun interface you'll have to build an IP packet.为了从 tun 接口发送和接收数据,您必须构建一个 IP 数据包。

Once you can send and receive IP packets, you'll have to implement the TCP protocol on top of that to be able to open a connection to an HTTP server.一旦您可以发送和接收 IP 数据包,您就必须在此基础上实现 TCP 协议才能打开与 HTTP 服务器的连接。 On this layer (TCP) is where the concept of "connection" appears.在这一层 (TCP) 上出现了“连接”的概念。

Once you can open and send/receive data over a TCP connection, you'll have to implement the HTTP protocol to be able to talk to the HTTP server, ie "GET http://192.168.0.53:8000/foo?bar=898 HTTP/1.1" .一旦您可以通过 TCP 连接打开和发送/接收数据,您就必须实现 HTTP 协议才能与 HTTP 服务器通信,即"GET http://192.168.0.53:8000/foo?bar=898 HTTP/1.1"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM