Lotsa changes :)

This commit is contained in:
bobbydigitales
2024-10-03 12:32:59 -07:00
parent c449267f01
commit 2236ff0e6d
41 changed files with 1402 additions and 541 deletions

190
main.go
View File

@@ -11,12 +11,13 @@ import (
"os/signal"
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"
"time"
// "strings"
"github.com/andybalholm/brotli"
"github.com/gorilla/websocket"
)
@@ -60,30 +61,33 @@ func dispatchMessage(message []byte, peer *Peer) ([]byte, error) {
}
const (
writeWait = 10 * time.Second
writeWait = 120 * time.Second
)
type Peer struct {
conn *websocket.Conn
send chan []byte
lastActive time.Time
closeOnce sync.Once
}
// func removePeer(peerID string, peer *Peer) {
// delete(peerConnections, peerID)
func removePeer(peerID string, peer *Peer) {
delete(peerConnections, peerID)
// for userID, peers := range userPeers {
// delete(peers, peerID)
// if len(peers) == 0 {
// delete(userPeers, userID)
// }
// }
for userID, peers := range userPeers {
delete(peers, peerID)
if len(peers) == 0 {
delete(userPeers, userID)
}
}
// delete(connectionPeers, peer.conn)
delete(connectionPeers, peer.conn)
// // Close the peer's send channel
// close(peer.send)
// }
// Close the peer's send channel safely
peer.closeOnce.Do(func() {
close(peer.send)
})
}
func handleWebSocket(w http.ResponseWriter, r *http.Request) {
log.Println("Websocket connection!", r.RemoteAddr)
@@ -131,7 +135,10 @@ func handleWebSocket(w http.ResponseWriter, r *http.Request) {
}
// Clean up when the connection is closed
close(peer.send)
peer.closeOnce.Do(func() {
close(peer.send)
})
peerID := connectionPeers[peer.conn]
if peerID != "" {
delete(peerConnections, peerID)
@@ -186,9 +193,10 @@ var connectionPeers = make(map[*websocket.Conn]string)
func handleHello(message []byte, peer *Peer) ([]byte, error) {
var m struct {
Type string `json:"type"`
UserID string `json:"user_id"`
PeerID string `json:"peer_id"`
Type string `json:"type"`
UserID string `json:"user_id"`
PeerID string `json:"peer_id"`
KnownUsers []string `json:"known_users"`
}
if err := json.Unmarshal(message, &m); err != nil {
@@ -200,6 +208,16 @@ func handleHello(message []byte, peer *Peer) ([]byte, error) {
userPeers[m.UserID] = make(PeerSet)
}
for _, knownUserID := range m.KnownUsers {
fmt.Printf("Adding user %s for peer %s\n", knownUserID, m.PeerID)
if userPeers[knownUserID] == nil {
userPeers[knownUserID] = make(PeerSet)
}
userPeers[knownUserID][m.PeerID] = struct{}{}
}
userPeers[m.UserID][m.PeerID] = struct{}{}
peerConnections[m.PeerID] = peer
connectionPeers[peer.conn] = m.PeerID
@@ -266,46 +284,74 @@ func (w *brotliResponseWriter) Write(b []byte) (int, error) {
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"))
// For anything under /static/, we serve it, unless it's a directory
// Otherwise we serve index.html and the app does the routing nad rendering.
log.Printf("%s %s %s", r.URL.Path, r.RemoteAddr, r.UserAgent())
if r.URL.Path == "/sw.js" {
http.ServeFile(w, r, filepath.Join(root, "static/sw.js"))
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)
if r.URL.Path == "/robots.txt" {
http.ServeFile(w, r, filepath.Join(root, "static/robots.txt"))
return
}
log.Printf("Serving: %s to ip %s, useragent %s", r.URL.Path, r.RemoteAddr, r.UserAgent())
if r.URL.Path == "/favicon.ico" {
http.ServeFile(w, r, filepath.Join(root, "static/favicon.ico"))
return
}
if strings.Contains(r.URL.Path, "/static/") {
log.Print("Serving static")
path := filepath.Join(root, r.URL.Path)
info, err := os.Stat(path)
if err != nil || info.IsDir() {
log.Printf("404 File not found/dir")
http.NotFound(w, r)
return
}
log.Printf("Serving")
h.ServeHTTP(w, r)
return
}
// // Serve index.html when root is requested
// if r.URL.Path == "/" {
log.Printf("Serving index %s", r.URL.Path)
http.ServeFile(w, r, filepath.Join(root, "/static/index.html"))
// return
// }
// w.Header().Set("Cache-Control", "no-cache")
// Check if client supports Brotli encoding
// if strings.Contains(r.Header.Get("Accept-Encoding"), "br") {
if false {
w.Header().Set("Content-Encoding", "br")
w.Header().Del("Content-Length") // Cannot know content length with compressed data
// if false {
// 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()
// // 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,
}
// // 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
}
// // Serve the file using http.ServeFile
// http.ServeFile(bw, r, path)
// return
// }
h.ServeHTTP(w, r)
// h.ServeHTTP(w, r)
}
}
@@ -348,39 +394,39 @@ func main() {
Handler: nil, // Use the default ServeMux
}
// // Start the inactivity monitor goroutine
// go func() {
// ticker := time.NewTicker(10 * time.Second)
// defer ticker.Stop()
// Start the inactivity monitor goroutine
go func() {
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
// for {
// select {
// case <-done:
// return
// case <-ticker.C:
// now := time.Now()
for {
select {
case <-done:
return
case <-ticker.C:
now := time.Now()
// // Collect inactive peers
// var inactivePeers []string
// for peerID, peer := range peerConnections {
// if now.Sub(peer.lastActive) > 60*time.Second {
// inactivePeers = append(inactivePeers, peerID)
// }
// }
// Collect inactive peers
var inactivePeers []string
for peerID, peer := range peerConnections {
if now.Sub(peer.lastActive) > 60*time.Second {
inactivePeers = append(inactivePeers, peerID)
}
}
// // Remove inactive peers
// for _, peerID := range inactivePeers {
// peer := peerConnections[peerID]
// Remove inactive peers
for _, peerID := range inactivePeers {
peer := peerConnections[peerID]
// if peer != nil {
// log.Printf("Peer %s inactive for more than 60 seconds. Closing connection.", peerID)
// peer.conn.Close()
// removePeer(peerID, peer)
// }
// }
// }
// }
// }()
if peer != nil {
log.Printf("Peer %s inactive for more than 60 seconds. Closing connection.", peerID)
peer.conn.Close()
removePeer(peerID, peer)
}
}
}
}
}()
// Run a goroutine to handle graceful shutdown
go func() {