package net import ( "context" "crypto/rand" "crypto/rsa" "crypto/tls" "crypto/x509" "crypto/x509/pkix" "fmt" "log" "math/big" "net" "net/http" "sort" "sync" "sync/atomic" "time" "thehouseoficarus/internal/color" "thehouseoficarus/internal/config" "thehouseoficarus/internal/player" ) type SessionState int const ( StateAccountName SessionState = iota StatePassword StateNewAccountPass StateNewAccount StateMenu StateNewCharName StateNewCharConfirm StateRenameAccount StateRenameChar StateRenameCharName StateDeleteChar StatePurgeAccount StateGame StateChangeDesc StateTalk StateTalkSequence StateDropAllConfirm StateRecipeChoice StateHowMany StateSmithProduct StateFletchProduct StateCraftProduct StateProductChoice StateColorChoice StateBank StateHacking StateDangerConfirm StateUndigConfirm ) type Session struct { Conn Conn State SessionState Account *player.Account Player *player.Player PendingChar string PendingPass string PendingMenu []map[string]string PendingItemID string PendingSkill string PendingLastFlag string PendingBackground bool Disconnecting bool DisconnectTicks int PendingUndigDir string PendingUndigRoom int promptVisible bool writeMu sync.Mutex // Outbound queueing. Transport sessions (telnet/TLS/websocket) are put // into queued mode by startWriter before their input goroutine begins: // Write* then enqueues into outbox, and the dedicated writer goroutine // performs the network I/O with a per-message write deadline. A stalled // or slow client therefore blocks at most its own queue, never the // world; an overflowing queue disconnects only that client. Sessions // without a writer (tests, synthetic conns) keep the legacy synchronous // path so message construction is shared and behavior identical. outbox chan string writerOnce sync.Once closeOnce sync.Once writeDone chan struct{} // seq is a monotonically increasing sequence number assigned when the // session is added to a Hub. Hub.AllSessions and Hub.PlayersInRoom sort // sessions by seq so per-tick consumers iterate players in a stable, // registration order instead of Go's randomized map iteration order. // Sessions constructed without being added (e.g. in tests) keep seq=0 // and tie-break by map-append order, which is also deterministic. seq uint64 } // sessionSeq is the source of Session.seq values. It is incremented under the // adding Hub's mutex, so no extra atomicity is needed for correctness; atomic // access keeps the package-level default counter safe if Hub.Add is ever // called concurrently across hubs (it isn't today, but this is defensive). var sessionSeq atomic.Uint64 type Server struct { config *config.Config telnetLn net.Listener telnetTLSLn net.Listener httpServer *http.Server httpsServer *http.Server httpsTLSConfig *tls.Config hub *Hub handler func(*Session, string) done chan struct{} shutdownOnce sync.Once listenErr error } type Hub struct { mu sync.Mutex sessions map[*Session]bool rooms map[int]map[*Session]bool onRemove func(*Session) onDisconnect func(*Session) } // writeQueueBounds bound the outbound queue: maxQueuedMessages before a // client is disconnected for falling behind, writeDeadline per message, and // closeFlushTimeout to drain the queue when the session is closing. const ( maxQueuedMessages = 512 writeDeadline = 5 * time.Second closeFlushTimeout = 2 * time.Second ) // startWriter puts the session into queued mode: writes are enqueued and a // dedicated goroutine drains the queue to the network with per-message write // deadlines. The goroutine closes the transport after the queue drains, so a // Close() during shutdown flushes pending output before the socket drops. // Must be called before the session's input goroutine starts. func (sess *Session) StartWriter() { sess.writerOnce.Do(func() { sess.outbox = make(chan string, maxQueuedMessages) sess.writeDone = make(chan struct{}) go func() { defer close(sess.writeDone) for msg := range sess.outbox { if dl, ok := sess.Conn.(interface { SetWriteDeadline(time.Time) error }); ok { _ = dl.SetWriteDeadline(time.Now().Add(writeDeadline)) } if _, err := sess.Conn.Write([]byte(msg)); err != nil { log.Printf("session write error: %v", err) // Abandon the queue and drop the transport; the input // goroutine notices the closed connection on its next // read and performs normal removal. sess.Conn.Close() return } } // Queue closed and drained: flush any final bytes and close. if dl, ok := sess.Conn.(interface { SetWriteDeadline(time.Time) error }); ok { _ = dl.SetWriteDeadline(time.Now().Add(closeFlushTimeout)) } sess.Conn.Close() }() }) } // enqueue appends msg to the outbound queue. When the client's queue is full // the session is disconnected instead of blocking the caller: the world must // never wait on a single slow client. func (sess *Session) enqueue(msg string) { select { case sess.outbox <- msg: default: log.Printf("session write queue overflow; disconnecting slow client") sess.Close() } } func NewHub() *Hub { return &Hub{ sessions: make(map[*Session]bool), rooms: make(map[int]map[*Session]bool), } } func (h *Hub) OnRemove(cb func(*Session)) { h.mu.Lock() defer h.mu.Unlock() h.onRemove = cb } func (h *Hub) OnDisconnect(cb func(*Session)) { h.mu.Lock() defer h.mu.Unlock() h.onDisconnect = cb } func (h *Hub) Add(s *Session) { h.mu.Lock() defer h.mu.Unlock() s.seq = sessionSeq.Add(1) h.sessions[s] = true } func (h *Hub) Remove(s *Session) { h.mu.Lock() if s.Player != nil && !s.Disconnecting { s.Disconnecting = true s.DisconnectTicks = 10 onDisconnect := h.onDisconnect h.mu.Unlock() if onDisconnect != nil { onDisconnect(s) } return } callback := h.hardRemoveLocked(s) h.mu.Unlock() // The removal callback runs outside the Hub mutex: it performs saves // and game-state cleanup, and must never serialize behind (or hold up) // Hub membership operations. if callback != nil { callback(s) } } func (h *Hub) HardRemove(s *Session) { h.mu.Lock() callback := h.hardRemoveLocked(s) h.mu.Unlock() if callback != nil { callback(s) } } // hardRemoveLocked removes the session from all membership structures and // returns the removal callback for the caller to invoke AFTER releasing the // Hub mutex (nil when none is registered). func (h *Hub) hardRemoveLocked(s *Session) func(*Session) { delete(h.sessions, s) for _, room := range h.rooms { delete(room, s) } return h.onRemove } func (h *Hub) EnterRoom(s *Session, roomID int) { h.mu.Lock() defer h.mu.Unlock() h.leaveRoomLocked(s) if h.rooms[roomID] == nil { h.rooms[roomID] = make(map[*Session]bool) } h.rooms[roomID][s] = true } func (h *Hub) LeaveRoom(s *Session) { h.mu.Lock() defer h.mu.Unlock() h.leaveRoomLocked(s) } func (h *Hub) leaveRoomLocked(s *Session) { for _, room := range h.rooms { delete(room, s) } } // RekeyRooms remaps Hub room membership buckets per idMap (oldID -> newID). // Uses delete-all-then-set-all so a swap (A<->B) cannot clobber an entry // mid-rekey. Sessions' own player state is migrated by the game layer; this // only moves the membership buckets. func (h *Hub) RekeyRooms(idMap map[int]int) { h.mu.Lock() defer h.mu.Unlock() if len(idMap) == 0 { return } moved := make(map[int]map[*Session]bool, len(h.rooms)) for id, set := range h.rooms { if newID, ok := idMap[id]; ok && newID != id { moved[newID] = set delete(h.rooms, id) } } for newID, set := range moved { h.rooms[newID] = set } } func (h *Hub) AllSessions() []*Session { h.mu.Lock() defer h.mu.Unlock() var out []*Session for s := range h.sessions { out = append(out, s) } sort.SliceStable(out, func(i, j int) bool { return out[i].seq < out[j].seq }) return out } func (h *Hub) PlayersInRoom(roomID int) []*Session { h.mu.Lock() defer h.mu.Unlock() var out []*Session if room, ok := h.rooms[roomID]; ok { for s := range room { out = append(out, s) } } sort.SliceStable(out, func(i, j int) bool { return out[i].seq < out[j].seq }) return out } func NewServer(cfg *config.Config) (*Server, error) { s := &Server{ config: cfg, hub: NewHub(), done: make(chan struct{}), } if cfg.Telnet.Enabled { addr := fmt.Sprintf(":%d", cfg.Telnet.Port) ln, err := net.Listen("tcp", addr) if err != nil { return nil, fmt.Errorf("telnet listen: %w", err) } s.telnetLn = ln } if cfg.TelnetTLS.Enabled { cert, err := loadOrGenerateCert(cfg, "telnet_tls") if err != nil { return nil, fmt.Errorf("telnet_tls cert: %w", err) } tlsCfg := &tls.Config{ Certificates: []tls.Certificate{cert}, MinVersion: tls.VersionTLS12, } addr := fmt.Sprintf(":%d", cfg.TelnetTLS.Port) ln, err := net.Listen("tcp", addr) if err != nil { return nil, fmt.Errorf("telnet_tls listen: %w", err) } s.telnetTLSLn = tls.NewListener(ln, tlsCfg) } if cfg.HTTP.Enabled { s.httpServer = &http.Server{ Addr: fmt.Sprintf(":%d", cfg.HTTP.Port), Handler: newHTTPMux(s), } } if cfg.HTTPS.Enabled { cert, err := loadOrGenerateCert(cfg, "https") if err != nil { return nil, fmt.Errorf("https cert: %w", err) } s.httpsTLSConfig = &tls.Config{ Certificates: []tls.Certificate{cert}, MinVersion: tls.VersionTLS12, } s.httpsServer = &http.Server{ Addr: fmt.Sprintf(":%d", cfg.HTTPS.Port), Handler: newHTTPMux(s), } } return s, nil } func loadOrGenerateCert(cfg *config.Config, service string) (tls.Certificate, error) { if cfg.TLS.CertFile != "" && cfg.TLS.KeyFile != "" { return tls.LoadX509KeyPair(cfg.TLS.CertFile, cfg.TLS.KeyFile) } log.Printf("%s: using self-signed certificate (no shared tls cert configured)", service) return generateSelfSignedCert() } func generateSelfSignedCert() (tls.Certificate, error) { key, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { return tls.Certificate{}, fmt.Errorf("rsa key generation: %w", err) } serial, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128)) if err != nil { return tls.Certificate{}, fmt.Errorf("serial number: %w", err) } now := time.Now() template := &x509.Certificate{ SerialNumber: serial, Subject: pkix.Name{ CommonName: "THOI Self-Signed Certificate", }, NotBefore: now.Add(-1 * time.Hour), NotAfter: now.Add(365 * 24 * time.Hour), KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, BasicConstraintsValid: true, IsCA: true, DNSNames: []string{"localhost"}, IPAddresses: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("::1")}, } certDER, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key) if err != nil { return tls.Certificate{}, fmt.Errorf("x509 creation: %w", err) } return tls.Certificate{ Certificate: [][]byte{certDER}, PrivateKey: key, }, nil } func (s *Server) Hub() *Hub { return s.hub } func (s *Server) ListenAndServe(handler func(*Session, string)) error { s.handler = handler if s.telnetLn != nil { go s.serveTelnet() } if s.telnetTLSLn != nil { go s.serveTelnetTLS() } if s.httpServer != nil { go s.serveHTTP() } if s.httpsServer != nil { go s.serveHTTPS() } if s.telnetLn == nil && s.telnetTLSLn == nil && s.httpServer == nil && s.httpsServer == nil { return fmt.Errorf("no listeners configured") } // Block until Shutdown is called (or a fatal listener error occurs). <-s.done return s.listenErr } // Shutdown stops all transports: HTTP(S) servers drain, listeners close, and // every session is closed (its outbound queue drains first). ListenAndServe // returns after Shutdown completes. It is safe to call more than once. func (s *Server) Shutdown() { s.shutdownOnce.Do(func() { if s.httpServer != nil { ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() _ = s.httpServer.Shutdown(ctx) } if s.httpsServer != nil { ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() _ = s.httpsServer.Shutdown(ctx) } if s.telnetLn != nil { s.telnetLn.Close() } if s.telnetTLSLn != nil { s.telnetTLSLn.Close() } for _, sess := range s.hub.AllSessions() { sess.Close() } close(s.done) }) <-s.done } func (s *Server) serveTelnet() { for { conn, err := s.telnetLn.Accept() if err != nil { return } sess := &Session{ Conn: newTCPConn(conn), State: StateAccountName, } s.hub.Add(sess) go s.handleSession(sess, s.handler) } } func (s *Server) serveTelnetTLS() { for { conn, err := s.telnetTLSLn.Accept() if err != nil { return } sess := &Session{ Conn: newTCPConn(conn), State: StateAccountName, } s.hub.Add(sess) go s.handleSession(sess, s.handler) } } func (s *Server) serveHTTP() { err := s.httpServer.ListenAndServe() if err != nil && err != http.ErrServerClosed { log.Printf("http server error: %v", err) } } func (s *Server) serveHTTPS() { addr := fmt.Sprintf(":%d", s.config.HTTPS.Port) ln, err := net.Listen("tcp", addr) if err != nil { log.Printf("https server error: %v", err) return } tlsLn := tls.NewListener(ln, s.httpsTLSConfig) if err := s.httpsServer.Serve(tlsLn); err != nil && err != http.ErrServerClosed { log.Printf("https server error: %v", err) } } func (s *Server) handleSession(sess *Session, handler func(*Session, string)) { defer func() { if r := recover(); r != nil { log.Printf("session panic: %v", r) } s.hub.Remove(sess) sess.Close() }() sess.Write("\033[2J\033[H") sess.Write(WelcomeBanner()) sess.Write("What's your account name? ") for { line, err := sess.Conn.ReadMessage() if err != nil { log.Printf("session read error: %v", err) return } if len(line) > 1024 { line = line[:1024] } handler(sess, line) } } // wrap applies the player's wrap_width option to outgoing text. It is a no-op // before login (no Player) so the welcome banner is never reflowed. The width // is clamped to a minimum of 80 columns. func (sess *Session) wrap(msg string) string { if sess.Player == nil { return msg } width := sess.Player.OptionInt("wrap_width") if width < 80 { width = 80 } return color.WrapANSI(msg, width) } // ClearPrompt marks the prompt as consumed so the next Write/WriteLine does not // emit a leading \r\n. Useful for clients that locally advance the line on Enter. func (sess *Session) ClearPrompt() { sess.writeMu.Lock() defer sess.writeMu.Unlock() sess.promptVisible = false } // dispatch delivers a fully-formatted message to the transport: enqueued in // queued mode (the writer goroutine owns the network), written synchronously // in direct mode (tests, synthetic conns). Caller holds writeMu. func (sess *Session) dispatch(msg string) { if sess.outbox != nil { sess.enqueue(msg) return } sess.Conn.Write([]byte(msg)) } // Write sends raw text with no wrapping. Used for prompts and partial lines. func (sess *Session) Write(msg string) { sess.writeMu.Lock() defer sess.writeMu.Unlock() if sess.promptVisible { sess.dispatch("\r\n") sess.promptVisible = false } sess.dispatch(msg) } func (sess *Session) WriteLine(msg string) { msg = sess.wrap(msg) sess.writeMu.Lock() defer sess.writeMu.Unlock() if sess.promptVisible { sess.dispatch("\r\n") sess.promptVisible = false } sess.dispatch(msg + "\r\n") } func (sess *Session) WriteLines(lines ...string) { sess.writeMu.Lock() defer sess.writeMu.Unlock() for i, l := range lines { if i == 0 && sess.promptVisible { sess.dispatch("\r\n") sess.promptVisible = false } sess.dispatch(sess.wrap(l) + "\r\n") } } func (sess *Session) Writef(format string, args ...interface{}) { msg := sess.wrap(fmt.Sprintf(format, args...)) sess.writeMu.Lock() defer sess.writeMu.Unlock() if sess.promptVisible { sess.dispatch("\r\n") sess.promptVisible = false } sess.dispatch(msg) } func (sess *Session) WritePrompt(prompt string) { sess.writeMu.Lock() defer sess.writeMu.Unlock() if sess.promptVisible { return } sess.dispatch(prompt) sess.promptVisible = true } func (sess *Session) Reprompt(prompt string) { sess.writeMu.Lock() defer sess.writeMu.Unlock() sess.dispatch(prompt) sess.promptVisible = true } // Close stops the session. In queued mode it closes the queue so the writer // goroutine drains pending messages and then closes the transport; in direct // mode it closes the transport immediately. Idempotent. func (sess *Session) Close() error { if sess.outbox != nil { sess.closeOnce.Do(func() { close(sess.outbox) }) return nil } var err error sess.closeOnce.Do(func() { err = sess.Conn.Close() }) return err }