~ knotless Search/
↑↓ move↵ open⇧↵ all results

socialweb.computer/socialweb-computer-kcp

socialweb.computer: a compute market on atproto (kcp)

1 collaborator
0
0
213 lines · 5.2 KB Raw History
package main

import (
	"context"
	"crypto/sha256"
	"encoding/json"
	"fmt"
	"io"
	"net"
	"net/http"
	"net/url"
	"os"
	"sort"
	"strings"
	"time"
)

const (
	collection = "com.fedfork.exec.pins"
	tries      = 3
)

type record struct {
	owner string
	did   string
	cid   string
	value map[string]json.RawMessage
}

var client = &http.Client{Timeout: 20 * time.Second}

func get(ctx context.Context, target string) ([]byte, error) {
	var last error
	for attempt := 0; attempt < tries; attempt++ {
		if attempt > 0 {
			time.Sleep(time.Duration(attempt) * 2 * time.Second)
		}
		req, err := http.NewRequestWithContext(ctx, http.MethodGet, target, nil)
		if err != nil {
			return nil, err
		}
		resp, err := client.Do(req)
		if err != nil {
			last = err
			continue
		}
		body, err := io.ReadAll(io.LimitReader(resp.Body, 4<<20))
		resp.Body.Close()
		if err != nil {
			last = err
			continue
		}
		if resp.StatusCode == http.StatusOK {
			return body, nil
		}
		last = fmt.Errorf("%s: %s %s", target, resp.Status, strings.TrimSpace(string(body)))
		if resp.StatusCode < 500 && resp.StatusCode != http.StatusTooManyRequests {
			break
		}
	}
	return nil, last
}

func resolveHandle(ctx context.Context, handle string) (string, error) {
	txts, _ := net.DefaultResolver.LookupTXT(ctx, "_atproto."+handle)
	for _, txt := range txts {
		if did, ok := strings.CutPrefix(txt, "did="); ok {
			return did, nil
		}
	}
	body, err := get(ctx, "https://"+handle+"/.well-known/atproto-did")
	if err != nil {
		return "", fmt.Errorf("handle %s: no _atproto TXT and %w", handle, err)
	}
	return strings.TrimSpace(string(body)), nil
}

func resolvePDS(ctx context.Context, did string) (string, error) {
	var target string
	switch {
	case strings.HasPrefix(did, "did:plc:"):
		target = "https://plc.directory/" + did
	case strings.HasPrefix(did, "did:web:"):
		target = "https://" + strings.TrimPrefix(did, "did:web:") + "/.well-known/did.json"
	default:
		return "", fmt.Errorf("%s: not a did:plc or did:web", did)
	}
	body, err := get(ctx, target)
	if err != nil {
		return "", err
	}
	var doc struct {
		Service []struct {
			ID              string `json:"id"`
			ServiceEndpoint string `json:"serviceEndpoint"`
		} `json:"service"`
	}
	if err := json.Unmarshal(body, &doc); err != nil {
		return "", fmt.Errorf("%s: %w", target, err)
	}
	for _, service := range doc.Service {
		if strings.HasSuffix(service.ID, "#atproto_pds") {
			return strings.TrimRight(service.ServiceEndpoint, "/"), nil
		}
	}
	return "", fmt.Errorf("%s: no #atproto_pds service", did)
}

func read(ctx context.Context, owner, rkey string) (record, error) {
	did := owner
	if !strings.HasPrefix(owner, "did:") {
		resolved, err := resolveHandle(ctx, owner)
		if err != nil {
			return record{}, err
		}
		did = resolved
	}
	pds, err := resolvePDS(ctx, did)
	if err != nil {
		return record{}, err
	}
	query := url.Values{"repo": {did}, "collection": {collection}, "rkey": {rkey}}
	body, err := get(ctx, pds+"/xrpc/com.atproto.repo.getRecord?"+query.Encode())
	if err != nil {
		return record{}, err
	}
	var got struct {
		CID   string                     `json:"cid"`
		Value map[string]json.RawMessage `json:"value"`
	}
	if err := json.Unmarshal(body, &got); err != nil {
		return record{}, fmt.Errorf("%s %s/%s: %w", owner, collection, rkey, err)
	}
	delete(got.Value, "createdAt")
	return record{owner: owner, did: did, cid: got.CID, value: got.Value}, nil
}

func canonical(raw json.RawMessage) string {
	var decoded any
	if err := json.Unmarshal(raw, &decoded); err != nil {
		return string(raw)
	}
	encoded, err := json.Marshal(decoded)
	if err != nil {
		return string(raw)
	}
	return string(encoded)
}

func digest(value map[string]json.RawMessage) string {
	keys := make([]string, 0, len(value))
	for key := range value {
		keys = append(keys, key)
	}
	sort.Strings(keys)
	sum := sha256.New()
	for _, key := range keys {
		fmt.Fprintf(sum, "%s=%s\n", key, canonical(value[key]))
	}
	return fmt.Sprintf("%x", sum.Sum(nil))
}

func differing(a, b map[string]json.RawMessage) []string {
	seen := map[string]bool{}
	var keys []string
	for _, side := range []map[string]json.RawMessage{a, b} {
		for key := range side {
			if seen[key] {
				continue
			}
			seen[key] = true
			left, inA := a[key]
			right, inB := b[key]
			if !inA || !inB || canonical(left) != canonical(right) {
				keys = append(keys, key)
			}
		}
	}
	sort.Strings(keys)
	return keys
}

func main() {
	if len(os.Args) < 4 {
		fmt.Fprintln(os.Stderr, "usage: pinsrecords <rkey> <handle or did> <handle or did>...")
		os.Exit(2)
	}
	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
	defer cancel()
	rkey := os.Args[1]
	var records []record
	for _, owner := range os.Args[2:] {
		got, err := read(ctx, owner, rkey)
		if err != nil {
			fmt.Fprintln(os.Stderr, "pinsrecords:", err)
			os.Exit(2)
		}
		fmt.Printf("%s/%s of %s (%s): cid %s, value sha256 %s\n", collection, rkey, got.owner, got.did, got.cid, digest(got.value))
		records = append(records, got)
	}
	failed := false
	for _, other := range records[1:] {
		keys := differing(records[0].value, other.value)
		if len(keys) == 0 {
			continue
		}
		failed = true
		fmt.Fprintf(os.Stderr, "pinsrecords: %s and %s differ in: %s\n", records[0].owner, other.owner, strings.Join(keys, ", "))
	}
	if failed {
		os.Exit(1)
	}
}