kosync

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 15, 2026 License: MIT Imports: 18 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package kosync speaks the KOReader progress-sync protocol, both as a client and as a server shelf can run itself.

CrossPoint's firmware ships a KOReader sync client, so this is how shelf learns where you are in a book. The alternative — parsing the device's on-card progress.bin — is a non-starter: the format is undocumented and its parent section.bin is already at version 30.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUserExists   = errors.New("kosync: user already exists")
	ErrNoSuchUser   = errors.New("kosync: no such user")
	ErrBadPassword  = errors.New("kosync: incorrect password")
	ErrNoProgress   = errors.New("kosync: no progress for that document")
	ErrEmptyRequest = errors.New("kosync: username and password are required")
)

Errors the store returns.

Functions

func DocumentID

func DocumentID(path string) (string, error)

DocumentID computes the partial-MD5 identifier for a file.

Sampling stops at the first offset past the end of the file, which is what makes a small book hash from just its opening bytes.

func FilenameID

func FilenameID(path string) string

FilenameID computes the alternative identifier: MD5 of the bare filename.

The device offers this as a match method for libraries where the same book has identical names but differing bytes across devices — a re-download, or a copy whose metadata was edited on one side only.

func FormatPercent

func FormatPercent(pct float64) string

FormatPercent renders a read percentage for display.

"done" rather than "100%" is deliberate: the device reports 0.9998 for a book read to its last page, so rounding to 100% would make "nearly finished" and "finished" indistinguishable — and that is the one distinction a reader actually cares about in a list.

Lives here rather than in each frontend so the CLI and the TUI cannot drift into disagreeing about what 99.6% means.

func IDFor

func IDFor(path string, method MatchMethod) (string, error)

IDFor computes the identifier for a file under the given method.

func NormalizeUsername

func NormalizeUsername(u string) string

NormalizeUsername trims a username for comparison. The protocol is silent on case, so shelf preserves it and only strips surrounding space, which is almost always a paste artifact rather than intent.

func Offsets

func Offsets() []int64

Offsets returns the sample offsets in order. Exported so a test can pin them against the values read out of KOReader and the CrossPoint firmware.

func PasswordKey

func PasswordKey(password string) string

PasswordKey derives the auth key the protocol sends in place of a password.

It is a plain MD5 of the password, which is weak by any modern standard, but it is what KOReader clients send and shelf does not get to redefine the protocol. The server stores this value rather than the password itself; see the note in store.go about what that does and does not protect.

Types

type MatchMethod

type MatchMethod string

MatchMethod selects how a book is identified.

const (
	// MatchBinary is the partial-MD5 of file contents. KOReader's default and
	// shelf's.
	MatchBinary MatchMethod = "binary"

	// MatchFilename is the MD5 of the filename.
	MatchFilename MatchMethod = "filename"
)

type Progress

type Progress struct {
	Document   string  `json:"document"`
	Progress   string  `json:"progress"`
	Percentage float64 `json:"percentage"`
	Device     string  `json:"device,omitempty"`
	DeviceID   string  `json:"device_id,omitempty"`

	// Position is CrossPoint's extension: a structured location (spine index,
	// page, xpath) that is richer than the standard progress string.
	//
	// It will almost always be empty here. The firmware only sends it when the
	// configured server URL exactly equals CrossPoint's own sync server, so a
	// self-hosted shelf never receives it. Stored verbatim when it does arrive,
	// which costs nothing and means shelf is ready if that check ever loosens.
	Position json.RawMessage `json:"position,omitempty"`

	// Timestamp is when the server recorded this, in Unix seconds.
	Timestamp int64 `json:"timestamp"`
}

Progress is one book's reading position.

type Server

type Server struct {

	// AllowRegistration controls whether POST /users/create works.
	//
	// The device has no other way to make an account, so this defaults on. It
	// is worth being able to turn off: this server is meant to sit on a home
	// network, and an open registration endpoint on a listener bound to all
	// interfaces is an invitation.
	AllowRegistration bool
	// contains filtered or unexported fields
}

Server implements the kosync API over a Store.

func NewServer

func NewServer(store *Store, log *slog.Logger) *Server

NewServer builds a server over a store.

func (*Server) Handler

func (s *Server) Handler() http.Handler

Handler returns the HTTP routes.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store persists users and reading progress.

Unlike the library index, this is NOT disposable: it is the only copy of where you are in each book. Deleting it loses reading positions that exist nowhere else, because the device pushes progress here and does not keep a synchronised copy of its own.

func OpenStore

func OpenStore(path string) (*Store, error)

OpenStore opens or creates the progress database.

func (*Store) AllProgress

func (s *Store) AllProgress(username string) (map[string]Progress, error)

AllProgress returns every recorded position for a user, keyed by document id.

This is what the library table uses: one query, then an in-memory join, since hashing every book on every listing would be far slower than the lookup.

func (*Store) Authenticate

func (s *Store) Authenticate(username, key string) error

Authenticate verifies a username and auth key.

func (*Store) CheckPassword

func (s *Store) CheckPassword(username, password string) error

CheckPassword verifies a plaintext password.

Authenticate takes the MD5 the kosync protocol puts on the wire; this takes the password itself, which is what HTTP Basic carries. Both reduce to the same stored verifier, so one account works for kosync and for the OPDS catalog without either caller needing to know how credentials are derived.

func (*Store) Close

func (s *Store) Close() error

Close releases the database.

func (*Store) CreateUser

func (s *Store) CreateUser(username, key string) error

CreateUser registers a user. key is the MD5 the client sends.

func (*Store) DeleteUser

func (s *Store) DeleteUser(username string) error

DeleteUser removes an account and everything it had read.

Progress goes with it deliberately: it is keyed by username, so leaving it would orphan rows that a later account of the same name would silently inherit.

func (*Store) GetProgress

func (s *Store) GetProgress(username, document string) (*Progress, error)

GetProgress returns a stored position.

func (*Store) Path

func (s *Store) Path() string

Path is the store's location on disk.

func (*Store) PutProgress

func (s *Store) PutProgress(username string, p Progress) (int64, error)

PutProgress records a reading position, replacing any previous one.

func (*Store) SetPassword

func (s *Store) SetPassword(username, key string) error

SetPassword replaces a user's credential. key is the MD5 the client sends.

A fresh salt is generated rather than reusing the old one, so two accounts that happen to share a password still store different verifiers.

func (*Store) UserList

func (s *Store) UserList() ([]UserInfo, error)

UserList returns every account with how many books it has progress for.

func (*Store) Users

func (s *Store) Users() ([]string, error)

Users lists registered usernames.

type UserInfo

type UserInfo struct {
	Username  string
	CreatedAt int64
	Books     int
}

UserInfo describes an account for display.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL