I' using go1.11 net/http and want to decect if a domain is ipv6-only.
What did you do?
I create my own DialContext because want I to detect if a domain is ipv6-only. code below
package main
import (
"errors"
"fmt"
"net"
"net/http"
"syscall"
"time"
)
func ModifiedTransport() {
var MyTransport = &http.Transport{
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: false,
Control: func(network, address string, c syscall.RawConn) error {
if network == "ipv4" {
// I want to cancel connection here client.Get("http://myexample.com") return a non-nil err.
return errors.New("you should not use ipv4")
}
return nil
},
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
var myClient = http.Client{Transport: MyTransport}
resp, myerr := myClient.Get("http://www.github.com")
if myerr != nil {
fmt.Println("request error")
return
}
var buffer = make([]byte, 1000)
resp.Body.Read(buffer)
fmt.Println(string(buffer))
}
func main(){
ModifiedTransport();
}
I do not now how to close the request even when I can get into network == "ipv4"
.
addition
Python can solve the question via Force requests to use IPv4 / IPv6. I do not know how to do it in golang. Could someone one help me? Thanks so much!