├── .cargo
└── config.toml
├── .github
└── workflows
│ └── lint_and_test.yml
├── .gitignore
├── Cargo.toml
├── LICENSE
├── README.md
├── graphviz_architecture.dot
└── src
├── backend.rs
├── common.rs
├── constants.rs
├── group.rs
├── lib.rs
├── main.rs
├── repo.rs
└── rpc.rs
/.cargo/config.toml:
--------------------------------------------------------------------------------
1 | [build]
2 | rustflags = [
3 | "-A", "unused-imports",
4 | "-A", "unused-variables",
5 | "-A", "unused-mut",
6 | "-A", "dead-code",
7 | "-A", "clippy::clone-on-copy",
8 | "-A", "clippy::needless-borrows-for-generic_args",
9 | ]
10 |
--------------------------------------------------------------------------------
/.github/workflows/lint_and_test.yml:
--------------------------------------------------------------------------------
1 | name: CI
2 |
3 | on: [push, pull_request]
4 |
5 | jobs:
6 | lint_and_test:
7 | runs-on: ${{ matrix.os }}
8 | strategy:
9 | matrix:
10 | os: [ubuntu-latest, macOS-latest]
11 | rust: [stable]
12 |
13 | steps:
14 | - name: Set up Rust toolchain
15 | uses: hecrj/setup-rust-action@v2
16 | with:
17 | rust-version: ${{ matrix.rust }}
18 |
19 | - name: Check out the code
20 | uses: actions/checkout@v4
21 |
22 | - name: Install Clippy
23 | run: rustup component add clippy
24 |
25 | - name: Run Clippy
26 | run: cargo clippy --all-targets --all-features -- -D warnings
27 |
28 | - name: Run tests
29 | env:
30 | RUST_MIN_STACK: 8388608
31 | run: cargo test --verbose -- --test-threads=1 --nocapture
32 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Generated by Cargo
2 | # will have compiled files and executables
3 | debug/
4 | target/
5 |
6 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
7 | # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
8 | Cargo.lock
9 |
10 | # These are backup files generated by rustfmt
11 | **/*.rs.bk
12 |
13 | # MSVC Windows builds of rustc generate these, which store debugging information
14 | *.pdb
15 |
16 |
17 | # Added by cargo
18 |
19 | /target
20 |
--------------------------------------------------------------------------------
/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "save-dweb-backend"
3 | version = "0.1.3"
4 | edition = "2021"
5 |
6 | [dependencies]
7 | iroh = "0.24.0"
8 | iroh-blobs = "0.24.0"
9 | veilid-core = { git = "https://gitlab.com/veilid/veilid.git", version = "0.4.3" }
10 | veilid-iroh-blobs = { git = "https://github.com/RangerMauve/veilid-iroh-blobs.git", version = "0.1.1" }
11 | tracing = "0.1"
12 | xdg = "2.4"
13 | tmpdir = "1"
14 | serde = "1.0.204"
15 | serde_cbor = "0.11.2"
16 | clap = { version = "4.5.9", features = ["derive"] }
17 | anyhow = "1.0.86"
18 | tokio = {version ="1.38.1", features=["full"] }
19 | tokio-stream = "0.1.15"
20 | async-stream = "0.3.5"
21 | futures = "0.3.31"
22 | futures-core = "0.3.31"
23 | futures-util = "0.3.31"
24 | bytes = "1.6.1"
25 | serial_test = "3.1.1"
26 | url = "2.5.2"
27 | hex = "0.4.3"
28 | rand = "0.8.5"
29 | base64 = "0.22.1"
30 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | # *Save* DWeb Backend Technical Documentation
3 |
4 |
5 | ## 1.0 Introduction
6 |
7 | OpenArchive’s *Save *application offers several storage options for archiving media via a mobile device. This documentation outlines the technical details for the DWeb Backend (working title *Raven*), a peer-to-peer (P2P), decentralized backend that ensures secure and efficient data management.
8 |
9 |
10 | ## 1.1 Overview
11 |
12 | P2P networks are decentralized systems where nodes (a.k.a. peers) function as both clients and servers, enabling direct resource sharing and collaboration without relying on centralized infrastructure. Typically, each participating node maintains equal privileges and can initiate or fulfill requests for data, bandwidth, or other network services. A key benefit of a P2P network is that its decentralized architecture removes the risk of a single point of failure or control found in centralized systems.
13 |
14 | The *Save* DWeb Backend relies on two P2P protocols, [Veilid](https://veilid.com/) and [Iroh](https://www.iroh.computer/), selected for their different strengths. Veilid offers secure peer discovery, connections, and public key cryptography. It provides encrypted and anonymous P2P connections by routing traffic through multiple peers in a similar setup to TOR. Iroh is used for blob replication and data storage. It handles collections of data that change over time, ensuring reliable data management with integrity verification.
15 |
16 |
17 | ## 1.2 Key Concepts & Terminology
18 |
19 |
20 |
21 | * **Peer-to-Peer (P2P)**: A decentralized network architecture where each participant (peer) can act as both a client and a server.
22 | * **Distributed Hash Table (DHT)**: A decentralized data structure that allows for the efficient storage and retrieval of key-value pairs across a network of peers.
23 | * **Blob**: A binary large object, typically used to store files or data collections in the backend.
24 | * **Veilid**: A protocol used for secure peer discovery and communication.
25 | * **Iroh**: A library for blob replication and data storage.
26 | * **Route ID: **A unique identifier for communication paths between peers.
27 | * **AppCall: **Veilid’s encrypted message system.
28 | * **Tunnel: **Multiplexed connection system used for peer communication.
29 |
30 |
31 | ## 1.3 Complementary resources
32 |
33 |
34 |
35 | * [Save DWeb Backend Privacy Overview and Risk Assessment](https://hackmd.io/@s74XZjUBQDuPPS04AgPvow/HkacodHgyx)
36 | * [Veilid’s Rust Documentation](https://docs.rs/veilid-core/latest/veilid_core/) for the Veilid-core library
37 | * [Iroh’s Rust Documentation](https://docs.rs/iroh/latest/iroh/) for the Iroh library
38 |
39 |
40 | ## 2.0 Architecture
41 |
42 |
43 | ### Save App Architecture
44 |
45 | ```mermaid
46 | graph TD;
47 | A["Android Kotlin"] -->|RPC| B["P2P Daemon Rust"];
48 | C["iOS Swift"] -->|RPC| B;
49 | B --> D["Data Repo"];
50 | D --> E["Personal Data"];
51 | D --> F["External"];
52 | G["Sync Admin"] -->|View/Remove
Via veilid?| H["Sync Server"];
53 | B -.->|Code reuse| H;
54 | I --> H;
55 | H --> I["P2P Sync Group"];
56 | I -->|View, Replicate| J["Other peers"];
57 | J -->|Add archives| I;
58 | ```
59 |
60 | P2P Data Synchronization and Replication Architecture
61 |
62 |
63 | ### P2P Connections
64 |
65 | ```mermaid
66 | graph TD;
67 | A["groupId"] -->|Find peers| B["veilidKeyValue"];
68 | B --> C["veilidTunnels"];
69 | C --> D["irohStorageVerification"];
70 | D --> E["irohDocs"];
71 | E --> F["groupDoc"];
72 | F --> G["personalRepo"];
73 | G --> H["CBORFileList"];
74 | D --> H;
75 | ```
76 |
77 | Group Data Retrieval and Verification Flow
78 |
79 |
80 | ## 2.1 High-Level Design
81 |
82 |
83 | ### Introduction
84 |
85 | `save-dweb-backend` is a decentralized web storage backend for the *Save* app with these core components:
86 |
87 |
88 |
89 | * `Backend`: the core system that manages the initialization and lifecycle of the Veilid instance, groups, and data repositories. ([Source](https://github.com/openarchive/save-dweb-backend/blob/main/src/backend.rs#L90-L111))
90 | * `Group`: a collection of peers identified by a Veilid DHT record keypair and encryption key, used to manage shared data repositories and secure communications. ([Source](https://github.com/openarchive/save-dweb-backend/blob/main/src/group.rs#L35-L43))
91 | * `Repo`: a data store within a group, identified by a Veilid DHT record keypair and encrypted with the group's shared secret. ([Source](https://github.com/openarchive/save-dweb-backend/blob/main/src/repo.rs#L25-L33))
92 |
93 |
94 | ### Basic Flow
95 |
96 |
97 |
98 | 1. Initialization: The backend sets up storage and loads known groups.
99 | 2. Groups: Users create or join groups with cryptographic keys.
100 | 3. Repositories: Each user in a group holds their data and route ID in a Repo which is linked to from the group.
101 | 4. Peer Communication: Veilid handles secure connections; Iroh provides data verification.
102 |
103 |
104 | ## 2.2 Groups
105 |
106 | Groups are the fundamental organizational unit in the system, enabling secure peer discovery and shared data access.
107 |
108 |
109 | ## 2.2.1 Group operations
110 |
111 |
112 | ### Create Group:
113 |
114 |
115 |
116 | * Generate a Veilid DHT record with ED25519 keypair and signed metadata
117 | * Generate a random shared secret using chacha20poly1305 encryption
118 | * The group is identified by its DHT record key, owner keypair, and encryption shared secret
119 |
120 | ([Source](https://github.com/OpenArchive/save-dweb-backend/blob/main/src/backend.rs#L343-L382))
121 |
122 |
123 |
124 | ### Join Group:
125 |
126 |
127 |
128 | * Use provided CommonKeypair or invite URL containing DHT key, owner keypair, and shared secret
129 | * Initialize Veilid DHT record using record ID and owner keypair
130 | * Use shared secret to decrypt group name and repository list
131 | * Add group to known groups list
132 |
133 | ([Source](https://github.com/openarchive/save-dweb-backend/blob/main/src/backend.rs#L296-L341))
134 |
135 |
136 |
137 | ## 2.2.2 Group structure
138 |
139 | `Group` struct manages decentralized storage, networking, and encryption, integrating a DHT record, encryption key, routing context, and APIs for Veilid and Iroh blobs, with a mutex-protected repo map.
140 |
141 | [Source](https://github.com/openarchive/save-dweb-backend/blob/main/src/group.rs#L45-L60)
142 |
143 |
144 | ### DHT Record Format:
145 |
146 |
147 |
148 | * Subkey 0: Reserved for group name
149 | * Subkeys 1+: Used by members to register their Data Repository keys
150 | * All values are encrypted with the group's shared secret
151 |
152 |
153 | ## 2.2.3 Group membership management
154 |
155 |
156 | ### Discovery Process:
157 |
158 |
159 |
160 | * Peers look up other group members Repos in the DHT
161 | * Peer’s Veilid Route IDs are looked up from their Repo
162 | * AppCalls are sent to member’s routes IDs for replication
163 | * Multiple “tunnels” are multiplexed over a single route ID
164 | * Tunnels are exposed as a duplex binary stream for replication
165 | * New peers request a sync of Iroh blobs
166 |
167 |
168 | ## 2.2.4 Data Management:
169 |
170 |
171 |
172 | * Personal Data:** **CBOR encoded list of file names mapping to file hashes
173 | * External Data:** **Peers track other peers’ data repositories and can optionally download blobs for backups.
174 |
175 |
176 | ## 2.3 Network Topology
177 |
178 | ```mermaid
179 | graph TD;
180 | A["BOOTSTRAP"] -->|Node List| B["YOU!"];
181 | B -->|Find Self| A;
182 |
183 | B -->|Ping| C["HEADLESS"];
184 | C -->|Status| B;
185 | B -->|Status| D["DWEB BACKEND DAEMON"];
186 | D -->|Ping| B;
187 | D -->|Gossip via app calls| E["SYNC GROUP"];
188 |
189 | C -->|Signal| F["MOBILE APP"];
190 | F -->|Signal| C;
191 |
192 | G -->|View, Replicate| E;
193 | E -->|Add archives| G["PEER"];
194 | ```
195 |
196 | Decentralized Web Synchronization and Communication Flow
197 |
198 |
199 | ## 2.4 Data Flow
200 |
201 |
202 | ### Data Flow Diagram
203 |
204 | ```mermaid
205 | graph TD;
206 | A["Backend Initialization"] --> B["Group Management"];
207 | B --> C["Repository Operations"];
208 | B --> D["Create/Join Groups"];
209 | B --> E["Manage Members"];
210 |
211 | C --> F["Peer Communication"];
212 | C --> G["Create Repos"];
213 |
214 | F --> H["Route Updates"];
215 | F --> I["Data Replication"];
216 |
217 | G --> J["File Transfer"];
218 |
219 | D --> K["Update Routes"];
220 |
221 | ```
222 |
223 | Data flow diagram illustrating the interactions between the core components
224 |
225 |
226 | ### Summary
227 |
228 | The data flow in the `save-dweb-backend` project involves the initialization of the backend, management of groups and repositories, and secure communication between peers. Each component interacts with others to ensure seamless data management and peer communication.
229 |
230 |
231 | ## 3.0 Core Features
232 |
233 |
234 | ## 3.1 Peer Discovery
235 |
236 | Peer discovery is performed on individual groups. A lookup is done on the group DHT record to list Repos. Then, the repo DHT records are queried for that members’ name, Route ID, and collection hash. These are all encrypted with the group secret key. The name represents a human readable name for the member, the Route ID is used to facilitate secure peer to peer connections, and the collection hash is the Iroh Blob hash representing the file list for the user.
237 |
238 |
239 | ## 3.1.1 Backend Initialization
240 |
241 | When the Backend is started, the Veilid and Iroh components are initialized. It sets up storage and loads previously saved groups and repositories by iterating through a list of known group IDs and initializing groups from disk. From there each group will initialize by attempting to load the user’s Repo and add themselves to the DHT if they are not found on it. ([Source](https://github.com/openarchive/save-dweb-backend/blob/main/src/backend.rs#L113-L156))
242 |
243 |
244 | ## 3.1.2 Repository Operations
245 |
246 |
247 |
248 | * Creation: Users create DHT records with private keypairs after loading a group
249 | * Registration: Repository DHT keys are added to the group's subkeys
250 | * File Operations: Create/Read/Update/Delete operations for files add data to Iroh, update the file list, and then publish the new file collection hash to the DHT
251 | * Routing: The `update_route_on_dht` method ensures repositories remain discoverable by publishing the current Veilid route ID onto the DHT
252 |
253 | ([Source](https://github.com/openarchive/save-dweb-backend/blob/main/src/repo.rs#L62-L77))
254 |
255 |
256 |
257 | ## 3.1.3 File Management
258 |
259 |
260 |
261 | * Format: Files uploaded to Iroh and then they are stored as a CBOR encoded` HashMap<String, Hash> `([Source](https://github.com/rangermauve/veilid-iroh-blobs/blob/default/src/iroh.rs#L38))
262 | * String: file path within dataset
263 | * Hash: 32-byte Blake3 iroh hash (raw bytes)
264 | * This encoded dada is then stored into Iroh and the Blake3 hash is used to represent the current state of the file list.
265 | * State Updates:
266 | 1. Get current hash or create empty file list
267 | 2. Load and parse HashMap from Iroh
268 | 3. Modify HashMap as needed
269 | 4. Encode to CBOR bytes and save to Iroh
270 | 5. Store latest hash on DHT ([Source](https://github.com/rangermauve/veilid-iroh-blobs/blob/default/src/iroh.rs#L683-L737))
271 |
272 |
273 | ## 3.2 Data Transmission
274 |
275 | Veilid provides a way to create routes that can receive messages. However, creating many tunnels is computationally expensive, so it is better to reuse a single route ID. On top of this foundation we built a way to multiplex several connections from several peers.
276 |
277 | Tunnels are identified using a route ID of the sender and an unsigned 32 bit integer. This allows peers to open multiple tunnels to others by increasing the 32 bit integer. ([Source](https://github.com/rangermauve/veilid-iroh-blobs/blob/default/src/tunnels.rs#L50-L66))
278 |
279 |
280 | #### Tunnels - Wire
281 |
282 | Messages are sent using Veilid AppCalls to routes. Each message is prefixed with a 32 bit unsigned integer, followed by 32 bytes for the route id. The rest of the AppCall is the actual contents of the packet for that tunnel.
283 |
284 | We use AppCalls to ensure ordering for tunnels. Each message for a tunnel is sent one at a time and waits for an AppCallResponse from the recipient before the next message is sent. Multiple tunnels can send at the same time without needing to wait for each other.
285 |
286 | The first message (PING) sent through a tunnel contains the bytes [0x07, 0x02, 0x08, 0x03] (SAVE on a phone dial pad) followed by the route ID blob needed to register the route with Veilid. When a peer gets a tunnel ID it has not seen before it should attempt to check if the message contains the PING and if not ignore the tunnel. If the PING is present, the application should register the tunnel and listen for subsequent messages. The Route ID from the tunnel ID is where responses must be sent.
287 |
288 |
289 | ```mermaid
290 | sequenceDiagram
291 | participant AppA
292 | participant TunnelsA
293 | participant TunnelsB
294 | participant AppB
295 |
296 | AppA->>TunnelsA: Open New Tunnel to RouteIDB:Blob
297 | note right of TunnelsA: Register RouteIDB:Blob
with Veilid and get RouteIDB
298 | note right of TunnelsA: Create Tunnel ID
(u32:RouteIDA)
299 |
300 | TunnelsA->>TunnelsB: (u32:RouteIDA)PING(RouteIDA:Blob)
301 | TunnelsA->>AppA: New Tunnel (u32:RouteIDB)
302 |
303 | note right of TunnelsB: Verify PING
304 | note right of TunnelsB: Register RouteIDA:Blob
305 |
306 | TunnelsB->>AppB: New Tunnel (u32:RouteIDA)
307 | AppB->>TunnelsB: Send BYTES to (u32:RouteIDA)
308 |
309 | TunnelsB->>TunnelsA: (u32:RouteIDB)(BYTES)
310 | AppA->>TunnelsA: New data (u32:RouteIDB): BYTES
311 |
312 | ```
313 | Tunnels and RouteIDs communication flow in Veilid
314 |
315 |
316 | ## 3.3 Security Implementation
317 |
318 |
319 | ### Encryption
320 |
321 | In-transic encryption is handled by Veilid’s routes. Connections use Onion Routing to send packets through at least one other node on each side, thus hiding the IP address of the sender and receiver from each other and preventing intermediate nodes from knowing the full route a packet will take.
322 |
323 |
324 |
325 | * Implements AEAD (Authenticated Encryption with Associated Data) encryption
326 | * Uses random nonces for each encryption operation
327 | * Combines encryption key with nonce for secure message encryption
328 | * Provides authentication of encrypted data
329 |
330 |
331 | ### Route Management
332 |
333 | Routes are created using Veilid’s routing context API. During route creation our system attempts to create new custom private routes up to 6 times ([Source](https://github.com/rangermauve/veilid-iroh-blobs/blob/default/src/lib.rs#L988-L1005)) before returning an error. We use the [low latency](https://docs.rs/veilid-core/latest/veilid_core/enum.Stability.html#variant.LowLatency) stability preference for routes because it has been the fastest and most reliable option in our testing. We use the NoPreference sequencing option in order to allow for both TCP and UDP connections which have different likelihood to connect in different network environments.
334 |
335 |
336 | ## 3.4 Data Replication Protocol
337 |
338 |
339 | ### Message Protocol
340 |
341 | In order to replicate data, we open Tunnels to other peers and then send messages prefixed by Command bytes specifying the type of message being sent.
342 |
343 | Command bytes for operations:
344 |
345 |
346 |
347 | * `0x00` (NO): Hash not found
348 | * `0x01` (YES): Hash exists
349 | * `0x10` (HAS): Query hash existence
350 | * `0x11` (ASK): Request hash data
351 | * `0x20` (DATA): Data chunk transfer
352 | * `0x22` (DONE): Transfer complete
353 | * `0xF0` (ERR): Error occurred
354 |
355 | ([Source](https://github.com/rangermauve/veilid-iroh-blobs/blob/default/src/iroh.rs#L40-L46))
356 |
357 | The replication process:
358 |
359 | * Check if we have data locally and return if it exists.
360 | * Downloading starts by sending an ASK request and waiting for a HAS response.
361 | * After the HAS response the peer will send a series of messages starting with DATA followed by a chunk of the raw data.
362 | * Once all the data has been sent the peer sends a DONE message and stops replicating the Tunnel.
363 | * This data stream is ingested into the local Iroh store.
364 | * Once the data stream is finished we verify the final hash from Iroh.
365 | * If it does not match the data is thrown out and replication is started with another peer.
366 | * When replication we first ask for a Repo’s collection hash in order to get the hashes for their file list.
367 | * Afterwards we may initiate downloads for individual files by first looking up their hash in the collection.
368 | * Replication connects to peers in the group in random order until one has the data, thus ensuring we don’t need a specific peer to be online as long as somebody has a copy.
369 | * Implements failure handling with peer fallback \
370 | ([Source](https://github.com/rangermauve/veilid-iroh-blobs/blob/default/src/iroh.rs#L450-L550))
371 |
372 |
373 | ### Data Replication in Group
374 |
375 | To download a Hash from the group we use the following algorithm:
376 |
377 |
378 |
379 | * List repos in the group
380 | * Get their route IDs
381 | * Shuffle the list of routes for load balancing
382 | * For each route:
383 | * Verify route ID blob
384 | * Send ASK command with hash
385 | * If successful:
386 | * Receive and verify data chunks
387 | * Store verified data
388 | * End replication
389 | * On failure:
390 | * Log error
391 | * Try next peer
392 | * If all peers failed, return an error to the application
393 |
394 | Due to the high latency of Veilid tunnels, it's faster to ask random peers for data than it is to get the list of peers that have data and select from them.
395 |
396 |
397 | ## 4. Trust Model
398 |
399 |
400 | ### Overview
401 |
402 | The **trust model** in `save-dweb-backend` is designed to ensure that **only authorized peers can interact with the system**, while protecting against **malicious actors**.
403 |
404 |
405 | ### Core Trust Assumptions
406 |
407 |
408 |
409 | * Group-Based Trust
410 | * A **peer is trusted** only if they possess the group’s **shared secret**. ([Source](https://github.com/openarchive/save-dweb-backend/blob/main/src/group.rs#L206-L207))
411 | * Trust is **not assumed by default**—new peers must be **invited to the group.**
412 | * End-to-End Data Integrity
413 | * Every piece of data is cryptographically **signed or encrypted**, ensuring that it has **not been modified** in transit.
414 | * Decentralized Trust Model
415 | * No **central authority** exists—peers **self-organize** and invite each other.
416 |
417 |
418 | ### Security Best Practices
419 |
420 |
421 |
422 | * **Regular Dependency Updates**: Keep all dependencies updated to mitigate vulnerabilities.
423 | * Use a tool like `cargo audit` to check for vulnerabilities in your dependencies.
424 |
425 |
426 | ## 5. Implementation
427 |
428 |
429 | ## 5.1 Technology Stack
430 |
431 | The Save DWeb Backend is built using the Rust programming language, leveraging its performance and safety features. The key components of the technology stack include:
432 |
433 |
434 |
435 | * **Rust**: The primary programming language used for developing the backend.
436 | * **Cargo**: The Rust package manager and build system, which manages dependencies and project configuration.
437 | * **Tokio**: An asynchronous runtime for Rust, enabling concurrent programming and efficient I/O operations.
438 | * **Serde**: A framework for serializing and deserializing Rust data structures, facilitating data interchange formats like JSON and CBOR.
439 | * **Iroh**: A library for blob replication and data storage, ensuring reliable data verification.
440 | * **Veilid**: A protocol for secure peer discovery and connections, providing encrypted and anonymous communication. ([Source](https://github.com/openarchive/save-dweb-backend/blob/main/Cargo.toml))
441 |
442 |
443 | ## 6. Development Guidelines
444 |
445 |
446 | ## 6.1 Project Setup
447 |
448 |
449 | ### Dependencies
450 |
451 |
452 |
453 | * Rust toolchain
454 | * Cargo package manager
455 | * Development dependencies listed in Cargo.toml ([Source](https://github.com/openarchive/save-dweb-backend/blob/main/Cargo.toml))
456 |
457 |
458 | ### Building and Testing
459 |
460 |
461 |
462 | * Run tests with `cargo test`
463 | * Format code with `cargo fmt`
464 | * Lint with `cargo clippy`
465 | * Run the backend with `cargo run`
466 |
467 | [Source](https://github.com/openarchive/save-dweb-backend/blob/main/README.md#running)
468 |
469 |
470 | ## 6.2 Code Organization
471 |
472 | The codebase is organized into several key modules:
473 |
474 |
475 |
476 | * `backend`: Core backend functionality and initialization
477 | * `common`: Shared utilities and traits
478 | * `group`: Group management and operations
479 | * `repo`: Repository handling
480 | * `rpc`: RPC service implementation
481 |
482 |
483 | ## 6.3 Contributing Guidelines
484 |
485 |
486 | ### Code Style
487 |
488 |
489 |
490 | * Follow Rust standard formatting guidelines
491 | * Use meaningful variable and function names
492 | * Document public APIs with rustdoc comments
493 |
494 |
495 | ### Testing
496 |
497 |
498 |
499 | * Write unit tests for new functionality
500 | * Ensure existing tests pass
501 | * Add integration tests for new features
502 |
503 |
504 | ### Security Considerations
505 |
506 |
507 |
508 | * Review cryptographic implementations
509 | * Validate input data
510 | * Handle errors appropriately
--------------------------------------------------------------------------------
/graphviz_architecture.dot:
--------------------------------------------------------------------------------
1 | digraph {
2 | rankdir=TD
3 | sync[label="Backups Server\n(Cloud/PC)" shape=house];
4 | android[label="Android Kotlin" shape=Msquare];
5 | ios[label="iOS Swift" shape=Msquare];
6 | daemon[label="DWeb Backend Daemon"];
7 | syncGroup[label="P2P Sync Group\n(Gossip via app calls)"];
8 | peer[label="Other peers" shape=Msquare]
9 | admin[label="Sync Admin\n(admin key pair)" shape=Msquare]
10 | repo[label="Group Data\n(Veilid DHT Record)" shape=cylinder]
11 | external[label="Others Data\n(Veilid DHT Record)" shape=folder]
12 | personal[label="Personal Data Repo\n(Veilid DHT Record)" shape=folder]
13 | vrpc[label="Veilid app calls"]
14 | rpc[label="Inter-process RPC/FFI"]
15 | daemon -> syncGroup;
16 | android -> rpc;
17 | ios -> rpc;
18 | rpc -> daemon[label="Unix domain socket or FFI"];
19 | peer -> syncGroup [label="Add archives"];
20 | syncGroup -> peer[label="View, Replicate"];
21 | sync -> syncGroup;
22 | syncGroup -> sync;
23 | admin -> vrpc[label="View/Remove Groups"];
24 | {
25 | rank=same;
26 | daemon -> sync[style=dashed label="Code reuse"]
27 | }
28 | repo -> external;
29 | repo -> personal;
30 | daemon -> repo;
31 | ios -> vrpc;
32 | android -> vrpc;
33 | vrpc -> sync[label="Veilid Route"];
34 | {
35 | rank=same;
36 | external -> peer[style=dashed];
37 | }
38 | }
--------------------------------------------------------------------------------
/src/backend.rs:
--------------------------------------------------------------------------------
1 | use crate::backend;
2 | use crate::common::{init_veilid, make_route, CommonKeypair, DHTEntity};
3 | use crate::constants::KNOWN_GROUP_LIST;
4 | use crate::group::{self, Group, URL_DHT_KEY, URL_ENCRYPTION_KEY, URL_PUBLIC_KEY, URL_SECRET_KEY};
5 | use crate::repo::Repo;
6 | use anyhow::{anyhow, Result};
7 | use clap::builder::Str;
8 | use iroh::node::Node;
9 | use iroh_blobs::format::collection::Collection;
10 | use iroh_blobs::util::SetTagOption;
11 | use iroh_blobs::Hash;
12 | use serde::{Deserialize, Serialize};
13 | use std::collections::HashMap;
14 | use std::mem;
15 | use std::ops::Deref;
16 | use std::path::{Path, PathBuf};
17 | use std::sync::Arc;
18 | use tokio::fs;
19 | use tokio::sync::Mutex;
20 | use tokio::sync::{
21 | broadcast,
22 | mpsc::{self, Receiver},
23 | oneshot,
24 | };
25 | use tracing::info;
26 | use url::Url;
27 | use veilid_core::{
28 | api_startup_config, vld0_generate_keypair, CryptoKey, CryptoSystem, CryptoSystemVLD0,
29 | CryptoTyped, DHTSchema, KeyPair, ProtectedStore, RoutingContext, SharedSecret, TypedKey,
30 | UpdateCallback, VeilidAPI, VeilidConfigInner, VeilidConfigProtectedStore, VeilidUpdate,
31 | CRYPTO_KEY_LENGTH, CRYPTO_KIND_VLD0,
32 | };
33 | use veilid_iroh_blobs::iroh::VeilidIrohBlobs;
34 | use veilid_iroh_blobs::tunnels::{OnNewRouteCallback, OnRouteDisconnectedCallback};
35 | use xdg::BaseDirectories;
36 |
37 | #[derive(Serialize, Deserialize, Debug)]
38 | pub struct KnownGroupList {
39 | groups: Vec,
40 | }
41 |
42 | pub struct BackendInner {
43 | path: PathBuf,
44 | veilid_api: Option,
45 | update_rx: Option>,
46 | groups: HashMap>,
47 | pub iroh_blobs: Option,
48 | on_new_route_callback: Option,
49 | }
50 |
51 | impl BackendInner {
52 | async fn save_known_group_ids(&self) -> Result<()> {
53 | let groups = self.groups.clone().into_keys().collect();
54 |
55 | let info = KnownGroupList { groups };
56 |
57 | println!("Saving group IDs {:?}", info);
58 | let data =
59 | serde_cbor::to_vec(&info).map_err(|e| anyhow!("Failed to serialize keypair: {}", e))?;
60 | self.veilid()?
61 | .protected_store()?
62 | .save_user_secret(KNOWN_GROUP_LIST, &data)
63 | .map_err(|e| anyhow!("Unable to store known group IDs: {}", e))?;
64 | Ok(())
65 | }
66 |
67 | fn veilid(&self) -> Result {
68 | Ok(self
69 | .veilid_api
70 | .as_ref()
71 | .ok_or_else(|| anyhow!("Veilid API not initialized"))?
72 | .clone())
73 | }
74 |
75 | fn iroh_blobs(&self) -> Result {
76 | Ok(self
77 | .iroh_blobs
78 | .as_ref()
79 | .ok_or_else(|| anyhow!("Veilid Iroh Blobs API not initialized"))?
80 | .clone())
81 | }
82 | }
83 |
84 | #[derive(Clone)]
85 | pub struct Backend {
86 | inner: Arc>,
87 | }
88 |
89 | impl Backend {
90 | pub fn new(base_path: &Path) -> Result {
91 | let inner = BackendInner {
92 | path: base_path.to_path_buf(),
93 | veilid_api: None,
94 | update_rx: None,
95 | groups: HashMap::new(),
96 | iroh_blobs: None,
97 | on_new_route_callback: None,
98 | };
99 |
100 | let backend = Backend {
101 | inner: Arc::new(Mutex::new(inner)),
102 | };
103 |
104 | Ok(backend)
105 | }
106 |
107 | pub async fn from_dependencies(
108 | base_path: &Path,
109 | veilid_api: VeilidAPI,
110 | update_rx: broadcast::Receiver,
111 | store: iroh_blobs::store::fs::Store,
112 | ) -> Result {
113 | let inner = BackendInner {
114 | path: base_path.to_path_buf(),
115 | veilid_api: Some(veilid_api.clone()),
116 | update_rx: Some(update_rx),
117 | groups: HashMap::new(),
118 | iroh_blobs: None,
119 | on_new_route_callback: None,
120 | };
121 |
122 | let backend = Backend {
123 | inner: Arc::new(Mutex::new(inner)),
124 | };
125 |
126 | let inner_clone = backend.inner.clone();
127 |
128 | let on_new_route_callback: OnNewRouteCallback = Arc::new(move |route_id, route_id_blob| {
129 | let inner = inner_clone.clone();
130 | println!("Re-generating route");
131 | tokio::spawn(async move {
132 | let inner = inner.lock().await;
133 |
134 | if let Some(on_new_route) = &inner.on_new_route_callback {
135 | on_new_route(route_id, route_id_blob)
136 | }
137 |
138 | for group in inner.groups.clone().into_values() {
139 | if let Some(repo) = group.get_own_repo().await {
140 | if let Err(err) = repo.update_route_on_dht().await {
141 | eprintln!(
142 | "Unable to update route after rebuild in group {} in repo {}: {}",
143 | group.id(),
144 | repo.id(),
145 | err
146 | );
147 | }
148 | }
149 | }
150 | });
151 | });
152 |
153 | let on_disconnected_callback: OnRouteDisconnectedCallback = Arc::new(move || {
154 | println!("Route died");
155 | });
156 |
157 | let (route_id, route_id_blob) = make_route(&veilid_api).await?;
158 | let routing_context = veilid_api.routing_context()?;
159 |
160 | let mut inner = backend.inner.lock().await;
161 |
162 | // Initialize iroh_blobs
163 | inner.iroh_blobs = Some(VeilidIrohBlobs::new(
164 | veilid_api.clone(),
165 | routing_context,
166 | route_id_blob,
167 | route_id,
168 | inner.update_rx.as_ref().unwrap().resubscribe(),
169 | store,
170 | Some(on_disconnected_callback), // TODO: Notify application of route closure?
171 | Some(on_new_route_callback),
172 | ));
173 |
174 | drop(inner);
175 |
176 | Ok(backend)
177 | }
178 |
179 | pub async fn start(&self) -> Result<()> {
180 | let mut inner = self.inner.lock().await;
181 |
182 | if inner.veilid_api.is_some() {
183 | return Err(anyhow!("Veilid already initialized"));
184 | }
185 | println!("Starting on {}", inner.path.display());
186 |
187 | let base_dir = inner.path.clone();
188 | fs::create_dir_all(&base_dir).await?;
189 |
190 | let (veilid_api, mut update_rx) = init_veilid(&base_dir, "openarchive".to_string()).await?;
191 |
192 | inner.veilid_api = Some(veilid_api.clone());
193 | inner.update_rx = Some(update_rx.resubscribe());
194 |
195 | // Initialize iroh_blobs store
196 | let store = iroh_blobs::store::fs::Store::load(base_dir.join("iroh")).await?;
197 |
198 | // Create route_id and route_id_blob
199 | let (route_id, route_id_blob) = make_route(&veilid_api).await?;
200 |
201 | // Get veilid_api and routing_context
202 | let routing_context = veilid_api.routing_context()?;
203 |
204 | let inner_clone = self.inner.clone();
205 |
206 | let on_new_route_callback: OnNewRouteCallback = Arc::new(move |route_id, route_id_blob| {
207 | let inner = inner_clone.clone();
208 | println!("Re-generating route");
209 | tokio::spawn(async move {
210 | let inner = inner.lock().await;
211 |
212 | if let Some(on_new_route) = &inner.on_new_route_callback {
213 | on_new_route(route_id, route_id_blob)
214 | }
215 |
216 | for group in inner.groups.clone().into_values() {
217 | if let Some(repo) = group.get_own_repo().await {
218 | if let Err(err) = repo.update_route_on_dht().await {
219 | eprintln!(
220 | "Unable to update route after rebuild in group {} in repo {}: {}",
221 | group.id(),
222 | repo.id(),
223 | err
224 | );
225 | }
226 | }
227 | }
228 | });
229 | });
230 |
231 | // Initialize iroh_blobs
232 | inner.iroh_blobs = Some(VeilidIrohBlobs::new(
233 | veilid_api.clone(),
234 | routing_context,
235 | route_id_blob,
236 | route_id,
237 | update_rx.resubscribe(),
238 | store,
239 | None, // TODO: Notify application of route closure?
240 | Some(on_new_route_callback),
241 | ));
242 |
243 | drop(inner);
244 |
245 | if let Err(err) = self.load_known_groups().await {
246 | eprintln!("No known groups on start");
247 | }
248 |
249 | Ok(())
250 | }
251 |
252 | pub async fn stop(&self) -> Result<()> {
253 | let mut inner = self.inner.lock().await;
254 | println!("Stopping Backend...");
255 | if let Some(iroh_blobs) = inner.iroh_blobs.take() {
256 | println!("Shutting down Veilid Iroh Blobs");
257 | iroh_blobs.shutdown().await?;
258 | println!("Veilid Iroh Blobs shut down successfully");
259 | }
260 | if inner.veilid_api.is_some() {
261 | println!("Shutting down Veilid API");
262 | let veilid = inner.veilid_api.take();
263 | veilid.unwrap().shutdown().await;
264 | println!("Veilid API shut down successfully");
265 | inner.groups = HashMap::new();
266 | }
267 | Ok(())
268 | }
269 |
270 | pub async fn set_on_new_route_callback(
271 | &self,
272 | on_new_route_connected_callback: OnNewRouteCallback,
273 | ) {
274 | let mut inner = self.inner.lock().await;
275 | inner.on_new_route_callback = Some(on_new_route_connected_callback);
276 | }
277 |
278 | pub async fn join_from_url(&self, url_string: &str) -> Result> {
279 | let keys = parse_url(url_string)?;
280 | self.join_group(keys).await
281 | }
282 |
283 | pub async fn get_route_id_blob(&self) -> Result> {
284 | if let Some(blobs) = self.get_iroh_blobs().await {
285 | Ok(blobs.route_id_blob().await)
286 | } else {
287 | Err(anyhow!("Veilid not initialized"))
288 | }
289 | }
290 |
291 | pub async fn join_group(&self, keys: CommonKeypair) -> Result> {
292 | let mut inner = self.inner.lock().await;
293 |
294 | let iroh_blobs = inner.iroh_blobs()?;
295 | let veilid = inner.veilid()?;
296 |
297 | let routing_context = veilid.routing_context()?;
298 | let crypto_system = veilid
299 | .crypto()?
300 | .get(CRYPTO_KIND_VLD0)
301 | .ok_or_else(|| anyhow!("Unable to init crypto system"));
302 |
303 | let record_key = TypedKey::new(CRYPTO_KIND_VLD0, keys.id);
304 | // First open the DHT record
305 | let dht_record = routing_context
306 | .open_dht_record(record_key.clone(), None) // Don't pass a writer here yet
307 | .await?;
308 |
309 | // Use the owner key from the DHT record as the default writer
310 | let owner_key = dht_record.owner(); // Call the owner() method to get the owner key
311 |
312 | // Reopen the DHT record with the owner key as the writer
313 | let dht_record = routing_context
314 | .open_dht_record(
315 | record_key.clone(),
316 | Some(KeyPair::new(
317 | owner_key.clone(),
318 | keys.secret_key.clone().unwrap(),
319 | )),
320 | )
321 | .await?;
322 |
323 | let mut group = Group::new(
324 | dht_record.clone(),
325 | keys.encryption_key.clone(),
326 | routing_context,
327 | veilid.clone(),
328 | iroh_blobs.clone(),
329 | );
330 |
331 | group.try_load_repo_from_disk().await;
332 | group.load_repos_from_dht().await?;
333 |
334 | inner.groups.insert(group.id(), Box::new(group.clone()));
335 |
336 | inner.save_known_group_ids().await?;
337 |
338 | Ok(Box::new(group))
339 | }
340 |
341 | pub async fn create_group(&self) -> Result {
342 | let mut inner = self.inner.lock().await;
343 | let iroh_blobs = inner.iroh_blobs()?;
344 | let veilid = inner.veilid()?;
345 |
346 | let routing_context = veilid.routing_context()?;
347 | let crypto = veilid.crypto()?;
348 | let crypto_system = crypto
349 | .get(CRYPTO_KIND_VLD0)
350 | .ok_or_else(|| anyhow!("Unable to init crypto system"))?;
351 |
352 | let schema = DHTSchema::dflt(65)?; // 64 members + a title
353 | let kind = Some(CRYPTO_KIND_VLD0);
354 | let owner_keypair = crypto_system.generate_keypair();
355 |
356 | let dht_record = routing_context
357 | .create_dht_record(schema, Some(owner_keypair), kind)
358 | .await?;
359 |
360 | let encryption_key = crypto_system.random_shared_secret();
361 |
362 | let group = Group::new(
363 | dht_record.clone(),
364 | encryption_key,
365 | routing_context,
366 | veilid.clone(),
367 | iroh_blobs.clone(),
368 | );
369 |
370 | let protected_store = veilid.protected_store().unwrap();
371 | CommonKeypair {
372 | id: group.id(),
373 | public_key: dht_record.owner().clone(),
374 | secret_key: group.get_secret_key(),
375 | encryption_key: group.get_encryption_key(),
376 | }
377 | .store_keypair(&protected_store)
378 | .await
379 | .map_err(|e| anyhow!(e))?;
380 |
381 | inner.groups.insert(group.id(), Box::new(group.clone()));
382 |
383 | inner.save_known_group_ids().await?;
384 |
385 | Ok(group)
386 | }
387 |
388 | pub async fn get_group(&self, record_key: &CryptoKey) -> Result> {
389 | let mut inner = self.inner.lock().await;
390 | if let Some(group) = inner.groups.get(record_key) {
391 | return Ok(group.clone());
392 | }
393 | let iroh_blobs = inner.iroh_blobs()?;
394 | let veilid = inner.veilid()?;
395 |
396 | let routing_context = veilid.routing_context()?;
397 | let protected_store = veilid.protected_store().unwrap();
398 |
399 | // Load the keypair associated with the record_key from the protected store
400 | let retrieved_keypair = CommonKeypair::load_keypair(&protected_store, record_key)
401 | .await
402 | .map_err(|_| anyhow!("Failed to load keypair"))?;
403 |
404 | let crypto = veilid.crypto()?;
405 | let crypto_system = crypto
406 | .get(CRYPTO_KIND_VLD0)
407 | .ok_or_else(|| anyhow!("Unable to init crypto system"))?;
408 |
409 | // Use the owner key from the DHT record as the default writer
410 | let owner_key = retrieved_keypair.public_key; // Call the owner() method to get the owner key
411 | let owner_secret = retrieved_keypair.secret_key;
412 | let record_key = TypedKey::new(CRYPTO_KIND_VLD0, *record_key);
413 |
414 | let owner = owner_secret.map(|secret| KeyPair::new(owner_key, secret));
415 |
416 | // Reopen the DHT record with the owner key as the writer
417 | let dht_record = routing_context
418 | .open_dht_record(record_key.clone(), owner)
419 | .await?;
420 |
421 | let mut group = Group::new(
422 | dht_record.clone(),
423 | retrieved_keypair.encryption_key.clone(),
424 | routing_context,
425 | veilid.clone(),
426 | iroh_blobs.clone(),
427 | );
428 |
429 | group.try_load_repo_from_disk().await;
430 | group.load_repos_from_dht().await?;
431 |
432 | inner.groups.insert(group.id(), Box::new(group.clone()));
433 |
434 | drop(inner);
435 |
436 | Ok(Box::new(group))
437 | }
438 |
439 | pub async fn list_groups(&self) -> Result>> {
440 | let mut inner = self.inner.lock().await;
441 | Ok(inner.groups.values().cloned().collect())
442 | }
443 |
444 | pub async fn load_known_groups(&self) -> Result<()> {
445 | for id in self.list_known_group_ids().await?.iter() {
446 | self.get_group(id).await?;
447 | }
448 | Ok(())
449 | }
450 |
451 | pub async fn list_known_group_ids(&self) -> Result> {
452 | let mut inner = self.inner.lock().await;
453 | let veilid = inner.veilid()?;
454 | let data = veilid
455 | .protected_store()?
456 | .load_user_secret(KNOWN_GROUP_LIST)
457 | .map_err(|_| anyhow!("Failed to load keypair"))?
458 | .ok_or_else(|| anyhow!("Keypair not found"))?;
459 | let info: KnownGroupList =
460 | serde_cbor::from_slice(&data).map_err(|_| anyhow!("Failed to deserialize keypair"))?;
461 | Ok(info.groups)
462 | }
463 |
464 | pub async fn close_group(&self, key: CryptoKey) -> Result<()> {
465 | let mut inner = self.inner.lock().await;
466 | if let Some(group) = inner.groups.remove(&key) {
467 | group.close().await.map_err(|e| anyhow!(e))?;
468 | } else {
469 | return Err(anyhow!("Group not found"));
470 | }
471 | Ok(())
472 | }
473 |
474 | pub async fn create_collection(&self) -> Result {
475 | // Initialize a new Iroh Node in memory
476 | let node = Node::memory().spawn().await?;
477 |
478 | // Get the Client from the node
479 | let iroh_client = node.client().blobs();
480 |
481 | // Create an empty Collection
482 | let mut collection = Collection::default();
483 |
484 | // Tag options for creating the collection
485 | let tag_option = SetTagOption::Auto;
486 |
487 | // No tags to delete, so we pass an empty vector
488 | let tags_to_delete = Vec::new();
489 |
490 | // Use the iroh_client instance to create the collection and get the root hash
491 | let (root_hash, _tag) = iroh_client
492 | .create_collection(collection, tag_option, tags_to_delete)
493 | .await?;
494 |
495 | // Return the root hash
496 | Ok(root_hash)
497 | }
498 |
499 | pub async fn subscribe_updates(&self) -> Option> {
500 | let mut inner = self.inner.lock().await;
501 | inner.update_rx.as_ref().map(|rx| rx.resubscribe())
502 | }
503 |
504 | pub async fn get_veilid_api(&self) -> Option {
505 | let mut inner = self.inner.lock().await;
506 |
507 | inner.veilid_api.clone()
508 | }
509 |
510 | pub async fn get_iroh_blobs(&self) -> Option {
511 | let mut inner = self.inner.lock().await;
512 | inner.iroh_blobs.clone()
513 | }
514 |
515 | pub async fn get_routing_context(&self) -> Option {
516 | let veilid_api = self.get_veilid_api().await?;
517 | veilid_api.routing_context().ok()
518 | }
519 | }
520 |
521 | async fn wait_for_network(update_rx: &mut broadcast::Receiver) -> Result<()> {
522 | while let Ok(update) = update_rx.recv().await {
523 | if let VeilidUpdate::Attachment(attachment_state) = update {
524 | if attachment_state.public_internet_ready {
525 | println!("Public internet ready!");
526 | break;
527 | }
528 | }
529 | }
530 | Ok(())
531 | }
532 |
533 | fn find_query(url: &Url, key: &str) -> Result {
534 | for (query_key, value) in url.query_pairs() {
535 | if query_key == key {
536 | return Ok(value.into_owned());
537 | }
538 | }
539 |
540 | Err(anyhow!("Unable to find parameter {} in URL {:?}", key, url))
541 | }
542 |
543 | pub fn crypto_key_from_query(url: &Url, key: &str) -> Result {
544 | let value = find_query(url, key)?;
545 | let bytes = hex::decode(value)?;
546 | let mut key_vec: [u8; CRYPTO_KEY_LENGTH] = [0; CRYPTO_KEY_LENGTH];
547 | key_vec.copy_from_slice(bytes.as_slice());
548 |
549 | let key = CryptoKey::from(key_vec);
550 | Ok(key)
551 | }
552 |
553 | pub fn parse_url(url_string: &str) -> Result {
554 | let url = Url::parse(url_string)?;
555 |
556 | let id = crypto_key_from_query(&url, URL_DHT_KEY)?;
557 | let encryption_key = crypto_key_from_query(&url, URL_ENCRYPTION_KEY)?;
558 | let public_key = crypto_key_from_query(&url, URL_PUBLIC_KEY)?;
559 | let secret_key = Some(crypto_key_from_query(&url, URL_SECRET_KEY)?);
560 |
561 | Ok(CommonKeypair {
562 | id,
563 | public_key,
564 | secret_key,
565 | encryption_key,
566 | })
567 | }
568 |
--------------------------------------------------------------------------------
/src/common.rs:
--------------------------------------------------------------------------------
1 | #![allow(async_fn_in_trait)]
2 | #![allow(clippy::async_yields_async)]
3 |
4 | use anyhow::{anyhow, Result};
5 | use serde::{Deserialize, Serialize};
6 | use std::{path::Path, path::PathBuf, sync::Arc};
7 | use tokio::sync::broadcast::{self, Receiver};
8 | use url::Url;
9 | use veilid_core::{
10 | CryptoKey, CryptoSystem, CryptoSystemVLD0, CryptoTyped, DHTRecordDescriptor, KeyPair, Nonce,
11 | ProtectedStore, RouteId, RoutingContext, Sequencing, SharedSecret, Stability, UpdateCallback,
12 | VeilidAPI, VeilidConfigInner, VeilidUpdate, CRYPTO_KIND_VLD0, VALID_CRYPTO_KINDS,
13 | };
14 |
15 | use crate::constants::ROUTE_ID_DHT_KEY;
16 |
17 | pub async fn make_route(veilid: &VeilidAPI) -> Result<(RouteId, Vec)> {
18 | let mut retries = 6;
19 | while retries > 0 {
20 | retries -= 1;
21 | let result = veilid
22 | .new_custom_private_route(
23 | &VALID_CRYPTO_KINDS,
24 | Stability::LowLatency,
25 | Sequencing::NoPreference,
26 | )
27 | .await;
28 |
29 | if let Ok(value) = result {
30 | return Ok(value);
31 | } else if let Err(e) = &result {
32 | eprintln!("Failed to create route: {}", e);
33 | }
34 | }
35 | Err(anyhow!("Unable to create route, reached max retries"))
36 | }
37 |
38 | pub async fn init_veilid(
39 | base_dir: &Path,
40 | namespace: String,
41 | ) -> Result<(VeilidAPI, Receiver)> {
42 | let config_inner = config_for_dir(base_dir.to_path_buf(), namespace);
43 |
44 | let (tx, mut rx) = broadcast::channel(32);
45 |
46 | let update_callback: UpdateCallback = Arc::new(move |update| {
47 | let tx = tx.clone();
48 | tokio::spawn(async move {
49 | if tx.send(update).is_err() {
50 | // TODO:
51 | //println!("receiver dropped");
52 | }
53 | });
54 | });
55 |
56 | // println!("Init veilid");
57 | let veilid = veilid_core::api_startup_config(update_callback, config_inner).await?;
58 |
59 | //println!("Attach veilid");
60 |
61 | veilid.attach().await?;
62 |
63 | //println!("Wait for veilid network");
64 |
65 | while let Ok(update) = rx.recv().await {
66 | if let VeilidUpdate::Attachment(attachment_state) = update {
67 | if attachment_state.public_internet_ready && attachment_state.state.is_attached() {
68 | println!("Public internet ready!");
69 | break;
70 | }
71 | }
72 | }
73 |
74 | Ok((veilid, rx))
75 | }
76 |
77 | pub fn config_for_dir(base_dir: PathBuf, namespace: String) -> VeilidConfigInner {
78 | VeilidConfigInner {
79 | program_name: "save-dweb-backend".to_string(),
80 | namespace,
81 | protected_store: veilid_core::VeilidConfigProtectedStore {
82 | // avoid prompting for password, don't do this in production
83 | always_use_insecure_storage: true,
84 | directory: base_dir
85 | .join("protected_store")
86 | .to_string_lossy()
87 | .to_string(),
88 | ..Default::default()
89 | },
90 | table_store: veilid_core::VeilidConfigTableStore {
91 | directory: base_dir.join("table_store").to_string_lossy().to_string(),
92 | ..Default::default()
93 | },
94 | block_store: veilid_core::VeilidConfigBlockStore {
95 | directory: base_dir.join("block_store").to_string_lossy().to_string(),
96 | ..Default::default()
97 | },
98 | ..Default::default()
99 | }
100 | }
101 |
102 | #[derive(Serialize, Deserialize, Clone)]
103 | pub struct CommonKeypair {
104 | pub id: CryptoKey,
105 | pub public_key: CryptoKey,
106 | pub secret_key: Option,
107 | pub encryption_key: SharedSecret,
108 | }
109 |
110 | impl CommonKeypair {
111 | pub async fn store_keypair(&self, protected_store: &ProtectedStore) -> Result<()> {
112 | let keypair_data =
113 | serde_cbor::to_vec(&self).map_err(|e| anyhow!("Failed to serialize keypair: {}", e))?;
114 | protected_store
115 | .save_user_secret(self.id.to_string(), &keypair_data)
116 | .map_err(|e| anyhow!("Unable to store keypair: {}", e))?;
117 | Ok(())
118 | }
119 |
120 | pub async fn load_keypair(protected_store: &ProtectedStore, id: &CryptoKey) -> Result {
121 | let keypair_data = protected_store
122 | .load_user_secret(id.to_string())
123 | .map_err(|_| anyhow!("Failed to load keypair"))?
124 | .ok_or_else(|| anyhow!("Keypair not found"))?;
125 | let retrieved_keypair: CommonKeypair = serde_cbor::from_slice(&keypair_data)
126 | .map_err(|_| anyhow!("Failed to deserialize keypair"))?;
127 | Ok(retrieved_keypair)
128 | }
129 | }
130 |
131 | pub trait DHTEntity {
132 | fn get_id(&self) -> CryptoKey;
133 | fn get_encryption_key(&self) -> SharedSecret;
134 | fn get_routing_context(&self) -> RoutingContext;
135 | fn get_veilid_api(&self) -> VeilidAPI;
136 | fn get_dht_record(&self) -> DHTRecordDescriptor;
137 | fn get_secret_key(&self) -> Option;
138 |
139 | // Default method to get the owner key
140 | fn owner_key(&self) -> CryptoKey {
141 | self.get_dht_record().owner().clone()
142 | }
143 |
144 | // Default method to get the owner secret
145 | fn owner_secret(&self) -> Option {
146 | self.get_dht_record().owner_secret().cloned()
147 | }
148 |
149 | fn encrypt_aead(&self, data: &[u8], associated_data: Option<&[u8]>) -> Result> {
150 | let veilid = self.get_veilid_api();
151 | let crypto = veilid.crypto()?;
152 | let crypto_system = crypto
153 | .get(CRYPTO_KIND_VLD0)
154 | .ok_or_else(|| anyhow!("Unable to init crypto system"))?;
155 | let nonce = crypto_system.random_nonce();
156 | let mut buffer = Vec::with_capacity(nonce.as_slice().len() + data.len());
157 | buffer.extend_from_slice(nonce.as_slice());
158 | let encrypted_chunk = crypto_system
159 | .encrypt_aead(data, &nonce, &self.get_encryption_key(), associated_data)
160 | .map_err(|e| anyhow!("Failed to encrypt data: {}", e))?;
161 | buffer.extend_from_slice(&encrypted_chunk);
162 | Ok(buffer)
163 | }
164 |
165 | fn decrypt_aead(&self, data: &[u8], associated_data: Option<&[u8]>) -> Result> {
166 | let veilid = self.get_veilid_api();
167 | let crypto = veilid.crypto()?;
168 | let crypto_system = crypto
169 | .get(CRYPTO_KIND_VLD0)
170 | .ok_or_else(|| anyhow!("Unable to init crypto system"))?;
171 |
172 | let nonce: [u8; 24] = data[..24]
173 | .try_into()
174 | .map_err(|_| anyhow!("Failed to convert nonce slice to array"))?;
175 | let nonce = Nonce::new(nonce);
176 | let encrypted_data = &data[24..];
177 | crypto_system
178 | .decrypt_aead(
179 | encrypted_data,
180 | &nonce,
181 | &self.get_encryption_key(),
182 | associated_data,
183 | )
184 | .map_err(|e| anyhow!("Failed to decrypt data: {}", e))
185 | }
186 |
187 | async fn set_name(&self, name: &str) -> Result<()> {
188 | let routing_context = self.get_routing_context();
189 | let key = self.get_dht_record().key().clone();
190 | let encrypted_name = self.encrypt_aead(name.as_bytes(), None)?;
191 | routing_context
192 | .set_dht_value(key, 0, encrypted_name, None)
193 | .await?;
194 | Ok(())
195 | }
196 |
197 | async fn get_name(&self) -> Result {
198 | let routing_context = self.get_routing_context();
199 | let key = self.get_dht_record().key().clone();
200 | let value = routing_context.get_dht_value(key, 0, false).await?;
201 | match value {
202 | Some(value) => {
203 | let decrypted_name = self.decrypt_aead(value.data(), None)?;
204 | Ok(String::from_utf8(decrypted_name)
205 | .map_err(|e| anyhow!("Failed to convert DHT value to string: {}", e))?)
206 | }
207 | None => Err(anyhow!("Value not found")),
208 | }
209 | }
210 |
211 | async fn close(&self) -> Result<()> {
212 | let routing_context = self.get_routing_context();
213 | let key = self.get_dht_record().key().clone();
214 | routing_context.close_dht_record(key).await?;
215 | Ok(())
216 | }
217 |
218 | async fn store_route_id_in_dht(&self, route_id_blob: Vec) -> Result<()> {
219 | let routing_context = &self.get_routing_context();
220 | let dht_record = self.get_dht_record();
221 | routing_context
222 | .set_dht_value(
223 | dht_record.key().clone(),
224 | ROUTE_ID_DHT_KEY,
225 | route_id_blob,
226 | None,
227 | )
228 | .await
229 | .map_err(|e| anyhow!("Failed to store route ID blob in DHT: {}", e))?;
230 |
231 | Ok(())
232 | }
233 |
234 | async fn get_route_id_from_dht(&self, subkey: u32) -> Result> {
235 | let routing_context = &self.get_routing_context();
236 |
237 | // Use the existing DHT record
238 | let dht_record = self.get_dht_record();
239 |
240 | // Get the stored route ID blob at subkey
241 | let stored_blob = routing_context
242 | .get_dht_value(dht_record.key().clone(), ROUTE_ID_DHT_KEY, false)
243 | .await?
244 | .ok_or_else(|| anyhow!("Route ID blob not found in DHT"))?;
245 |
246 | Ok(stored_blob.data().to_vec())
247 | }
248 |
249 | // Send an AppMessage to the repo owner using the stored route ID blob
250 | async fn send_message_to_owner(
251 | &self,
252 | veilid: &VeilidAPI,
253 | message: Vec,
254 | subkey: u32,
255 | ) -> Result<()> {
256 | let routing_context = self.get_routing_context();
257 |
258 | // Retrieve the route ID blob from DHT
259 | let route_id_blob = self.get_route_id_from_dht(subkey).await?;
260 |
261 | // Import the route using the blob via VeilidAPI
262 | let route_id = match veilid.import_remote_private_route(route_id_blob) {
263 | Ok(route) => route,
264 | Err(e) => {
265 | eprintln!("Failed to import remote private route: {:?}", e);
266 | return Err(e.into());
267 | }
268 | };
269 |
270 | // Send an AppMessage to the repo owner using the imported route ID
271 | if let Err(e) = routing_context
272 | .app_message(veilid_core::Target::PrivateRoute(route_id), message)
273 | .await
274 | {
275 | eprintln!("Failed to send message: {:?}", e);
276 | return Err(e.into());
277 | }
278 |
279 | Ok(())
280 | }
281 |
282 | fn get_write_key(&self) -> Option {
283 | unimplemented!("WIP")
284 | }
285 |
286 | async fn members(&self) -> Result> {
287 | unimplemented!("WIP")
288 | }
289 |
290 | async fn join(&self) -> Result<()> {
291 | unimplemented!("WIP")
292 | }
293 |
294 | async fn leave(&self) -> Result<()> {
295 | unimplemented!("WIP")
296 | }
297 | }
298 |
--------------------------------------------------------------------------------
/src/constants.rs:
--------------------------------------------------------------------------------
1 | pub const GROUP_NOT_FOUND: &str = "Group not found";
2 | pub const UNABLE_TO_SET_GROUP_NAME: &str = "Unable to set group name";
3 | pub const UNABLE_TO_GET_GROUP_NAME: &str = "Unable to get group name";
4 | pub const TEST_GROUP_NAME: &str = "Test Group";
5 | pub const UNABLE_TO_STORE_KEYPAIR: &str = "Unable to store keypair";
6 | pub const FAILED_TO_LOAD_KEYPAIR: &str = "Failed to load keypair";
7 | pub const KEYPAIR_NOT_FOUND: &str = "Keypair not found";
8 | pub const FAILED_TO_DESERIALIZE_KEYPAIR: &str = "Failed to deserialize keypair";
9 | pub const ROUTE_ID_DHT_KEY: u32 = 2;
10 |
11 | pub const KNOWN_GROUP_LIST: &str = "SAVE_BACKEND_KNOWN_GROUPS";
12 |
--------------------------------------------------------------------------------
/src/group.rs:
--------------------------------------------------------------------------------
1 | use crate::common::CommonKeypair;
2 | use crate::repo::Repo;
3 | use crate::{common::DHTEntity, repo};
4 | use anyhow::{anyhow, Error, Result};
5 | use bytes::Bytes;
6 | use hex::ToHex;
7 | use iroh::net::key::SecretKey;
8 | use iroh_blobs::Hash;
9 | use rand::seq::SliceRandom;
10 | use rand::thread_rng;
11 | use serde::{Deserialize, Serialize};
12 | use std::any::Any;
13 | use std::collections::HashMap;
14 | use std::future::Future;
15 | use std::time::{SystemTime, UNIX_EPOCH};
16 |
17 | use std::path::PathBuf;
18 | use std::result;
19 | use std::sync::Arc;
20 | use tokio::sync::{mpsc, Mutex};
21 | use url::Url;
22 | use veilid_core::{
23 | CryptoKey, CryptoSystemVLD0, CryptoTyped, DHTRecordDescriptor, DHTReportScope, DHTSchema,
24 | KeyPair, ProtectedStore, RoutingContext, SharedSecret, TypedKey, ValueSubkeyRangeSet,
25 | VeilidAPI, VeilidUpdate, CRYPTO_KEY_LENGTH, CRYPTO_KIND_VLD0,
26 | };
27 | use veilid_iroh_blobs::iroh::VeilidIrohBlobs;
28 |
29 | pub const PROTOCOL_SCHEME: &str = "save+dweb:";
30 | pub const URL_DHT_KEY: &str = "dht";
31 | pub const URL_ENCRYPTION_KEY: &str = "enc";
32 | pub const URL_PUBLIC_KEY: &str = "pk";
33 | pub const URL_SECRET_KEY: &str = "sk";
34 |
35 | #[derive(Clone)]
36 | pub struct Group {
37 | pub dht_record: DHTRecordDescriptor,
38 | pub encryption_key: SharedSecret,
39 | pub routing_context: RoutingContext,
40 | pub repos: Arc>>,
41 | pub veilid: VeilidAPI,
42 | pub iroh_blobs: VeilidIrohBlobs,
43 | }
44 |
45 | impl Group {
46 | pub fn new(
47 | dht_record: DHTRecordDescriptor,
48 | encryption_key: SharedSecret,
49 | routing_context: RoutingContext,
50 | veilid: VeilidAPI,
51 | iroh_blobs: VeilidIrohBlobs,
52 | ) -> Self {
53 | Self {
54 | dht_record,
55 | encryption_key,
56 | routing_context,
57 | repos: Arc::new(Mutex::new(HashMap::new())),
58 | veilid,
59 | iroh_blobs,
60 | }
61 | }
62 |
63 | pub fn id(&self) -> CryptoKey {
64 | self.dht_record.key().value.clone()
65 | }
66 |
67 | pub fn owner_key(&self) -> CryptoKey {
68 | self.dht_record.owner().clone()
69 | }
70 |
71 | pub fn owner_secret(&self) -> Option {
72 | self.dht_record.owner_secret().cloned()
73 | }
74 |
75 | async fn add_repo(&mut self, repo: Repo) -> Result<()> {
76 | let id = repo.id();
77 | self.repos.lock().await.insert(id, repo);
78 | Ok(())
79 | }
80 |
81 | pub async fn get_repo(&self, id: &CryptoKey) -> Result {
82 | self.repos
83 | .lock()
84 | .await
85 | .get(id)
86 | .ok_or_else(|| anyhow!("Repo not loaded"))
87 | .cloned()
88 | }
89 |
90 | pub async fn has_repo(&self, id: &CryptoKey) -> bool {
91 | self.repos.lock().await.contains_key(id)
92 | }
93 |
94 | pub async fn list_repos(&self) -> Vec {
95 | self.repos
96 | .lock()
97 | .await
98 | .values()
99 | .map(|repo| repo.get_id())
100 | .collect()
101 | }
102 |
103 | pub async fn get_own_repo(&self) -> Option {
104 | self.repos
105 | .lock()
106 | .await
107 | .values()
108 | .find(|repo| repo.can_write())
109 | .cloned()
110 | }
111 |
112 | pub async fn list_peer_repos(&self) -> Vec {
113 | self.repos
114 | .lock()
115 | .await
116 | .values()
117 | .filter(|repo| !repo.can_write())
118 | .cloned()
119 | .collect()
120 | }
121 |
122 | pub async fn download_hash_from_peers(&self, hash: &Hash) -> Result<()> {
123 | // Ask peers to download in random order
124 | let mut rng = thread_rng();
125 | let mut repos = self.list_peer_repos().await;
126 | repos.shuffle(&mut rng);
127 |
128 | if repos.is_empty() {
129 | return Err(anyhow!("Cannot download hash. No other peers found"));
130 | }
131 |
132 | for repo in repos.iter() {
133 | if let Ok(route_id_blob) = repo.get_route_id_blob().await {
134 | // It's faster to try and fail, than to ask then try
135 | let result = self
136 | .iroh_blobs
137 | .download_file_from(route_id_blob, hash)
138 | .await;
139 | if result.is_ok() {
140 | return Ok(());
141 | } else {
142 | eprintln!("Unable to download from peer, {}", result.unwrap_err());
143 | }
144 | }
145 | }
146 |
147 | Err(anyhow!("Unable to download from any peer"))
148 | }
149 |
150 | pub async fn peers_have_hash(&self, hash: &Hash) -> Result {
151 | for repo in self.list_peer_repos().await.iter() {
152 | if let Ok(route_id_blob) = repo.get_route_id_blob().await {
153 | println!("Asking {} from {} via {:?}", hash, repo.id(), route_id_blob);
154 | if let Ok(has) = self.iroh_blobs.ask_hash(route_id_blob, *hash).await {
155 | if has {
156 | return Ok(true);
157 | }
158 | }
159 | }
160 | }
161 |
162 | Ok(false)
163 | }
164 |
165 | pub async fn has_hash(&self, hash: &Hash) -> Result {
166 | let has = self.iroh_blobs.has_hash(hash).await;
167 |
168 | Ok(has)
169 | }
170 |
171 | pub async fn get_stream_from_hash(
172 | &self,
173 | hash: &Hash,
174 | ) -> Result>> {
175 | if !self.has_hash(hash).await? {
176 | self.download_hash_from_peers(hash).await?
177 | }
178 |
179 | let receiver = self.iroh_blobs.read_file(*hash).await.unwrap();
180 |
181 | Ok(receiver)
182 | }
183 |
184 | pub async fn get_repo_name(&self, repo_key: CryptoKey) -> Result {
185 | if let Some(repo) = self.repos.lock().await.get(&repo_key) {
186 | repo.get_name().await
187 | } else {
188 | Err(anyhow!("Repo not found"))
189 | }
190 | }
191 |
192 | pub fn get_url(&self) -> String {
193 | let mut url = Url::parse(format!("{0}:?", PROTOCOL_SCHEME).as_str()).unwrap();
194 |
195 | url.query_pairs_mut()
196 | .append_pair(URL_DHT_KEY, self.id().encode_hex::().as_str())
197 | .append_pair(
198 | URL_ENCRYPTION_KEY,
199 | self.get_encryption_key().encode_hex::().as_str(),
200 | )
201 | .append_pair(
202 | URL_PUBLIC_KEY,
203 | self.owner_key().encode_hex::().as_str(),
204 | )
205 | .append_pair(
206 | URL_SECRET_KEY,
207 | self.owner_secret().unwrap().encode_hex::().as_str(),
208 | );
209 |
210 | url.to_string()
211 | }
212 |
213 | async fn dht_repo_count(&self) -> Result {
214 | let dht_record = &self.dht_record;
215 | let range = ValueSubkeyRangeSet::full();
216 | let scope = DHTReportScope::UpdateGet;
217 |
218 | let record_key = dht_record.key().clone();
219 |
220 | let report = self
221 | .routing_context
222 | .inspect_dht_record(record_key, range, scope)
223 | .await?;
224 |
225 | let size = report.network_seqs().len();
226 |
227 | let mut count = 0;
228 |
229 | while count < (size - 1) {
230 | let value = self
231 | .routing_context
232 | .get_dht_value(record_key, (count + 1).try_into()?, true)
233 | .await?;
234 | if value.is_some() {
235 | count += 1;
236 | } else {
237 | return Ok(count);
238 | }
239 | }
240 |
241 | Ok(count)
242 | }
243 |
244 | pub async fn advertise_own_repo(&self) -> Result<()> {
245 | let repo = self
246 | .get_own_repo()
247 | .await
248 | .ok_or_else(|| anyhow!("No own repo found for group"))?;
249 |
250 | let repo_key = repo.id().to_vec();
251 |
252 | let count = self.dht_repo_count().await? + 1;
253 |
254 | self.routing_context
255 | .set_dht_value(
256 | self.dht_record.key().clone(),
257 | count.try_into()?,
258 | repo_key,
259 | None,
260 | )
261 | .await?;
262 |
263 | Ok(())
264 | }
265 |
266 | pub async fn load_repo_from_network(
267 | &mut self,
268 | repo_id: TypedKey,
269 | ) -> Result {
270 | // TODO: Load keypair from DHT
271 | // let protected_store = self.protected_store().unwrap();
272 | // Load keypair using the repo ID
273 | // let retrieved_keypair = CommonKeypair::load_keypair(&protected_store, &repo_id.value)
274 | // .await
275 | // .map_err(|_| anyhow!("Failed to load keypair for repo_id: {:?}", repo_id))?;
276 | // Some(KeyPair::new(
277 | // owner_key.clone(),
278 | // retrieved_keypair.secret_key.clone().unwrap(),
279 | // ))
280 | let keypair = None;
281 |
282 | let veilid = self.get_veilid_api();
283 | let mut dht_record: Option = None;
284 | let mut retries = 6;
285 |
286 | while retries > 0 {
287 | retries -= 1;
288 | let dht_record_result = self
289 | .routing_context
290 | .open_dht_record(repo_id.clone(), keypair.clone())
291 | .await;
292 |
293 | match dht_record_result {
294 | Ok(record) => {
295 | dht_record = Some(record);
296 | break;
297 | }
298 | Err(e) => {
299 | eprintln!(
300 | "Failed to open DHT record: {}. Retries left: {}",
301 | e, retries
302 | );
303 | if retries == 0 {
304 | return Err(anyhow!(
305 | "Unable to open DHT record, reached max retries: {}",
306 | e
307 | ));
308 | }
309 | }
310 | }
311 |
312 | // Add a delay before retrying (wit exponential backoff)
313 | tokio::time::sleep(std::time::Duration::from_millis(100 * (7 - retries) as u64)).await;
314 | }
315 |
316 | // Ensure that `dht_record` is set before proceeding
317 | let dht_record = dht_record.ok_or_else(|| anyhow!("DHT record retrieval failed"))?;
318 |
319 | let repo = Repo {
320 | dht_record,
321 | encryption_key: self.encryption_key.clone(),
322 | secret_key: None,
323 | routing_context: self.routing_context.clone(),
324 | veilid: veilid.clone(),
325 | iroh_blobs: self.iroh_blobs.clone(),
326 | };
327 |
328 | self.add_repo(repo.clone()).await?;
329 |
330 | Ok(repo)
331 | }
332 |
333 | async fn load_repo_from_dht(&mut self, subkey: u32) -> Result> {
334 | let repo_id_raw = self
335 | .routing_context
336 | .get_dht_value(self.dht_record.key().clone(), subkey, true)
337 | .await?
338 | .ok_or_else(|| anyhow!("Unable to load repo ID from DHT"))?;
339 |
340 | let mut repo_id_buffer: [u8; CRYPTO_KEY_LENGTH] = [0; CRYPTO_KEY_LENGTH];
341 |
342 | // Validate the length before copying
343 | if repo_id_raw.data().len() != repo_id_buffer.len() {
344 | return Err(anyhow!(
345 | "Slice length mismatch: expected {}, got {}",
346 | repo_id_buffer.len(),
347 | repo_id_raw.data().len()
348 | ));
349 | }
350 |
351 | repo_id_buffer.copy_from_slice(repo_id_raw.data());
352 |
353 | let repo_id = TypedKey::new(CRYPTO_KIND_VLD0, CryptoKey::from(repo_id_buffer));
354 |
355 | if self.repos.lock().await.contains_key(&repo_id.value) {
356 | return Ok(repo_id);
357 | }
358 | self.load_repo_from_network(repo_id).await?;
359 |
360 | Ok(repo_id)
361 | }
362 |
363 | pub async fn load_repos_from_dht(&mut self) -> Result<()> {
364 | let count = self.dht_repo_count().await?;
365 |
366 | let mut i = 1;
367 | while i <= count {
368 | println!("Loading from DHT {}", i);
369 | if let Err(e) = self.load_repo_from_dht(i.try_into()?).await {
370 | eprintln!("Warning: Failed to load repo {} from DHT: {:?}", i, e);
371 | }
372 | i += 1;
373 | }
374 |
375 | Ok(())
376 | }
377 |
378 | pub async fn try_load_repo_from_disk(&mut self) -> bool {
379 | if let Err(err) = self.load_repo_from_disk().await {
380 | eprintln!("Unable to load own repo from disk {}", err);
381 | false
382 | } else {
383 | true
384 | }
385 | }
386 |
387 | pub async fn load_repo_from_disk(&mut self) -> Result {
388 | let protected_store = self.veilid.protected_store().unwrap();
389 |
390 | let mut group_repo_key = self.id().to_string();
391 | group_repo_key.push_str("-repo");
392 |
393 | let key_bytes = protected_store
394 | .load_user_secret(group_repo_key)
395 | .map_err(|err| anyhow!("Unable to load repo from disk"))?
396 | .ok_or_else(|| anyhow!("No repo exists on disk for this group"))?;
397 |
398 | let mut id_bytes: [u8; CRYPTO_KEY_LENGTH] = [0; CRYPTO_KEY_LENGTH];
399 | id_bytes.copy_from_slice(&key_bytes);
400 | let repo_id = TypedKey::new(CRYPTO_KIND_VLD0, CryptoKey::from(id_bytes));
401 |
402 | let keypair = self.get_repo_keypair(repo_id).await?;
403 |
404 | let dht_record = self
405 | .routing_context
406 | .open_dht_record(
407 | repo_id.clone(),
408 | Some(KeyPair::new(
409 | keypair.public_key.clone(),
410 | keypair.secret_key.clone().unwrap(),
411 | )),
412 | )
413 | .await?;
414 |
415 | let secret_key = keypair
416 | .secret_key
417 | .map(|key| TypedKey::new(CRYPTO_KIND_VLD0, key));
418 |
419 | let repo = Repo::new(
420 | dht_record,
421 | self.encryption_key.clone(),
422 | secret_key,
423 | self.routing_context.clone(),
424 | self.veilid.clone(),
425 | self.iroh_blobs.clone(),
426 | );
427 | repo.update_route_on_dht().await?;
428 |
429 | self.add_repo(repo.clone()).await?;
430 |
431 | Ok(repo)
432 | }
433 |
434 | pub async fn create_repo(&mut self) -> Result {
435 | if self.get_own_repo().await.is_some() {
436 | return Err(anyhow!("Own repo already created for group"));
437 | }
438 |
439 | // Create a new DHT record for the repo
440 | let schema = DHTSchema::dflt(3)?;
441 | let kind = Some(CRYPTO_KIND_VLD0);
442 | let repo_dht_record = self
443 | .routing_context
444 | .create_dht_record(schema, None, kind)
445 | .await?;
446 |
447 | // Identify the repo with the DHT record's key
448 | let repo_id = repo_dht_record.key().clone();
449 |
450 | // Use the group's encryption key for the repo
451 | let encryption_key = self.get_encryption_key().clone();
452 |
453 | // Wrap the secret key in CryptoTyped for storage
454 | let secret_key_typed =
455 | CryptoTyped::new(CRYPTO_KIND_VLD0, self.get_secret_key().unwrap().clone());
456 |
457 | let repo = Repo::new(
458 | repo_dht_record.clone(),
459 | encryption_key,
460 | Some(secret_key_typed),
461 | self.routing_context.clone(),
462 | self.veilid.clone(),
463 | self.iroh_blobs.clone(),
464 | );
465 |
466 | // This should happen every time the route ID changes
467 | repo.update_route_on_dht().await?;
468 |
469 | let protected_store = self.veilid.protected_store().unwrap();
470 |
471 | let keypair = CommonKeypair {
472 | id: repo.id(),
473 | public_key: repo_dht_record.owner().clone(),
474 | secret_key: repo_dht_record.owner_secret().copied(),
475 | encryption_key: encryption_key.clone(),
476 | };
477 |
478 | keypair
479 | .store_keypair(&protected_store)
480 | .await
481 | .map_err(|e| anyhow!(e))?;
482 |
483 | let mut group_repo_key = self.id().to_string();
484 | group_repo_key.push_str("-repo");
485 | let key_bytes = *repo.id();
486 | protected_store
487 | .save_user_secret(group_repo_key, key_bytes.as_slice())
488 | .map_err(|e| anyhow!("Unable to store repo id for group: {}", e))?;
489 |
490 | self.add_repo(repo).await?;
491 |
492 | self.advertise_own_repo().await?;
493 |
494 | self.get_own_repo()
495 | .await
496 | .ok_or_else(|| anyhow!("Unexpected error, created repo not persisted"))
497 | }
498 |
499 | async fn get_repo_keypair(&self, repo_id: TypedKey) -> Result {
500 | let protected_store = self.veilid.protected_store()?;
501 |
502 | // Load keypair using the repo ID
503 | CommonKeypair::load_keypair(&protected_store, &repo_id.value)
504 | .await
505 | .map_err(|_| anyhow!("Failed to load keypair for repo_id: {:?}", repo_id))
506 | }
507 |
508 | pub async fn watch_changes(&self, on_change: F) -> Result<()>
509 | where
510 | F: Fn() -> Fut + Send + Sync + 'static,
511 | Fut: Future