mirror of
https://github.com/octoleo/syncthing.git
synced 2024-12-22 02:48:59 +00:00
77970d5113
At a high level, this is what I've done and why: - I'm moving the protobuf generation for the `protocol`, `discovery` and `db` packages to the modern alternatives, and using `buf` to generate because it's nice and simple. - After trying various approaches on how to integrate the new types with the existing code, I opted for splitting off our own data model types from the on-the-wire generated types. This means we can have a `FileInfo` type with nicer ergonomics and lots of methods, while the protobuf generated type stays clean and close to the wire protocol. It does mean copying between the two when required, which certainly adds a small amount of inefficiency. If we want to walk this back in the future and use the raw generated type throughout, that's possible, this however makes the refactor smaller (!) as it doesn't change everything about the type for everyone at the same time. - I have simply removed in cold blood a significant number of old database migrations. These depended on previous generations of generated messages of various kinds and were annoying to support in the new fashion. The oldest supported database version now is the one from Syncthing 1.9.0 from Sep 7, 2020. - I changed config structs to be regular manually defined structs. For the sake of discussion, some things I tried that turned out not to work... ### Embedding / wrapping Embedding the protobuf generated structs in our existing types as a data container and keeping our methods and stuff: ``` package protocol type FileInfo struct { *generated.FileInfo } ``` This generates a lot of problems because the internal shape of the generated struct is quite different (different names, different types, more pointers), because initializing it doesn't work like you'd expect (i.e., you end up with an embedded nil pointer and a panic), and because the types of child types don't get wrapped. That is, even if we also have a similar wrapper around a `Vector`, that's not the type you get when accessing `someFileInfo.Version`, you get the `*generated.Vector` that doesn't have methods, etc. ### Aliasing ``` package protocol type FileInfo = generated.FileInfo ``` Doesn't help because you can't attach methods to it, plus all the above. ### Generating the types into the target package like we do now and attaching methods This fails because of the different shape of the generated type (as in the embedding case above) plus the generated struct already has a bunch of methods that we can't necessarily override properly (like `String()` and a bunch of getters). ### Methods to functions I considered just moving all the methods we attach to functions in a specific package, so that for example ``` package protocol func (f FileInfo) Equal(other FileInfo) bool ``` would become ``` package fileinfos func Equal(a, b *generated.FileInfo) bool ``` and this would mostly work, but becomes quite verbose and cumbersome, and somewhat limits discoverability (you can't see what methods are available on the type in auto completions, etc). In the end I did this in some cases, like in the database layer where a lot of things like `func (fv *FileVersion) IsEmpty() bool` becomes `func fvIsEmpty(fv *generated.FileVersion)` because they were anyway just internal methods. Fixes #8247
136 lines
3.5 KiB
Go
136 lines
3.5 KiB
Go
// Copyright (C) 2016 The Syncthing Authors.
|
|
//
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
package protocol
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"errors"
|
|
"io"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"github.com/syncthing/syncthing/internal/gen/bep"
|
|
)
|
|
|
|
const (
|
|
HelloMessageMagic uint32 = 0x2EA7D90B
|
|
Version13HelloMagic uint32 = 0x9F79BC40 // old
|
|
)
|
|
|
|
var (
|
|
// ErrTooOldVersion is returned by ExchangeHello when the other side
|
|
// speaks an older, incompatible version of the protocol.
|
|
ErrTooOldVersion = errors.New("the remote device speaks an older version of the protocol not compatible with this version")
|
|
// ErrUnknownMagic is returned by ExchangeHello when the other side
|
|
// speaks something entirely unknown.
|
|
ErrUnknownMagic = errors.New("the remote device speaks an unknown (newer?) version of the protocol")
|
|
)
|
|
|
|
type Hello struct {
|
|
DeviceName string
|
|
ClientName string
|
|
ClientVersion string
|
|
NumConnections int
|
|
Timestamp int64
|
|
}
|
|
|
|
func (h *Hello) toWire() *bep.Hello {
|
|
return &bep.Hello{
|
|
DeviceName: h.DeviceName,
|
|
ClientName: h.ClientName,
|
|
ClientVersion: h.ClientVersion,
|
|
NumConnections: int32(h.NumConnections),
|
|
Timestamp: h.Timestamp,
|
|
}
|
|
}
|
|
|
|
func helloFromWire(w *bep.Hello) Hello {
|
|
return Hello{
|
|
DeviceName: w.DeviceName,
|
|
ClientName: w.ClientName,
|
|
ClientVersion: w.ClientVersion,
|
|
NumConnections: int(w.NumConnections),
|
|
Timestamp: w.Timestamp,
|
|
}
|
|
}
|
|
|
|
func (Hello) Magic() uint32 {
|
|
return HelloMessageMagic
|
|
}
|
|
|
|
func ExchangeHello(c io.ReadWriter, h Hello) (Hello, error) {
|
|
if h.Timestamp == 0 {
|
|
panic("bug: missing timestamp in outgoing hello")
|
|
}
|
|
if err := writeHello(c, h); err != nil {
|
|
return Hello{}, err
|
|
}
|
|
return readHello(c)
|
|
}
|
|
|
|
// IsVersionMismatch returns true if the error is a reliable indication of a
|
|
// version mismatch that we might want to alert the user about.
|
|
func IsVersionMismatch(err error) bool {
|
|
return errors.Is(err, ErrTooOldVersion) || errors.Is(err, ErrUnknownMagic)
|
|
}
|
|
|
|
func readHello(c io.Reader) (Hello, error) {
|
|
header := make([]byte, 4)
|
|
if _, err := io.ReadFull(c, header); err != nil {
|
|
return Hello{}, err
|
|
}
|
|
|
|
switch binary.BigEndian.Uint32(header) {
|
|
case HelloMessageMagic:
|
|
// This is a v0.14 Hello message in proto format
|
|
if _, err := io.ReadFull(c, header[:2]); err != nil {
|
|
return Hello{}, err
|
|
}
|
|
msgSize := binary.BigEndian.Uint16(header[:2])
|
|
if msgSize > 32767 {
|
|
return Hello{}, errors.New("hello message too big")
|
|
}
|
|
buf := make([]byte, msgSize)
|
|
if _, err := io.ReadFull(c, buf); err != nil {
|
|
return Hello{}, err
|
|
}
|
|
|
|
var wh bep.Hello
|
|
if err := proto.Unmarshal(buf, &wh); err != nil {
|
|
return Hello{}, err
|
|
}
|
|
|
|
return helloFromWire(&wh), nil
|
|
|
|
case 0x00010001, 0x00010000, Version13HelloMagic:
|
|
// This is the first word of an older cluster config message or an
|
|
// old magic number. (Version 0, message ID 1, message type 0,
|
|
// compression enabled or disabled)
|
|
return Hello{}, ErrTooOldVersion
|
|
}
|
|
|
|
return Hello{}, ErrUnknownMagic
|
|
}
|
|
|
|
func writeHello(c io.Writer, h Hello) error {
|
|
msg, err := proto.Marshal(h.toWire())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(msg) > 32767 {
|
|
// The header length must be a positive signed int16
|
|
panic("bug: attempting to serialize too large hello message")
|
|
}
|
|
|
|
header := make([]byte, 6, 6+len(msg))
|
|
binary.BigEndian.PutUint32(header[:4], h.Magic())
|
|
binary.BigEndian.PutUint16(header[4:], uint16(len(msg)))
|
|
|
|
_, err = c.Write(append(header, msg...))
|
|
return err
|
|
}
|