Home  >  Article  >  Backend Development  >  golang set dns

golang set dns

WBOY
WBOYOriginal
2023-05-05 22:29:081320browse

Golang is an open source programming language for modern programming languages ​​popular for its memory safety and high concurrency capabilities. In Golang, setting up DNS is also a common need, and the process is not that simple. This article will be based on Golang language and introduce you how to set DNS in the program.

1. Basic knowledge of DNS

Domain Name System (DNS) is a service of the Internet. As a distributed database that maps domain names and IP addresses to each other, it can Make it easier for people to access the Internet. Compared with IP addresses, domain names are easier to remember and more intuitive, which greatly improves people's usage efficiency.

When a computer user enters a domain name in a browser or other Internet application, the user's computer will first send a request to the local domain name server. If the local domain name server does not have mapping information for the domain name, it will send a request to the international domain name server. DNS is queried and the corresponding IP address is finally returned.

2. DNS setting method in Golang

In Golang, you can use the ResolveIPAddr method in the net package to perform DNS address resolution . ResolveIPAddr The function receives a network type and an address string and returns the IP address. Here is a basic example program that uses DNS resolution to resolve a domain name into an address:

package main

import (
    "fmt"
    "net"
)

func main() {
    ip, err := net.ResolveIPAddr("ip", "www.google.com")
    if err != nil {
        fmt.Println("解析域名失败!", err)
        return
    }
    fmt.Println("Google 的 IP 地址是:", ip)
}

When you run the above code, you can see Google's IP address information in the output.

However, in some cases, we need to manually set the DNS address to achieve finer control. DNS can be set using the Dialer type in the net package. The following is a sample code for setting a local DNS address:

package main

import (
    "fmt"
    "net"
    "net/http"
    "time"
)

func main() {
    // 创建一个新的 Dialer
    d := &net.Dialer{
        Timeout:   30 * time.Second, // 连接超时时间
        KeepAlive: 30 * time.Second, // 保持连接
        DualStack: true,             // 支持 IPv4 和 IPv6
    }

    // 设置 DNS
    resolver := &net.Resolver{
        PreferGo: true,
        Dial:     d.Dial,
    }

    // 将代理设置为 http.Transport 中的 Dial 函数
    transport := &http.Transport{
        DialContext: (&net.Dialer{
            Timeout:   30 * time.Second, // 连接超时时间
            KeepAlive: 30 * time.Second, // 保持连接
            DualStack: true,             // 支持 IPv4 和 IPv6
            Resolver:  resolver,         // 使用新设置的解析器
        }).DialContext,
        TLSHandshakeTimeout: 10 * time.Second, // TLS 握手超时时间
    }

    // 设置 http 客户端
    client := &http.Client{
        Timeout:   time.Second * 60, // 超时时间
        Transport: transport,        // 使用新设置的 transport
    }

    // 访问一个带有 DNS 规则的网站
    req, err := http.NewRequest(http.MethodGet, "http://www.google.com", nil)
    if err != nil {
        fmt.Println(err)
        return
    }
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println(err)
        return
    }
    defer resp.Body.Close()

    body, err := io.ReadAll(resp.Body)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(string(body))
}

In the above code, we use the net.Dialer type to set some parameters of the connection, including timeout and supported protocol types . Use the net.Resolver type to set some parameters of the DNS, including giving priority to using Go's DNS resolver, using net.Dialer to connect, etc., and pass it to ##Resolver property in #net.Dialer. Use the second DialContext function in http.Transport to set up the new parser. Finally, use the new Transport in the http client.

3. Summary

This article introduces the basic method of setting up DNS in Golang. By using the

net package we can parse the domain name and try to convert it to an IP address. At the same time, in order to better control the function of the program, we can also use the two types net.Dialer and http.Transport for more precise control. Of course, in actual development, we may need to combine specific needs and environments and adopt corresponding setting methods for development.

The above is the detailed content of golang set dns. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:golang comment errorNext article:golang comment error