Files
Claude 4d8f8a9f11 ZMODEM: resume, and zstd compression with CANZSTD/ZTZSTD
The receiver sets CANZSTD (0x40) in the ZF1 of its ZRINIT. The sender
sets ZTZSTD (8) in the ZF2 of a ZFILE to send that file as independent
zstd frames (at most 64 KiB of the file each, halved on each ZRPOS).
All positions stay offsets in the file, and the receiver writes and
acknowledges only whole frames, so ZRPOS recovery and resume work as
before. Legacy peers do not set the bit and get plain ZMODEM.

Uploads of a known size that stop early are kept for 7 days and resumed
by ZRPOS when the same user sends the same file again. Downloads seek on
ZRPOS (the download reader did not seek before). A second ZRINIT after
the first is now dropped, so its stale ZRPOS no longer sends a segment
again.

zmodem_zstd in [general] turns zstd off.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRR1WNeCEtXUcjE8RgY
2026-11-20 22:04:05 +00:00

112 lines
3.4 KiB
Go

package server
import (
"errors"
"fmt"
"net"
"strconv"
"strings"
"github.com/D-Modem/bbs/internal/config"
"github.com/D-Modem/bbs/internal/ftp"
)
// parseSize reads a size: a number of bytes, or with K, M or G.
func parseSize(s *config.Section, key string, def int64) (int64, error) {
v := strings.ToUpper(strings.TrimSpace(s.String(key, "")))
if v == "" {
return def, nil
}
mult := int64(1)
switch {
case strings.HasSuffix(v, "K"):
mult, v = 1<<10, strings.TrimSuffix(v, "K")
case strings.HasSuffix(v, "M"):
mult, v = 1<<20, strings.TrimSuffix(v, "M")
case strings.HasSuffix(v, "G"):
mult, v = 1<<30, strings.TrimSuffix(v, "G")
}
n, err := strconv.ParseInt(v, 10, 64)
if err != nil || n < 0 || n > (1<<42)/mult {
return 0, fmt.Errorf("%s: %q is not a size (bytes, or with K, M, G)", key, s.String(key, ""))
}
return n * mult, nil
}
// files reads the file areas' options of [general].
func (c *Config) files(g *config.Section) error {
c.FilesDir = g.String("files_dir", c.FilesDir)
var err error
if c.MaxFileSize, err = parseSize(g, "max_file_size", 64<<20); err != nil {
return err
}
if c.MaxFileSize == 0 {
return errors.New("max_file_size: 0 takes no files")
}
if c.UploadQuota, err = parseSize(g, "upload_quota", 256<<20); err != nil {
return err
}
if c.ZmodemZstd, err = g.Bool("zmodem_zstd", true); err != nil {
return err
}
return nil
}
// ftpSection reads [ftp].
func (c *Config) ftpSection(f *config.Section) error {
if f == nil {
return nil
}
set := &ftp.Settings{Address: f.String("address", ":2121"), Banner: c.Settings.Name}
if _, _, err := net.SplitHostPort(set.Address); err != nil {
return fmt.Errorf("address: %v", err)
}
set.PublicHost = f.String("public_host", "")
if set.PublicHost != "" && net.ParseIP(set.PublicHost) == nil {
return errors.New("public_host: an IP address (the library takes no names)")
}
if r := f.String("passive_ports", ""); r != "" {
lo, hi, ok := strings.Cut(r, "-")
a, err1 := strconv.Atoi(strings.TrimSpace(lo))
b, err2 := strconv.Atoi(strings.TrimSpace(hi))
if !ok || err1 != nil || err2 != nil || a < 1024 || b > 65535 || a > b {
return fmt.Errorf("passive_ports: %q is not a range like 50000-50100", r)
}
set.PassiveMin, set.PassiveMax = a, b
}
set.TLS = strings.ToLower(f.String("tls", ftp.TLSRequired))
switch set.TLS {
case ftp.TLSNo:
c.Warnings = append(c.Warnings, "[ftp] tls = no: passwords go over the network in the clear")
case ftp.TLSOptional:
c.Warnings = append(c.Warnings, "[ftp] tls = optional: clients that do not ask for TLS send passwords in the clear")
case ftp.TLSRequired, ftp.TLSImplicit:
default:
return fmt.Errorf("tls: %q is not required, implicit, optional or no", set.TLS)
}
set.CertFile = f.String("cert_file", "")
set.KeyFile = f.String("key_file", "")
if set.TLS != ftp.TLSNo {
var err error
if set.CertFile, set.KeyFile, err = c.tlsFiles(set.CertFile, set.KeyFile); err != nil {
return fmt.Errorf("TLS %v (or tls = no)", err)
}
}
var err error
if set.Anonymous, err = f.Bool("anonymous", false); err != nil {
return err
}
if set.MaxClients, err = f.Int("max_clients", 32, 1, 10000); err != nil {
return err
}
if set.MaxPerIP, err = f.Int("max_per_ip", 4, 0, 1000); err != nil {
return err
}
if set.IdleTimeout, err = f.Seconds("idle_timeout", 300, 30, 86400); err != nil {
return err
}
c.FTPAdvertise = f.String("advertise", set.Address)
c.FTP = set
return nil
}