Go library for working with check-host.net, providing an easy interface for performing network checks: Ping, HTTP, TCP, UDP, DNS, and node information via their API.
go get github.com/d1manpro/checkhostYou can create a client with default settings, which uses an http.Client with a 10-second timeout:
package main
import (
"fmt"
"github.com/d1manpro/checkhost"
)
func main() {
client := checkhost.New()
fmt.Println(client)
}Or create a client with a custom HTTP client:
package main
import (
"fmt"
"net/http"
"time"
"github.com/d1manpro/checkhost"
)
func main() {
client := checkhost.New(
checkhost.WithHTTPClient(&http.Client{Timeout: 15 * time.Second}),
)
fmt.Println(client)
}Used to pass request parameters:
type RequestData struct {
Host string // Domain or IP to check
MaxNodes int // Maximum number of nodes to use (ignored if Nodes is set)
Nodes []string // Specific nodes to use
}Behavior:
- If
Nodesis specified, the check will use only these nodes. - If
Nodesis empty butMaxNodesis set, the check will use up toMaxNodesnodes. - If neither
NodesnorMaxNodesis set, the check defaults to using 3 nodes.
Common fields in a check response:
type CheckResponse struct {
OK int `json:"ok"`
RequestID string `json:"request_id"`
PermanentLink string `json:"permanent_link"`
Nodes map[string][]string `json:"nodes"`
}Error returned when waiting for a result times out.
resp, err := client.CheckPing(checkhost.RequestData{Host: "example.com"})
result, err := client.WaitPingResult(resp.RequestID, 30*time.Second, 2*time.Second)CheckPing— returnsCheckPingResponsewithRequestID.WaitPingResult— returnsPingResult, a map of node names toPingResultItem.
type PingResultItem struct {
Status string // Check status, e.g. "ok"
Time float64 // Response time
IP string // Target IP address
}resp, err := client.CheckHTTP(checkhost.RequestData{Host: "example.com"})
result, err := client.WaitHTTPResult(resp.RequestID, 30*time.Second, 2*time.Second)CheckHTTP— returnsCheckHTTPResponsewithRequestID.WaitHTTPResult— returnsHTTPResult, a map of node names toHTTPResultItem.
type HTTPResultItem struct {
Success bool // true if the check succeeded
Time float64 // Response time
Message string // Error or additional message
Code string // HTTP status code
IP string // Target IP address
}tcpResp, err := client.CheckTCP(checkhost.RequestData{Host: "example.com"})
tcpResult, err := client.WaitTCPResult(tcpResp.RequestID, 30*time.Second, 2*time.Second)CheckTCP— returnsCheckTCPResponsewithRequestID.WaitTCPResult— returnsTCPResult, a map of node names toTCPResultItem.
type TCPResultItem struct {
Time float64 // Connection time
IP string // Target IP
Error string // Error message
}udpResp, err := client.CheckUDP(checkhost.RequestData{Host: "example.com"})
udpResult, err := client.WaitUDPResult(udpResp.RequestID, 30*time.Second, 2*time.Second)CheckUDP— returnsCheckUDPResponsewithRequestID.WaitUDPResult— returnsUDPResult, a map of node names toUDPResultItem.
type UDPResultItem struct {
Time float64 // Response time
IP string // Target IP
Error string // Error message
}resp, err := client.CheckDNS(checkhost.RequestData{Host: "example.com"})
result, err := client.WaitDNSResult(resp.RequestID, 30*time.Second, 2*time.Second)CheckDNS— returnsCheckDNSResponsewithRequestID.WaitDNSResult— returnsDNSResult, a map of node names toDNSResultItem.
type DNSResultItem struct {
A []string // IPv4 addresses
AAAA []string // IPv6 addresses
TTL *int // TTL of the record
Error string // Error message
}nodes, err := client.GetNodes() // Detailed node information
ips, err := client.GetNodesIPs() // Only node IPstype Node struct {
ASN string // Node provider ASN
IP string // Node IP
Location []string // Node geolocation
}Developed by @d1manpro. Licensed under MIT License.