Golang 网络编程(三):tun 网络编程
source link: https://pandaychen.github.io/2023/08/30/A-GOLANG-TUN-NETWORK-DEV/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
0x00 前言
0x01 基础知识
iobased or fdbased
启动 tun 网卡的方式如何选型?fdbased 和 iobased 的区别主要体现在数据的传输方式上
当使用 fdbased 方式启动 tun 网卡时,tun 网卡会被创建为一个虚拟的网络接口,数据通过文件系统进行读写,即数据传输通过文件的方式进行。这种方式的优点是简单易用,不需要特殊的设备或驱动程序,适用于小型应用程序。但是,由于数据传输需要经过文件系统,因此可能会影响数据传输的速度
当使用 iobased 方式启动 tun 网卡时,tun 网卡会被创建为一个虚拟的网络接口,数据通过输入 / 输出方式进行读写,即数据直接传输到设备或驱动程序中。这种方式的优点是数据传输速度快,适用于大型应用程序。但是,由于需要特殊的设备或驱动程序支持,因此相对来说比较复杂
小结一下,当需要处理大量数据或需要更快的速度时,建议使用 iobased 方式启动 tun 网卡;当数据量较小或需要更简单的管理和备份时,建议使用 fdbased 方式启动 tun 网卡
0x02 tun And 转发代理原理
- 从 Tun 读取 IP 数据包,写进
gVisor
网络栈 gVisor
网络栈上注册HandlePacket
函数用于处理 TCP/UDP/ICMP 等,将gonet.Conn
和gonet.PacketConn
传给 proto 里实现的gonet.Handler
处理- 双向流 copy
0x03 TUN gvisor 开发基础
netstack 开发示例
服务端代码参考:tun_tcp_echo 客户端代码参考:tun_tcp_connect
gvisor 库开发示例
相比于 netstack,gvisor 稍微有些不同,如下:
服务端代码参考:tun_tcp_echo 客户端代码参考:tun_tcp_connect
0x04 配置基础
以 tun2socks 为例
1、关闭虚拟网卡tun0
ip link set tun0 down #将tun0网卡设为下线状态,停止其网络连接
2、删除虚拟网卡tun0
ip link delete tun0 #该命令将删除tun0网卡,彻底关闭其网络连接。请注意,执行该命令后,tun0网卡的配置信息将被永久删除,无法恢复
删除指定的路由
下述静态路由,通过route del -net 192.168.10.0 netmask 255.255.255.0 dev eth0
指令进行删除:
192.168.10.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
0x05 TUN with DNS
0x0 参考
Related Issues not found
Please contact @pandaychen to initialize the comment
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK