262 lines
7.6 KiB
Go
262 lines
7.6 KiB
Go
// TODO
|
|
// 1. Convery network messages to flatbuffers so we don't need to parse JSON in Go.
|
|
// Use binary WS messages. Generate for Go and JS and use that everywhere. We can
|
|
// also use this for WebRTC messages.
|
|
// 2. Keep a list of all nodes that bootstrapped in the last N minutes.
|
|
// 3. When a node bootstraps, send it a random list of the nodes we know about.
|
|
// 4. Do the signalling to connect those nodes.
|
|
// 5. Each node will know about N other nodes. To find get the data for a feed,
|
|
// a node will need to find another node that has that data. To do that we'll need to
|
|
// implement a search message that is sent to all currently connected nodes, and they
|
|
// forward to all their nodes, passing back the address of the node that has the data.
|
|
// Once we find it, we'll do the signalling to connect to it via Web RTC via our existing connected nodes.
|
|
// -----
|
|
// Feeds. People can curate feeds which can be any combination of hashtags, serch terms and users.
|
|
// Invite-only communities. Just block everyone else even if they post to it.
|
|
// Limit to friends and friends of friends
|
|
|
|
// MVP
|
|
// You connect to the person you want to get the post from to get the post
|
|
// they give you the post
|
|
// If they're offline, you can't get their updates.
|
|
// This is very stupid, but it's simplest thing.
|
|
// The bootstrap server connects you to them directly via WebRTC
|
|
// This will make the thing actually function as a little toy for people to play with.
|
|
// This will let us test whether background tabs respond to webrtc requests.
|
|
|
|
// THEN
|
|
// Need to have identity sorted out
|
|
// When you read someone's posts, you also cache them locally
|
|
// cache priority goes mutuals->people you follow->people who you folllow, follow, so you're always
|
|
// caching your mutual's posts
|
|
// Posts are samll, so caching per-post will work fine.
|
|
// Then the process is for the bootstrap server to remember all nodes and what they're caching
|
|
// This will allow distributed content delivery but put a memory and bendwidth strain on the
|
|
// bootstrap sever. Look into Web Transport for the raspberry pi overhead. Could buy a few more RPIs
|
|
// and make a little cluster
|
|
|
|
// ✅ Domain name so we can get a certificate and serve HTTPS / HTTP3
|
|
|
|
// Think about compiling Typescript on initial access and caching the JS in a service worker
|
|
// so you don't need a build system to change things.
|
|
// Think about self-hosting the client so the system can be completely self-hosted
|
|
|
|
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/andybalholm/brotli"
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
var upgrader = websocket.Upgrader{
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
origin := r.Header.Get("Origin")
|
|
return origin == "https://ddlion.net"
|
|
},
|
|
}
|
|
|
|
func websocketCloseHandler(code int, text string) error {
|
|
log.Print("Client closed websocket.")
|
|
return nil
|
|
}
|
|
|
|
type Message struct {
|
|
Type string `json:"type"`
|
|
}
|
|
|
|
type MessageHandler func([]byte, *websocket.Conn) error
|
|
|
|
var messageHandlers = make(map[string]MessageHandler)
|
|
|
|
func registerHandler(messageType string, handler MessageHandler) {
|
|
messageHandlers[messageType] = handler
|
|
}
|
|
|
|
func dispatchMessage(message []byte, conn *websocket.Conn) error {
|
|
var msg Message
|
|
if err := json.Unmarshal(message, &msg); err != nil {
|
|
return err
|
|
}
|
|
|
|
handler, ok := messageHandlers[msg.Type]
|
|
if !ok {
|
|
log.Printf("No handler registered for message type: %s", msg.Type)
|
|
return nil
|
|
}
|
|
|
|
return handler(message, conn)
|
|
}
|
|
|
|
func handleWebSocket(w http.ResponseWriter, r *http.Request) {
|
|
log.Println("Websocket connection!", r.RemoteAddr)
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
if err != nil {
|
|
log.Println("Upgrade error:", err)
|
|
return
|
|
}
|
|
defer conn.Close()
|
|
|
|
conn.SetCloseHandler(websocketCloseHandler)
|
|
|
|
for {
|
|
_, message, err := conn.ReadMessage()
|
|
if err != nil {
|
|
log.Println("ReadMessage error:", err)
|
|
break
|
|
}
|
|
log.Printf("recv: %s", message)
|
|
|
|
if err := dispatchMessage(message, conn); err != nil {
|
|
log.Printf("Error dispatching message: %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Example handlers
|
|
func handlePing(message []byte, conn *websocket.Conn) error {
|
|
var pingMsg struct {
|
|
Type string `json:"type"`
|
|
PeerID string `json:"peer_id"`
|
|
}
|
|
if err := json.Unmarshal(message, &pingMsg); err != nil {
|
|
return err
|
|
}
|
|
log.Printf("Received ping from peer: %s", pingMsg.PeerID)
|
|
return nil
|
|
}
|
|
|
|
type PeerSet map[string]struct{}
|
|
|
|
var userPeers = make(map[string]PeerSet)
|
|
var peerConnections = make(map[string]*websocket.Conn)
|
|
|
|
func handleHello(message []byte, conn *websocket.Conn) error {
|
|
|
|
var m struct {
|
|
Type string `json:"type"`
|
|
UserID string `json:"user_id"`
|
|
PeerID string `json:"peer_id"`
|
|
}
|
|
|
|
if err := json.Unmarshal(message, &m); err != nil {
|
|
return err
|
|
}
|
|
|
|
if userPeers[m.UserID] == nil {
|
|
userPeers[m.UserID] = make(PeerSet)
|
|
}
|
|
|
|
userPeers[m.UserID][m.PeerID] = struct{}{}
|
|
peerConnections[m.PeerID] = conn
|
|
|
|
jsonData, _ := json.MarshalIndent(userPeers, "", " ")
|
|
fmt.Println(string(jsonData), peerConnections)
|
|
|
|
log.Printf("Received connect from peer: %s, user:%s", m.PeerID, m.UserID)
|
|
return nil
|
|
}
|
|
|
|
// LoggingHandler logs requests and delegates them to the underlying handler.
|
|
// type LoggingHandler struct {
|
|
// handler http.Handler
|
|
// }
|
|
|
|
// func (lh *LoggingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
// log.Printf("Serving file: %s", r.URL.Path)
|
|
// lh.handler.ServeHTTP(w, r)
|
|
// }
|
|
|
|
// BrotliResponseWriter wraps http.ResponseWriter to support Brotli compression
|
|
type brotliResponseWriter struct {
|
|
http.ResponseWriter
|
|
Writer io.Writer
|
|
}
|
|
|
|
func (w *brotliResponseWriter) Write(b []byte) (int, error) {
|
|
return w.Writer.Write(b)
|
|
}
|
|
|
|
// noDirListing wraps an http.FileServer handler to prevent directory listings
|
|
func noDirListing(h http.Handler, root string) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Serve index.html when root is requested
|
|
if r.URL.Path == "/" {
|
|
http.ServeFile(w, r, filepath.Join(root, "index.html"))
|
|
return
|
|
}
|
|
|
|
path := filepath.Join(root, r.URL.Path)
|
|
info, err := os.Stat(path)
|
|
if err != nil || info.IsDir() {
|
|
log.Printf("404 File not found/dir serving: %s to ip %s, useragent %s", r.URL.Path, r.RemoteAddr, r.UserAgent())
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
log.Printf("Serving: %s to ip %s, useragent %s", r.URL.Path, r.RemoteAddr, r.UserAgent())
|
|
|
|
// Check if client supports Brotli encoding
|
|
if strings.Contains(r.Header.Get("Accept-Encoding"), "br") {
|
|
w.Header().Set("Content-Encoding", "br")
|
|
w.Header().Del("Content-Length") // Cannot know content length with compressed data
|
|
|
|
// Wrap the ResponseWriter with Brotli writer
|
|
brWriter := brotli.NewWriter(w)
|
|
defer brWriter.Close()
|
|
|
|
// Create a ResponseWriter that writes to brWriter
|
|
bw := &brotliResponseWriter{
|
|
ResponseWriter: w,
|
|
Writer: brWriter,
|
|
}
|
|
|
|
// Serve the file using http.ServeFile
|
|
http.ServeFile(bw, r, path)
|
|
return
|
|
}
|
|
|
|
h.ServeHTTP(w, r)
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
dir := "./"
|
|
port := 6789
|
|
|
|
addr := ":" + strconv.Itoa(port)
|
|
log.Printf("Starting server on %s", addr)
|
|
|
|
// Register handlers
|
|
registerHandler("hello", handleHello)
|
|
registerHandler("ping", handlePing)
|
|
|
|
// Set up file server and WebSocket endpoint
|
|
fs := http.FileServer(http.Dir(dir))
|
|
// loggingHandler := &LoggingHandler{handler: fs}
|
|
// http.Handle("/", loggingHandler)
|
|
http.Handle("/", noDirListing(fs, dir))
|
|
|
|
http.HandleFunc("/ws", handleWebSocket)
|
|
|
|
// Configure and start the HTTP server
|
|
server := &http.Server{
|
|
Addr: addr,
|
|
Handler: nil, // nil uses the default ServeMux, which we configured above
|
|
}
|
|
|
|
log.Printf("Server is configured and serving on port %d...", port)
|
|
log.Fatal(server.ListenAndServeTLS("/etc/letsencrypt/live/ddlion.net/fullchain.pem", "/etc/letsencrypt/live/ddlion.net/privkey.pem"))
|
|
}
|