“golang.org/x/net/ipv4” 在 Mac 上工作,但在 Linux 上不工作。

huangapple go评论94阅读模式
英文:

"golang.org/x/net/ipv4" working on Mac, but not on Linux

问题

我正在使用"golang.org/x/net/ipv4"来使用其SetTTL函数。不幸的是,在Linux上它似乎不起作用,只能在Mac上使用,尽管文档指出Linux支持所有这些函数。

以下是一个最小化的示例,包括Dockerfile:

main.go:

package main

import (
  "fmt"
  "net"
  "bufio"
  xnet "golang.org/x/net/ipv4"
)

const Host = "google.com"

func main() {
  var err error
  conn, err := net.Dial("tcp4", Host + ":80")
  if err != nil {
    panic(err)
  }
  defer conn.Close()
  xconn := xnet.NewConn(conn)
  err = xconn.SetTTL(5)
  if err != nil {
    panic(err)
  }
  defer xconn.Close()
  fmt.Fprint(conn, "GET / HTTP/1.1\r\nHOST: google.com\r\n\r\n")
  firstLine, err := bufio.NewReader(xconn).ReadString('\n')
  if err != nil {
    panic(err)
  }
  fmt.Println(firstLine)
}

Dockerfile:

FROM golang:1.8.1-alpine

RUN apk --no-cache add git
RUN go get golang.org/x/net/ipv4
COPY . /go/src/me.com/me/xnetproblem
RUN go install me.com/me/xnetproblem
CMD ["/go/bin/xnetproblem"]

我运行了以下命令:

docker build -t xnet .

我得到了以下输出:

john xnetproblem > docker build -t xnet .
Sending build context to Docker daemon 90.62 kB
Step 1/6 : FROM golang:1.8.1-alpine
[snip]
Step 5/6 : RUN go install me.com/me/xnetproblem
 ---> Running in c3802fe61d63
# me.com/me/xnetproblem
src/me.com/me/xnetproblem/main.go:25: xconn.Close undefined (type *ipv4.Conn has no field or method Close)
src/me.com/me/xnetproblem/main.go:28: cannot use xconn (type *ipv4.Conn) as type io.Reader in argument to bufio.NewReader:
    *ipv4.Conn does not implement io.Reader (missing Read method)
The command '/bin/sh -c go install me.com/me/xnetproblem' returned a non-zero code: 2

在本地使用go install而不是Docker时,该程序在Mac上工作,但在Linux上不工作。

英文:

I'm using "golang.org/x/net/ipv4" in order to use its SetTTL function. Unfortunately it does not seem to work on Linux, only on Mac, even though the documentation indicates Linux supports all the functions.

Here's a minimal example of the problem, with a Dockerfile:

main.go:

package main

import (
  "fmt"
  "net"
  "bufio"
  xnet "golang.org/x/net/ipv4"
)

const Host = "google.com"

func main() {
  var err error
  conn, err := net.Dial("tcp4", Host + ":80")
  if err != nil {
    panic(err)
  }
  defer conn.Close()
  xconn := xnet.NewConn(conn)
  err = xconn.SetTTL(5)
  if err != nil {
    panic(err)
  }
  defer xconn.Close()
  fmt.Fprint(conn, "GET / HTTP/1.1\r\nHOST: google.com\r\n\r\n")
  firstLine, err := bufio.NewReader(xconn).ReadString('\n')
  if err != nil {
    panic(err)
  }
  fmt.Println(firstLine)
}

Dockerfile:

FROM golang:1.8.1-alpine

RUN apk --no-cache add git
RUN go get golang.org/x/net/ipv4
COPY . /go/src/me.com/me/xnetproblem
RUN go install me.com/me/xnetproblem
CMD ["/go/bin/xnetproblem"]

I run this command:

docker build -t xnet .

I get this output:

john xnetproblem > docker build -t xnet .
Sending build context to Docker daemon 90.62 kB
Step 1/6 : FROM golang:1.8.1-alpine
[snip]
Step 5/6 : RUN go install me.com/me/xnetproblem
 ---> Running in c3802fe61d63
# me.com/me/xnetproblem
src/me.com/me/xnetproblem/main.go:25: xconn.Close undefined (type *ipv4.Conn has no field or method Close)
src/me.com/me/xnetproblem/main.go:28: cannot use xconn (type *ipv4.Conn) as type io.Reader in argument to bufio.NewReader:
    *ipv4.Conn does not implement io.Reader (missing Read method)
The command '/bin/sh -c go install me.com/me/xnetproblem' returned a non-zero code: 2

Using go install natively, instead of Docker, the program works on Mac but not on Linux.

答案1

得分: 0

感谢@JimB的评论,我意识到我的Mac上安装了一个旧版本的ipv4包。更新后,我能够修复代码。

这是一个完整可工作的版本:

package main

import (
  "fmt"
  "net"
  "bufio"
  "golang.org/x/net/ipv4"
)

const Host = "google.com"

func main() {
  var err error
  conn, err := net.Dial("tcp4", Host + ":80")
  if err != nil {
    panic(err)
  }
  defer conn.Close()

  if err = ipv4.NewConn(conn).SetTTL(5); err != nil {
    panic(err)
  }

  fmt.Fprint(conn, fmt.Sprintf("GET / HTTP/1.1\r\nHost: %v\r\n\r\n", Host))
  firstLine, err := bufio.NewReader(conn).ReadString('\n')
  if err != nil {
    panic(err)
  }
  fmt.Println(firstLine)
}

以下是输出结果:

HTTP/1.1 301 Moved Permanently
英文:

Thanks to @JimB's comment I realized my Mac had an old version of the ipv4 package installed. After updating I was able to fix the code.

Here's a complete working version:

package main

import (
  "fmt"
  "net"
  "bufio"
  "golang.org/x/net/ipv4"
)

const Host = "google.com"

func main() {
  var err error
  conn, err := net.Dial("tcp4", Host + ":80")
  if err != nil {
    panic(err)
  }
  defer conn.Close()

  if err = ipv4.NewConn(conn).SetTTL(5); err != nil {
	  panic(err)
  }

  fmt.Fprint(conn, fmt.Sprintf("GET / HTTP/1.1\r\nHost: %v\r\n\r\n", Host))
  firstLine, err := bufio.NewReader(conn).ReadString('\n')
  if err != nil {
    panic(err)
  }
  fmt.Println(firstLine)
}

Here is the output:

HTTP/1.1 301 Moved Permanently

huangapple
  • 本文由 发表于 2017年6月28日 05:47:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/44790504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定