linux - Go 中的服务器没有监听 tun0

标签 linux go tun

我正在运行一个简单的标准 Go TCP 服务器,我想将它绑定(bind)到我的 tun0 接口(interface)。

服务器是:

func main() {
    conn, err := net.Listen("tcp", "10.1.0.21:4444")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer conn.Close()

    fmt.Println("Running on 10.1.0.21:4444")
    c, err := conn.Accept()
    if err != nil {
        fmt.Println(err)
        return
    }

    for {
        data, err := bufio.NewReader(c).ReadString('\n')
        if err != nil {
            fmt.Println(err)
            return
        }

        fmt.Print("Got ", data)
        myTime := "Hello"
        c.Write([]byte(myTime))
    }
}

作为 root,要创建 tun0,我运行:
ip tuntap add mode tun dev tun0
ip addr add 10.1.0.20/24 dev tun0
ip link set dev tun0 up

在收听 10.1.0.21 时,服务器无法运行:

listen tcp 10.1.0.21:4444: bind: cannot assign requested address



它可以收听10.1.0.20 ,但 Wireshark(以 root 身份)不会拦截 tun0 上的任何数据包,而我正在使用 netcat ( nc ) 向它发送数据包并且服务器成功响应。

为什么绑定(bind)不了10.1.0.21 ?为什么 Wireshark 在监听 10.1.0.20 时无法拦截任何流量?

最佳答案

确保选择环回接口(interface)来捕获该流量,因为流量是本地的。另见:https://www.naturalborncoder.com/virtualization/2014/10/17/understanding-tun-tap-interfaces/

还有您关于绑定(bind)到 10.1.0.21 的其他问题已在上面的评论中回答。

关于linux - Go 中的服务器没有监听 tun0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60691907/

相关文章:

c++ - 尝试在 Eclipse 中运行程序时出现 "Error opening terminal: unknown."

linux - 在Dockerfile中使用sudo(Alpine)

unit-testing - 如何在向 API 发送 http 请求时进行模拟

linux-kernel - 如何在 Rust 中调用 "ioctl"?并连接 Linux "tun"驱动程序

linux - 是否有作为 TUN 接口(interface)运行的用户空间 TCP 堆栈?

linux - 如何在shell脚本中创建一个文件

linux - 在 IDA 中使用参数调试可执行文件

linux - 错误消息 "go: go.mod file not found in current directory or any parent directory; see ' go help modules'"

html - golang html模板格式不正确