netcheck: one module, a Go tools bundle and a TypeScript one (hq ADR 0188, 0193)
ADR 0193 says the node's runtime launches every served bundle over MCP stdio and knows no language, and ADR 0188 says one module may carry several bundles in any language. Nothing in the catalogue shows both at once: every tools bundle is TypeScript, and the only Go bundle is the runtime itself. netcheck is the smallest real module that does — read-only checks from a machine, worth having on their own: - tools-go (Go SDK go/v0.1.6): netcheck_tcp (one connect, nothing sent) and netcheck_dns (A/AAAA/CNAME/TXT/MX through the machine's resolver). - tools-typescript (@novox/mesh-sdk): netcheck_http (HEAD or GET, body neither sent nor read, redirects reported not followed, anything but http(s) refused). Both say loads; the module lists its tools. No container, no image, no env: nothing to be given, so the runtime's own words suffice.
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"net"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Bounds on what a caller may ask: a check is a probe, never a wait anyone can make long.
|
||||
const (
|
||||
DefaultTimeout = 3 * time.Second
|
||||
MostTimeout = 30 * time.Second
|
||||
)
|
||||
|
||||
// TCPResult is what netcheck_tcp answers.
|
||||
type TCPResult struct {
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
Address string `json:"address,omitempty"`
|
||||
Reachable bool `json:"reachable"`
|
||||
ElapsedMS int64 `json:"elapsed_ms"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// CheckTCP opens one TCP connection and closes it, sending nothing. A port that refuses or a host
|
||||
// that does not answer is a result, not a failure of the tool; only a malformed question is.
|
||||
func CheckTCP(host string, port int, timeout time.Duration) (TCPResult, error) {
|
||||
if port < 1 || port > 65535 {
|
||||
return TCPResult{}, fmt.Errorf("port %d is not a TCP port (1-65535)", port)
|
||||
}
|
||||
out := TCPResult{Host: host, Port: port}
|
||||
start := time.Now()
|
||||
conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, strconv.Itoa(port)), timeout)
|
||||
out.ElapsedMS = time.Since(start).Milliseconds()
|
||||
if err != nil {
|
||||
out.Error = err.Error()
|
||||
return out, nil
|
||||
}
|
||||
out.Address = conn.RemoteAddr().String()
|
||||
out.Reachable = true
|
||||
_ = conn.Close()
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// DNSResult is what netcheck_dns answers.
|
||||
type DNSResult struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Answers []string `json:"answers"`
|
||||
ElapsedMS int64 `json:"elapsed_ms"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// DNSTypes are the record types netcheck_dns looks up.
|
||||
var DNSTypes = []string{"A", "AAAA", "CNAME", "TXT", "MX"}
|
||||
|
||||
// CheckDNS looks a name up with the machine's resolver. Built without cgo, Go's own resolver reads
|
||||
// the machine's /etc/resolv.conf and /etc/hosts, which is the resolver this machine's programs use.
|
||||
// A name that does not resolve is a result with its error; an unknown type is refused.
|
||||
func CheckDNS(name, kind string, timeout time.Duration) (DNSResult, error) {
|
||||
kind = strings.ToUpper(strings.TrimSpace(kind))
|
||||
if kind == "" {
|
||||
kind = "A"
|
||||
}
|
||||
known := false
|
||||
for _, t := range DNSTypes {
|
||||
known = known || t == kind
|
||||
}
|
||||
if !known {
|
||||
return DNSResult{}, fmt.Errorf("type %q is not one netcheck_dns looks up (%s)", kind, strings.Join(DNSTypes, ", "))
|
||||
}
|
||||
out := DNSResult{Name: name, Type: kind, Answers: []string{}}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
r := net.DefaultResolver
|
||||
start := time.Now()
|
||||
var err error
|
||||
switch kind {
|
||||
case "A", "AAAA":
|
||||
network := "ip4"
|
||||
if kind == "AAAA" {
|
||||
network = "ip6"
|
||||
}
|
||||
var ips []net.IP
|
||||
if ips, err = r.LookupIP(ctx, network, name); err == nil {
|
||||
for _, ip := range ips {
|
||||
out.Answers = append(out.Answers, ip.String())
|
||||
}
|
||||
}
|
||||
case "CNAME":
|
||||
var cname string
|
||||
if cname, err = r.LookupCNAME(ctx, name); err == nil {
|
||||
out.Answers = append(out.Answers, cname)
|
||||
}
|
||||
case "TXT":
|
||||
var txts []string
|
||||
if txts, err = r.LookupTXT(ctx, name); err == nil {
|
||||
out.Answers = append(out.Answers, txts...)
|
||||
}
|
||||
case "MX":
|
||||
var mxs []*net.MX
|
||||
if mxs, err = r.LookupMX(ctx, name); err == nil {
|
||||
for _, mx := range mxs {
|
||||
out.Answers = append(out.Answers, fmt.Sprintf("%d %s", mx.Pref, mx.Host))
|
||||
}
|
||||
}
|
||||
}
|
||||
out.ElapsedMS = time.Since(start).Milliseconds()
|
||||
if err != nil {
|
||||
out.Error = err.Error()
|
||||
}
|
||||
if kind != "MX" {
|
||||
sort.Strings(out.Answers)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// text is a required string argument.
|
||||
func text(args map[string]any, key string) (string, error) {
|
||||
s, _ := args[key].(string)
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
return "", fmt.Errorf("%s is required", key)
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// whole is an integer argument, given as a JSON number or a numeric string; fallback when absent.
|
||||
func whole(args map[string]any, key string, fallback int) (int, error) {
|
||||
v, given := args[key]
|
||||
if !given || v == nil {
|
||||
if fallback == 0 {
|
||||
return 0, fmt.Errorf("%s is required", key)
|
||||
}
|
||||
return fallback, nil
|
||||
}
|
||||
switch n := v.(type) {
|
||||
case float64:
|
||||
if n != math.Trunc(n) {
|
||||
return 0, fmt.Errorf("%s must be a whole number, not %v", key, n)
|
||||
}
|
||||
return int(n), nil
|
||||
case string:
|
||||
i, err := strconv.Atoi(strings.TrimSpace(n))
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("%s must be a whole number, not %q", key, n)
|
||||
}
|
||||
return i, nil
|
||||
}
|
||||
return 0, errors.New(key + " must be a whole number")
|
||||
}
|
||||
|
||||
// timeoutOf is timeout_ms, defaulted and bounded.
|
||||
func timeoutOf(args map[string]any) (time.Duration, error) {
|
||||
ms, err := whole(args, "timeout_ms", int(DefaultTimeout/time.Millisecond))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if ms < 1 {
|
||||
return 0, fmt.Errorf("timeout_ms must be at least 1, not %d", ms)
|
||||
}
|
||||
d := time.Duration(ms) * time.Millisecond
|
||||
if d > MostTimeout {
|
||||
d = MostTimeout
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestATCPPortThatListensIsReachableAndOneThatDoesNotIsNot(t *testing.T) {
|
||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
port := l.Addr().(*net.TCPAddr).Port
|
||||
got, err := CheckTCP("127.0.0.1", port, time.Second)
|
||||
if err != nil || !got.Reachable || got.Error != "" {
|
||||
t.Fatalf("a listening port: %+v, %v", got, err)
|
||||
}
|
||||
l.Close()
|
||||
got, err = CheckTCP("127.0.0.1", port, time.Second)
|
||||
if err != nil || got.Reachable || got.Error == "" {
|
||||
t.Fatalf("a closed port is reported as a result with its error, not a failure: %+v, %v", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAPortOutsideTheRangeIsRefused(t *testing.T) {
|
||||
for _, p := range []int{0, -1, 65536} {
|
||||
if _, err := CheckTCP("127.0.0.1", p, time.Second); err == nil {
|
||||
t.Errorf("port %d was accepted", p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDNSAnswersFromTheMachinesResolverAndRefusesAnUnknownType(t *testing.T) {
|
||||
got, err := CheckDNS("localhost", "a", time.Second)
|
||||
if err != nil || got.Type != "A" || len(got.Answers) == 0 {
|
||||
t.Fatalf("localhost A: %+v, %v", got, err)
|
||||
}
|
||||
if _, err := CheckDNS("localhost", "SRV", time.Second); err == nil {
|
||||
t.Fatal("an unknown record type was accepted")
|
||||
}
|
||||
got, err = CheckDNS("no-such-name.invalid", "A", time.Second)
|
||||
if err != nil || got.Error == "" || len(got.Answers) != 0 {
|
||||
t.Fatalf("a name that does not resolve is a result with its error: %+v, %v", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimeoutIsDefaultedAndBounded(t *testing.T) {
|
||||
if d, _ := timeoutOf(map[string]any{}); d != DefaultTimeout {
|
||||
t.Errorf("default: %v", d)
|
||||
}
|
||||
if d, _ := timeoutOf(map[string]any{"timeout_ms": float64(10 * 60 * 1000)}); d != MostTimeout {
|
||||
t.Errorf("bounded: %v", d)
|
||||
}
|
||||
if _, err := timeoutOf(map[string]any{"timeout_ms": float64(0)}); err == nil {
|
||||
t.Error("a zero timeout was accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBothToolsAreListedUnprefixed(t *testing.T) {
|
||||
names := map[string]bool{}
|
||||
for _, tool := range tools() {
|
||||
names[tool.Name] = true
|
||||
}
|
||||
if !names["netcheck_tcp"] || !names["netcheck_dns"] || len(names) != 2 {
|
||||
t.Fatalf("tools: %v", names)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
// netcheck's Go tools bundle (novox/hq ADR 0188, ADR 0193): a process the node's runtime launches
|
||||
// and speaks MCP over stdio to, through the Go SDK. It serves the two checks that are the machine's
|
||||
// own sockets and resolver — a TCP connect and a DNS lookup — and nothing that changes anything.
|
||||
// The module's HTTP check is its TypeScript bundle; the runtime serves both under one module.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// An empty name serves as the module the runtime names (MESH_SERVED_MODULE): netcheck.
|
||||
if err := stdio.Serve("", tools()); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func tools() []stdio.Tool {
|
||||
return []stdio.Tool{
|
||||
{
|
||||
Name: "netcheck_tcp",
|
||||
Description: "Check whether a TCP port is reachable from this machine: opens one connection " +
|
||||
"and closes it at once, sending nothing. Answers reachable, elapsed_ms and the error when not.",
|
||||
Input: map[string]any{
|
||||
"host": map[string]any{"type": "string", "description": "host name or IP address"},
|
||||
"port": map[string]any{"type": "integer", "description": "TCP port, 1-65535"},
|
||||
"timeout_ms": map[string]any{"type": "integer", "description": "give up after this long (default 3000, at most 30000)"},
|
||||
},
|
||||
Run: func(args map[string]any) (any, error) {
|
||||
host, err := text(args, "host")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
port, err := whole(args, "port", 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
timeout, err := timeoutOf(args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return CheckTCP(host, port, timeout)
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "netcheck_dns",
|
||||
Description: "Look a name up with this machine's resolver (its /etc/resolv.conf and /etc/hosts). " +
|
||||
"type is A, AAAA, CNAME, TXT or MX; answers the records found, or the error.",
|
||||
Input: map[string]any{
|
||||
"name": map[string]any{"type": "string", "description": "the name to look up"},
|
||||
"type": map[string]any{"type": "string", "enum": []string{"A", "AAAA", "CNAME", "TXT", "MX"}, "description": "record type (default A)"},
|
||||
"timeout_ms": map[string]any{"type": "integer", "description": "give up after this long (default 3000, at most 30000)"},
|
||||
},
|
||||
Run: func(args map[string]any) (any, error) {
|
||||
name, err := text(args, "name")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
kind, _ := args["type"].(string)
|
||||
timeout, err := timeoutOf(args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return CheckDNS(name, kind, timeout)
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user