Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
80 lines
2.6 KiB
Go
80 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// cmdAddress asks the running node on this machine for the addresses someone
|
|
// else can use to join the network through it, and prints them with the
|
|
// command to use. It is how a friend joins when the usual bootstrap name is
|
|
// down, or to start a network of one's own.
|
|
func cmdAddress(args []string) error {
|
|
fs := flag.NewFlagSet("address", flag.ContinueOnError)
|
|
httpAddr := fs.String("http", "127.0.0.1:8080", "HTTP address of the running node")
|
|
if err := fs.Parse(args); err != nil {
|
|
return err
|
|
}
|
|
share, err := fetchShare(context.Background(), *httpAddr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
out, ok := formatShare(share)
|
|
fmt.Print(out)
|
|
if !ok {
|
|
return fmt.Errorf("no address to share yet")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func fetchShare(ctx context.Context, httpAddr string) ([]string, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
defer cancel()
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://"+httpAddr+"/api/v1/status", nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("no node answers at %s (is it running? use --http if it listens elsewhere): %w", httpAddr, err)
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("the node at %s answered %s", httpAddr, resp.Status)
|
|
}
|
|
var st struct {
|
|
Share []string `json:"share"`
|
|
}
|
|
if err := json.NewDecoder(io.LimitReader(resp.Body, 1<<20)).Decode(&st); err != nil {
|
|
return nil, fmt.Errorf("unreadable answer from %s: %w", httpAddr, err)
|
|
}
|
|
return st.Share, nil
|
|
}
|
|
|
|
// formatShare words the answer. ok is false when there is nothing to share.
|
|
func formatShare(share []string) (string, bool) {
|
|
if len(share) == 0 {
|
|
return "This node has no address that others can reach yet.\n" +
|
|
"A node behind a router needs a few seconds to learn its public address or to\n" +
|
|
"take a relay; try again in a minute. If it stays empty, the node has no\n" +
|
|
"public address and no relay: run it with --public and --announce, see k8s/README.md.\n", false
|
|
}
|
|
var b strings.Builder
|
|
b.WriteString("Someone who wants to join through this node starts theirs with:\n\n")
|
|
fmt.Fprintf(&b, " allmende run --bootstrap %s\n", share[0])
|
|
if len(share) > 1 {
|
|
b.WriteString("\nOther addresses of this node (any one is enough):\n")
|
|
for _, a := range share[1:] {
|
|
fmt.Fprintf(&b, " %s\n", a)
|
|
}
|
|
}
|
|
b.WriteString("\nThe address works while this node runs; a node behind a router is reached\nthrough its relay, which can change.\n")
|
|
return b.String(), true
|
|
}
|