Files
bbs/internal/server/httpconfig.go
T
Claude f832430916 Add a web terminal page: the BBS in a browser
[http] serves /terminal (and / goes there): a page with xterm.js 5.5 and
its fit addon, built into the binary (MIT licensed, license included),
joined by a WebSocket (/terminal/ws, subprotocol bbs.terminal, binary
frames) to the text BBS, run in-process as for a telnet caller: a node
shown as "web terminal", the BBS's own login, ANSI and UTF-8, the size
the page fitted.

The page has a strict Content Security Policy (scripts only from the
BBS, no inline scripts, no framing); the WebSocket is taken only from
the BBS's own pages (or the cors origin), under the HTTP server's TLS,
connection and rate limits.  terminal = no turns it off.

The WebSocket's own stream closes the connection when a read deadline
passes, which the text BBS uses for idle time and notices: the line
adapter SSH sessions had (reads with deadlines that only end the read)
now serves both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
2026-10-12 08:26:24 +00:00

73 lines
2.1 KiB
Go

package server
import (
"errors"
"fmt"
"net"
"net/url"
"strconv"
"strings"
"time"
"github.com/Neo-Desktop/bbs-sec/internal/api"
"github.com/Neo-Desktop/bbs-sec/internal/config"
)
// httpSection reads [http].
func (c *Config) httpSection(s *config.Section) error {
if s == nil {
return nil
}
h := &api.Settings{Address: s.String("address", ":8443")}
host, _, err := net.SplitHostPort(h.Address)
if err != nil {
return fmt.Errorf("address: %v", err)
}
if h.TLS, err = s.Bool("tls", true); err != nil {
return err
}
h.CertFile, h.KeyFile = s.String("cert_file", ""), s.String("key_file", "")
if h.TLS && (h.CertFile == "" || h.KeyFile == "") {
return errors.New("cert_file and key_file are needed for TLS (or tls = no, behind a proxy that has it)")
}
if !h.TLS {
if ip := net.ParseIP(host); host == "" || ip == nil || !ip.IsLoopback() {
c.Warnings = append(c.Warnings, "[http] tls = no on an address others reach: tokens and passwords go in the clear")
}
}
if h.Anonymous, err = s.Bool("anonymous", true); err != nil {
return err
}
if h.CORS = s.String("cors", ""); h.CORS != "" {
u, err := url.Parse(h.CORS)
if err != nil || u.Scheme == "" || u.Host == "" || u.Path != "" || strings.Contains(h.CORS, "*") {
return errors.New("cors: one origin, like https://www.example.org (no *)")
}
}
if h.Metrics = s.String("metrics", ""); h.Metrics != "" {
if _, _, err := net.SplitHostPort(h.Metrics); err != nil {
return fmt.Errorf("metrics: %v", err)
}
}
if h.MaxPerIP, err = s.Int("max_per_ip", 20, 0, 10000); err != nil {
return err
}
rate := s.String("rate", "10")
if h.Rate, err = strconv.ParseFloat(rate, 64); err != nil || h.Rate < 0 || h.Rate > 10000 {
return fmt.Errorf("rate: %q is not requests a second (0: no limit)", rate)
}
if h.Burst, err = s.Int("burst", 20, 1, 100000); err != nil {
return err
}
hours, err := s.Int("session_hours", 24, 1, 24*90)
if err != nil {
return err
}
h.SessionTTL = time.Duration(hours) * time.Hour
if h.Terminal, err = s.Bool("terminal", true); err != nil {
return err
}
c.HTTP = h
return nil
}