diff --git a/bbs.conf.sample b/bbs.conf.sample index 7b11160..c1af673 100644 --- a/bbs.conf.sample +++ b/bbs.conf.sample @@ -96,7 +96,8 @@ throttle_time = 600 ; whose line has a speed of its own) may have the BBS send to them at a ; modem's speed, 300 to 115200 bit/s, ANSI art drawn as it was. Each ; picks theirs on the profile (B), or with Ctrl-B at any prompt; it is -; kept with their account. line_speeds = no: none may. line_speed: what +; kept with their account. Break (telnet's or SSH's) or Ctrl-X sends the +; rest of a slow screen at once. line_speeds = no: none may. line_speed: what ; callers get until they pick (off: full speed). line_speed_ask = yes: ; asked at each login line_speeds = yes diff --git a/docs/wiki/Configuration-Reference.md b/docs/wiki/Configuration-Reference.md index 8efef86..fe98506 100644 --- a/docs/wiki/Configuration-Reference.md +++ b/docs/wiki/Configuration-Reference.md @@ -101,9 +101,9 @@ The system: its name, database, log, logins, and what callers see. | `idle_timeout` | | yes | seconds to log in, and for a key once logged in. Examples: `600`. | | `throttle_failures` | | yes | this many failed logins (or new accounts) from one IP address or caller ID within throttle_time seconds turn it away until they age out (0: never). Callers with neither (a modem server's, no caller ID) are not throttled: one would lock out all. Examples: `10`. | | `throttle_time` | | yes | this many failed logins (or new accounts) from one IP address or caller ID within throttle_time seconds turn it away until they age out (0: never). Callers with neither (a modem server's, no caller ID) are not throttled: one would lock out all. Examples: `600`. | -| `line_speeds` | | yes | line speeds: callers by telnet, SSH or the web (not modem callers, whose line has a speed of its own) may have the BBS send to them at a modem's speed, 300 to 115200 bit/s, ANSI art drawn as it was. Each picks theirs on the profile (B), or with Ctrl-B at any prompt; it is kept with their account. line_speeds = no: none may. line_speed: what callers get until they pick (off: full speed). line_speed_ask = yes: asked at each login. Examples: `yes`. | -| `line_speed` | | yes | line speeds: callers by telnet, SSH or the web (not modem callers, whose line has a speed of its own) may have the BBS send to them at a modem's speed, 300 to 115200 bit/s, ANSI art drawn as it was. Each picks theirs on the profile (B), or with Ctrl-B at any prompt; it is kept with their account. line_speeds = no: none may. line_speed: what callers get until they pick (off: full speed). line_speed_ask = yes: asked at each login. Examples: `off`. | -| `line_speed_ask` | | yes | line speeds: callers by telnet, SSH or the web (not modem callers, whose line has a speed of its own) may have the BBS send to them at a modem's speed, 300 to 115200 bit/s, ANSI art drawn as it was. Each picks theirs on the profile (B), or with Ctrl-B at any prompt; it is kept with their account. line_speeds = no: none may. line_speed: what callers get until they pick (off: full speed). line_speed_ask = yes: asked at each login. Examples: `no`. | +| `line_speeds` | | yes | line speeds: callers by telnet, SSH or the web (not modem callers, whose line has a speed of its own) may have the BBS send to them at a modem's speed, 300 to 115200 bit/s, ANSI art drawn as it was. Each picks theirs on the profile (B), or with Ctrl-B at any prompt; it is kept with their account. Break (telnet's or SSH's) or Ctrl-X sends the rest of a slow screen at once. line_speeds = no: none may. line_speed: what callers get until they pick (off: full speed). line_speed_ask = yes: asked at each login. Examples: `yes`. | +| `line_speed` | | yes | line speeds: callers by telnet, SSH or the web (not modem callers, whose line has a speed of its own) may have the BBS send to them at a modem's speed, 300 to 115200 bit/s, ANSI art drawn as it was. Each picks theirs on the profile (B), or with Ctrl-B at any prompt; it is kept with their account. Break (telnet's or SSH's) or Ctrl-X sends the rest of a slow screen at once. line_speeds = no: none may. line_speed: what callers get until they pick (off: full speed). line_speed_ask = yes: asked at each login. Examples: `off`. | +| `line_speed_ask` | | yes | line speeds: callers by telnet, SSH or the web (not modem callers, whose line has a speed of its own) may have the BBS send to them at a modem's speed, 300 to 115200 bit/s, ANSI art drawn as it was. Each picks theirs on the profile (B), or with Ctrl-B at any prompt; it is kept with their account. Break (telnet's or SSH's) or Ctrl-X sends the rest of a slow screen at once. line_speeds = no: none may. line_speed: what callers get until they pick (off: full speed). line_speed_ask = yes: asked at each login. Examples: `no`. | | `geoip` | | yes | where callers are. geoip: a country database, a MaxMind DB (.mmdb: GeoLite2 Country, DB-IP Lite...) or a CSV of ranges, read again when it changes; with it the log, who's online and the web forum's flags give callers' countries. A city database (GeoLite2 City, DB-IP City Lite, IP2Location DB3 and up) gives the log and the sysop their region and city too (some 120 MB of memory). geoip_credit: the credit its licence asks for, at the foot of the pages that show it. See docs/wiki/Modem-Callers.md. Examples: `/var/lib/bbs/dbip-country-lite.mmdb`, `/var/lib/bbs/dbip-city-lite.mmdb`. | | `geoip_credit` | | yes | where callers are. geoip: a country database, a MaxMind DB (.mmdb: GeoLite2 Country, DB-IP Lite...) or a CSV of ranges, read again when it changes; with it the log, who's online and the web forum's flags give callers' countries. A city database (GeoLite2 City, DB-IP City Lite, IP2Location DB3 and up) gives the log and the sysop their region and city too (some 120 MB of memory). geoip_credit: the credit its licence asks for, at the foot of the pages that show it. See docs/wiki/Modem-Callers.md. Examples: `IP geolocation by DB-IP`. | | `geoip_credit_url` | | yes | where callers are. geoip: a country database, a MaxMind DB (.mmdb: GeoLite2 Country, DB-IP Lite...) or a CSV of ranges, read again when it changes; with it the log, who's online and the web forum's flags give callers' countries. A city database (GeoLite2 City, DB-IP City Lite, IP2Location DB3 and up) gives the log and the sysop their region and city too (some 120 MB of memory). geoip_credit: the credit its licence asks for, at the foot of the pages that show it. See docs/wiki/Modem-Callers.md. Examples: `https://db-ip.com`. | diff --git a/internal/server/server.go b/internal/server/server.go index 35654a8..8d4e875 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -605,6 +605,11 @@ func (s *Server) text(rw io.ReadWriter, c deadliner, l *Listener, node *bbs.Node deadliner }{rw, c} t := textui.NewTerm(conn) + if b, ok := rw.(interface{ SetOnBreak(func()) }); ok { + // Break (telnet's, SSH's) skips the rest of a screen slowed to a line speed + b.SetOnBreak(t.Break) + defer b.SetOnBreak(nil) + } t.Clean8 = true t.Ident = node.Caller.Ident if sz, ok := rw.(interface{ Size() (int, int) }); ok { diff --git a/internal/server/speed_test.go b/internal/server/speed_test.go index c70a1bc..ca07f2c 100644 --- a/internal/server/speed_test.go +++ b/internal/server/speed_test.go @@ -1,8 +1,11 @@ package server import ( + "io" + "net" "strings" "testing" + "time" "github.com/D-Modem/bbs/internal/bbs" "golang.org/x/crypto/ssh" @@ -94,3 +97,46 @@ func TestLineSpeedModem(t *testing.T) { t.Errorf("line speeds offered a modem caller: %q", d.before) } } + +// TestLineSpeedSkip: at 300 bit/s an SSH Break (RFC 4335), or Ctrl-X, +// sends the rest of the screen at once. +func TestLineSpeedSkip(t *testing.T) { + e, addr, _ := startSSH(t, nil) + u, _ := e.b.Store.UserByName("alice") + if err := e.b.Store.SetPrefs(u.ID, "baud=300"); err != nil { + t.Fatal(err) + } + c, err := sshDial(t, addr, "alice", ssh.Password("alicepass1")) + if err != nil { + t.Fatal(err) + } + s, err := c.NewSession() + if err != nil { + t.Fatal(err) + } + if err := s.RequestPty("xterm-256color", 40, 100, ssh.TerminalModes{}); err != nil { + t.Fatal(err) + } + in, _ := s.StdinPipe() + out, _ := s.StdoutPipe() + if err := s.Shell(); err != nil { + t.Fatal(err) + } + a, b := net.Pipe() + go func() { io.Copy(a, out); a.Close() }() + go io.Copy(in, a) + t.Cleanup(func() { s.Close(); b.Close() }) + d := &dialog{t: t, c: b} + + // the main menu, some 1500 bytes: most of a minute at 300 bit/s + time.Sleep(300 * time.Millisecond) + if ok, err := s.SendRequest("break", true, ssh.Marshal(struct{ Ms uint32 }{500})); err != nil || !ok { + t.Fatalf("break: %v %v", ok, err) + } + d.expect("Command: ") + // and slow again after: the profile, skipped by Ctrl-X + d.send("P") + time.Sleep(300 * time.Millisecond) + d.send("\x18") + d.expect("[Q]uit: ") +} diff --git a/internal/server/ssh.go b/internal/server/ssh.go index 5643265..ecda5b3 100644 --- a/internal/server/ssh.go +++ b/internal/server/ssh.go @@ -263,10 +263,15 @@ func (s *Server) sshCall(c net.Conn, l *Listener, caller *bbs.Caller, remote str } } c.SetReadDeadline(time.Time{}) + t := newSSHTerm(ch, c, pty) + defer t.close() go func() { for req := range creqs { + if req.Type == "break" { // RFC 4335: OpenSSH's ~B, PuTTY's Break + t.sendBreak() + } // window-change: the size is the one the session began with - req.Reply(req.Type == "window-change", nil) + req.Reply(req.Type == "window-change" || req.Type == "break", nil) } }() @@ -274,8 +279,6 @@ func (s *Server) sshCall(c net.Conn, l *Listener, caller *bbs.Caller, remote str caller.TrustedUser, caller.TrustedHow = u, sc.Permissions.Extensions["how"] } caller.Terminal = bbs.Clean(pty.Term, 40) - t := newSSHTerm(ch, c, pty) - defer t.close() node, err := s.B.Enter(caller, func() { c.Close() }) if err != nil { s.B.Log.Printf("[listen %s] %s: %v", l.Name, remote, err) @@ -319,6 +322,24 @@ type sshTerm struct { mu sync.Mutex dl time.Time changed chan struct{} // closed when dl changes + onBreak func() // SetOnBreak's +} + +// SetOnBreak has f called when the client sends a break. +func (t *sshTerm) SetOnBreak(f func()) { + t.mu.Lock() + t.onBreak = f + t.mu.Unlock() +} + +// sendBreak is the client's break: SetOnBreak's func, if set. +func (t *sshTerm) sendBreak() { + t.mu.Lock() + f := t.onBreak + t.mu.Unlock() + if f != nil { + f() + } } // newSSHTerm is ch as a terminal, conn closed when a write hangs; the pty's diff --git a/internal/telnet/telnet.go b/internal/telnet/telnet.go index 7cbcfe2..3d73375 100644 --- a/internal/telnet/telnet.go +++ b/internal/telnet/telnet.go @@ -21,6 +21,9 @@ import ( const ( SE = 240 NOP = 241 + BRK = 243 // Break + IP = 244 // Interrupt Process + AO = 245 // Abort Output GA = 249 SB = 250 WILL = 251 @@ -72,6 +75,8 @@ type Conn struct { lastCR bool early []byte // data that came while negotiating out []byte // answers to send, in order + + onBreak func() // SetOnBreak's } const ( @@ -87,6 +92,15 @@ func New(c net.Conn) *Conn { return &Conn{Conn: c, r: bufio.NewReaderSize(c, 4096)} } +// SetOnBreak has f called when the client sends Break, Interrupt Process +// or Abort Output (its Break key, or a menu's "send break"); they are seen +// as the client's data is read. +func (c *Conn) SetOnBreak(f func()) { + c.mu.Lock() + c.onBreak = f + c.mu.Unlock() +} + // TermType is the terminal type the client gave ("" if none). func (c *Conn) TermType() string { c.mu.Lock() @@ -222,6 +236,11 @@ func (c *Conn) input(b byte) (byte, bool) { c.cmd, c.state = b, stOpt case SB: c.sub, c.state = c.sub[:0], stSub + case BRK, IP, AO: + if c.onBreak != nil { + c.onBreak() + } + c.state = stData default: // NOP, GA, AYT, ...: nothing to do c.state = stData } diff --git a/internal/telnet/telnet_test.go b/internal/telnet/telnet_test.go index 3b8d7fc..60e8c71 100644 --- a/internal/telnet/telnet_test.go +++ b/internal/telnet/telnet_test.go @@ -240,3 +240,27 @@ func TestHandoffContrary(t *testing.T) { t.Error("the terminal type asked for again after DONT") } } + +// TestBreak: Break, Interrupt Process and Abort Output call SetOnBreak's +// func, and are not data. +func TestBreak(t *testing.T) { + a, b := net.Pipe() + defer a.Close() + defer b.Close() + c := New(a) + n := 0 + c.SetOnBreak(func() { n++ }) + go b.Write([]byte{'a', IAC, BRK, 'b', IAC, IP, IAC, AO, 'c'}) + got := make([]byte, 0, 3) + buf := make([]byte, 16) + for len(got) < 3 { + k, err := c.Read(buf) + if err != nil { + t.Fatal(err) + } + got = append(got, buf[:k]...) + } + if string(got) != "abc" || n != 3 { + t.Errorf("data %q, %d breaks", got, n) + } +} diff --git a/internal/textui/speed.go b/internal/textui/speed.go index 7e7f405..34248a7 100644 --- a/internal/textui/speed.go +++ b/internal/textui/speed.go @@ -3,6 +3,7 @@ package textui import ( "strconv" "sync" + "sync/atomic" "time" "github.com/D-Modem/bbs/internal/bbs" @@ -11,7 +12,9 @@ import ( // Line speeds: a caller by telnet, SSH or the web may have what the BBS // sends come at a modem's speed, as it did when ANSI art was drawn a line // at a time. Only what is sent is slowed: keys come in at once, and the -// BBS reads them as soon as it is done sending. +// BBS reads them as soon as it is done sending. A Break (telnet's, SSH's) +// or Ctrl-X while a screen is coming sends the rest of it at once, up to +// the next prompt: skipped, but drawn whole, with no escape sequence cut. // pacer is the line under a terminal, sending at most bps bits a second // (8N1: ten bits a character); 0 is the line's own speed. @@ -22,8 +25,16 @@ type pacer struct { next time.Time // when the next byte may go direct bool // a file transfer's bytes: at the line's own speed sleep func(time.Duration) + + skip atomic.Bool // Break or Ctrl-X: at the line's own speed to the next prompt + poll func() // looks for Ctrl-X (and a telnet Break) among the keys come + lastPoll time.Time + noPoll atomic.Bool // a door reads the keys: poll is not called } +// pollEvery is how often keys are looked at while a screen is slowed. +const pollEvery = 100 * time.Millisecond + // pace is how far ahead of the line a write may get: the bytes of this // long go in one write. const pace = 20 * time.Millisecond @@ -45,6 +56,15 @@ func (p *pacer) Write(b []byte) (int, error) { } sent := 0 for sent < len(b) { + if p.poll != nil && !p.noPoll.Load() && time.Since(p.lastPoll) >= pollEvery { + p.lastPoll = time.Now() + p.poll() + } + if p.skip.Load() { + m, err := p.Conn.Write(b[sent:]) + p.next = time.Now() + return sent + m, err + } n := min(chunk, len(b)-sent) if d := time.Until(p.next); d > 0 { p.sleep(d) @@ -201,3 +221,28 @@ func (s *session) pickSpeed(restore bool) error { } return s.n.SetPref("baud", v) } + +// skipKey is Ctrl-X: while a screen is slowed, the rest of it at once. +const skipKey = 0x18 + +// Break is the caller's Break (telnet's, SSH's): a screen being slowed to +// the line speed comes the rest of the way at once. Any goroutine may +// call it. +func (t *Term) Break() { t.line.skip.Store(true) } + +// pollSkip looks at the keys come while a screen is slowed: a Ctrl-X first +// among them is taken as Break. Reading lets a telnet Break be seen. +func (t *Term) pollSkip() { + if t.r.Buffered() == 0 { + t.c.SetReadDeadline(time.Now().Add(2 * time.Millisecond)) + t.r.Peek(1) + t.c.SetReadDeadline(time.Time{}) + } + if t.r.Buffered() == 0 { + return + } + if b, err := t.r.Peek(1); err == nil && b[0] == skipKey && !t.PETSCII { + t.r.ReadByte() + t.Break() + } +} diff --git a/internal/textui/speed_test.go b/internal/textui/speed_test.go index 30326b5..5a916bd 100644 --- a/internal/textui/speed_test.go +++ b/internal/textui/speed_test.go @@ -2,6 +2,8 @@ package textui import ( "bytes" + "os" + "strings" "testing" "time" ) @@ -9,7 +11,7 @@ import ( // TestPacer: at a line speed, what is sent takes as long as at a modem's // (ten bits a character); transfers' bytes and full speed do not wait. func TestPacer(t *testing.T) { - c := &bufConn{} + c := &keysConn{in: bytes.NewReader(nil)} term := NewTerm(c) data := bytes.Repeat([]byte("x"), 480) @@ -32,10 +34,66 @@ func TestPacer(t *testing.T) { if d := time.Since(start); d > 100*time.Millisecond { t.Errorf("a transfer's bytes took %v", d) } - if c.Len() != 3*len(data) { - t.Errorf("sent %d bytes, not %d", c.Len(), 3*len(data)) + if c.out.Len() != 3*len(data) { + t.Errorf("sent %d bytes, not %d", c.out.Len(), 3*len(data)) } if term.LineSpeed() != 9600 { t.Errorf("speed %d", term.LineSpeed()) } } + +// keysConn is a line with keys typed ahead, keeping what is written. +type keysConn struct { + in *bytes.Reader + out bytes.Buffer +} + +func (c *keysConn) Read(p []byte) (int, error) { + if c.in.Len() == 0 { + time.Sleep(time.Millisecond) + return 0, os.ErrDeadlineExceeded + } + return c.in.Read(p) +} +func (c *keysConn) Write(p []byte) (int, error) { return c.out.Write(p) } +func (c *keysConn) SetReadDeadline(time.Time) error { return nil } + +// TestSkip: Ctrl-X typed, or a Break, while a screen comes slowly sends the +// rest of it at once, all of it, and the next key read is slow again. +func TestSkip(t *testing.T) { + screen := strings.Repeat("ANSI art line\x1b[1;36m!\x1b[0m\r\n", 120) // 3000 bytes: 25 s at 1200 + c := &keysConn{in: bytes.NewReader([]byte{0x18, 'x'})} + term := NewTerm(c) + term.SetLineSpeed(1200) + start := time.Now() + term.Raw([]byte(screen)) + if d := time.Since(start); d > 2*time.Second { + t.Errorf("Ctrl-X: the screen took %v", d) + } + if !strings.Contains(c.out.String(), screen) { + t.Errorf("sent %d bytes, not the whole screen", c.out.Len()) + } + if r, err := term.readRune(); err != nil || r != 'x' { + t.Errorf("the key after Ctrl-X: %q %v", r, err) + } + if term.line.skip.Load() { + t.Error("still skipping after a key was read") + } + + // a Break from another goroutine (SSH's) + c2 := &keysConn{in: bytes.NewReader(nil)} + term = NewTerm(c2) + term.SetLineSpeed(1200) + go func() { + time.Sleep(200 * time.Millisecond) + term.Break() + }() + start = time.Now() + term.Raw([]byte(screen)) + if d := time.Since(start); d < 150*time.Millisecond || d > 2*time.Second { + t.Errorf("Break: the screen took %v", d) + } + if c2.out.String() != screen { + t.Errorf("sent %d bytes, not the screen", c2.out.Len()) + } +} diff --git a/internal/textui/term.go b/internal/textui/term.go index 0b5c25b..5406d49 100644 --- a/internal/textui/term.go +++ b/internal/textui/term.go @@ -166,8 +166,10 @@ func (t *Term) Raw(b []byte) error { func NewTerm(c Conn) *Term { line := &pacer{Conn: c, sleep: time.Sleep} ec := &echoConn{Conn: line} - return &Term{c: ec, echo: ec, line: line, r: bufio.NewReaderSize(c, 1024), w: bufio.NewWriterSize(ec, 4096), + t := &Term{c: ec, echo: ec, line: line, r: bufio.NewReaderSize(c, 1024), w: bufio.NewWriterSize(ec, 4096), Cols: 80, Rows: 24, Idle: 10 * time.Minute} + line.poll = t.pollSkip + return t } // echoConn gives what is written to the caller to Echo as well (for a @@ -387,6 +389,8 @@ func (t *Term) read(keys bool) (rune, error) { if err := t.Flush(); err != nil { return 0, err } + t.line.skip.Store(false) // a skipped screen ends at its prompt + t.line.noPoll.Store(false) start := time.Now() for { limit := start.Add(t.Idle) @@ -1111,6 +1115,8 @@ func (t *Term) Port() (port, error) { func (t *Term) DoorPort() (port, error) { p, err := t.Port() p.paced = true + // the door's own goroutine reads the keys till it is done: not polled + t.line.noPoll.Store(true) return p, err }