An in browser local PDS localpds.at
20

Configure Feed

Select the types of activity you want to include in your feed.

Inital backend

authored by

Niall Bunting and committed by
Niall Bunting
(May 29, 2026, 9:00 AM +0100) fa6fe474 fc48d301

+310
backend/backend-linux

This is a binary file and will not be displayed.

+189
backend/backend.go
··· 1 + package main 2 + 3 + import ( 4 + "encoding/base64" 5 + "encoding/json" 6 + "log" 7 + "net/http" 8 + "strings" 9 + "sync" 10 + "time" 11 + 12 + "github.com/gin-gonic/gin" 13 + "github.com/google/uuid" 14 + "github.com/gorilla/websocket" 15 + ) 16 + 17 + const SecretKey = "your-shared-secret-key" 18 + 19 + // Base WS Message payload structure 20 + type WSMessage struct { 21 + Secret string `json:"secret,omitempty"` 22 + Type string `json:"type"` 23 + PeerID string `json:"peerId,omitempty"` 24 + Target string `json:"target,omitempty"` 25 + Sender string `json:"sender,omitempty"` 26 + RequestID string `json:"requestId,omitempty"` 27 + URL string `json:"url,omitempty"` 28 + Signal json.RawMessage `json:"signal,omitempty"` 29 + CarBytes string `json:"carBytes,omitempty"` 30 + Error string `json:"error,omitempty"` 31 + } 32 + 33 + // Thread-safe registry for connected extensions and pending HTTP requests 34 + type Registry struct { 35 + mu sync.RWMutex 36 + peers map[string]*websocket.Conn 37 + pendingSyncs map[string]chan []byte 38 + } 39 + 40 + var registry = &Registry{ 41 + peers: make(map[string]*websocket.Conn), 42 + pendingSyncs: make(map[string]chan []byte), 43 + } 44 + 45 + var upgrader = websocket.Upgrader{ 46 + CheckOrigin: func(r *http.Request) bool { 47 + return true // Allow extensions to connect from chrome-extension:// origins 48 + }, 49 + } 50 + 51 + func main() { 52 + r := gin.Default() 53 + 54 + // 1. Signaling & P2P Coordination WebSocket Endpoint 55 + r.GET("/ws/sync", func(c *gin.Context) { 56 + conn, err := upgrader.Upgrade(c.Writer, c.Request, nil) 57 + if err != nil { 58 + log.Printf("Upgrade error: %v", err) 59 + return 60 + } 61 + defer conn.Close() 62 + 63 + var currentPeerID string 64 + 65 + defer func() { 66 + if currentPeerID != "" { 67 + registry.mu.Lock() 68 + delete(registry.peers, currentPeerID) 69 + registry.mu.Unlock() 70 + log.Printf("Peer disconnected: %s", currentPeerID) 71 + } 72 + }() 73 + 74 + for { 75 + _, msgBytes, err := conn.ReadMessage() 76 + if err != nil { 77 + break 78 + } 79 + 80 + var msg WSMessage 81 + if err := json.Unmarshal(msgBytes, &msg); err != nil { 82 + continue 83 + } 84 + 85 + // All signaling operations require the shared secret guardrail 86 + if msg.Secret != SecretKey { 87 + conn.WriteJSON(WSMessage{Type: "error", Error: "Unauthorized"}) 88 + return // close connection 89 + } 90 + 91 + switch msg.Type { 92 + case "register": 93 + currentPeerID = msg.PeerID 94 + registry.mu.Lock() 95 + registry.peers[currentPeerID] = conn 96 + registry.mu.Unlock() 97 + log.Printf("Peer registered: %s", currentPeerID) 98 + 99 + case "signal": 100 + // Extension routes WebRTC SDP/ICE signaling to another peer 101 + registry.mu.RLock() 102 + targetConn, exists := registry.peers[msg.Target] 103 + registry.mu.RUnlock() 104 + 105 + if exists { 106 + targetConn.WriteJSON(WSMessage{ 107 + Type: "signal", 108 + Sender: currentPeerID, 109 + Signal: msg.Signal, 110 + }) 111 + } 112 + 113 + case "relay-response-data": 114 + // Extension returns raw CAR data requested by the XRPC proxy 115 + registry.mu.Lock() 116 + resChan, exists := registry.pendingSyncs[msg.RequestID] 117 + if exists { 118 + rawBytes, err := base64.StdEncoding.DecodeString(msg.CarBytes) 119 + if err == nil { 120 + resChan <- rawBytes 121 + } 122 + delete(registry.pendingSyncs, msg.RequestID) 123 + } 124 + registry.mu.Unlock() 125 + } 126 + } 127 + }) 128 + 129 + // 2. Bluesky XRPC Relay Sync Proxy Endpoint 130 + r.GET("/xrpc/:method", func(c *gin.Context) { 131 + method := c.Param("method") 132 + 133 + // Ensure we are only intercepting the sync methods we care about 134 + if !strings.HasPrefix(method, "com.atproto.sync.") { 135 + c.JSON(http.StatusNotFound, gin.H{"error": "MethodNotImplemented", "message": "Only sync methods are handled here"}) 136 + return 137 + } 138 + 139 + registry.mu.RLock() 140 + peerConn, online := registry.peers[did] 141 + registry.mu.RUnlock() 142 + 143 + if !online { 144 + c.JSON(http.StatusNotFound, gin.H{"error": "RepoNotFound", "message": "Peer extension is not online"}) 145 + return 146 + } 147 + 148 + reqID := uuid.New().String() 149 + responseChan := make(chan []byte, 1) 150 + 151 + registry.mu.Lock() 152 + registry.pendingSyncs[reqID] = responseChan 153 + registry.mu.Unlock() 154 + 155 + // Request the extension to compile the data for this specific route 156 + err := peerConn.WriteJSON(WSMessage{ 157 + Type: "relay-request", 158 + RequestID: reqID, 159 + URL: c.Request.URL.String(), // Preserves the full /xrpc/com.atproto.sync.getRepo?did=... 160 + }) 161 + 162 + if err != nil { 163 + registry.mu.Lock() 164 + delete(registry.pendingSyncs, reqID) 165 + registry.mu.Unlock() 166 + c.JSON(http.StatusInternalServerError, gin.H{"error": "InternalError", "message": "Failed to poll extension"}) 167 + return 168 + } 169 + 170 + // Block and wait for the extension to respond via WebSocket, or timeout 171 + select { 172 + case carBytes := <-responseChan: 173 + c.Header("Content-Type", "application/vnd.ipld.car") 174 + c.Header("Atproto-Repo-Rev", "optional-tid-placeholder") 175 + c.Data(http.StatusOK, "application/vnd.ipld.car", carBytes) 176 + 177 + case <-time.After(15 * time.Second): 178 + registry.mu.Lock() 179 + delete(registry.pendingSyncs, reqID) 180 + registry.mu.Unlock() 181 + c.JSON(http.StatusGatewayTimeout, gin.H{"error": "UpstreamTimeout", "message": "Extension failed to stream CAR blocks in time"}) 182 + } 183 + }) 184 + 185 + log.Println("Server running on :3030") 186 + if err := r.Run(":3000"); err != nil { 187 + log.Fatalf("Server failed to start: %v", err) 188 + } 189 + }
+38
backend/go.mod
··· 1 + module bsky-p2p-sync 2 + 3 + go 1.25.0 4 + 5 + require ( 6 + github.com/bytedance/gopkg v0.1.3 // indirect 7 + github.com/bytedance/sonic v1.15.0 // indirect 8 + github.com/bytedance/sonic/loader v0.5.0 // indirect 9 + github.com/cloudwego/base64x v0.1.6 // indirect 10 + github.com/gabriel-vasile/mimetype v1.4.12 // indirect 11 + github.com/gin-contrib/sse v1.1.0 // indirect 12 + github.com/gin-gonic/gin v1.12.0 // indirect 13 + github.com/go-playground/locales v0.14.1 // indirect 14 + github.com/go-playground/universal-translator v0.18.1 // indirect 15 + github.com/go-playground/validator/v10 v10.30.1 // indirect 16 + github.com/goccy/go-json v0.10.5 // indirect 17 + github.com/goccy/go-yaml v1.19.2 // indirect 18 + github.com/google/uuid v1.6.0 // indirect 19 + github.com/gorilla/websocket v1.5.3 // indirect 20 + github.com/json-iterator/go v1.1.12 // indirect 21 + github.com/klauspost/cpuid/v2 v2.3.0 // indirect 22 + github.com/leodido/go-urn v1.4.0 // indirect 23 + github.com/mattn/go-isatty v0.0.20 // indirect 24 + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect 25 + github.com/modern-go/reflect2 v1.0.2 // indirect 26 + github.com/pelletier/go-toml/v2 v2.2.4 // indirect 27 + github.com/quic-go/qpack v0.6.0 // indirect 28 + github.com/quic-go/quic-go v0.59.0 // indirect 29 + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect 30 + github.com/ugorji/go/codec v1.3.1 // indirect 31 + go.mongodb.org/mongo-driver/v2 v2.5.0 // indirect 32 + golang.org/x/arch v0.22.0 // indirect 33 + golang.org/x/crypto v0.48.0 // indirect 34 + golang.org/x/net v0.51.0 // indirect 35 + golang.org/x/sys v0.41.0 // indirect 36 + golang.org/x/text v0.34.0 // indirect 37 + google.golang.org/protobuf v1.36.10 // indirect 38 + )
+82
backend/go.sum
··· 1 + github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M= 2 + github.com/bytedance/gopkg v0.1.3/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM= 3 + github.com/bytedance/sonic v1.15.0 h1:/PXeWFaR5ElNcVE84U0dOHjiMHQOwNIx3K4ymzh/uSE= 4 + github.com/bytedance/sonic v1.15.0/go.mod h1:tFkWrPz0/CUCLEF4ri4UkHekCIcdnkqXw9VduqpJh0k= 5 + github.com/bytedance/sonic/loader v0.5.0 h1:gXH3KVnatgY7loH5/TkeVyXPfESoqSBSBEiDd5VjlgE= 6 + github.com/bytedance/sonic/loader v0.5.0/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo= 7 + github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M= 8 + github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU= 9 + github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 10 + github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 11 + github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw= 12 + github.com/gabriel-vasile/mimetype v1.4.12/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s= 13 + github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w= 14 + github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM= 15 + github.com/gin-gonic/gin v1.12.0 h1:b3YAbrZtnf8N//yjKeU2+MQsh2mY5htkZidOM7O0wG8= 16 + github.com/gin-gonic/gin v1.12.0/go.mod h1:VxccKfsSllpKshkBWgVgRniFFAzFb9csfngsqANjnLc= 17 + github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= 18 + github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= 19 + github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= 20 + github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= 21 + github.com/go-playground/validator/v10 v10.30.1 h1:f3zDSN/zOma+w6+1Wswgd9fLkdwy06ntQJp0BBvFG0w= 22 + github.com/go-playground/validator/v10 v10.30.1/go.mod h1:oSuBIQzuJxL//3MelwSLD5hc2Tu889bF0Idm9Dg26cM= 23 + github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= 24 + github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= 25 + github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM= 26 + github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= 27 + github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= 28 + github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= 29 + github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 30 + github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= 31 + github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= 32 + github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= 33 + github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= 34 + github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y= 35 + github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= 36 + github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= 37 + github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= 38 + github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= 39 + github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= 40 + github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 41 + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= 42 + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 43 + github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= 44 + github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= 45 + github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= 46 + github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= 47 + github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 48 + github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8= 49 + github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII= 50 + github.com/quic-go/quic-go v0.59.0 h1:OLJkp1Mlm/aS7dpKgTc6cnpynnD2Xg7C1pwL6vy/SAw= 51 + github.com/quic-go/quic-go v0.59.0/go.mod h1:upnsH4Ju1YkqpLXC305eW3yDZ4NfnNbmQRCMWS58IKU= 52 + github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 53 + github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= 54 + github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= 55 + github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= 56 + github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 57 + github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 58 + github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= 59 + github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= 60 + github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= 61 + github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= 62 + github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= 63 + github.com/ugorji/go/codec v1.3.1 h1:waO7eEiFDwidsBN6agj1vJQ4AG7lh2yqXyOXqhgQuyY= 64 + github.com/ugorji/go/codec v1.3.1/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4= 65 + go.mongodb.org/mongo-driver/v2 v2.5.0 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF3xaE= 66 + go.mongodb.org/mongo-driver/v2 v2.5.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0= 67 + golang.org/x/arch v0.22.0 h1:c/Zle32i5ttqRXjdLyyHZESLD/bB90DCU1g9l/0YBDI= 68 + golang.org/x/arch v0.22.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A= 69 + golang.org/x/crypto v0.48.0 h1:/VRzVqiRSggnhY7gNRxPauEQ5Drw9haKdM0jqfcCFts= 70 + golang.org/x/crypto v0.48.0/go.mod h1:r0kV5h3qnFPlQnBSrULhlsRfryS2pmewsg+XfMgkVos= 71 + golang.org/x/net v0.51.0 h1:94R/GTO7mt3/4wIKpcR5gkGmRLOuE/2hNGeWq/GBIFo= 72 + golang.org/x/net v0.51.0/go.mod h1:aamm+2QF5ogm02fjy5Bb7CQ0WMt1/WVM7FtyaTLlA9Y= 73 + golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 74 + golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k= 75 + golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= 76 + golang.org/x/text v0.34.0 h1:oL/Qq0Kdaqxa1KbNeMKwQq0reLCCaFtqu2eNuSeNHbk= 77 + golang.org/x/text v0.34.0/go.mod h1:homfLqTYRFyVYemLBFl5GgL/DWEiH5wcsQ5gSh1yziA= 78 + google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE= 79 + google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= 80 + gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 81 + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 82 + gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+1
backend/makefile
··· 1 + GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o backend-linux main.go