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. 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.

Upvotes: 0

Views: 1168

Answers (1)

L. Sportelli
L. Sportelli

Reputation: 26

A TUN interface sends and receives IP packets. This means that the data you give to iface.send must be an IP packet in order to be delivered. 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.

In order to send and receive data from a tun interface you'll have to build an IP packet.

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. On this layer (TCP) is where the concept of "connection" appears.

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, i.e. "GET http://192.168.0.53:8000/foo?bar=898 HTTP/1.1".

Upvotes: 1

Related Questions