Thanks to visit codestin.com
Credit goes to github.com

Skip to content

d1manpro/checkhost

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CheckHost GoLang Library

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.


Installation

go get github.com/d1manpro/checkhost

Client Initialization

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

Main Types

RequestData

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 Nodes is specified, the check will use only these nodes.
  • If Nodes is empty but MaxNodes is set, the check will use up to MaxNodes nodes.
  • If neither Nodes nor MaxNodes is set, the check defaults to using 3 nodes.

CheckResponse

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"`
}

ErrTimeout

Error returned when waiting for a result times out.


Ping Check

resp, err := client.CheckPing(checkhost.RequestData{Host: "example.com"})
result, err := client.WaitPingResult(resp.RequestID, 30*time.Second, 2*time.Second)
  • CheckPing — returns CheckPingResponse with RequestID.
  • WaitPingResult — returns PingResult, a map of node names to PingResultItem.

PingResultItem

type PingResultItem struct {
    Status string  // Check status, e.g. "ok"
    Time   float64 // Response time
    IP     string  // Target IP address
}

HTTP Check

resp, err := client.CheckHTTP(checkhost.RequestData{Host: "example.com"})
result, err := client.WaitHTTPResult(resp.RequestID, 30*time.Second, 2*time.Second)
  • CheckHTTP — returns CheckHTTPResponse with RequestID.
  • WaitHTTPResult — returns HTTPResult, a map of node names to HTTPResultItem.

HTTPResultItem

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
}

TCP Check

tcpResp, err := client.CheckTCP(checkhost.RequestData{Host: "example.com"})
tcpResult, err := client.WaitTCPResult(tcpResp.RequestID, 30*time.Second, 2*time.Second)
  • CheckTCP — returns CheckTCPResponse with RequestID.
  • WaitTCPResult — returns TCPResult, a map of node names to TCPResultItem.

TCPResultItem

type TCPResultItem struct {
    Time  float64 // Connection time
    IP    string  // Target IP
    Error string  // Error message
}

UDP Check

udpResp, err := client.CheckUDP(checkhost.RequestData{Host: "example.com"})
udpResult, err := client.WaitUDPResult(udpResp.RequestID, 30*time.Second, 2*time.Second)
  • CheckUDP — returns CheckUDPResponse with RequestID.
  • WaitUDPResult — returns UDPResult, a map of node names to UDPResultItem.

UDPResultItem

type UDPResultItem struct {
    Time  float64 // Response time
    IP    string  // Target IP
    Error string  // Error message
}

DNS Check

resp, err := client.CheckDNS(checkhost.RequestData{Host: "example.com"})
result, err := client.WaitDNSResult(resp.RequestID, 30*time.Second, 2*time.Second)
  • CheckDNS — returns CheckDNSResponse with RequestID.
  • WaitDNSResult — returns DNSResult, a map of node names to DNSResultItem.

DNSResultItem

type DNSResultItem struct {
    A     []string // IPv4 addresses
    AAAA  []string // IPv6 addresses
    TTL   *int     // TTL of the record
    Error string   // Error message
}

Nodes Information

nodes, err := client.GetNodes()   // Detailed node information
ips, err := client.GetNodesIPs()  // Only node IPs

Node

type Node struct {
    ASN      string   // Node provider ASN
    IP       string   // Node IP
    Location []string // Node geolocation
}

🧑‍💻 Author

Developed by @d1manpro. Licensed under MIT License.