Multicast only finds peers on the same LAN. Reach off-LAN boxes by probing them directly over unicast (works over any routable address; Tailscale is the easy, secure choice): - discovery.Probe: unicast POST /register (https->http fallback) with a two-way handshake, so send and receive both work; offline peers age out. - internal/tailscale: Peers() shells `tailscale status --json` for online peers. - config.KnownPeers: persisted manual remotes. - main.go: watchRemotes goroutine probes knownPeers ∪ tailscale peers every 10s, in both the normal TUI path and quick-send. - TUI: `+` on Devices opens an add-remote modal (host/IP/Tailscale name). - install.sh: interactive local/remote install prompt; remote mode locks port 53317 to the Tailscale interface (ufw), with container/userspace-networking detection. README documents remote devices. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
64 lines
2.2 KiB
Go
64 lines
2.2 KiB
Go
package discovery
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"omarchy-send/internal/protocol"
|
|
)
|
|
|
|
// TestProbeRegistersPeer drives Probe against a stub /register that behaves like
|
|
// a real peer: it records the caller and returns its own DeviceInfo. The https
|
|
// attempt fails against the plain-http test server and Probe falls back to http.
|
|
func TestProbeRegistersPeer(t *testing.T) {
|
|
peerInfo := protocol.DeviceInfo{Alias: "Remote", Fingerprint: "remote-fp", Port: 53317, Protocol: "http"}
|
|
var sawOurInfo bool
|
|
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/api/localsend/v2/register", func(w http.ResponseWriter, r *http.Request) {
|
|
var in protocol.DeviceInfo
|
|
if err := json.NewDecoder(r.Body).Decode(&in); err == nil && in.Fingerprint == "self-fp" {
|
|
sawOurInfo = true
|
|
}
|
|
_ = json.NewEncoder(w).Encode(peerInfo)
|
|
})
|
|
srv := httptest.NewServer(mux)
|
|
defer srv.Close()
|
|
host := strings.TrimPrefix(srv.URL, "http://") // host:port
|
|
|
|
d := New(protocol.DeviceInfo{Fingerprint: "self-fp", Alias: "Self"})
|
|
if err := d.Probe(context.Background(), host); err != nil {
|
|
t.Fatalf("Probe failed: %v", err)
|
|
}
|
|
if !sawOurInfo {
|
|
t.Error("peer did not receive our device info in the register body")
|
|
}
|
|
peers := d.Snapshot()
|
|
if len(peers) != 1 || peers[0].Info.Fingerprint != "remote-fp" {
|
|
t.Fatalf("peer not recorded as expected: %+v", peers)
|
|
}
|
|
if wantIP := strings.Split(host, ":")[0]; peers[0].IP != wantIP {
|
|
t.Errorf("peer IP = %q, want %q (the host we dialed)", peers[0].IP, wantIP)
|
|
}
|
|
}
|
|
|
|
func TestProbeUnreachableErrors(t *testing.T) {
|
|
d := New(protocol.DeviceInfo{Fingerprint: "self-fp"})
|
|
// 127.0.0.1:1 — nothing listening; both https and http should fail fast.
|
|
if err := d.Probe(context.Background(), "127.0.0.1:1"); err == nil {
|
|
t.Fatal("expected an error probing an unreachable host")
|
|
}
|
|
}
|
|
|
|
func TestHostPortDefaults(t *testing.T) {
|
|
if h, p := hostPort("colossus"); h != "colossus" || p != protocol.DefaultPort {
|
|
t.Errorf("hostPort(bare) = %q,%d; want colossus,%d", h, p, protocol.DefaultPort)
|
|
}
|
|
if h, p := hostPort("100.64.0.2:9999"); h != "100.64.0.2" || p != 9999 {
|
|
t.Errorf("hostPort(host:port) = %q,%d; want 100.64.0.2,9999", h, p)
|
|
}
|
|
}
|