2017-06-27 1 views
0

Ich benutze "golang.org/x/net/ipv4", um die SetTTL-Funktion zu verwenden. Leider scheint es auf Linux nicht zu funktionieren, nur auf Mac, obwohl die Dokumentation darauf hinweist, dass Linux alle Funktionen unterstützt."golang.org/x/net/ipv4" arbeitet am Mac, aber nicht unter Linux

Hier ist ein minimales Beispiel für das Problem, mit einem 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"] 

ich diesen Befehl ausführen:

docker build -t xnet . 

Ich bekomme th ausgegeben:

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 

Mit go install nativ, statt Docker, arbeitet das Programm auf dem Mac, aber nicht auf Linux.

+0

die CRLF in diesem String Sehen macht mich zurückschrecken ... – RayfenWindspear

+0

@RayfenWindspear: warum? Das ist die Spezifikation – JimB

+0

@JohnT. Kannst du sicherstellen, dass deine Version von Go und der Checkout von 'golang.org/x/net/ipv4' auf deinem Mac aktuell sind? Die Fehler, die Sie in Docker sehen, sind korrekt, der Code ist falsch. – JimB

Antwort

0

Dank @ JimBs Kommentar wurde mir klar, dass auf meinem Mac eine alte Version des ipv4 Pakets installiert war. Nach der Aktualisierung konnte ich den Code reparieren.

Hier ist eine komplette Arbeitsversion:

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

Hier ist die Ausgabe:

HTTP/1.1 301 Moved Permanently 
Verwandte Themen