Discover LAN servers and relay authenticated browser peer connections
This commit is contained in:
+10
@@ -1265,3 +1265,13 @@ has a 20-second context, bounded Git output/time and file traversal, and at most
|
||||
100 deletion candidates. Partial scans cannot claim that the storage target has
|
||||
been reached. Cleanup refusals and limits remain visible rather than discarding
|
||||
protected data to satisfy a quota.
|
||||
|
||||
|
||||
### LAN server discovery
|
||||
|
||||
`{"type":"discover_servers"}` returns `discovered_servers` with a
|
||||
`discovered_servers` array of `{name,endpoint,auth,version}` records. The server
|
||||
browses `_pipelines._tcp.local` on its own LAN, with a bounded DNS-SD query; it
|
||||
does not scan arbitrary ports. An unavailable browse returns a protocol error.
|
||||
The authenticated web front door exposes the same result at `GET /api/discovery`,
|
||||
including before the browser has opened its application websocket.
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
# Browser discovery and simultaneous server connections
|
||||
|
||||
The server adds a bounded `discover_servers` RPC and local `/discovery` HTTP
|
||||
handler using its existing mDNS dependency. Advertisements are validated, sorted
|
||||
and deduplicated; only valid Pipelines `/connect` records are returned. The web
|
||||
front door exposes this as authenticated `GET /api/discovery`. This searches the
|
||||
hosting machine's LAN, which can differ from the browser's current network.
|
||||
|
||||
For browser federation, authenticated same-origin `POST /api/peers/:serverId`
|
||||
accepts `{endpoints:[wsURLs],password?:string}` and responds with `connected`,
|
||||
`needs_password`, and successful `connected_endpoints`. It signs into each remote
|
||||
machine directly, retaining cookies only in proxy memory, scoped to the local
|
||||
login session, server id and exact registered alias. `DELETE` revokes that server.
|
||||
A proxy restart requires signing into remote peers again; remote harness sessions
|
||||
and background work are independent and are not stopped by this relay.
|
||||
|
||||
The browser opens `/peers/:serverId/connect?endpoint=<original ws URL>` and reads
|
||||
files/artifacts/updates or uploads through `/peers/:serverId/<resource>`, using the
|
||||
same optional `endpoint` query. These are same-origin paths, so HTTPS browsers can
|
||||
reach trusted LAN servers without cross-origin cookies or mixed-content sockets.
|
||||
Local browser cookies and Origin are never forwarded to remote machines; only the
|
||||
selected peer's exact stored credential is used. No redirects are followed, no
|
||||
remote Set-Cookie is exposed, and proxying is limited to registered endpoints and
|
||||
known application resource paths. Mutating registration calls require same Origin
|
||||
and JSON, and all endpoints remain behind the local login gate.
|
||||
|
||||
New local sessions have unique nonce-bearing signed cookies, while existing
|
||||
legacy cookies remain valid. Profile edits revoke previous aliases immediately;
|
||||
registration generations prevent late responses resurrecting deleted profiles.
|
||||
Failed reauthentication to an unchanged profile preserves its existing cookie and
|
||||
websocket. Expired owner records close their sockets and are pruned before limits.
|
||||
|
||||
Verification: all 13 Node tests passed, including isolated real HTTP/WS peers,
|
||||
per-browser and per-server cookie isolation, scoped files and websocket routing,
|
||||
authenticated discovery, revocation races, failed sign-in preservation, and owner
|
||||
expiry cleanup. Go discovery/protocol/server tests and focused vet passed. All
|
||||
fixtures used loopback/scratch state; no operator sessions, passwords, deployments
|
||||
or workflows were changed. Parent owns Flutter integration and delivery.
|
||||
@@ -0,0 +1,113 @@
|
||||
package discovery
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/mdns"
|
||||
)
|
||||
|
||||
// FoundServer is a validated service advertised on the serving host's LAN.
|
||||
type FoundServer struct {
|
||||
Name string `json:"name"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
Auth string `json:"auth"`
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
// Browse sends one bounded DNS-SD query, never a network port scan.
|
||||
func Browse(ctx context.Context) ([]FoundServer, error) {
|
||||
return browse(ctx, mdns.QueryContext)
|
||||
}
|
||||
|
||||
func browse(ctx context.Context, query func(context.Context, *mdns.QueryParam) error) ([]FoundServer, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 4*time.Second)
|
||||
defer cancel()
|
||||
entries := make(chan *mdns.ServiceEntry, 128)
|
||||
result := make(chan error, 1)
|
||||
go func() {
|
||||
result <- query(ctx, &mdns.QueryParam{Service: ServiceType, Domain: "local", Timeout: 3 * time.Second,
|
||||
Entries: entries, WantUnicastResponse: true, Logger: log.New(io.Discard, "", 0)})
|
||||
}()
|
||||
found := map[string]FoundServer{}
|
||||
consume := func(entry *mdns.ServiceEntry) {
|
||||
if item, ok := discovered(entry); ok && len(found) < 128 {
|
||||
found[item.Endpoint] = item
|
||||
}
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case entry := <-entries:
|
||||
consume(entry)
|
||||
case err := <-result:
|
||||
for {
|
||||
select {
|
||||
case entry := <-entries:
|
||||
consume(entry)
|
||||
default:
|
||||
values := make([]FoundServer, 0, len(found))
|
||||
for _, item := range found {
|
||||
values = append(values, item)
|
||||
}
|
||||
sort.Slice(values, func(i, j int) bool { return values[i].Endpoint < values[j].Endpoint })
|
||||
return values, err
|
||||
}
|
||||
}
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func discovered(entry *mdns.ServiceEntry) (FoundServer, bool) {
|
||||
if entry == nil || entry.Port < 1 || entry.Port > 65535 {
|
||||
return FoundServer{}, false
|
||||
}
|
||||
fields := map[string]string{}
|
||||
for _, value := range entry.InfoFields {
|
||||
if key, text, ok := strings.Cut(value, "="); ok {
|
||||
fields[key] = text
|
||||
}
|
||||
}
|
||||
path := fields["path"]
|
||||
if path != "/connect" {
|
||||
return FoundServer{}, false
|
||||
}
|
||||
ip := entry.AddrV4
|
||||
if ip == nil {
|
||||
ip = entry.AddrV6
|
||||
}
|
||||
if ip == nil && entry.AddrV6IPAddr != nil {
|
||||
ip = entry.AddrV6IPAddr.IP
|
||||
}
|
||||
if ip == nil || ip.IsUnspecified() || ip.IsMulticast() || ip.IsLoopback() || ip.IsLinkLocalUnicast() {
|
||||
return FoundServer{}, false
|
||||
}
|
||||
if fields["tls"] != "0" && fields["tls"] != "1" {
|
||||
return FoundServer{}, false
|
||||
}
|
||||
scheme := "ws"
|
||||
if fields["tls"] == "1" {
|
||||
scheme = "wss"
|
||||
}
|
||||
auth := fields["auth"]
|
||||
if auth != "password" && auth != "none" {
|
||||
return FoundServer{}, false
|
||||
}
|
||||
name := strings.TrimSpace(fields["name"])
|
||||
if name == "" {
|
||||
name = strings.TrimSuffix(entry.Host, ".")
|
||||
}
|
||||
if name == "" || len(name) > 200 {
|
||||
return FoundServer{}, false
|
||||
}
|
||||
endpoint := (&url.URL{Scheme: scheme, Host: net.JoinHostPort(ip.String(), strconv.Itoa(entry.Port)), Path: path}).String()
|
||||
return FoundServer{Name: name, Endpoint: endpoint, Auth: auth, Version: fields["v"]}, true
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package discovery
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/mdns"
|
||||
)
|
||||
|
||||
func service(ip string) *mdns.ServiceEntry {
|
||||
return &mdns.ServiceEntry{AddrV4: net.ParseIP(ip), Host: "server.local.", Port: 8083,
|
||||
InfoFields: []string{"name=Linux desktop", "path=/connect", "tls=0", "auth=password", "v=release"}}
|
||||
}
|
||||
|
||||
func TestBrowseCollectsDeduplicatedValidAdvertisements(t *testing.T) {
|
||||
values, err := browse(context.Background(), func(ctx context.Context, p *mdns.QueryParam) error {
|
||||
if p.Service != ServiceType || p.Timeout.Seconds() > 4 {
|
||||
t.Fatal("unbounded or wrong service query")
|
||||
}
|
||||
for _, entry := range []*mdns.ServiceEntry{service("192.168.1.3"), service("192.168.1.2"), service("192.168.1.3"), service("127.0.0.1"), nil} {
|
||||
p.Entries <- entry
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil || len(values) != 2 {
|
||||
t.Fatalf("values=%+v err=%v", values, err)
|
||||
}
|
||||
if values[0].Endpoint != "ws://192.168.1.2:8083/connect" || values[0].Name != "Linux desktop" || values[0].Auth != "password" {
|
||||
t.Fatalf("wrong record: %+v", values[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestBrowseHonorsCancellation(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
_, err := browse(ctx, func(ctx context.Context, p *mdns.QueryParam) error { <-ctx.Done(); return ctx.Err() })
|
||||
if err == nil {
|
||||
t.Fatal("canceled browse must fail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscoveryRejectsMalformedAndUnsafeAdvertisements(t *testing.T) {
|
||||
for _, ip := range []string{"0.0.0.0", "127.0.0.1", "224.0.0.1", "169.254.3.1", "::", "::1", "fe80::1"} {
|
||||
if _, ok := discovered(service(ip)); ok {
|
||||
t.Fatalf("accepted address %s", ip)
|
||||
}
|
||||
}
|
||||
for _, fields := range [][]string{{"path=//evil/connect", "auth=password", "tls=0"}, {"path=/connect", "auth=unknown", "tls=0"}, {"path=/connect", "auth=password", "tls=garbage"}} {
|
||||
item := service("192.168.1.2")
|
||||
item.InfoFields = fields
|
||||
if _, ok := discovered(item); ok {
|
||||
t.Fatalf("accepted invalid TXT %+v", fields)
|
||||
}
|
||||
}
|
||||
item := service("192.168.1.2")
|
||||
item.Port = 65536
|
||||
if _, ok := discovered(item); ok {
|
||||
t.Fatal("accepted invalid port")
|
||||
}
|
||||
}
|
||||
@@ -287,8 +287,9 @@ type Message struct {
|
||||
// AttentionAcks is the ack_attention body, the attention_acks reply (the
|
||||
// whole set) and the attention_acks_changed push (the delta). See
|
||||
// attention_acks.go.
|
||||
AttentionAcks *AttentionAcks `json:"attention_acks,omitempty"`
|
||||
ServerStats *ServerStats `json:"server_stats,omitempty"`
|
||||
AttentionAcks *AttentionAcks `json:"attention_acks,omitempty"`
|
||||
DiscoveredServers []DiscoveredServer `json:"discovered_servers,omitzero"`
|
||||
ServerStats *ServerStats `json:"server_stats,omitempty"`
|
||||
// App notification stream (see notifications.go): After is the
|
||||
// subscribe_notifications cursor, Notifications the subscribe reply,
|
||||
// Notification one push, and IDs the clear_notifications request, the
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package protocol
|
||||
|
||||
const (
|
||||
DiscoverServersRequest = "discover_servers"
|
||||
DiscoverServersResult = "discovered_servers"
|
||||
)
|
||||
|
||||
// DiscoveredServer is one Pipelines DNS-SD advertisement on the serving LAN.
|
||||
type DiscoveredServer struct {
|
||||
Name string `json:"name"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
Auth string `json:"auth"`
|
||||
Version string `json:"version"`
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/discovery"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
var discoverySlots = make(chan struct{}, 4)
|
||||
|
||||
func discoverServers(ctx context.Context) ([]discovery.FoundServer, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 4*time.Second)
|
||||
defer cancel()
|
||||
select {
|
||||
case discoverySlots <- struct{}{}:
|
||||
defer func() { <-discoverySlots }()
|
||||
return discovery.Browse(ctx)
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
func handleServerDiscovery(ctx context.Context, request protocol.Message, write func(protocol.Message) error) {
|
||||
servers, err := discoverServers(ctx)
|
||||
if err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "LAN discovery unavailable: " + err.Error()})
|
||||
return
|
||||
}
|
||||
items := make([]protocol.DiscoveredServer, 0, len(servers))
|
||||
for _, server := range servers {
|
||||
items = append(items, protocol.DiscoveredServer(server))
|
||||
}
|
||||
_ = write(protocol.Message{Type: protocol.DiscoverServersResult, ID: request.ID, DiscoveredServers: items})
|
||||
}
|
||||
|
||||
func serveServerDiscovery(w http.ResponseWriter, r *http.Request) {
|
||||
servers, err := discoverServers(r.Context())
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err != nil {
|
||||
http.Error(w, "LAN discovery unavailable", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(struct {
|
||||
Servers []discovery.FoundServer `json:"discovered_servers"`
|
||||
}{servers})
|
||||
}
|
||||
@@ -153,6 +153,7 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
|
||||
}
|
||||
runs.serveArtifact(w, r)
|
||||
})
|
||||
mux.HandleFunc("GET /discovery", serveServerDiscovery)
|
||||
mux.HandleFunc("GET /files", serveHomeFile)
|
||||
mux.HandleFunc("POST /chat-uploads", serveChatUpload)
|
||||
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -276,6 +277,10 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
|
||||
if handleDirectoryListing(request, write) {
|
||||
continue
|
||||
}
|
||||
if request.Type == protocol.DiscoverServersRequest {
|
||||
go handleServerDiscovery(connCtx, request, write)
|
||||
continue
|
||||
}
|
||||
if request.Type == protocol.ServerStatsRequest {
|
||||
go handleServerStats(connCtx, request, runs, options.ServerStatsSampler, started, build, write)
|
||||
continue
|
||||
|
||||
+20
-6
@@ -18,7 +18,7 @@
|
||||
//
|
||||
// The default host is 127.0.0.1. Use --host 0.0.0.0 only on a trusted LAN/VPN.
|
||||
import { createServer, request } from "node:http";
|
||||
import { createHmac, timingSafeEqual } from "node:crypto";
|
||||
import { createHmac, timingSafeEqual, randomBytes } from "node:crypto";
|
||||
import { createReadStream, readFileSync } from "node:fs";
|
||||
import { homedir } from "node:os";
|
||||
import { readFile, stat } from "node:fs/promises";
|
||||
@@ -26,6 +26,7 @@ import { extname, join, normalize, resolve, sep } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import net from "node:net";
|
||||
import { sameOriginUpgrade } from "./src/proxySecurity.js";
|
||||
import { PeerRelay, proxyDiscovery } from "./src/peerRelay.js";
|
||||
|
||||
// The proxy's own directory holds the sign-in page's icons.
|
||||
const HERE = resolve(fileURLToPath(new URL(".", import.meta.url)));
|
||||
@@ -86,7 +87,8 @@ function cookieValue(req, name) {
|
||||
}
|
||||
|
||||
function signSession(expires) {
|
||||
return `${expires}.${createHmac("sha256", auth.secret).update(String(expires)).digest("base64url")}`;
|
||||
const payload = `${expires}.${randomBytes(18).toString("base64url")}`;
|
||||
return `${payload}.${createHmac("sha256", auth.secret).update(payload).digest("base64url")}`;
|
||||
}
|
||||
|
||||
const SESSION_LONG_SECONDS = 30 * 24 * 60 * 60;
|
||||
@@ -99,9 +101,11 @@ function sessionToken(remember = false) {
|
||||
|
||||
function validSession(value) {
|
||||
if (!auth || typeof value !== "string") return false;
|
||||
const [expires, signature] = value.split(".");
|
||||
if (!expires || !signature) return false;
|
||||
const expected = createHmac("sha256", auth.secret).update(expires).digest();
|
||||
const parts = value.split(".");
|
||||
if (parts.length !== 2 && parts.length !== 3) return false;
|
||||
const expires = parts[0], signature = parts.at(-1);
|
||||
if (!/^\d+$/.test(expires) || !signature) return false;
|
||||
const expected = createHmac("sha256", auth.secret).update(parts.slice(0, -1).join(".")).digest();
|
||||
let given;
|
||||
try {
|
||||
given = Buffer.from(signature, "base64url");
|
||||
@@ -116,6 +120,8 @@ function isAuthed(req) {
|
||||
return !auth || validSession(cookieValue(req, SESSION_COOKIE));
|
||||
}
|
||||
|
||||
const peerRelay = new PeerRelay((req) => AUTH_DISABLED ? "test-auth-disabled" : cookieValue(req, SESSION_COOKIE));
|
||||
|
||||
function passwordOk(candidate) {
|
||||
if (!auth) return true;
|
||||
const given = Buffer.from(String(candidate ?? ""));
|
||||
@@ -439,12 +445,13 @@ const server = createServer(async (req, res) => {
|
||||
res.writeHead(303, { Location: safeNext(fields.get("next")) }).end();
|
||||
return;
|
||||
} else if (pathname === "/logout") {
|
||||
peerRelay.forget(req);
|
||||
res.setHeader("Set-Cookie", `${SESSION_COOKIE}=; Path=/; HttpOnly; SameSite=Strict; Max-Age=0`);
|
||||
res.writeHead(303, { Location: "/login" }).end();
|
||||
return;
|
||||
} else if (!isAuthed(req)) {
|
||||
const wantsHtml = String(req.headers.accept ?? "").includes("text/html");
|
||||
if (req.method === "GET" && wantsHtml) {
|
||||
if (req.method === "GET" && wantsHtml && !pathname.startsWith("/api/") && !pathname.startsWith("/peers/")) {
|
||||
res.writeHead(302, { Location: `/login?next=${encodeURIComponent(req.url ?? "/")}` }).end();
|
||||
return;
|
||||
}
|
||||
@@ -454,6 +461,12 @@ const server = createServer(async (req, res) => {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (pathname === "/api/discovery" && req.method === "GET") {
|
||||
await proxyDiscovery(req, res, targetHost, targetPort);
|
||||
return;
|
||||
}
|
||||
if (await peerRelay.api(req, res)) return;
|
||||
if (peerRelay.http(req, res)) return;
|
||||
if (pathname.startsWith("/artifacts/") || pathname === "/files") {
|
||||
proxyArtifact(req, res);
|
||||
return;
|
||||
@@ -563,6 +576,7 @@ server.on("upgrade", (req, clientSocket, clientHead) => {
|
||||
clientSocket.destroy();
|
||||
return;
|
||||
}
|
||||
if (peerRelay.upgrade(req, clientSocket, clientHead)) return;
|
||||
if (pathname !== "/connect") {
|
||||
clientSocket.destroy();
|
||||
return;
|
||||
|
||||
@@ -0,0 +1,180 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { authenticate, endpoint, json } from "./peerTransport.js";
|
||||
import { sameOriginUpgrade } from "./proxySecurity.js";
|
||||
|
||||
async function body(req) {
|
||||
const chunks = [];
|
||||
let size = 0;
|
||||
for await (const chunk of req) {
|
||||
size += chunk.length;
|
||||
if (size > 65536) throw new Error("Request too large");
|
||||
chunks.push(chunk);
|
||||
}
|
||||
return JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
||||
}
|
||||
|
||||
export class PeerRegistry {
|
||||
constructor(scopeFor) {
|
||||
this.scopeFor = scopeFor;
|
||||
this.sessions = new Map();
|
||||
}
|
||||
owner(req) {
|
||||
const token = this.scopeFor(req);
|
||||
if (!token) throw new Error("Sign in first");
|
||||
const key = createHash("sha256").update(token).digest("hex");
|
||||
for (const [old, value] of this.sessions) {
|
||||
if (Date.now() - value.used > 12 * 3600000) {
|
||||
for (const peer of value.peers.values())
|
||||
for (const socket of peer.sockets) socket.destroy();
|
||||
this.sessions.delete(old);
|
||||
}
|
||||
}
|
||||
if (!this.sessions.has(key)) {
|
||||
if (this.sessions.size >= 256)
|
||||
throw new Error("Too many browser sessions");
|
||||
this.sessions.set(key, {
|
||||
peers: new Map(),
|
||||
versions: new Map(),
|
||||
used: Date.now(),
|
||||
});
|
||||
}
|
||||
const owner = this.sessions.get(key);
|
||||
owner.used = Date.now();
|
||||
return owner;
|
||||
}
|
||||
forget(req) {
|
||||
try {
|
||||
const token = this.scopeFor(req);
|
||||
if (!token) return;
|
||||
const key = createHash("sha256").update(token).digest("hex");
|
||||
const owner = this.sessions.get(key);
|
||||
if (!owner) return;
|
||||
for (const peer of owner.peers.values())
|
||||
for (const socket of peer.sockets) socket.destroy();
|
||||
this.sessions.delete(key);
|
||||
} catch {}
|
||||
}
|
||||
async api(req, res) {
|
||||
const parsed = new URL(req.url, "http://localhost");
|
||||
const match = /^\/api\/peers\/([^/]+)$/.exec(parsed.pathname);
|
||||
if (!match) return false;
|
||||
if (
|
||||
!sameOriginUpgrade(req.headers.origin, req.headers.host) ||
|
||||
(["POST", "DELETE"].includes(req.method) && !req.headers.origin)
|
||||
) {
|
||||
json(res, 403, { error: "Same-origin request required" });
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
const id = decodeURIComponent(match[1]);
|
||||
if (!id || id.length > 256) throw new Error("Invalid server id");
|
||||
const owner = this.owner(req);
|
||||
if (req.method === "DELETE") {
|
||||
owner.versions.set(id, (owner.versions.get(id) ?? 0) + 1);
|
||||
const previous = owner.peers.get(id);
|
||||
if (previous) for (const socket of previous.sockets) socket.destroy();
|
||||
owner.peers.delete(id);
|
||||
json(res, 200, { connected: false, needs_password: false });
|
||||
return true;
|
||||
}
|
||||
if (req.method === "GET") {
|
||||
const peer = owner.peers.get(id);
|
||||
json(res, 200, {
|
||||
connected: Boolean(peer?.connected),
|
||||
needs_password: !peer?.connected,
|
||||
});
|
||||
return true;
|
||||
}
|
||||
if (
|
||||
req.method !== "POST" ||
|
||||
!String(req.headers["content-type"]).startsWith("application/json")
|
||||
) {
|
||||
json(res, 405, { error: "POST JSON required" });
|
||||
return true;
|
||||
}
|
||||
const data = await body(req);
|
||||
if (
|
||||
!Array.isArray(data.endpoints) ||
|
||||
data.endpoints.length < 1 ||
|
||||
data.endpoints.length > 8 ||
|
||||
(data.password !== undefined &&
|
||||
(typeof data.password !== "string" || data.password.length > 4096))
|
||||
)
|
||||
throw new Error("Invalid server addresses");
|
||||
const urls = [
|
||||
...new Set(data.endpoints.map((value) => endpoint(value).href)),
|
||||
];
|
||||
const previous = owner.peers.get(id);
|
||||
if (!previous && owner.peers.size >= 64)
|
||||
throw new Error("Too many saved servers");
|
||||
const sameEndpoints =
|
||||
previous &&
|
||||
previous.endpoints.length === urls.length &&
|
||||
urls.every((url) => previous.endpoints.includes(url));
|
||||
const version = (owner.versions.get(id) ?? 0) + 1;
|
||||
owner.versions.set(id, version);
|
||||
if (previous && !sameEndpoints) {
|
||||
for (const socket of previous.sockets) socket.destroy();
|
||||
owner.peers.delete(id);
|
||||
}
|
||||
const peer = {
|
||||
endpoints: urls,
|
||||
cookies: new Map(),
|
||||
sockets: new Set(),
|
||||
connected: false,
|
||||
active: urls[0],
|
||||
};
|
||||
const results = await Promise.all(
|
||||
urls.map(async (value) => {
|
||||
try {
|
||||
const old = previous?.endpoints.includes(value)
|
||||
? (previous.cookies.get(value) ?? "")
|
||||
: "";
|
||||
const status = await authenticate(
|
||||
new URL(value),
|
||||
old,
|
||||
data.password,
|
||||
);
|
||||
if (status.connected) {
|
||||
peer.cookies.set(value, status.cookie);
|
||||
peer.active = value;
|
||||
peer.connected = true;
|
||||
}
|
||||
return status;
|
||||
} catch {
|
||||
return { connected: false, needs_password: false };
|
||||
}
|
||||
}),
|
||||
);
|
||||
if (owner.versions.get(id) !== version) {
|
||||
json(res, 409, { error: "Server selection changed during sign-in" });
|
||||
return true;
|
||||
}
|
||||
if (sameEndpoints && !peer.connected) {
|
||||
json(res, 200, {
|
||||
connected: false,
|
||||
needs_password: results.some((r) => r.needs_password),
|
||||
previous_connection_kept: true,
|
||||
error: "Sign-in failed. The existing connection was kept.",
|
||||
});
|
||||
return true;
|
||||
}
|
||||
if (sameEndpoints) peer.sockets = previous.sockets;
|
||||
owner.peers.set(id, peer);
|
||||
json(res, 200, {
|
||||
connected_endpoints: [...peer.cookies.keys()],
|
||||
connected: peer.connected,
|
||||
needs_password:
|
||||
!peer.connected && results.some((r) => r.needs_password),
|
||||
...(!peer.connected && !results.some((r) => r.needs_password)
|
||||
? { error: "Server could not be reached" }
|
||||
: {}),
|
||||
});
|
||||
} catch {
|
||||
json(res, 400, {
|
||||
error: "Server registration failed. Check the addresses and sign-in.",
|
||||
});
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
import {
|
||||
endpoint,
|
||||
webOrigin,
|
||||
boundedRequest,
|
||||
remoteRequest,
|
||||
json,
|
||||
} from "./peerTransport.js";
|
||||
import { PeerRegistry } from "./peerRegistry.js";
|
||||
|
||||
const allowed = (path) =>
|
||||
path === "/files" ||
|
||||
path === "/chat-uploads" ||
|
||||
path.startsWith("/artifacts/") ||
|
||||
path.startsWith("/updates/");
|
||||
export class PeerRelay extends PeerRegistry {
|
||||
target(req, path) {
|
||||
const parsed = new URL(req.url, "http://localhost");
|
||||
const match = /^\/peers\/([^/]+)\/(.*)$/.exec(parsed.pathname);
|
||||
if (!match) return null;
|
||||
const owner = this.owner(req);
|
||||
const peer = owner.peers.get(decodeURIComponent(match[1]));
|
||||
if (!peer) throw new Error("Select and sign in to this server again");
|
||||
const selected = endpoint(
|
||||
parsed.searchParams.get("endpoint") ?? peer.active,
|
||||
).href;
|
||||
if (!peer.endpoints.includes(selected) || !peer.cookies.has(selected))
|
||||
throw new Error("Server address is not signed in");
|
||||
parsed.searchParams.delete("endpoint");
|
||||
const url = new URL(path ?? `/${match[2]}`, webOrigin(selected));
|
||||
url.search = parsed.search;
|
||||
if (url.origin !== webOrigin(selected).origin)
|
||||
throw new Error("Remote origin changed");
|
||||
return { url, cookie: peer.cookies.get(selected), peer };
|
||||
}
|
||||
http(req, res) {
|
||||
if (!req.url.startsWith("/peers/")) return false;
|
||||
try {
|
||||
const target = this.target(req);
|
||||
if (
|
||||
!target ||
|
||||
!allowed(target.url.pathname) ||
|
||||
!["GET", "HEAD", "POST"].includes(req.method) ||
|
||||
(req.method === "POST" && target.url.pathname !== "/chat-uploads")
|
||||
) {
|
||||
json(res, 404, { error: "Unsupported remote resource" });
|
||||
return true;
|
||||
}
|
||||
const headers = { ...(target.cookie ? { cookie: target.cookie } : {}) };
|
||||
for (const name of [
|
||||
"content-type",
|
||||
"content-length",
|
||||
"range",
|
||||
"x-file-name",
|
||||
"x-upload-name",
|
||||
"x-chat-file-name",
|
||||
"x-pipelines-upload-name",
|
||||
])
|
||||
if (req.headers[name]) headers[name] = req.headers[name];
|
||||
const upstream = remoteRequest(target.url, {
|
||||
method: req.method,
|
||||
headers,
|
||||
});
|
||||
upstream.setTimeout(30000, () => upstream.destroy());
|
||||
upstream.on("response", (response) => {
|
||||
const safe = {};
|
||||
for (const name of [
|
||||
"content-type",
|
||||
"content-length",
|
||||
"content-range",
|
||||
"accept-ranges",
|
||||
"content-disposition",
|
||||
])
|
||||
if (response.headers[name]) safe[name] = response.headers[name];
|
||||
res.writeHead(
|
||||
response.statusCode >= 300 && response.statusCode < 400
|
||||
? 502
|
||||
: response.statusCode,
|
||||
{ ...safe, "Cache-Control": "no-store" },
|
||||
);
|
||||
response.pipe(res);
|
||||
});
|
||||
upstream.on("error", () => {
|
||||
if (!res.headersSent)
|
||||
json(res, 502, { error: "Remote server unavailable" });
|
||||
else res.destroy();
|
||||
});
|
||||
req.on("aborted", () => upstream.destroy());
|
||||
res.on("close", () => upstream.destroy());
|
||||
req.pipe(upstream);
|
||||
} catch {
|
||||
json(res, 401, { error: "Sign in to the remote server again" });
|
||||
}
|
||||
return true;
|
||||
}
|
||||
upgrade(req, socket, head) {
|
||||
if (!req.url.startsWith("/peers/")) return false;
|
||||
try {
|
||||
const target = this.target(req, "/connect");
|
||||
const headers = {
|
||||
host: target.url.host,
|
||||
connection: "Upgrade",
|
||||
upgrade: "websocket",
|
||||
"sec-websocket-key": req.headers["sec-websocket-key"],
|
||||
"sec-websocket-version": "13",
|
||||
...(target.cookie ? { cookie: target.cookie } : {}),
|
||||
};
|
||||
const upstream = remoteRequest(target.url, { headers });
|
||||
upstream.setTimeout(10000, () => upstream.destroy());
|
||||
upstream.on("upgrade", (response, remote, remoteHead) => {
|
||||
socket.write(
|
||||
`HTTP/1.1 101 Switching Protocols\r\n${Object.entries(
|
||||
response.headers,
|
||||
)
|
||||
.filter(([k]) =>
|
||||
[
|
||||
"upgrade",
|
||||
"connection",
|
||||
"sec-websocket-accept",
|
||||
"sec-websocket-protocol",
|
||||
].includes(k),
|
||||
)
|
||||
.map(([k, v]) => `${k}: ${v}`)
|
||||
.join("\r\n")}\r\n\r\n`,
|
||||
);
|
||||
target.peer.sockets.add(socket);
|
||||
remote.setTimeout(0);
|
||||
socket.once("close", () => {
|
||||
target.peer.sockets.delete(socket);
|
||||
remote.destroy();
|
||||
});
|
||||
remote.once("close", () => socket.destroy());
|
||||
socket.on("error", () => remote.destroy());
|
||||
remote.on("error", () => socket.destroy());
|
||||
if (head?.length) remote.write(head);
|
||||
if (remoteHead?.length) socket.write(remoteHead);
|
||||
remote.pipe(socket);
|
||||
socket.pipe(remote);
|
||||
});
|
||||
upstream.on("response", (response) => {
|
||||
response.resume();
|
||||
socket.end(
|
||||
`HTTP/1.1 ${response.statusCode === 401 ? 401 : 502} Remote Unavailable\r\nContent-Length: 0\r\n\r\n`,
|
||||
);
|
||||
});
|
||||
upstream.on("error", () => socket.destroy());
|
||||
socket.once("close", () => upstream.destroy());
|
||||
upstream.end();
|
||||
} catch {
|
||||
socket.end("HTTP/1.1 401 Unauthorized\r\nContent-Length: 0\r\n\r\n");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
export async function proxyDiscovery(req, res, host, port) {
|
||||
try {
|
||||
const response = await boundedRequest(
|
||||
new URL(
|
||||
`http://${host.includes(":") ? `[${host}]` : host}:${port}/discovery`,
|
||||
),
|
||||
);
|
||||
res
|
||||
.writeHead(response.status, {
|
||||
"Content-Type": "application/json",
|
||||
"Cache-Control": "no-store",
|
||||
})
|
||||
.end(response.body);
|
||||
} catch {
|
||||
json(res, 503, { error: "LAN discovery unavailable" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
import http from "node:http";
|
||||
import https from "node:https";
|
||||
|
||||
export function endpoint(value) {
|
||||
const url = new URL(value);
|
||||
if (
|
||||
!["ws:", "wss:"].includes(url.protocol) ||
|
||||
!url.hostname ||
|
||||
url.username ||
|
||||
url.password ||
|
||||
url.hash ||
|
||||
url.search ||
|
||||
url.pathname !== "/connect"
|
||||
)
|
||||
throw new Error("Use a ws(s) server address ending in /connect");
|
||||
return url;
|
||||
}
|
||||
export function webOrigin(url) {
|
||||
const result = new URL(url);
|
||||
result.protocol = result.protocol === "wss:" ? "https:" : "http:";
|
||||
result.pathname = "/";
|
||||
return result;
|
||||
}
|
||||
export function remoteRequest(url, options = {}) {
|
||||
return (url.protocol === "https:" ? https : http).request(url, options);
|
||||
}
|
||||
|
||||
// Never follows redirects or sends local browser cookies to another machine.
|
||||
export function boundedRequest(
|
||||
url,
|
||||
{ method = "GET", cookie = "", body = "", headers = {} } = {},
|
||||
) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = remoteRequest(url, {
|
||||
method,
|
||||
headers: { ...headers, ...(cookie ? { Cookie: cookie } : {}) },
|
||||
});
|
||||
const timer = setTimeout(
|
||||
() => request.destroy(new Error("Server timed out")),
|
||||
8000,
|
||||
);
|
||||
request.once("error", (error) => {
|
||||
clearTimeout(timer);
|
||||
reject(error);
|
||||
});
|
||||
request.once("response", (response) => {
|
||||
const chunks = [];
|
||||
let bytes = 0;
|
||||
response.on("data", (chunk) => {
|
||||
bytes += chunk.length;
|
||||
if (bytes > 65536) {
|
||||
response.destroy();
|
||||
request.destroy(new Error("Server response was too large"));
|
||||
return;
|
||||
}
|
||||
chunks.push(chunk);
|
||||
});
|
||||
response.once("error", (error) => {
|
||||
clearTimeout(timer);
|
||||
reject(error);
|
||||
});
|
||||
response.once("end", () => {
|
||||
clearTimeout(timer);
|
||||
resolve({
|
||||
status: response.statusCode,
|
||||
headers: response.headers,
|
||||
body: Buffer.concat(chunks).toString("utf8"),
|
||||
});
|
||||
});
|
||||
});
|
||||
request.end(body);
|
||||
});
|
||||
}
|
||||
|
||||
export function sessionCookie(response) {
|
||||
return (response.headers["set-cookie"] ?? [])
|
||||
.map((value) => value.split(";")[0])
|
||||
.filter((value) => value.startsWith("pipelines_session="))
|
||||
.join("; ");
|
||||
}
|
||||
|
||||
export async function authenticate(url, cookie, password) {
|
||||
const login = new URL("/login", webOrigin(url));
|
||||
let response;
|
||||
if (password !== undefined) {
|
||||
const body = new URLSearchParams({ password, remember: "on" }).toString();
|
||||
response = await boundedRequest(login, {
|
||||
method: "POST",
|
||||
body,
|
||||
cookie,
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
"Content-Length": Buffer.byteLength(body),
|
||||
},
|
||||
});
|
||||
if ([302, 303].includes(response.status)) {
|
||||
cookie = sessionCookie(response) || cookie;
|
||||
} else if (response.status === 401 || response.status === 429) {
|
||||
return { connected: false, needs_password: true, cookie: "" };
|
||||
}
|
||||
}
|
||||
response = await boundedRequest(login, { cookie });
|
||||
if ([302, 303].includes(response.status))
|
||||
return { connected: true, needs_password: false, cookie };
|
||||
if (
|
||||
response.status === 401 ||
|
||||
(response.status === 200 && /name=["']password["']/.test(response.body))
|
||||
) {
|
||||
return { connected: false, needs_password: true, cookie: "" };
|
||||
}
|
||||
if (response.status === 200)
|
||||
return { connected: true, needs_password: false, cookie };
|
||||
if (response.status === 404) {
|
||||
const health = await boundedRequest(new URL("/healthz", webOrigin(url)));
|
||||
if (health.status === 200 && JSON.parse(health.body)?.healthy === true)
|
||||
return { connected: true, needs_password: false, cookie: "" };
|
||||
}
|
||||
throw new Error("Server could not be reached");
|
||||
}
|
||||
|
||||
export function json(res, status, value) {
|
||||
res
|
||||
.writeHead(status, {
|
||||
"Content-Type": "application/json",
|
||||
"Cache-Control": "no-store",
|
||||
})
|
||||
.end(JSON.stringify(value));
|
||||
}
|
||||
@@ -0,0 +1,471 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { createServer, request } from "node:http";
|
||||
import { createHash, randomBytes } from "node:crypto";
|
||||
import { once } from "node:events";
|
||||
import { mkdtemp, writeFile, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { PeerRegistry } from "../src/peerRegistry.js";
|
||||
import { spawn } from "node:child_process";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
async function fixture(t) {
|
||||
const observations = [];
|
||||
const gates = {};
|
||||
const hold = (name) => {
|
||||
let release, enter;
|
||||
const promise = new Promise((done) => {
|
||||
release = done;
|
||||
});
|
||||
const entered = new Promise((done) => {
|
||||
enter = done;
|
||||
});
|
||||
gates[name] = { promise, enter };
|
||||
t.after(release);
|
||||
return { entered, release };
|
||||
};
|
||||
const remotes = [];
|
||||
for (const name of ["one", "two"]) {
|
||||
const server = createServer(async (req, res) => {
|
||||
observations.push({
|
||||
name,
|
||||
path: req.url,
|
||||
cookie: req.headers.cookie,
|
||||
upload: req.headers["x-pipelines-upload-name"],
|
||||
});
|
||||
if (req.url === "/login" && req.method === "GET" && gates[name]) {
|
||||
gates[name].enter();
|
||||
await gates[name].promise;
|
||||
}
|
||||
if (req.url === "/discovery") {
|
||||
res.setHeader("Content-Type", "application/json");
|
||||
res.end(
|
||||
JSON.stringify({
|
||||
discovered_servers: [
|
||||
{
|
||||
name: "Linux desktop",
|
||||
endpoint: "ws://192.168.1.2:8083/connect",
|
||||
auth: "password",
|
||||
version: "test",
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (req.url === "/login" && req.method === "POST") {
|
||||
let body = "";
|
||||
for await (const chunk of req) body += chunk;
|
||||
if (new URLSearchParams(body).get("password") !== `password-${name}`) {
|
||||
res.writeHead(401).end();
|
||||
return;
|
||||
}
|
||||
res
|
||||
.writeHead(303, {
|
||||
"Set-Cookie": `pipelines_session=remote-${name}; HttpOnly`,
|
||||
Location: "/",
|
||||
})
|
||||
.end();
|
||||
return;
|
||||
}
|
||||
if (req.url === "/login") {
|
||||
if (req.headers.cookie === `pipelines_session=remote-${name}`)
|
||||
res.writeHead(302, { Location: "/" }).end();
|
||||
else res.end('<input name="password">');
|
||||
return;
|
||||
}
|
||||
res.setHeader("Set-Cookie", "do-not-forward=private");
|
||||
res.end(
|
||||
JSON.stringify({ name, url: req.url, cookie: req.headers.cookie }),
|
||||
);
|
||||
});
|
||||
server.on("upgrade", (req, socket) => {
|
||||
observations.push({
|
||||
name,
|
||||
path: req.url,
|
||||
cookie: req.headers.cookie,
|
||||
origin: req.headers.origin,
|
||||
});
|
||||
if (req.headers.cookie !== `pipelines_session=remote-${name}`) {
|
||||
socket.end("HTTP/1.1 401 Unauthorized\r\nContent-Length: 0\r\n\r\n");
|
||||
return;
|
||||
}
|
||||
const accept = createHash("sha1")
|
||||
.update(
|
||||
req.headers["sec-websocket-key"] +
|
||||
"258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
|
||||
)
|
||||
.digest("base64");
|
||||
socket.write(
|
||||
`HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: ${accept}\r\n\r\n`,
|
||||
);
|
||||
const message = Buffer.from(JSON.stringify({ name }));
|
||||
socket.write(
|
||||
Buffer.concat([Buffer.from([0x81, message.length]), message]),
|
||||
);
|
||||
socket.on("error", () => {});
|
||||
});
|
||||
server.listen(0, "127.0.0.1");
|
||||
await once(server, "listening");
|
||||
t.after(() => {
|
||||
server.closeAllConnections();
|
||||
server.close();
|
||||
});
|
||||
remotes.push(`ws://127.0.0.1:${server.address().port}/connect`);
|
||||
}
|
||||
const dir = await mkdtemp(join(tmpdir(), "peer-relay-"));
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
const auth = join(dir, "auth.json");
|
||||
await writeFile(
|
||||
auth,
|
||||
JSON.stringify({ password: "local-password", secret: "s".repeat(40) }),
|
||||
);
|
||||
const reserve = createServer().listen(0, "127.0.0.1");
|
||||
await once(reserve, "listening");
|
||||
const port = reserve.address().port;
|
||||
await new Promise((done) => reserve.close(done));
|
||||
const child = spawn(
|
||||
process.execPath,
|
||||
[
|
||||
"serve.mjs",
|
||||
"--port",
|
||||
String(port),
|
||||
"--pipelines",
|
||||
new URL(remotes[0]).host,
|
||||
],
|
||||
{
|
||||
cwd: fileURLToPath(new URL("..", import.meta.url)),
|
||||
env: {
|
||||
...process.env,
|
||||
PIPELINES_WEB_AUTH: "",
|
||||
PIPELINES_WEB_AUTH_FILE: auth,
|
||||
},
|
||||
stdio: ["ignore", "pipe", "inherit"],
|
||||
},
|
||||
);
|
||||
t.after(async () => {
|
||||
const exited = once(child, "exit");
|
||||
child.kill();
|
||||
await exited;
|
||||
});
|
||||
await once(child.stdout, "data");
|
||||
const base = `http://127.0.0.1:${port}`;
|
||||
const login = async () => {
|
||||
const reply = await fetch(base + "/login", {
|
||||
method: "POST",
|
||||
body: new URLSearchParams({ password: "local-password" }),
|
||||
redirect: "manual",
|
||||
});
|
||||
return reply.headers.get("set-cookie").split(";")[0];
|
||||
};
|
||||
const cookie = await login();
|
||||
const register = (id, data, who = cookie, origin = base) =>
|
||||
fetch(base + "/api/peers/" + id, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Cookie: who,
|
||||
Origin: origin,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return { base, cookie, remotes, observations, register, login, hold };
|
||||
}
|
||||
|
||||
test("browser can authenticate both machines, relay files and keep local credentials isolated", async (t) => {
|
||||
const f = await fixture(t);
|
||||
assert.equal(
|
||||
(await f.register("one", { endpoints: [f.remotes[0]] }, "")).status,
|
||||
401,
|
||||
);
|
||||
const needing = await f.register("one", { endpoints: [f.remotes[0]] });
|
||||
assert.equal((await needing.json()).needs_password, true);
|
||||
assert.equal(
|
||||
(
|
||||
await (
|
||||
await f.register("one", {
|
||||
endpoints: [f.remotes[0]],
|
||||
password: "password-one",
|
||||
})
|
||||
).json()
|
||||
).connected,
|
||||
true,
|
||||
);
|
||||
assert.equal(
|
||||
(
|
||||
await (
|
||||
await f.register("two", {
|
||||
endpoints: [f.remotes[1]],
|
||||
password: "password-two",
|
||||
})
|
||||
).json()
|
||||
).connected,
|
||||
true,
|
||||
);
|
||||
for (const [id, name] of [
|
||||
["one", "one"],
|
||||
["two", "two"],
|
||||
]) {
|
||||
const reply = await fetch(
|
||||
f.base + `/peers/${id}/files?path=%2Fhome%2Freport`,
|
||||
{ headers: { Cookie: f.cookie } },
|
||||
);
|
||||
assert.equal(reply.status, 200);
|
||||
assert.equal(reply.headers.get("set-cookie"), null);
|
||||
assert.deepEqual(await reply.json(), {
|
||||
name,
|
||||
url: "/files?path=%2Fhome%2Freport",
|
||||
cookie: `pipelines_session=remote-${name}`,
|
||||
});
|
||||
}
|
||||
const otherCookie = await f.login();
|
||||
assert.notEqual(otherCookie, f.cookie);
|
||||
assert.equal(
|
||||
(
|
||||
await fetch(f.base + "/peers/one/files", {
|
||||
headers: { Cookie: otherCookie },
|
||||
})
|
||||
).status,
|
||||
401,
|
||||
);
|
||||
assert.equal(
|
||||
(
|
||||
await f.register(
|
||||
"one",
|
||||
{ endpoints: [f.remotes[0]], password: "password-one" },
|
||||
f.cookie,
|
||||
"http://evil.test",
|
||||
)
|
||||
).status,
|
||||
403,
|
||||
);
|
||||
assert.equal(
|
||||
(
|
||||
await f.register("bad", {
|
||||
endpoints: ["ws://user:secret@localhost/connect"],
|
||||
})
|
||||
).status,
|
||||
400,
|
||||
);
|
||||
assert.equal(
|
||||
(
|
||||
await fetch(f.base + "/peers/one//evil.test/files", {
|
||||
headers: { Cookie: f.cookie },
|
||||
})
|
||||
).status,
|
||||
401,
|
||||
);
|
||||
assert.equal(
|
||||
(
|
||||
await fetch(f.base + "/peers/one/other", {
|
||||
headers: { Cookie: f.cookie },
|
||||
})
|
||||
).status,
|
||||
404,
|
||||
);
|
||||
assert.equal(
|
||||
f.observations.some((x) => x.cookie === f.cookie),
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
test("websocket relay authenticates the chosen machine without forwarding browser Origin", async (t) => {
|
||||
const f = await fixture(t);
|
||||
await f.register("two", {
|
||||
endpoints: [f.remotes[1]],
|
||||
password: "password-two",
|
||||
});
|
||||
const result = await new Promise((resolve, reject) => {
|
||||
const req = request(
|
||||
f.base +
|
||||
"/peers/two/connect?endpoint=" +
|
||||
encodeURIComponent(f.remotes[1]),
|
||||
{
|
||||
headers: {
|
||||
Cookie: f.cookie,
|
||||
Origin: f.base,
|
||||
Connection: "Upgrade",
|
||||
Upgrade: "websocket",
|
||||
"Sec-WebSocket-Key": randomBytes(16).toString("base64"),
|
||||
"Sec-WebSocket-Version": "13",
|
||||
},
|
||||
},
|
||||
);
|
||||
req.on("upgrade", (res, socket, head) => {
|
||||
const receive = (chunk) => {
|
||||
socket.destroy();
|
||||
resolve({ status: res.statusCode, text: chunk.subarray(2).toString() });
|
||||
};
|
||||
if (head.length) receive(head);
|
||||
else socket.once("data", receive);
|
||||
});
|
||||
req.on("response", (res) => reject(new Error(String(res.statusCode))));
|
||||
req.on("error", reject);
|
||||
req.end();
|
||||
});
|
||||
assert.equal(result.status, 101);
|
||||
assert.deepEqual(JSON.parse(result.text), { name: "two" });
|
||||
const observed = f.observations.find(
|
||||
(x) => x.name === "two" && x.path === "/connect",
|
||||
);
|
||||
assert.equal(observed.cookie, "pipelines_session=remote-two");
|
||||
assert.equal(observed.origin, undefined);
|
||||
});
|
||||
|
||||
test("removing or changing a profile revokes its previous remote route", async (t) => {
|
||||
const f = await fixture(t);
|
||||
await f.register("server", {
|
||||
endpoints: [f.remotes[0]],
|
||||
password: "password-one",
|
||||
});
|
||||
await f.register("server", {
|
||||
endpoints: [f.remotes[1]],
|
||||
password: "password-two",
|
||||
});
|
||||
assert.equal(
|
||||
(
|
||||
await fetch(
|
||||
f.base +
|
||||
"/peers/server/files?endpoint=" +
|
||||
encodeURIComponent(f.remotes[0]),
|
||||
{ headers: { Cookie: f.cookie } },
|
||||
)
|
||||
).status,
|
||||
401,
|
||||
);
|
||||
const removed = await fetch(f.base + "/api/peers/server", {
|
||||
method: "DELETE",
|
||||
headers: { Cookie: f.cookie, Origin: f.base },
|
||||
});
|
||||
assert.equal(removed.status, 200);
|
||||
assert.equal(
|
||||
(
|
||||
await fetch(f.base + "/peers/server/files", {
|
||||
headers: { Cookie: f.cookie },
|
||||
})
|
||||
).status,
|
||||
401,
|
||||
);
|
||||
});
|
||||
|
||||
test("browser discovery is authenticated and served from the hosting machine", async (t) => {
|
||||
const f = await fixture(t);
|
||||
assert.equal((await fetch(f.base + "/api/discovery")).status, 401);
|
||||
const result = await fetch(f.base + "/api/discovery", {
|
||||
headers: { Cookie: f.cookie },
|
||||
});
|
||||
assert.equal(result.status, 200);
|
||||
assert.equal(
|
||||
(await result.json()).discovered_servers[0].name,
|
||||
"Linux desktop",
|
||||
);
|
||||
assert.equal(f.observations.filter((x) => x.path === "/discovery").length, 1);
|
||||
});
|
||||
|
||||
test("failed password retry keeps the previous authenticated route and socket", async (t) => {
|
||||
const f = await fixture(t);
|
||||
await f.register("one", {
|
||||
endpoints: [f.remotes[0]],
|
||||
password: "password-one",
|
||||
});
|
||||
const live = await new Promise((resolve, reject) => {
|
||||
const req = request(f.base + "/peers/one/connect", {
|
||||
headers: {
|
||||
Cookie: f.cookie,
|
||||
Origin: f.base,
|
||||
Connection: "Upgrade",
|
||||
Upgrade: "websocket",
|
||||
"Sec-WebSocket-Key": randomBytes(16).toString("base64"),
|
||||
"Sec-WebSocket-Version": "13",
|
||||
},
|
||||
});
|
||||
req.on("upgrade", (res, socket) => resolve(socket));
|
||||
req.on("response", (res) => reject(new Error(String(res.statusCode))));
|
||||
req.on("error", reject);
|
||||
req.end();
|
||||
});
|
||||
t.after(() => live.destroy());
|
||||
const failed = await f.register("one", {
|
||||
endpoints: [f.remotes[0]],
|
||||
password: "wrong-password",
|
||||
});
|
||||
const status = await failed.json();
|
||||
assert.equal(status.previous_connection_kept, true);
|
||||
assert.equal(status.needs_password, true);
|
||||
const file = await fetch(f.base + "/peers/one/files", {
|
||||
headers: { Cookie: f.cookie },
|
||||
});
|
||||
assert.equal(file.status, 200);
|
||||
assert.equal((await file.json()).cookie, "pipelines_session=remote-one");
|
||||
assert.equal(live.destroyed, false);
|
||||
});
|
||||
|
||||
test("changed aliases revoke immediately and late sign-in cannot resurrect a deleted profile", async (t) => {
|
||||
const f = await fixture(t);
|
||||
await f.register("server", {
|
||||
endpoints: [f.remotes[0]],
|
||||
password: "password-one",
|
||||
});
|
||||
const gate = f.hold("two");
|
||||
const changing = f.register("server", {
|
||||
endpoints: [f.remotes[1]],
|
||||
password: "password-two",
|
||||
});
|
||||
await gate.entered;
|
||||
assert.equal(
|
||||
(
|
||||
await fetch(
|
||||
f.base +
|
||||
"/peers/server/files?endpoint=" +
|
||||
encodeURIComponent(f.remotes[0]),
|
||||
{ headers: { Cookie: f.cookie } },
|
||||
)
|
||||
).status,
|
||||
401,
|
||||
);
|
||||
await fetch(f.base + "/api/peers/server", {
|
||||
method: "DELETE",
|
||||
headers: { Cookie: f.cookie, Origin: f.base },
|
||||
});
|
||||
gate.release();
|
||||
assert.equal((await changing).status, 409);
|
||||
assert.equal(
|
||||
(
|
||||
await fetch(f.base + "/peers/server/files", {
|
||||
headers: { Cookie: f.cookie },
|
||||
})
|
||||
).status,
|
||||
401,
|
||||
);
|
||||
});
|
||||
|
||||
test("expired registry owners are closed and pruned before the capacity check", () => {
|
||||
const registry = new PeerRegistry((req) => req.token);
|
||||
let closed = 0;
|
||||
for (let i = 0; i < 256; i++)
|
||||
registry.sessions.set(String(i), {
|
||||
used: 0,
|
||||
versions: new Map(),
|
||||
peers: new Map([
|
||||
[
|
||||
"x",
|
||||
{
|
||||
sockets: new Set([
|
||||
{
|
||||
destroy() {
|
||||
closed++;
|
||||
},
|
||||
},
|
||||
]),
|
||||
},
|
||||
],
|
||||
]),
|
||||
});
|
||||
registry.owner({ token: "new-session" });
|
||||
assert.equal(closed, 256);
|
||||
assert.equal(registry.sessions.size, 1);
|
||||
const before = registry.sessions.size;
|
||||
registry.forget({ token: "unknown-invalid-session" });
|
||||
assert.equal(registry.sessions.size, before);
|
||||
});
|
||||
Reference in New Issue
Block a user