#pragma once #include "crypto/crypto.h" #include "net/net.h" #include "protocol/wire.h" #include #include #include #include #include #include namespace vn::proto { constexpr std::uint16_t PROTOCOL_VERSION = 1; struct Hello { std::uint16_t min_version = PROTOCOL_VERSION; std::uint16_t max_version = PROTOCOL_VERSION; std::vector capabilities; std::string repo; std::string client_nonce; }; struct HelloOk { std::uint16_t version = 0; std::vector capabilities; std::string server_nonce; std::uint8_t auth_required = 0; }; struct Auth { std::string public_key; std::string signature; }; struct AuthOk { std::uint8_t role = 0; std::string fingerprint; }; struct RefInfo { std::string name; Id target{}; std::string change_id; std::uint8_t kind = 0; // 0 bookmark, 1 tag }; struct Negotiate { std::vector wants; std::vector haves; }; struct UpdateRef { std::string name; std::optional expected; std::optional target; }; std::vector encode_hello(const Hello& hello); std::optional decode_hello(Bytes data); std::vector encode_hello_ok(const HelloOk& hello); std::optional decode_hello_ok(Bytes data); std::vector encode_auth(const Auth& auth); std::optional decode_auth(Bytes data); std::vector encode_auth_ok(const AuthOk& auth); std::optional decode_auth_ok(Bytes data); std::vector encode_refs(const std::vector& refs); std::optional> decode_refs(Bytes data); std::vector encode_ids(const std::vector& ids); std::optional> decode_ids(Bytes data, std::size_t max); std::vector encode_negotiate(const std::vector& wants, const std::vector& haves); std::optional decode_negotiate(Bytes data); std::vector encode_negotiate_ok(const std::vector& known, std::uint8_t enough); std::optional> decode_negotiate_ok(Bytes data); std::vector encode_bitmap(const std::vector& bits); std::optional> decode_bitmap(Bytes data); std::vector encode_update_refs(const std::vector& updates); std::optional> decode_update_refs(Bytes data); // The bytes signed in AUTH. std::string auth_message(std::uint16_t version, std::string_view repo, Bytes server_nonce, Bytes client_nonce, Bytes binding); std::string_view role_name(std::uint8_t role); // --------------------------------------------------------------------------- // URLs // --------------------------------------------------------------------------- struct Url { bool tls = false; bool ssh = false; std::string host; int port = 0; std::string path; }; std::optional parse_url(std::string_view url); std::string url_repo_name(std::string_view url); // --------------------------------------------------------------------------- // Client // --------------------------------------------------------------------------- class Client { public: Client() = default; ~Client() = default; Client(Client&&) noexcept = default; Client& operator=(Client&&) noexcept = default; Client(const Client&) = delete; Client& operator=(const Client&) = delete; // Connect and authenticate. An empty key name means anonymous unless the // server requires authentication, in which case "default" is used. static std::optional connect(std::string_view url, std::string_view key_name, std::string_view repo_name, std::string_view tls_pin = {}); static const std::string& last_connect_error(); bool list_refs(std::vector& refs); bool negotiate(const std::vector& wants, const std::vector& haves); bool fetch_skeleton(const fs::path& repo, const std::vector& wants); bool get_chunks(const fs::path& repo, const std::vector& ids); bool have_query(const std::vector& ids, std::vector& bits); bool push_objects(const fs::path& repo, const std::vector& ids, const std::function& progress = {}); bool update_refs(const std::vector& updates); bool whoami(std::uint8_t& role, std::string& fingerprint); [[nodiscard]] const std::string& error() const { return last_error_; } [[nodiscard]] std::uint8_t role() const { return role_; } [[nodiscard]] bool authenticated() const { return authed_; } [[nodiscard]] const std::string& fingerprint() const { return fingerprint_; } [[nodiscard]] bool has_connection() const { return stream_.valid(); } private: std::optional request(std::uint8_t kind, Bytes payload); bool receive_stream(const fs::path& repo); void set_error(std::string text); void set_error_frame(const wire::Frame& frame); net::Stream stream_; std::string binding_; std::uint16_t version_ = PROTOCOL_VERSION; std::uint8_t role_ = 0; bool authed_ = false; std::string fingerprint_; std::string last_error_; }; } // namespace vn::proto