"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to force IPv4 or IPv6 connections in Go\'s net/http client?

How to force IPv4 or IPv6 connections in Go\'s net/http client?

Published on 2024-11-12
Browse:882

How to force IPv4 or IPv6 connections in Go\'s net/http client?

Forcing IPv4 or IPv6 in net/http Client

In Go 1.11, the net/http package provides a DialContext function that allows you to intercept and control the outgoing network connection. This function can be used to force the client to use either IPv4 or IPv6.

IPv4-Only Connections

To restrict connections to IPv4 addresses, modify the DualStack field of the Dialer to false and define a Control function that returns an error for network type "ipv4":

type MyTransport struct {
    http.RoundTripper
}

func (t *MyTransport) RoundTrip(req *http.Request) (*http.Response, error) {
    return http.DefaultTransport.RoundTrip(req)
}

func main() {
    MyTransport := &MyTransport{
        DialContext: (&net.Dialer{
            DualStack: false,
            Control: func(network, address string, c syscall.RawConn) error {
                if network == "ipv4" {
                    return errors.New("you should not use ipv4")
                }
                return nil
            },
        }).DialContext,
    }

    client := http.Client{Transport: MyTransport}

    _, err := client.Get("http://www.github.com")
    if err != nil {
        fmt.Println(err)
    }
}

This modification intercepts network connections and returns an error if an IPv4 connection is attempted.

Note: The network value passed to the Control function can be "tcp4" for IPv4 and "tcp6" for IPv6, even if "tcp" was passed to the DialContext function.

IPv6-Only Connections

To force IPv6-only connections, set DualStack to false and return an error for network type "ipv6":

type MyTransport struct {
    http.RoundTripper
}

func (t *MyTransport) RoundTrip(req *http.Request) (*http.Response, error) {
    return http.DefaultTransport.RoundTrip(req)
}

func main() {
    MyTransport := &MyTransport{
        DialContext: (&net.Dialer{
            DualStack: false,
            Control: func(network, address string, c syscall.RawConn) error {
                if network == "ipv6" {
                    return errors.New("you should not use ipv6")
                }
                return nil
            },
        }).DialContext,
    }

    client := http.Client{Transport: MyTransport}

    _, err := client.Get("http://www.github.com")
    if err != nil {
        fmt.Println(err)
    }
}
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3