// Command nostrino-probe asks a gateway one question: can I open a tunnel to you, and what does // your node say? It is the cheapest liveness check that exercises the whole path a wallet uses, // so a monitor can tell "publishing announcements" from "actually serving". package main import ( "context" "encoding/hex" "encoding/json" "errors" "flag" "fmt" "net" "os" "time" "git.smesh.lol/nostrino/pkg/chain" "git.smesh.lol/nostrino/pkg/p2p" "git.smesh.lol/nostrino/pkg/tunnel" ) type probe struct { OK bool `json:"ok"` Chain string `json:"chain"` Gateway string `json:"gateway"` ElapsedMS int64 `json:"elapsed_ms"` Error string `json:"error,omitempty"` Protocol int32 `json:"protocol,omitempty"` Services uint64 `json:"services,omitempty"` Tip int32 `json:"tip,omitempty"` UserAgent string `json:"user_agent,omitempty"` } func main() { var ( relays string gateway string chainName = "blake" transport = "btc" timeout = 20 * time.Second ) flag.StringVar(&relays, "relay", "", "relay URL") flag.StringVar(&gateway, "gateway", "", "gateway pubkey (hex)") flag.StringVar(&chainName, "chain", chainName, "chain to ask the gateway for") flag.StringVar(&transport, "transport", transport, "profile that supplies the P2P magic") flag.DurationVar(&timeout, "timeout", timeout, "overall timeout") flag.Parse() out := probe{Chain: chainName, Gateway: gateway} started := time.Now() fail := func(err error) { out.ElapsedMS = time.Since(started).Milliseconds() out.Error = err.Error() body, _ := json.Marshal(out) fmt.Println(string(body)) os.Exit(1) } profile, err := chain.ByName(transport) if err != nil { fail(err) } if len(gateway) != 64 { fail(errors.New("a 64-hex gateway pubkey is required")) } var key [32]byte if _, err := hex.DecodeString(gateway); err != nil { fail(err) } raw, _ := hex.DecodeString(gateway) copy(key[:], raw) list := splitList(relays) if len(list) == 0 { fail(errors.New("no relay")) } // A probe names its target, so it must dial that gateway and not whichever one discovery // happens to return first: asking a different gateway and printing the requested key is how a // monitor reports a dead gateway as healthy. uri := &tunnel.URI{Gateway: key, Relays: list, Version: 1} ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() conn, err := tunnel.Dial(ctx, tunnel.DialConfig{URI: uri, Key: key, Chain: chainName}) if err != nil { fail(fmt.Errorf("tunnel: %w", err)) } defer conn.Close() peer := p2p.New(conn, p2p.Config{ Magic: profile.Magic, UserAgent: "/nostrino-probe:0.2/", Services: 1 | 8, }) version, err := peer.Handshake(ctx) if err != nil { fail(fmt.Errorf("handshake: %w", err)) } out.OK = true out.ElapsedMS = time.Since(started).Milliseconds() out.Protocol = version.ProtocolVersion out.Services = version.Services out.Tip = version.LastBlock out.UserAgent = version.UserAgent body, _ := json.Marshal(out) fmt.Println(string(body)) } func splitList(value string) []string { var out []string for _, part := range stringSplit(value, ',') { if part = trim(part); part != "" { out = append(out, part) } } return out } func stringSplit(value string, sep byte) []string { var out []string current := make([]byte, 0, len(value)) for i := 0; i < len(value); i++ { if value[i] == sep { out = append(out, string(current)) current = current[:0] continue } current = append(current, value[i]) } return append(out, string(current)) } func trim(value string) string { for len(value) > 0 && (value[0] == ' ' || value[0] == '\t') { value = value[1:] } for len(value) > 0 && (value[len(value)-1] == ' ' || value[len(value)-1] == '\t') { value = value[:len(value)-1] } return value } var _ = net.Dial