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 ¶
- Variables
- func DocumentID(path string) (string, error)
- func FilenameID(path string) string
- func FormatPercent(pct float64) string
- func IDFor(path string, method MatchMethod) (string, error)
- func NormalizeUsername(u string) string
- func Offsets() []int64
- func PasswordKey(password string) string
- type MatchMethod
- type Progress
- type Server
- type Store
- func (s *Store) AllProgress(username string) (map[string]Progress, error)
- func (s *Store) Authenticate(username, key string) error
- func (s *Store) CheckPassword(username, password string) error
- func (s *Store) Close() error
- func (s *Store) CreateUser(username, key string) error
- func (s *Store) DeleteUser(username string) error
- func (s *Store) GetProgress(username, document string) (*Progress, error)
- func (s *Store) Path() string
- func (s *Store) PutProgress(username string, p Progress) (int64, error)
- func (s *Store) SetPassword(username, key string) error
- func (s *Store) UserList() ([]UserInfo, error)
- func (s *Store) Users() ([]string, error)
- type UserInfo
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
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 (*Store) AllProgress ¶
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 ¶
Authenticate verifies a username and auth key.
func (*Store) CheckPassword ¶
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) CreateUser ¶
CreateUser registers a user. key is the MD5 the client sends.
func (*Store) DeleteUser ¶
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 ¶
GetProgress returns a stored position.
func (*Store) PutProgress ¶
PutProgress records a reading position, replacing any previous one.
func (*Store) SetPassword ¶
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.