├── .github
└── workflows
│ └── build.yaml
├── .gitignore
├── LICENSE
├── README.md
├── cmd
└── toxstatus
│ ├── cmd
│ ├── root.go
│ └── version.go
│ └── main.go
├── flake.lock
├── flake.nix
├── go.mod
├── go.sum
└── internal
├── crawler
├── crawler.go
└── util.go
├── db
├── db.go
├── embed.go
├── gen.go
├── models.go
├── open.go
├── queries.sql
├── queries.sql.go
├── schema.sql
├── sqlc.yml
└── types.go
├── models
└── models.go
├── repo
├── repo.go
└── repo_test.go
└── version
└── version.go
/.github/workflows/build.yaml:
--------------------------------------------------------------------------------
1 | name: build
2 | on: [pull_request, push]
3 | jobs:
4 | build:
5 | runs-on: ubuntu-latest
6 | steps:
7 | - uses: actions/checkout@v4
8 | - uses: DeterminateSystems/nix-installer-action@de22e16c4711fca50c816cc9081563429d1cf563
9 | with:
10 | diagnostic-endpoint:
11 | - uses: DeterminateSystems/magic-nix-cache-action@fc6aaceb40b9845a02b91e059ec147e78d1b4e41
12 | with:
13 | diagnostic-endpoint:
14 | - name: No diff
15 | run: |
16 | nix develop -c go mod tidy
17 | nix develop -c go generate ./...
18 | nix develop -c go fmt ./...
19 | git diff --exit-code
20 | - name: Test
21 | run: |
22 | nix develop -c go test -v ./...
23 | - name: Build
24 | run: |
25 | nix build --print-build-logs
26 | - name: Check version number
27 | if: startsWith(github.ref_name, 'v')
28 | run: |
29 | if ! ./result/bin/toxstatus version | grep -q '${{ github.ref_name }}'; then
30 | echo "Version information doesn't match"
31 | exit 1
32 | fi
33 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | result
2 | nodes.db*
3 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU AFFERO GENERAL PUBLIC LICENSE
2 | Version 3, 19 November 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 Affero General Public License is a free, copyleft license for
11 | software and other kinds of works, specifically designed to ensure
12 | cooperation with the community in the case of network server software.
13 |
14 | The licenses for most software and other practical works are designed
15 | to take away your freedom to share and change the works. By contrast,
16 | our General Public Licenses are intended to guarantee your freedom to
17 | share and change all versions of a program--to make sure it remains free
18 | software for all its users.
19 |
20 | When we speak of free software, we are referring to freedom, not
21 | price. Our General Public Licenses are designed to make sure that you
22 | have the freedom to distribute copies of free software (and charge for
23 | them if you wish), that you receive source code or can get it if you
24 | want it, that you can change the software or use pieces of it in new
25 | free programs, and that you know you can do these things.
26 |
27 | Developers that use our General Public Licenses protect your rights
28 | with two steps: (1) assert copyright on the software, and (2) offer
29 | you this License which gives you legal permission to copy, distribute
30 | and/or modify the software.
31 |
32 | A secondary benefit of defending all users' freedom is that
33 | improvements made in alternate versions of the program, if they
34 | receive widespread use, become available for other developers to
35 | incorporate. Many developers of free software are heartened and
36 | encouraged by the resulting cooperation. However, in the case of
37 | software used on network servers, this result may fail to come about.
38 | The GNU General Public License permits making a modified version and
39 | letting the public access it on a server without ever releasing its
40 | source code to the public.
41 |
42 | The GNU Affero General Public License is designed specifically to
43 | ensure that, in such cases, the modified source code becomes available
44 | to the community. It requires the operator of a network server to
45 | provide the source code of the modified version running there to the
46 | users of that server. Therefore, public use of a modified version, on
47 | a publicly accessible server, gives the public access to the source
48 | code of the modified version.
49 |
50 | An older license, called the Affero General Public License and
51 | published by Affero, was designed to accomplish similar goals. This is
52 | a different license, not a version of the Affero GPL, but Affero has
53 | released a new version of the Affero GPL which permits relicensing under
54 | this license.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | TERMS AND CONDITIONS
60 |
61 | 0. Definitions.
62 |
63 | "This License" refers to version 3 of the GNU Affero General Public License.
64 |
65 | "Copyright" also means copyright-like laws that apply to other kinds of
66 | works, such as semiconductor masks.
67 |
68 | "The Program" refers to any copyrightable work licensed under this
69 | License. Each licensee is addressed as "you". "Licensees" and
70 | "recipients" may be individuals or organizations.
71 |
72 | To "modify" a work means to copy from or adapt all or part of the work
73 | in a fashion requiring copyright permission, other than the making of an
74 | exact copy. The resulting work is called a "modified version" of the
75 | earlier work or a work "based on" the earlier work.
76 |
77 | A "covered work" means either the unmodified Program or a work based
78 | on the Program.
79 |
80 | To "propagate" a work means to do anything with it that, without
81 | permission, would make you directly or secondarily liable for
82 | infringement under applicable copyright law, except executing it on a
83 | computer or modifying a private copy. Propagation includes copying,
84 | distribution (with or without modification), making available to the
85 | public, and in some countries other activities as well.
86 |
87 | To "convey" a work means any kind of propagation that enables other
88 | parties to make or receive copies. Mere interaction with a user through
89 | a computer network, with no transfer of a copy, is not conveying.
90 |
91 | An interactive user interface displays "Appropriate Legal Notices"
92 | to the extent that it includes a convenient and prominently visible
93 | feature that (1) displays an appropriate copyright notice, and (2)
94 | tells the user that there is no warranty for the work (except to the
95 | extent that warranties are provided), that licensees may convey the
96 | work under this License, and how to view a copy of this License. If
97 | the interface presents a list of user commands or options, such as a
98 | menu, a prominent item in the list meets this criterion.
99 |
100 | 1. Source Code.
101 |
102 | The "source code" for a work means the preferred form of the work
103 | for making modifications to it. "Object code" means any non-source
104 | form of a work.
105 |
106 | A "Standard Interface" means an interface that either is an official
107 | standard defined by a recognized standards body, or, in the case of
108 | interfaces specified for a particular programming language, one that
109 | is widely used among developers working in that language.
110 |
111 | The "System Libraries" of an executable work include anything, other
112 | than the work as a whole, that (a) is included in the normal form of
113 | packaging a Major Component, but which is not part of that Major
114 | Component, and (b) serves only to enable use of the work with that
115 | Major Component, or to implement a Standard Interface for which an
116 | implementation is available to the public in source code form. A
117 | "Major Component", in this context, means a major essential component
118 | (kernel, window system, and so on) of the specific operating system
119 | (if any) on which the executable work runs, or a compiler used to
120 | produce the work, or an object code interpreter used to run it.
121 |
122 | The "Corresponding Source" for a work in object code form means all
123 | the source code needed to generate, install, and (for an executable
124 | work) run the object code and to modify the work, including scripts to
125 | control those activities. However, it does not include the work's
126 | System Libraries, or general-purpose tools or generally available free
127 | programs which are used unmodified in performing those activities but
128 | which are not part of the work. For example, Corresponding Source
129 | includes interface definition files associated with source files for
130 | the work, and the source code for shared libraries and dynamically
131 | linked subprograms that the work is specifically designed to require,
132 | such as by intimate data communication or control flow between those
133 | subprograms and other parts of the work.
134 |
135 | The Corresponding Source need not include anything that users
136 | can regenerate automatically from other parts of the Corresponding
137 | Source.
138 |
139 | The Corresponding Source for a work in source code form is that
140 | same work.
141 |
142 | 2. Basic Permissions.
143 |
144 | All rights granted under this License are granted for the term of
145 | copyright on the Program, and are irrevocable provided the stated
146 | conditions are met. This License explicitly affirms your unlimited
147 | permission to run the unmodified Program. The output from running a
148 | covered work is covered by this License only if the output, given its
149 | content, constitutes a covered work. This License acknowledges your
150 | rights of fair use or other equivalent, as provided by copyright law.
151 |
152 | You may make, run and propagate covered works that you do not
153 | convey, without conditions so long as your license otherwise remains
154 | in force. You may convey covered works to others for the sole purpose
155 | of having them make modifications exclusively for you, or provide you
156 | with facilities for running those works, provided that you comply with
157 | the terms of this License in conveying all material for which you do
158 | not control copyright. Those thus making or running the covered works
159 | for you must do so exclusively on your behalf, under your direction
160 | and control, on terms that prohibit them from making any copies of
161 | your copyrighted material outside their relationship with you.
162 |
163 | Conveying under any other circumstances is permitted solely under
164 | the conditions stated below. Sublicensing is not allowed; section 10
165 | makes it unnecessary.
166 |
167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
168 |
169 | No covered work shall be deemed part of an effective technological
170 | measure under any applicable law fulfilling obligations under article
171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
172 | similar laws prohibiting or restricting circumvention of such
173 | measures.
174 |
175 | When you convey a covered work, you waive any legal power to forbid
176 | circumvention of technological measures to the extent such circumvention
177 | is effected by exercising rights under this License with respect to
178 | the covered work, and you disclaim any intention to limit operation or
179 | modification of the work as a means of enforcing, against the work's
180 | users, your or third parties' legal rights to forbid circumvention of
181 | technological measures.
182 |
183 | 4. Conveying Verbatim Copies.
184 |
185 | You may convey verbatim copies of the Program's source code as you
186 | receive it, in any medium, provided that you conspicuously and
187 | appropriately publish on each copy an appropriate copyright notice;
188 | keep intact all notices stating that this License and any
189 | non-permissive terms added in accord with section 7 apply to the code;
190 | keep intact all notices of the absence of any warranty; and give all
191 | recipients a copy of this License along with the Program.
192 |
193 | You may charge any price or no price for each copy that you convey,
194 | and you may offer support or warranty protection for a fee.
195 |
196 | 5. Conveying Modified Source Versions.
197 |
198 | You may convey a work based on the Program, or the modifications to
199 | produce it from the Program, in the form of source code under the
200 | terms of section 4, provided that you also meet all of these conditions:
201 |
202 | a) The work must carry prominent notices stating that you modified
203 | it, and giving a relevant date.
204 |
205 | b) The work must carry prominent notices stating that it is
206 | released under this License and any conditions added under section
207 | 7. This requirement modifies the requirement in section 4 to
208 | "keep intact all notices".
209 |
210 | c) You must license the entire work, as a whole, under this
211 | License to anyone who comes into possession of a copy. This
212 | License will therefore apply, along with any applicable section 7
213 | additional terms, to the whole of the work, and all its parts,
214 | regardless of how they are packaged. This License gives no
215 | permission to license the work in any other way, but it does not
216 | invalidate such permission if you have separately received it.
217 |
218 | d) If the work has interactive user interfaces, each must display
219 | Appropriate Legal Notices; however, if the Program has interactive
220 | interfaces that do not display Appropriate Legal Notices, your
221 | work need not make them do so.
222 |
223 | A compilation of a covered work with other separate and independent
224 | works, which are not by their nature extensions of the covered work,
225 | and which are not combined with it such as to form a larger program,
226 | in or on a volume of a storage or distribution medium, is called an
227 | "aggregate" if the compilation and its resulting copyright are not
228 | used to limit the access or legal rights of the compilation's users
229 | beyond what the individual works permit. Inclusion of a covered work
230 | in an aggregate does not cause this License to apply to the other
231 | parts of the aggregate.
232 |
233 | 6. Conveying Non-Source Forms.
234 |
235 | You may convey a covered work in object code form under the terms
236 | of sections 4 and 5, provided that you also convey the
237 | machine-readable Corresponding Source under the terms of this License,
238 | in one of these ways:
239 |
240 | a) Convey the object code in, or embodied in, a physical product
241 | (including a physical distribution medium), accompanied by the
242 | Corresponding Source fixed on a durable physical medium
243 | customarily used for software interchange.
244 |
245 | b) Convey the object code in, or embodied in, a physical product
246 | (including a physical distribution medium), accompanied by a
247 | written offer, valid for at least three years and valid for as
248 | long as you offer spare parts or customer support for that product
249 | model, to give anyone who possesses the object code either (1) a
250 | copy of the Corresponding Source for all the software in the
251 | product that is covered by this License, on a durable physical
252 | medium customarily used for software interchange, for a price no
253 | more than your reasonable cost of physically performing this
254 | conveying of source, or (2) access to copy the
255 | Corresponding Source from a network server at no charge.
256 |
257 | c) Convey individual copies of the object code with a copy of the
258 | written offer to provide the Corresponding Source. This
259 | alternative is allowed only occasionally and noncommercially, and
260 | only if you received the object code with such an offer, in accord
261 | with subsection 6b.
262 |
263 | d) Convey the object code by offering access from a designated
264 | place (gratis or for a charge), and offer equivalent access to the
265 | Corresponding Source in the same way through the same place at no
266 | further charge. You need not require recipients to copy the
267 | Corresponding Source along with the object code. If the place to
268 | copy the object code is a network server, the Corresponding Source
269 | may be on a different server (operated by you or a third party)
270 | that supports equivalent copying facilities, provided you maintain
271 | clear directions next to the object code saying where to find the
272 | Corresponding Source. Regardless of what server hosts the
273 | Corresponding Source, you remain obligated to ensure that it is
274 | available for as long as needed to satisfy these requirements.
275 |
276 | e) Convey the object code using peer-to-peer transmission, provided
277 | you inform other peers where the object code and Corresponding
278 | Source of the work are being offered to the general public at no
279 | charge under subsection 6d.
280 |
281 | A separable portion of the object code, whose source code is excluded
282 | from the Corresponding Source as a System Library, need not be
283 | included in conveying the object code work.
284 |
285 | A "User Product" is either (1) a "consumer product", which means any
286 | tangible personal property which is normally used for personal, family,
287 | or household purposes, or (2) anything designed or sold for incorporation
288 | into a dwelling. In determining whether a product is a consumer product,
289 | doubtful cases shall be resolved in favor of coverage. For a particular
290 | product received by a particular user, "normally used" refers to a
291 | typical or common use of that class of product, regardless of the status
292 | of the particular user or of the way in which the particular user
293 | actually uses, or expects or is expected to use, the product. A product
294 | is a consumer product regardless of whether the product has substantial
295 | commercial, industrial or non-consumer uses, unless such uses represent
296 | the only significant mode of use of the product.
297 |
298 | "Installation Information" for a User Product means any methods,
299 | procedures, authorization keys, or other information required to install
300 | and execute modified versions of a covered work in that User Product from
301 | a modified version of its Corresponding Source. The information must
302 | suffice to ensure that the continued functioning of the modified object
303 | code is in no case prevented or interfered with solely because
304 | modification has been made.
305 |
306 | If you convey an object code work under this section in, or with, or
307 | specifically for use in, a User Product, and the conveying occurs as
308 | part of a transaction in which the right of possession and use of the
309 | User Product is transferred to the recipient in perpetuity or for a
310 | fixed term (regardless of how the transaction is characterized), the
311 | Corresponding Source conveyed under this section must be accompanied
312 | by the Installation Information. But this requirement does not apply
313 | if neither you nor any third party retains the ability to install
314 | modified object code on the User Product (for example, the work has
315 | been installed in ROM).
316 |
317 | The requirement to provide Installation Information does not include a
318 | requirement to continue to provide support service, warranty, or updates
319 | for a work that has been modified or installed by the recipient, or for
320 | the User Product in which it has been modified or installed. Access to a
321 | network may be denied when the modification itself materially and
322 | adversely affects the operation of the network or violates the rules and
323 | protocols for communication across the network.
324 |
325 | Corresponding Source conveyed, and Installation Information provided,
326 | in accord with this section must be in a format that is publicly
327 | documented (and with an implementation available to the public in
328 | source code form), and must require no special password or key for
329 | unpacking, reading or copying.
330 |
331 | 7. Additional Terms.
332 |
333 | "Additional permissions" are terms that supplement the terms of this
334 | License by making exceptions from one or more of its conditions.
335 | Additional permissions that are applicable to the entire Program shall
336 | be treated as though they were included in this License, to the extent
337 | that they are valid under applicable law. If additional permissions
338 | apply only to part of the Program, that part may be used separately
339 | under those permissions, but the entire Program remains governed by
340 | this License without regard to the additional permissions.
341 |
342 | When you convey a copy of a covered work, you may at your option
343 | remove any additional permissions from that copy, or from any part of
344 | it. (Additional permissions may be written to require their own
345 | removal in certain cases when you modify the work.) You may place
346 | additional permissions on material, added by you to a covered work,
347 | for which you have or can give appropriate copyright permission.
348 |
349 | Notwithstanding any other provision of this License, for material you
350 | add to a covered work, you may (if authorized by the copyright holders of
351 | that material) supplement the terms of this License with terms:
352 |
353 | a) Disclaiming warranty or limiting liability differently from the
354 | terms of sections 15 and 16 of this License; or
355 |
356 | b) Requiring preservation of specified reasonable legal notices or
357 | author attributions in that material or in the Appropriate Legal
358 | Notices displayed by works containing it; or
359 |
360 | c) Prohibiting misrepresentation of the origin of that material, or
361 | requiring that modified versions of such material be marked in
362 | reasonable ways as different from the original version; or
363 |
364 | d) Limiting the use for publicity purposes of names of licensors or
365 | authors of the material; or
366 |
367 | e) Declining to grant rights under trademark law for use of some
368 | trade names, trademarks, or service marks; or
369 |
370 | f) Requiring indemnification of licensors and authors of that
371 | material by anyone who conveys the material (or modified versions of
372 | it) with contractual assumptions of liability to the recipient, for
373 | any liability that these contractual assumptions directly impose on
374 | those licensors and authors.
375 |
376 | All other non-permissive additional terms are considered "further
377 | restrictions" within the meaning of section 10. If the Program as you
378 | received it, or any part of it, contains a notice stating that it is
379 | governed by this License along with a term that is a further
380 | restriction, you may remove that term. If a license document contains
381 | a further restriction but permits relicensing or conveying under this
382 | License, you may add to a covered work material governed by the terms
383 | of that license document, provided that the further restriction does
384 | not survive such relicensing or conveying.
385 |
386 | If you add terms to a covered work in accord with this section, you
387 | must place, in the relevant source files, a statement of the
388 | additional terms that apply to those files, or a notice indicating
389 | where to find the applicable terms.
390 |
391 | Additional terms, permissive or non-permissive, may be stated in the
392 | form of a separately written license, or stated as exceptions;
393 | the above requirements apply either way.
394 |
395 | 8. Termination.
396 |
397 | You may not propagate or modify a covered work except as expressly
398 | provided under this License. Any attempt otherwise to propagate or
399 | modify it is void, and will automatically terminate your rights under
400 | this License (including any patent licenses granted under the third
401 | paragraph of section 11).
402 |
403 | However, if you cease all violation of this License, then your
404 | license from a particular copyright holder is reinstated (a)
405 | provisionally, unless and until the copyright holder explicitly and
406 | finally terminates your license, and (b) permanently, if the copyright
407 | holder fails to notify you of the violation by some reasonable means
408 | prior to 60 days after the cessation.
409 |
410 | Moreover, your license from a particular copyright holder is
411 | reinstated permanently if the copyright holder notifies you of the
412 | violation by some reasonable means, this is the first time you have
413 | received notice of violation of this License (for any work) from that
414 | copyright holder, and you cure the violation prior to 30 days after
415 | your receipt of the notice.
416 |
417 | Termination of your rights under this section does not terminate the
418 | licenses of parties who have received copies or rights from you under
419 | this License. If your rights have been terminated and not permanently
420 | reinstated, you do not qualify to receive new licenses for the same
421 | material under section 10.
422 |
423 | 9. Acceptance Not Required for Having Copies.
424 |
425 | You are not required to accept this License in order to receive or
426 | run a copy of the Program. Ancillary propagation of a covered work
427 | occurring solely as a consequence of using peer-to-peer transmission
428 | to receive a copy likewise does not require acceptance. However,
429 | nothing other than this License grants you permission to propagate or
430 | modify any covered work. These actions infringe copyright if you do
431 | not accept this License. Therefore, by modifying or propagating a
432 | covered work, you indicate your acceptance of this License to do so.
433 |
434 | 10. Automatic Licensing of Downstream Recipients.
435 |
436 | Each time you convey a covered work, the recipient automatically
437 | receives a license from the original licensors, to run, modify and
438 | propagate that work, subject to this License. You are not responsible
439 | for enforcing compliance by third parties with this License.
440 |
441 | An "entity transaction" is a transaction transferring control of an
442 | organization, or substantially all assets of one, or subdividing an
443 | organization, or merging organizations. If propagation of a covered
444 | work results from an entity transaction, each party to that
445 | transaction who receives a copy of the work also receives whatever
446 | licenses to the work the party's predecessor in interest had or could
447 | give under the previous paragraph, plus a right to possession of the
448 | Corresponding Source of the work from the predecessor in interest, if
449 | the predecessor has it or can get it with reasonable efforts.
450 |
451 | You may not impose any further restrictions on the exercise of the
452 | rights granted or affirmed under this License. For example, you may
453 | not impose a license fee, royalty, or other charge for exercise of
454 | rights granted under this License, and you may not initiate litigation
455 | (including a cross-claim or counterclaim in a lawsuit) alleging that
456 | any patent claim is infringed by making, using, selling, offering for
457 | sale, or importing the Program or any portion of it.
458 |
459 | 11. Patents.
460 |
461 | A "contributor" is a copyright holder who authorizes use under this
462 | License of the Program or a work on which the Program is based. The
463 | work thus licensed is called the contributor's "contributor version".
464 |
465 | A contributor's "essential patent claims" are all patent claims
466 | owned or controlled by the contributor, whether already acquired or
467 | hereafter acquired, that would be infringed by some manner, permitted
468 | by this License, of making, using, or selling its contributor version,
469 | but do not include claims that would be infringed only as a
470 | consequence of further modification of the contributor version. For
471 | purposes of this definition, "control" includes the right to grant
472 | patent sublicenses in a manner consistent with the requirements of
473 | this License.
474 |
475 | Each contributor grants you a non-exclusive, worldwide, royalty-free
476 | patent license under the contributor's essential patent claims, to
477 | make, use, sell, offer for sale, import and otherwise run, modify and
478 | propagate the contents of its contributor version.
479 |
480 | In the following three paragraphs, a "patent license" is any express
481 | agreement or commitment, however denominated, not to enforce a patent
482 | (such as an express permission to practice a patent or covenant not to
483 | sue for patent infringement). To "grant" such a patent license to a
484 | party means to make such an agreement or commitment not to enforce a
485 | patent against the party.
486 |
487 | If you convey a covered work, knowingly relying on a patent license,
488 | and the Corresponding Source of the work is not available for anyone
489 | to copy, free of charge and under the terms of this License, through a
490 | publicly available network server or other readily accessible means,
491 | then you must either (1) cause the Corresponding Source to be so
492 | available, or (2) arrange to deprive yourself of the benefit of the
493 | patent license for this particular work, or (3) arrange, in a manner
494 | consistent with the requirements of this License, to extend the patent
495 | license to downstream recipients. "Knowingly relying" means you have
496 | actual knowledge that, but for the patent license, your conveying the
497 | covered work in a country, or your recipient's use of the covered work
498 | in a country, would infringe one or more identifiable patents in that
499 | country that you have reason to believe are valid.
500 |
501 | If, pursuant to or in connection with a single transaction or
502 | arrangement, you convey, or propagate by procuring conveyance of, a
503 | covered work, and grant a patent license to some of the parties
504 | receiving the covered work authorizing them to use, propagate, modify
505 | or convey a specific copy of the covered work, then the patent license
506 | you grant is automatically extended to all recipients of the covered
507 | work and works based on it.
508 |
509 | A patent license is "discriminatory" if it does not include within
510 | the scope of its coverage, prohibits the exercise of, or is
511 | conditioned on the non-exercise of one or more of the rights that are
512 | specifically granted under this License. You may not convey a covered
513 | work if you are a party to an arrangement with a third party that is
514 | in the business of distributing software, under which you make payment
515 | to the third party based on the extent of your activity of conveying
516 | the work, and under which the third party grants, to any of the
517 | parties who would receive the covered work from you, a discriminatory
518 | patent license (a) in connection with copies of the covered work
519 | conveyed by you (or copies made from those copies), or (b) primarily
520 | for and in connection with specific products or compilations that
521 | contain the covered work, unless you entered into that arrangement,
522 | or that patent license was granted, prior to 28 March 2007.
523 |
524 | Nothing in this License shall be construed as excluding or limiting
525 | any implied license or other defenses to infringement that may
526 | otherwise be available to you under applicable patent law.
527 |
528 | 12. No Surrender of Others' Freedom.
529 |
530 | If conditions are imposed on you (whether by court order, agreement or
531 | otherwise) that contradict the conditions of this License, they do not
532 | excuse you from the conditions of this License. If you cannot convey a
533 | covered work so as to satisfy simultaneously your obligations under this
534 | License and any other pertinent obligations, then as a consequence you may
535 | not convey it at all. For example, if you agree to terms that obligate you
536 | to collect a royalty for further conveying from those to whom you convey
537 | the Program, the only way you could satisfy both those terms and this
538 | License would be to refrain entirely from conveying the Program.
539 |
540 | 13. Remote Network Interaction; Use with the GNU General Public License.
541 |
542 | Notwithstanding any other provision of this License, if you modify the
543 | Program, your modified version must prominently offer all users
544 | interacting with it remotely through a computer network (if your version
545 | supports such interaction) an opportunity to receive the Corresponding
546 | Source of your version by providing access to the Corresponding Source
547 | from a network server at no charge, through some standard or customary
548 | means of facilitating copying of software. This Corresponding Source
549 | shall include the Corresponding Source for any work covered by version 3
550 | of the GNU General Public License that is incorporated pursuant to the
551 | following paragraph.
552 |
553 | Notwithstanding any other provision of this License, you have
554 | permission to link or combine any covered work with a work licensed
555 | under version 3 of the GNU General Public License into a single
556 | combined work, and to convey the resulting work. The terms of this
557 | License will continue to apply to the part which is the covered work,
558 | but the work with which it is combined will remain governed by version
559 | 3 of the GNU General Public License.
560 |
561 | 14. Revised Versions of this License.
562 |
563 | The Free Software Foundation may publish revised and/or new versions of
564 | the GNU Affero General Public License from time to time. Such new versions
565 | will be similar in spirit to the present version, but may differ in detail to
566 | address new problems or concerns.
567 |
568 | Each version is given a distinguishing version number. If the
569 | Program specifies that a certain numbered version of the GNU Affero General
570 | Public License "or any later version" applies to it, you have the
571 | option of following the terms and conditions either of that numbered
572 | version or of any later version published by the Free Software
573 | Foundation. If the Program does not specify a version number of the
574 | GNU Affero General Public License, you may choose any version ever published
575 | by the Free Software Foundation.
576 |
577 | If the Program specifies that a proxy can decide which future
578 | versions of the GNU Affero General Public License can be used, that proxy's
579 | public statement of acceptance of a version permanently authorizes you
580 | to choose that version for the Program.
581 |
582 | Later license versions may give you additional or different
583 | permissions. However, no additional obligations are imposed on any
584 | author or copyright holder as a result of your choosing to follow a
585 | later version.
586 |
587 | 15. Disclaimer of Warranty.
588 |
589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
597 |
598 | 16. Limitation of Liability.
599 |
600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
608 | SUCH DAMAGES.
609 |
610 | 17. Interpretation of Sections 15 and 16.
611 |
612 | If the disclaimer of warranty and limitation of liability provided
613 | above cannot be given local legal effect according to their terms,
614 | reviewing courts shall apply local law that most closely approximates
615 | an absolute waiver of all civil liability in connection with the
616 | Program, unless a warranty or assumption of liability accompanies a
617 | copy of the Program in return for a fee.
618 |
619 | END OF TERMS AND CONDITIONS
620 |
621 | How to Apply These Terms to Your New Programs
622 |
623 | If you develop a new program, and you want it to be of the greatest
624 | possible use to the public, the best way to achieve this is to make it
625 | free software which everyone can redistribute and change under these terms.
626 |
627 | To do so, attach the following notices to the program. It is safest
628 | to attach them to the start of each source file to most effectively
629 | state the exclusion of warranty; and each file should have at least
630 | the "copyright" line and a pointer to where the full notice is found.
631 |
632 |
633 | Copyright (C)
634 |
635 | This program is free software: you can redistribute it and/or modify
636 | it under the terms of the GNU Affero General Public License as published by
637 | the Free Software Foundation, either version 3 of the License, or
638 | (at your option) any later version.
639 |
640 | This program is distributed in the hope that it will be useful,
641 | but WITHOUT ANY WARRANTY; without even the implied warranty of
642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
643 | GNU Affero General Public License for more details.
644 |
645 | You should have received a copy of the GNU Affero General Public License
646 | along with this program. If not, see .
647 |
648 | Also add information on how to contact you by electronic and paper mail.
649 |
650 | If your software can interact with users remotely through a computer
651 | network, you should also make sure that it provides a way for users to
652 | get its source. For example, if your program is a web application, its
653 | interface could display a "Source" link that leads users to an archive
654 | of the code. There are many ways you could offer source, and different
655 | solutions will be better for different programs; see section 13 for the
656 | specific requirements.
657 |
658 | You should also get your employer (if you work as a programmer) or school,
659 | if any, to sign a "copyright disclaimer" for the program, if necessary.
660 | For more information on this, and how to apply and follow the GNU AGPL, see
661 | .
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ToxStatus 
2 |
3 | Status page for the [Tox](https://tox.chat/) network that keeps track of
4 | bootstrap nodes.
5 |
6 | The master branch is currently in a __WIP__ state. The latest stable version is
7 | [v1.0.0](https://github.com/Tox/ToxStatus/releases/tag/v1.0.0).
8 |
9 | ## Screenshots
10 |
11 | 
12 |
13 | 
14 |
15 | ## Tool
16 |
17 | Besides being a full status page, ToxStatus can also be used as a command line
18 | tool to quickly check the status of a node.
19 |
20 | ```none
21 | ~> ./ToxStatus --help
22 | Usage of ./ToxStatus:
23 | -ip string
24 | ip address to probe, ipv4 and ipv6 are both supported (default "127.0.0.1")
25 | -key string
26 | public key of the node
27 | -net string
28 | network type, either 'udp' or 'tcp' (default "udp")
29 | -port int
30 | port to probe (default 33445)
31 | ```
32 |
--------------------------------------------------------------------------------
/cmd/toxstatus/cmd/root.go:
--------------------------------------------------------------------------------
1 | package cmd
2 |
3 | import (
4 | "context"
5 | "errors"
6 | "fmt"
7 | "log/slog"
8 | "net"
9 | "net/http"
10 | "net/http/pprof"
11 | "os"
12 | "os/signal"
13 | "runtime"
14 | "sync"
15 | "syscall"
16 | "time"
17 |
18 | "github.com/Tox/ToxStatus/internal/crawler"
19 | "github.com/Tox/ToxStatus/internal/db"
20 | "github.com/Tox/ToxStatus/internal/repo"
21 | "github.com/alexbakker/tox4go/toxstatus"
22 | "github.com/lmittmann/tint"
23 | "github.com/mattn/go-isatty"
24 | _ "github.com/mattn/go-sqlite3"
25 | "github.com/spf13/cobra"
26 | )
27 |
28 | var (
29 | Root = &cobra.Command{
30 | Use: "toxstatus",
31 | Short: "Status page for the Tox network that keeps track of bootstrap nodes",
32 | Run: startRoot,
33 | }
34 | rootFlags = struct {
35 | HTTPAddr string
36 | HTTPClientTimeout time.Duration
37 | PprofAddr string
38 | ToxUDPAddr string
39 | DB string
40 | DBCacheSize int
41 | LogLevel string
42 | Workers int
43 | }{}
44 | )
45 |
46 | func init() {
47 | const maxDefaultWorkers = 8
48 | Root.Flags().StringVar(&rootFlags.HTTPAddr, "http-addr", ":8003", "the network address to listen on for the HTTP server")
49 | Root.Flags().DurationVar(&rootFlags.HTTPClientTimeout, "http-client-timeout", 10*time.Second, "the http client timeout for requests to nodes.tox.chat")
50 | Root.Flags().StringVar(&rootFlags.PprofAddr, "pprof-addr", "", "the network address to listen of for the pprof HTTP server")
51 | Root.Flags().StringVar(&rootFlags.ToxUDPAddr, "tox-udp-addr", ":33450", "the UDP network address to listen on for Tox")
52 | Root.Flags().StringVar(&rootFlags.DB, "db", "", "the sqlite database file to use")
53 | Root.Flags().IntVar(&rootFlags.DBCacheSize, "db-cache-size", 100000, "the sqlite cache size to use (in KB)")
54 | Root.Flags().StringVar(&rootFlags.LogLevel, "log-level", "info", "the log level to use")
55 | Root.Flags().IntVar(&rootFlags.Workers, "workers", min(maxDefaultWorkers, runtime.NumCPU()), "the amount of workers to use")
56 | Root.MarkFlagRequired("db")
57 | }
58 |
59 | func startRoot(cmd *cobra.Command, args []string) {
60 | ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
61 | defer cancel()
62 |
63 | var level slog.Level
64 | if err := level.UnmarshalText([]byte(rootFlags.LogLevel)); err != nil {
65 | fmt.Fprintf(os.Stderr, "bad log level: %s\n", rootFlags.LogLevel)
66 | os.Exit(1)
67 | return
68 | }
69 |
70 | logger := slog.New(tint.NewHandler(os.Stderr, &tint.Options{
71 | Level: level,
72 | NoColor: !isatty.IsTerminal(os.Stderr.Fd()),
73 | }))
74 |
75 | db.RegisterPragmaHook(rootFlags.DBCacheSize)
76 | readConn, writeConn, err := db.OpenReadWrite(ctx, rootFlags.DB, db.OpenOptions{})
77 | if err != nil {
78 | logErrorAndExit(logger, "Unable to open db", slog.Any("err", err))
79 | }
80 | defer func() {
81 | readConn.Close()
82 | writeConn.Close()
83 | }()
84 |
85 | if rootFlags.PprofAddr != "" {
86 | logger.Info("Starting pprof server")
87 |
88 | l, err := net.Listen("tcp", rootFlags.PprofAddr)
89 | if err != nil {
90 | logErrorAndExit(logger, "Unable to start pprof server", slog.Any("err", err))
91 | return
92 | }
93 |
94 | go func() {
95 | mux := http.NewServeMux()
96 | mux.HandleFunc("/debug/pprof/", pprof.Index)
97 | mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
98 | mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
99 | mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
100 | mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
101 |
102 | if err := http.Serve(l, mux); err != nil && !errors.Is(err, http.ErrServerClosed) {
103 | logger.Error("Unable to run pprof server", slog.Any("err", err))
104 | }
105 | }()
106 | }
107 |
108 | nodesRepo := repo.New(readConn, writeConn)
109 | cr, err := crawler.New(nodesRepo, crawler.CrawlerOptions{
110 | Logger: logger,
111 | HTTPAddr: rootFlags.HTTPAddr,
112 | ToxUDPAddr: rootFlags.ToxUDPAddr,
113 | Workers: rootFlags.Workers,
114 | })
115 | if err != nil {
116 | logErrorAndExit(logger, "Unable to initialize Tox crawler", slog.Any("err", err))
117 | return
118 | }
119 |
120 | logger.Info("Querying nodes.tox.chat for bootstrap nodes")
121 |
122 | // Kick off by bootstrapping from nodes in the nodes.tox.chat list
123 | tsClient := toxstatus.Client{HTTPClient: &http.Client{Timeout: rootFlags.HTTPClientTimeout}}
124 | bsNodes, err := tsClient.GetNodes(ctx)
125 | if err != nil {
126 | logErrorAndExit(logger, "Unable to fetch nodes from", slog.Any("err", err))
127 | return
128 | }
129 |
130 | for _, node := range bsNodes {
131 | logger.Debug("Found bootstrap node",
132 | slog.String("public_key", node.PublicKey.String()),
133 | slog.String("net", node.Type.Net()),
134 | slog.String("addr", node.Addr().String()))
135 | }
136 |
137 | var wg sync.WaitGroup
138 | wg.Add(1)
139 | go func() {
140 | defer wg.Done()
141 |
142 | logger.Info("Starting Tox crawler")
143 |
144 | if err := cr.Run(ctx, bsNodes); err != nil && !errors.Is(err, context.Canceled) {
145 | logErrorAndExit(logger, "Unable to run Tox crawler", slog.Any("err", err))
146 | }
147 | }()
148 |
149 | <-ctx.Done()
150 | logger.Info("Stopping Tox crawler")
151 | wg.Wait()
152 |
153 | logger.Info("Bye!")
154 | }
155 |
156 | func logErrorAndExit(logger *slog.Logger, msg string, args ...any) {
157 | logger.Error(msg, args...)
158 | os.Exit(1)
159 | }
160 |
--------------------------------------------------------------------------------
/cmd/toxstatus/cmd/version.go:
--------------------------------------------------------------------------------
1 | package cmd
2 |
3 | import (
4 | "fmt"
5 | "os"
6 |
7 | "github.com/Tox/ToxStatus/internal/version"
8 | sqlite3 "github.com/mattn/go-sqlite3"
9 | "github.com/spf13/cobra"
10 | )
11 |
12 | var (
13 | versionCmd = &cobra.Command{
14 | Use: "version",
15 | Short: "Version information",
16 | Run: startVersion,
17 | }
18 | )
19 |
20 | func init() {
21 | Root.AddCommand(versionCmd)
22 | }
23 |
24 | func startVersion(cmd *cobra.Command, args []string) {
25 | vs, err := version.String()
26 | if err != nil {
27 | exitWithError(err.Error())
28 | return
29 | }
30 |
31 | sqliteVersion, _, _ := sqlite3.Version()
32 |
33 | fmt.Print(vs)
34 | if ts := version.HumanRevisionTime(); ts != "" {
35 | fmt.Printf(" (%s)", ts)
36 | }
37 | fmt.Println()
38 | fmt.Printf("sqlite: %s \n", sqliteVersion)
39 | fmt.Println("https://github.com/Tox/ToxStatus (AGPLv3)")
40 | }
41 |
42 | func exitWithError(s string) {
43 | fmt.Fprintf(os.Stderr, "error: %s\n", s)
44 | os.Exit(1)
45 | }
46 |
--------------------------------------------------------------------------------
/cmd/toxstatus/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "os"
5 |
6 | "github.com/Tox/ToxStatus/cmd/toxstatus/cmd"
7 | )
8 |
9 | func main() {
10 | if err := cmd.Root.Execute(); err != nil {
11 | os.Exit(1)
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/flake.lock:
--------------------------------------------------------------------------------
1 | {
2 | "nodes": {
3 | "flake-utils": {
4 | "inputs": {
5 | "systems": "systems"
6 | },
7 | "locked": {
8 | "lastModified": 1710146030,
9 | "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=",
10 | "owner": "numtide",
11 | "repo": "flake-utils",
12 | "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a",
13 | "type": "github"
14 | },
15 | "original": {
16 | "owner": "numtide",
17 | "repo": "flake-utils",
18 | "type": "github"
19 | }
20 | },
21 | "nixpkgs": {
22 | "locked": {
23 | "lastModified": 1711703276,
24 | "narHash": "sha256-iMUFArF0WCatKK6RzfUJknjem0H9m4KgorO/p3Dopkk=",
25 | "owner": "NixOS",
26 | "repo": "nixpkgs",
27 | "rev": "d8fe5e6c92d0d190646fb9f1056741a229980089",
28 | "type": "github"
29 | },
30 | "original": {
31 | "id": "nixpkgs",
32 | "ref": "nixos-unstable",
33 | "type": "indirect"
34 | }
35 | },
36 | "root": {
37 | "inputs": {
38 | "flake-utils": "flake-utils",
39 | "nixpkgs": "nixpkgs"
40 | }
41 | },
42 | "systems": {
43 | "locked": {
44 | "lastModified": 1681028828,
45 | "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
46 | "owner": "nix-systems",
47 | "repo": "default",
48 | "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
49 | "type": "github"
50 | },
51 | "original": {
52 | "owner": "nix-systems",
53 | "repo": "default",
54 | "type": "github"
55 | }
56 | }
57 | },
58 | "root": "root",
59 | "version": 7
60 | }
61 |
--------------------------------------------------------------------------------
/flake.nix:
--------------------------------------------------------------------------------
1 | {
2 | description = "Nix flake for ToxStatus";
3 | inputs.nixpkgs.url = "nixpkgs/nixos-unstable";
4 | inputs.flake-utils.url = "github:numtide/flake-utils";
5 |
6 | outputs = { self, nixpkgs, flake-utils }:
7 | flake-utils.lib.eachDefaultSystem (system: let
8 | pkgs = nixpkgs.legacyPackages.${system};
9 | toxStatusVersion = "2.0.0-dev1";
10 | in {
11 | packages = flake-utils.lib.flattenTree rec {
12 | default = toxstatus;
13 | toxstatus = with pkgs; buildGoModule rec {
14 | pname = "toxstatus";
15 | version = toxStatusVersion;
16 | src = ./.;
17 |
18 | subPackages = [ "cmd/toxstatus" ];
19 | vendorHash = "sha256-kD00o5RpT+qq9QJAUR5NLgwMQn6IofkdpWk2mbfBa2g=";
20 |
21 | ldflags = let
22 | pkgPath = "github.com/Tox/ToxStatus/internal/version";
23 | in [
24 | "-X ${pkgPath}.Number=${version}"
25 | "-X ${pkgPath}.Revision=${self.shortRev or "dirty"}"
26 | "-X ${pkgPath}.RevisionTime=${toString self.lastModified}"
27 | ];
28 |
29 | doCheck = false;
30 | };
31 | };
32 | devShell = with pkgs; mkShell {
33 | hardeningDisable = [ "fortify" ];
34 | buildInputs = [
35 | go
36 | graphviz # for pprof
37 | sqlite
38 | ];
39 | };
40 | }
41 | );
42 | }
43 |
--------------------------------------------------------------------------------
/go.mod:
--------------------------------------------------------------------------------
1 | module github.com/Tox/ToxStatus
2 |
3 | go 1.21
4 |
5 | toolchain go1.21.5
6 |
7 | require (
8 | github.com/alexbakker/tox4go v0.0.0-20240316121347-9721c625a54e
9 | github.com/lmittmann/tint v1.0.4
10 | github.com/mattn/go-isatty v0.0.20
11 | github.com/mattn/go-sqlite3 v1.14.22
12 | github.com/spf13/cobra v1.8.0
13 | github.com/sqlc-dev/sqlc v1.26.0
14 | golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8
15 | )
16 |
17 | require (
18 | filippo.io/edwards25519 v1.1.0 // indirect
19 | github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
20 | github.com/cubicdaiya/gonp v1.0.4 // indirect
21 | github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect
22 | github.com/davecgh/go-spew v1.1.1 // indirect
23 | github.com/dustin/go-humanize v1.0.1 // indirect
24 | github.com/fatih/structtag v1.2.0 // indirect
25 | github.com/go-sql-driver/mysql v1.8.1 // indirect
26 | github.com/golang/protobuf v1.5.4 // indirect
27 | github.com/google/cel-go v0.20.1 // indirect
28 | github.com/google/uuid v1.6.0 // indirect
29 | github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
30 | github.com/inconshreveable/mousetrap v1.1.0 // indirect
31 | github.com/jackc/pgpassfile v1.0.0 // indirect
32 | github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
33 | github.com/jackc/pgx/v5 v5.5.5 // indirect
34 | github.com/jackc/puddle/v2 v2.2.1 // indirect
35 | github.com/jinzhu/inflection v1.0.0 // indirect
36 | github.com/ncruces/go-strftime v0.1.9 // indirect
37 | github.com/pganalyze/pg_query_go/v5 v5.1.0 // indirect
38 | github.com/pingcap/errors v0.11.5-0.20240311024730-e056997136bb // indirect
39 | github.com/pingcap/failpoint v0.0.0-20220801062533-2eaa32854a6c // indirect
40 | github.com/pingcap/log v1.1.0 // indirect
41 | github.com/pingcap/tidb/pkg/parser v0.0.0-20240401090316-c9a250a80fbc // indirect
42 | github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
43 | github.com/riza-io/grpc-go v0.2.0 // indirect
44 | github.com/spf13/pflag v1.0.5 // indirect
45 | github.com/stoewer/go-strcase v1.3.0 // indirect
46 | github.com/tetratelabs/wazero v1.7.0 // indirect
47 | github.com/wasilibs/go-pgquery v0.0.0-20240319230125-b9b2e95c69a7 // indirect
48 | go.uber.org/atomic v1.11.0 // indirect
49 | go.uber.org/multierr v1.11.0 // indirect
50 | go.uber.org/zap v1.27.0 // indirect
51 | golang.org/x/crypto v0.21.0 // indirect
52 | golang.org/x/net v0.22.0 // indirect
53 | golang.org/x/sync v0.6.0 // indirect
54 | golang.org/x/sys v0.18.0 // indirect
55 | golang.org/x/text v0.14.0 // indirect
56 | google.golang.org/genproto/googleapis/api v0.0.0-20240325203815-454cdb8f5daa // indirect
57 | google.golang.org/genproto/googleapis/rpc v0.0.0-20240325203815-454cdb8f5daa // indirect
58 | google.golang.org/grpc v1.62.1 // indirect
59 | google.golang.org/protobuf v1.33.0 // indirect
60 | gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
61 | gopkg.in/yaml.v3 v3.0.1 // indirect
62 | modernc.org/gc/v3 v3.0.0-20240304020402-f0dba7c97c2b // indirect
63 | modernc.org/libc v1.49.0 // indirect
64 | modernc.org/mathutil v1.6.0 // indirect
65 | modernc.org/memory v1.7.2 // indirect
66 | modernc.org/sqlite v1.29.5 // indirect
67 | modernc.org/strutil v1.2.0 // indirect
68 | modernc.org/token v1.1.0 // indirect
69 | )
70 |
71 | replace github.com/lmittmann/tint => github.com/alexbakker/tint v0.0.0-20240104110959-ed2aff4c49ad
72 |
--------------------------------------------------------------------------------
/go.sum:
--------------------------------------------------------------------------------
1 | filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
2 | filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
3 | github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
4 | github.com/alexbakker/tint v0.0.0-20240104110959-ed2aff4c49ad h1:LBMnYWlEI5NKxOAsfmSiZdZKppav5x9GmEWpAVKPhHU=
5 | github.com/alexbakker/tint v0.0.0-20240104110959-ed2aff4c49ad/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
6 | github.com/alexbakker/tox4go v0.0.0-20240316121347-9721c625a54e h1:u9zJSvlV1fRvnZN5su8/cvWTapNBnwhdA0UMSdAj7DE=
7 | github.com/alexbakker/tox4go v0.0.0-20240316121347-9721c625a54e/go.mod h1:MFdCNLiNjzme/UOeYncukPA9atsPU9qTK+TuqE4Eyq4=
8 | github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI=
9 | github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g=
10 | github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
11 | github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
12 | github.com/cubicdaiya/gonp v1.0.4 h1:ky2uIAJh81WiLcGKBVD5R7KsM/36W6IqqTy6Bo6rGws=
13 | github.com/cubicdaiya/gonp v1.0.4/go.mod h1:iWGuP/7+JVTn02OWhRemVbMmG1DOUnmrGTYYACpOI0I=
14 | github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 h1:iwZdTE0PVqJCos1vaoKsclOGD3ADKpshg3SRtYBbwso=
15 | github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548/go.mod h1:e6NPNENfs9mPDVNRekM7lKScauxd5kXTr1Mfyig6TDM=
16 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
17 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
18 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
19 | github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
20 | github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
21 | github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
22 | github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
23 | github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
24 | github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
25 | github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
26 | github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
27 | github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
28 | github.com/google/cel-go v0.20.1 h1:nDx9r8S3L4pE61eDdt8igGj8rf5kjYR3ILxWIpWNi84=
29 | github.com/google/cel-go v0.20.1/go.mod h1:kWcIzTsPX0zmQ+H3TirHstLLf9ep5QTsZBN9u4dOYLg=
30 | github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
31 | github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
32 | github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
33 | github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
34 | github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo=
35 | github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
36 | github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
37 | github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
38 | github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
39 | github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
40 | github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
41 | github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
42 | github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
43 | github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 h1:L0QtFUgDarD7Fpv9jeVMgy/+Ec0mtnmYuImjTz6dtDA=
44 | github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
45 | github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
46 | github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
47 | github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
48 | github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
49 | github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
50 | github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
51 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
52 | github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
53 | github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
54 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
55 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
56 | github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
57 | github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
58 | github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
59 | github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
60 | github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
61 | github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
62 | github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
63 | github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
64 | github.com/pganalyze/pg_query_go/v5 v5.1.0 h1:MlxQqHZnvA3cbRQYyIrjxEjzo560P6MyTgtlaf3pmXg=
65 | github.com/pganalyze/pg_query_go/v5 v5.1.0/go.mod h1:FsglvxidZsVN+Ltw3Ai6nTgPVcK2BPukH3jCDEqc1Ug=
66 | github.com/pingcap/errors v0.11.0/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8=
67 | github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8=
68 | github.com/pingcap/errors v0.11.5-0.20240311024730-e056997136bb h1:3pSi4EDG6hg0orE1ndHkXvX6Qdq2cZn8gAPir8ymKZk=
69 | github.com/pingcap/errors v0.11.5-0.20240311024730-e056997136bb/go.mod h1:X2r9ueLEUZgtx2cIogM0v4Zj5uvvzhuuiu7Pn8HzMPg=
70 | github.com/pingcap/failpoint v0.0.0-20220801062533-2eaa32854a6c h1:CgbKAHto5CQgWM9fSBIvaxsJHuGP0uM74HXtv3MyyGQ=
71 | github.com/pingcap/failpoint v0.0.0-20220801062533-2eaa32854a6c/go.mod h1:4qGtCB0QK0wBzKtFEGDhxXnSnbQApw1gc9siScUl8ew=
72 | github.com/pingcap/log v1.1.0 h1:ELiPxACz7vdo1qAvvaWJg1NrYFoY6gqAh/+Uo6aXdD8=
73 | github.com/pingcap/log v1.1.0/go.mod h1:DWQW5jICDR7UJh4HtxXSM20Churx4CQL0fwL/SoOSA4=
74 | github.com/pingcap/tidb/pkg/parser v0.0.0-20240401090316-c9a250a80fbc h1:ho1BOyysYRWENF4nxYKSErjwwxr4PfUaFmFlLenksg8=
75 | github.com/pingcap/tidb/pkg/parser v0.0.0-20240401090316-c9a250a80fbc/go.mod h1:c/4la2yfv1vBYvtIG8WCDyDinLMDIUC5+zLRHiafY+Y=
76 | github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
77 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
78 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
79 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
80 | github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
81 | github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
82 | github.com/riza-io/grpc-go v0.2.0 h1:2HxQKFVE7VuYstcJ8zqpN84VnAoJ4dCL6YFhJewNcHQ=
83 | github.com/riza-io/grpc-go v0.2.0/go.mod h1:2bDvR9KkKC3KhtlSHfR3dAXjUMT86kg4UfWFyVGWqi8=
84 | github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
85 | github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
86 | github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
87 | github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
88 | github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
89 | github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
90 | github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
91 | github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
92 | github.com/sqlc-dev/sqlc v1.26.0 h1:bW6TA1vVdi2lfqsEddN5tSznRMYcWez7hf+AOqSiEp8=
93 | github.com/sqlc-dev/sqlc v1.26.0/go.mod h1:k2F3RWilLCup3D0XufrzZENCyXjtplALmHDmOt4v5bs=
94 | github.com/stoewer/go-strcase v1.3.0 h1:g0eASXYtp+yvN9fK8sH94oCIk0fau9uV1/ZdJ0AVEzs=
95 | github.com/stoewer/go-strcase v1.3.0/go.mod h1:fAH5hQ5pehh+j3nZfvwdk2RgEgQjAoM8wodgtPmh1xo=
96 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
97 | github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
98 | github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
99 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
100 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
101 | github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
102 | github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
103 | github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
104 | github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
105 | github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
106 | github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
107 | github.com/tetratelabs/wazero v1.7.0 h1:jg5qPydno59wqjpGrHph81lbtHzTrWzwwtD4cD88+hQ=
108 | github.com/tetratelabs/wazero v1.7.0/go.mod h1:ytl6Zuh20R/eROuyDaGPkp82O9C/DJfXAwJfQ3X6/7Y=
109 | github.com/wasilibs/go-pgquery v0.0.0-20240319230125-b9b2e95c69a7 h1:sqqLVb63En4uTKFKBWSJ7c1aIFonhM1yn35/+KchOf4=
110 | github.com/wasilibs/go-pgquery v0.0.0-20240319230125-b9b2e95c69a7/go.mod h1:ZAUjWnxivykc22k0TKFZylOV0WlVQ9nWMExfGFIBuF4=
111 | go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
112 | go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
113 | go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
114 | go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
115 | go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
116 | go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
117 | go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
118 | go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
119 | go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
120 | go.uber.org/multierr v1.7.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
121 | go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
122 | go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
123 | go.uber.org/zap v1.19.0/go.mod h1:xg/QME4nWcxGxrpdeYfq7UvYrLh66cuVKdrbD1XF/NI=
124 | go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
125 | go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
126 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
127 | golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
128 | golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
129 | golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 h1:aAcj0Da7eBAtrTp03QXWvm88pSyOt+UgdZw2BFZ+lEw=
130 | golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ=
131 | golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
132 | golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
133 | golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
134 | golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
135 | golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
136 | golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
137 | golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
138 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
139 | golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
140 | golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
141 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
142 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
143 | golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
144 | golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
145 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
146 | golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
147 | golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
148 | golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
149 | golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
150 | golang.org/x/tools v0.0.0-20191108193012-7d206e10da11/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
151 | golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw=
152 | golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc=
153 | golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
154 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
155 | google.golang.org/genproto/googleapis/api v0.0.0-20240325203815-454cdb8f5daa h1:Jt1XW5PaLXF1/ePZrznsh/aAUvI7Adfc3LY1dAKlzRs=
156 | google.golang.org/genproto/googleapis/api v0.0.0-20240325203815-454cdb8f5daa/go.mod h1:K4kfzHtI0kqWA79gecJarFtDn/Mls+GxQcg3Zox91Ac=
157 | google.golang.org/genproto/googleapis/rpc v0.0.0-20240325203815-454cdb8f5daa h1:RBgMaUMP+6soRkik4VoN8ojR2nex2TqZwjSSogic+eo=
158 | google.golang.org/genproto/googleapis/rpc v0.0.0-20240325203815-454cdb8f5daa/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
159 | google.golang.org/grpc v1.62.1 h1:B4n+nfKzOICUXMgyrNd19h/I9oH0L1pizfk1d4zSgTk=
160 | google.golang.org/grpc v1.62.1/go.mod h1:IWTG0VlJLCh1SkC58F7np9ka9mx/WNkjl4PGJaiq+QE=
161 | google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
162 | google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
163 | google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
164 | google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
165 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
166 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
167 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
168 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
169 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
170 | gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
171 | gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
172 | gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
173 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
174 | gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
175 | gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
176 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
177 | gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
178 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
179 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
180 | modernc.org/cc/v4 v4.19.5 h1:QlsZyQ1zf78DGeqnQ9ILi9hXyMdoC5e1qoGNUyBjHQw=
181 | modernc.org/cc/v4 v4.19.5/go.mod h1:HM7VJTZbUCR3rV8EYBi9wxnJ0ZBRiGE5OeGXNA0IsLQ=
182 | modernc.org/ccgo/v4 v4.13.1 h1:qBttaSxEHNze36VBivw1/vkHuyjMDN3RY5wQX+p1Oxg=
183 | modernc.org/ccgo/v4 v4.13.1/go.mod h1:Td6RI9W9G2ZpKHaJ7UeGEiB2aIpoDqLBnm4wtkbJTbQ=
184 | modernc.org/fileutil v1.3.0 h1:gQ5SIzK3H9kdfai/5x41oQiKValumqNTDXMvKo62HvE=
185 | modernc.org/fileutil v1.3.0/go.mod h1:XatxS8fZi3pS8/hKG2GH/ArUogfxjpEKs3Ku3aK4JyQ=
186 | modernc.org/gc/v2 v2.4.1 h1:9cNzOqPyMJBvrUipmynX0ZohMhcxPtMccYgGOJdOiBw=
187 | modernc.org/gc/v2 v2.4.1/go.mod h1:wzN5dK1AzVGoH6XOzc3YZ+ey/jPgYHLuVckd62P0GYU=
188 | modernc.org/gc/v3 v3.0.0-20240304020402-f0dba7c97c2b h1:BnN1t+pb1cy61zbvSUV7SeI0PwosMhlAEi/vBY4qxp8=
189 | modernc.org/gc/v3 v3.0.0-20240304020402-f0dba7c97c2b/go.mod h1:Qz0X07sNOR1jWYCrJMEnbW/X55x206Q7Vt4mz6/wHp4=
190 | modernc.org/libc v1.49.0 h1:/kkNBuCXvlTbOGwrQdgR67eK1Y9+kR+fhdBd89C64VM=
191 | modernc.org/libc v1.49.0/go.mod h1:DNz0lgQgT6FPIPm8rHtjFj0FL5/YOr/NYFXWYBcSxMw=
192 | modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
193 | modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo=
194 | modernc.org/memory v1.7.2 h1:Klh90S215mmH8c9gO98QxQFsY+W451E8AnzjoE2ee1E=
195 | modernc.org/memory v1.7.2/go.mod h1:NO4NVCQy0N7ln+T9ngWqOQfi7ley4vpwvARR+Hjw95E=
196 | modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4=
197 | modernc.org/opt v0.1.3/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0=
198 | modernc.org/sortutil v1.2.0 h1:jQiD3PfS2REGJNzNCMMaLSp/wdMNieTbKX920Cqdgqc=
199 | modernc.org/sortutil v1.2.0/go.mod h1:TKU2s7kJMf1AE84OoiGppNHJwvB753OYfNl2WRb++Ss=
200 | modernc.org/sqlite v1.29.5 h1:8l/SQKAjDtZFo9lkJLdk8g9JEOeYRG4/ghStDCCTiTE=
201 | modernc.org/sqlite v1.29.5/go.mod h1:S02dvcmm7TnTRvGhv8IGYyLnIt7AS2KPaB1F/71p75U=
202 | modernc.org/strutil v1.2.0 h1:agBi9dp1I+eOnxXeiZawM8F4LawKv4NzGWSaLfyeNZA=
203 | modernc.org/strutil v1.2.0/go.mod h1:/mdcBmfOibveCTBxUl5B5l6W+TTH1FXPLHZE6bTosX0=
204 | modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
205 | modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
206 |
--------------------------------------------------------------------------------
/internal/crawler/crawler.go:
--------------------------------------------------------------------------------
1 | package crawler
2 |
3 | import (
4 | "bytes"
5 | "context"
6 | "errors"
7 | "fmt"
8 | "log/slog"
9 | "net"
10 | "sync"
11 | "sync/atomic"
12 | "time"
13 |
14 | "github.com/Tox/ToxStatus/internal/repo"
15 | "github.com/alexbakker/tox4go/bootstrap"
16 | "github.com/alexbakker/tox4go/dht"
17 | "github.com/alexbakker/tox4go/dht/ping"
18 | "github.com/alexbakker/tox4go/transport"
19 | )
20 |
21 | type Crawler struct {
22 | repo *repo.NodesRepo
23 | opts CrawlerOptions
24 | logger *slog.Logger
25 |
26 | m sync.Mutex
27 | ident *dht.Identity
28 | pings *ping.Set
29 |
30 | started atomic.Bool
31 | sendChan chan *dhtPacket
32 | sendInfoChan chan *infoPacket
33 | handleChan chan *dhtPacket
34 | handleInfoChan chan *infoPacket
35 | recvChan chan *rawPacket
36 | }
37 |
38 | type CrawlerOptions struct {
39 | Logger *slog.Logger
40 | HTTPAddr string
41 | ToxUDPAddr string
42 | Workers int
43 | }
44 |
45 | type infoPacket struct {
46 | Packet bootstrap.Packet
47 | Addr *net.UDPAddr
48 | }
49 |
50 | type dhtPacket struct {
51 | Packet dht.Packet
52 | Node *dht.Node
53 | }
54 |
55 | type rawPacket struct {
56 | Data []byte
57 | Addr *net.UDPAddr
58 | }
59 |
60 | func New(nodesRepo *repo.NodesRepo, opts CrawlerOptions) (*Crawler, error) {
61 | ident, err := dht.NewIdentity(dht.IdentityOptions{
62 | // Large cache for precomputed shared keys to improve performance
63 | SharedKeyCacheSize: 10000,
64 | })
65 | if err != nil {
66 | return nil, err
67 | }
68 |
69 | if opts.Workers < 2 || opts.Workers%2 != 0 {
70 | return nil, fmt.Errorf("bad number of workers: %d (must be a multiple of 2)", opts.Workers)
71 | }
72 |
73 | c := &Crawler{
74 | repo: nodesRepo,
75 | opts: opts,
76 | logger: opts.Logger,
77 | ident: ident,
78 | pings: ping.NewSet(ping.DefaultTimeout),
79 | sendChan: make(chan *dhtPacket),
80 | sendInfoChan: make(chan *infoPacket),
81 | handleChan: make(chan *dhtPacket),
82 | handleInfoChan: make(chan *infoPacket),
83 | recvChan: make(chan *rawPacket),
84 | }
85 |
86 | return c, nil
87 | }
88 |
89 | func (c *Crawler) Run(ctx context.Context, bsNodes []*dht.Node) error {
90 | if !c.started.CompareAndSwap(false, true) {
91 | return errors.New("attempt to start crawler twice")
92 | }
93 |
94 | tp, err := transport.NewUDPTransport("udp", c.opts.ToxUDPAddr, func(data []byte, addr *net.UDPAddr) {
95 | // We need to copy the packet data, because once this function returns,
96 | // the backing buffer will be reused for the next packet, so the
97 | // contents of the data slice will get overwritten.
98 | cdata := make([]byte, len(data))
99 | copy(cdata, data)
100 |
101 | select {
102 | case <-ctx.Done():
103 | return
104 | case c.recvChan <- &rawPacket{Data: cdata, Addr: addr}:
105 | }
106 | })
107 | if err != nil {
108 | return fmt.Errorf("tox udp transport: %w", err)
109 | }
110 |
111 | ctx, cancel := context.WithCancel(ctx)
112 | defer cancel()
113 |
114 | var wg sync.WaitGroup
115 | listenErrChan := make(chan error)
116 | go func() {
117 | defer close(listenErrChan)
118 |
119 | if err := tp.Listen(); err != nil {
120 | listenErrChan <- err
121 | }
122 | }()
123 |
124 | workers := c.opts.Workers / 2
125 | for i := 0; i < workers; i++ {
126 | wg.Add(1)
127 | go func(i int) {
128 | var total uint64
129 | logger := c.logger.With(slog.Int("worker", i))
130 | defer func() {
131 | logger.Info("Stopping packet transmitter", slog.Uint64("packets", total))
132 | wg.Done()
133 | }()
134 |
135 | logger.Info("Starting packet transmitter")
136 |
137 | for {
138 | select {
139 | case <-ctx.Done():
140 | return
141 | case packet := <-c.sendChan:
142 | if err := c.sendPacket(tp, packet.Packet, packet.Node); err != nil {
143 | c.logger.Error("Unable to send packet",
144 | slog.String("public_key", packet.Node.PublicKey.String()),
145 | slog.String("net", packet.Node.Type.Net()),
146 | slog.String("addr", packet.Node.Addr().String()),
147 | slog.Any("err", err))
148 |
149 | if errors.Is(err, net.ErrClosed) {
150 | return
151 | }
152 | }
153 | case packet := <-c.sendInfoChan:
154 | if err := c.sendInfoPacket(tp, packet.Packet, packet.Addr); err != nil {
155 | c.logger.Error("Unable to send bootstrap info packet",
156 | slog.String("addr", packet.Addr.String()),
157 | slog.Any("err", err))
158 |
159 | if errors.Is(err, net.ErrClosed) {
160 | return
161 | }
162 | }
163 | }
164 |
165 | total++
166 | }
167 | }(i)
168 | }
169 |
170 | for i := 0; i < workers; i++ {
171 | wg.Add(1)
172 | go func(i int) {
173 | var total uint64
174 | logger := c.logger.With(slog.Int("worker", i))
175 | defer func() {
176 | logger.Info("Stopping packet receiver", slog.Uint64("packets", total))
177 | wg.Done()
178 | }()
179 |
180 | logger.Info("Starting packet receiver")
181 |
182 | for {
183 | select {
184 | case <-ctx.Done():
185 | return
186 | case packet := <-c.recvChan:
187 | if err := c.receivePacket(ctx, packet.Data, packet.Addr); err != nil {
188 | c.logger.Error("Unable to receive raw packet",
189 | slog.String("net", packet.Addr.Network()),
190 | slog.String("addr", packet.Addr.String()),
191 | slog.Any("err", err))
192 |
193 | if errors.Is(err, context.Canceled) {
194 | return
195 | }
196 | }
197 | }
198 |
199 | total++
200 | }
201 | }(i)
202 | }
203 |
204 | wg.Add(1)
205 | go func() {
206 | defer wg.Done()
207 |
208 | for {
209 | select {
210 | case <-ctx.Done():
211 | return
212 | case packet := <-c.handleChan:
213 | if err := c.handleDHTPacket(ctx, packet.Packet, packet.Node); err != nil {
214 | c.logger.Error("Unable to handle packet",
215 | slog.String("public_key", packet.Node.PublicKey.String()),
216 | slog.String("net", packet.Node.Type.Net()),
217 | slog.String("addr", packet.Node.Addr().String()),
218 | slog.Any("err", err))
219 | }
220 | case packet := <-c.handleInfoChan:
221 | if err := c.handleInfoPacket(ctx, packet.Packet, packet.Addr); err != nil {
222 | c.logger.Error("Unable to handle bootstrap info packet",
223 | slog.String("addr", packet.Addr.String()),
224 | slog.Any("err", err))
225 | }
226 | }
227 | }
228 | }()
229 |
230 | wg.Add(1)
231 | go func() {
232 | defer wg.Done()
233 |
234 | c.logger.Info("Bootstrapping...", slog.Int("nodes", len(bsNodes)))
235 |
236 | for _, bsNode := range bsNodes {
237 | if err := ctx.Err(); err != nil {
238 | return
239 | }
240 |
241 | logger := slog.With(
242 | slog.String("public_key", bsNode.PublicKey.String()),
243 | slog.String("addr", bsNode.Addr().String()),
244 | )
245 |
246 | if !isGlobalUnicast(bsNode.IP) {
247 | logger.Warn("Node ip is not a global unicast address")
248 | continue
249 | }
250 |
251 | if _, err := c.repo.TrackDHTNode(ctx, bsNode); err != nil {
252 | logger.Error("Unable to track bootstrap node", slog.Any("err", err))
253 | continue
254 | }
255 |
256 | if err := c.getNodes(ctx, bsNode, c.ident.PublicKey); err != nil {
257 | logger.Error("Unable to query bootstrap node", slog.Any("err", err))
258 | }
259 | }
260 |
261 | // Wait for boostrapping to have gathered some node responses
262 | select {
263 | case <-ctx.Done():
264 | return
265 | case <-time.After(2 * time.Second):
266 | }
267 |
268 | // TODO: Remove nodes that we haven't successfully pinged in a while
269 | // Periodically query the nodes we know
270 | pkgen := getPublicKeyGenerator(199)
271 | for {
272 | c.logger.Info("Rotating target keys")
273 | var targetKeys []*dht.PublicKey
274 | for i := 0; i < 8; i++ {
275 | key := pkgen()
276 | targetKeys = append(targetKeys, key)
277 | c.logger.Info(key.String())
278 | }
279 |
280 | nodes, err := c.repo.GetResponsiveDHTNodes(ctx)
281 | if err == nil {
282 | c.logger.Info("Crawling...", slog.Int("nodes", len(nodes)))
283 |
284 | for _, node := range nodes {
285 | for _, targetKey := range targetKeys {
286 | if err := ctx.Err(); err != nil {
287 | return
288 | }
289 | if err := c.getNodes(ctx, node, targetKey); err != nil {
290 | c.logger.Error("Unable to query node",
291 | slog.String("public_key", node.PublicKey.String()),
292 | slog.String("addr", node.Addr().String()),
293 | slog.Any("err", err))
294 | }
295 | }
296 | }
297 | } else {
298 | c.logger.Error("Unable to obtain responsive dht nodes", slog.Any("err", err))
299 | }
300 |
301 | select {
302 | case <-ctx.Done():
303 | return
304 | case <-time.After(5 * time.Second):
305 | }
306 | }
307 | }()
308 |
309 | wg.Add(1)
310 | go func() {
311 | defer wg.Done()
312 |
313 | for {
314 | count, err := c.repo.GetNodeCount(ctx)
315 | if err == nil {
316 | c.logger.Info("Total number of nodes", slog.Int64("count", count))
317 | } else {
318 | c.logger.Error("Unable to query db for total number of nodes", slog.Any("err", err))
319 | }
320 |
321 | select {
322 | case <-ctx.Done():
323 | return
324 | case <-time.After(1 * time.Second):
325 | }
326 | }
327 | }()
328 |
329 | wg.Add(1)
330 | go func() {
331 | defer wg.Done()
332 |
333 | for {
334 | const retryPingDelay = 10 * time.Second
335 | nodes, err := c.repo.GetUnresponsiveDHTNodes(ctx, retryPingDelay)
336 | if err == nil {
337 | var pingedNodes int
338 | for _, node := range nodes {
339 | if err := ctx.Err(); err != nil {
340 | return
341 | }
342 |
343 | if err := c.getNodes(ctx, node, c.ident.PublicKey); err != nil {
344 | c.logger.Error("Unable to ping node",
345 | slog.String("public_key", node.PublicKey.String()),
346 | slog.String("addr", node.Addr().String()),
347 | slog.Any("err", err))
348 | } else {
349 | pingedNodes++
350 | }
351 | }
352 |
353 | c.logger.Info("Pinged nodes", slog.Int("count", pingedNodes))
354 | } else {
355 | c.logger.Error("Unable to obtain unresponsive dht nodes", slog.Any("err", err))
356 | }
357 |
358 | select {
359 | case <-ctx.Done():
360 | return
361 | case <-time.After(1 * time.Second):
362 | }
363 | }
364 | }()
365 |
366 | wg.Add(1)
367 | go func() {
368 | defer wg.Done()
369 |
370 | for {
371 | reqTimes := make(map[int64]time.Time)
372 | nodes, err := c.repo.GetNodesWithStaleBootstrapInfo(ctx)
373 | if err == nil {
374 | for _, node := range nodes {
375 | for _, addr := range node.Addresses {
376 | dhtNode, err := addr.DHTNode()
377 | if err != nil {
378 | c.logger.Error("Unable to convert db node address to dht node", slog.Any("err", err))
379 | continue
380 | }
381 |
382 | packet := infoPacket{
383 | Packet: new(bootstrap.InfoRequestPacket),
384 | Addr: dhtNode.Addr().(*net.UDPAddr),
385 | }
386 |
387 | select {
388 | case <-ctx.Done():
389 | return
390 | case c.sendInfoChan <- &packet:
391 | reqTimes[node.ID] = time.Now()
392 | }
393 | }
394 | }
395 | } else {
396 | c.logger.Error("Unable to obtain dht nodes with stale bootstrap info", slog.Any("err", err))
397 | }
398 |
399 | if err := c.repo.UpdateNodeInfoRequestTime(ctx, reqTimes); err != nil {
400 | c.logger.Error("Unable to update node bootstrap info request time", slog.Any("err", err))
401 | }
402 |
403 | select {
404 | case <-ctx.Done():
405 | return
406 | case <-time.After(1 * time.Second):
407 | }
408 | }
409 | }()
410 |
411 | select {
412 | case err = <-listenErrChan:
413 | cancel()
414 | case <-ctx.Done():
415 | err = ctx.Err()
416 | }
417 |
418 | wg.Wait()
419 | tp.Close()
420 | <-listenErrChan
421 | return err
422 | }
423 |
424 | func (c *Crawler) handleDHTPacket(ctx context.Context, packet dht.Packet, node *dht.Node) error {
425 | var err error
426 | switch packet := packet.(type) {
427 | case *dht.GetNodesPacket:
428 | case *dht.SendNodesPacket:
429 | err = c.handleSendNodesPacket(ctx, node, packet)
430 | case *dht.PingRequestPacket:
431 | case *dht.PingResponsePacket:
432 | default:
433 | err = fmt.Errorf("unsupported dht packet type: %d", packet.ID())
434 | }
435 |
436 | return err
437 | }
438 |
439 | func (c *Crawler) handleInfoPacket(ctx context.Context, packet bootstrap.Packet, addr *net.UDPAddr) error {
440 | var err error
441 | switch packet := packet.(type) {
442 | case *bootstrap.InfoResponsePacket:
443 | err = c.handleBootstrapInfoPacket(ctx, addr, packet)
444 | default:
445 | err = fmt.Errorf("unsupported bootstrap info packet type: %d", packet.ID())
446 | }
447 |
448 | return err
449 | }
450 |
451 | func (c *Crawler) handleSendNodesPacket(ctx context.Context, node *dht.Node, packet *dht.SendNodesPacket) error {
452 | c.m.Lock()
453 | if _, err := c.pings.Pop(node.PublicKey, packet.PingID); err != nil {
454 | c.m.Unlock()
455 | return fmt.Errorf("unexpected sendnodes packet: %w", err)
456 | }
457 | c.m.Unlock()
458 |
459 | // Insert/update the known nodes list
460 | if err := c.repo.PongDHTNode(ctx, node); err != nil {
461 | return fmt.Errorf("update node pong time: %w", err)
462 | }
463 |
464 | var errs []error
465 | for _, packetNode := range packet.Nodes {
466 | // Don't query our own node or ones we've seen before
467 | if bytes.Equal(packetNode.PublicKey[:], c.ident.PublicKey[:]) {
468 | continue
469 | }
470 |
471 | logger := c.logger.With(slog.String("public_key", packetNode.PublicKey.String()),
472 | slog.String("net", packetNode.Type.Net()),
473 | slog.String("addr", packetNode.Addr().String()))
474 |
475 | if !isGlobalUnicast(packetNode.IP) {
476 | logger.Warn("Node ip is not a global unicast address")
477 | continue
478 | }
479 |
480 | found, err := c.repo.HasNodeByPublicKey(ctx, packetNode.PublicKey)
481 | if err != nil {
482 | return fmt.Errorf("check whether node is known: %w", err)
483 | }
484 | if found {
485 | continue
486 | }
487 |
488 | logger.Info("Tracking new node")
489 |
490 | if _, err := c.repo.TrackDHTNode(ctx, packetNode); err != nil {
491 | logger.Error("Unable to track node", slog.Any("err", err))
492 | continue
493 | }
494 |
495 | if err := c.getNodes(ctx, packetNode, c.ident.PublicKey); err != nil {
496 | errs = append(errs, err)
497 | }
498 | }
499 |
500 | if err := errors.Join(errs...); err != nil {
501 | return fmt.Errorf("query sent nodes: %w", errors.Join(errs...))
502 | }
503 |
504 | return nil
505 | }
506 |
507 | func (c *Crawler) handleBootstrapInfoPacket(ctx context.Context, addr *net.UDPAddr, packet *bootstrap.InfoResponsePacket) error {
508 | c.logger.Debug("Handling bootstrap info response packet", slog.String("addr", addr.String()))
509 | return c.repo.UpdateNodeInfo(ctx, addr, packet.MOTD, packet.Version)
510 | }
511 |
512 | // getNodes queries the given DHT node to search for the given publicKey.
513 | func (c *Crawler) getNodes(ctx context.Context, node *dht.Node, publicKey *dht.PublicKey) error {
514 | c.logger.Debug("Querying node",
515 | slog.String("public_key", node.PublicKey.String()),
516 | slog.String("net", node.Type.Net()),
517 | slog.String("addr", node.Addr().String()))
518 |
519 | c.m.Lock()
520 | ping, err := c.pings.Add(node.PublicKey)
521 | if err != nil {
522 | c.m.Unlock()
523 | return err
524 | }
525 | c.m.Unlock()
526 |
527 | if err := c.repo.PingDHTNode(ctx, node); err != nil {
528 | return fmt.Errorf("track node ping: %s", err)
529 | }
530 |
531 | packet := &dht.GetNodesPacket{
532 | PublicKey: publicKey,
533 | PingID: ping.ID(),
534 | }
535 |
536 | select {
537 | case <-ctx.Done():
538 | return ctx.Err()
539 | case c.sendChan <- &dhtPacket{Packet: packet, Node: node}:
540 | }
541 |
542 | return nil
543 | }
544 |
545 | func (c *Crawler) sendPacket(tp transport.Transport, packet dht.Packet, destNode *dht.Node) error {
546 | c.logger.Debug("Sending packet",
547 | slog.String("public_key", destNode.PublicKey.String()),
548 | slog.String("net", destNode.Type.Net()),
549 | slog.String("addr", destNode.Addr().String()),
550 | slog.String("packet_type", packet.ID().String()))
551 |
552 | dhtPacket, err := c.ident.EncryptPacket(packet, destNode.PublicKey)
553 | if err != nil {
554 | return err
555 | }
556 |
557 | packetBytes, err := dhtPacket.MarshalBinary()
558 | if err != nil {
559 | return err
560 | }
561 |
562 | return tp.SendPacket(packetBytes, destNode.Addr().(*net.UDPAddr))
563 | }
564 |
565 | func (c *Crawler) sendInfoPacket(tp transport.Transport, packet bootstrap.Packet, addr *net.UDPAddr) error {
566 | c.logger.Debug("Sending bootstrap info request packet",
567 | slog.String("addr", addr.String()),
568 | slog.String("packet_type", packet.ID().String()))
569 |
570 | rawPacket, err := bootstrap.MarshalPacket(packet)
571 | if err != nil {
572 | return err
573 | }
574 |
575 | packetBytes, err := rawPacket.MarshalBinary()
576 | if err != nil {
577 | return err
578 | }
579 |
580 | return tp.SendPacket(packetBytes, addr)
581 | }
582 |
583 | func (c *Crawler) receivePacket(ctx context.Context, data []byte, addr *net.UDPAddr) error {
584 | var nodeType dht.NodeType
585 | if addr.IP.To4() != nil {
586 | nodeType = dht.NodeTypeUDPIP4
587 | } else {
588 | nodeType = dht.NodeTypeUDPIP6
589 | }
590 |
591 | logger := c.logger.With(slog.String("net", nodeType.Net()), slog.String("addr", addr.String()))
592 |
593 | bsPacket, err := bootstrap.UnmarshalBinary(data)
594 | if err == nil {
595 | c.handleInfoChan <- &infoPacket{Addr: addr, Packet: bsPacket}
596 | return nil
597 | }
598 | if !errors.Is(err, bootstrap.ErrUnknownPacketType) {
599 | return fmt.Errorf("bootstrap info packet check: %w", err)
600 | }
601 |
602 | var encryptedPacket dht.EncryptedPacket
603 | if err := encryptedPacket.UnmarshalBinary(data); err != nil {
604 | return fmt.Errorf("unmarshal encrypted packet: %w", err)
605 | }
606 |
607 | // We're only interested in sendnodes packets
608 | logger = logger.With(slog.String("packet_type", encryptedPacket.Type.String()))
609 | if encryptedPacket.Type != dht.PacketTypeSendNodes {
610 | logger.Debug("Ignoring non-sendnodes packet")
611 | return nil
612 | }
613 | logger.Debug("Decrypting DHT packet")
614 |
615 | decryptedPacket, err := c.ident.DecryptPacket(&encryptedPacket)
616 | if err != nil {
617 | return fmt.Errorf("decrypt packet: %w", err)
618 | }
619 |
620 | node := &dht.Node{
621 | IP: addr.IP,
622 | Port: addr.Port,
623 | PublicKey: encryptedPacket.SenderPublicKey,
624 | Type: nodeType,
625 | }
626 |
627 | select {
628 | case <-ctx.Done():
629 | return ctx.Err()
630 | case c.handleChan <- &dhtPacket{Packet: decryptedPacket, Node: node}:
631 | }
632 |
633 | return nil
634 | }
635 |
--------------------------------------------------------------------------------
/internal/crawler/util.go:
--------------------------------------------------------------------------------
1 | package crawler
2 |
3 | import (
4 | "encoding/binary"
5 | "net"
6 |
7 | "github.com/alexbakker/tox4go/dht"
8 | )
9 |
10 | func getPublicKeyGenerator(n int) func() *dht.PublicKey {
11 | var counterBytes [2]byte
12 | step := (1 << (len(counterBytes) * 8)) / n
13 |
14 | i := 0
15 | return func() *dht.PublicKey {
16 | val := step * i
17 | binary.BigEndian.PutUint16(counterBytes[:], uint16(val))
18 |
19 | i = (i + 1) % n
20 |
21 | var res dht.PublicKey
22 | copy(res[:len(counterBytes)], counterBytes[:])
23 | return &res
24 | }
25 | }
26 |
27 | func isGlobalUnicast(ip net.IP) bool {
28 | return !ip.IsUnspecified() &&
29 | !ip.IsLoopback() &&
30 | !ip.IsPrivate() &&
31 | !ip.IsMulticast() &&
32 | !ip.IsLinkLocalUnicast() &&
33 | !ip.IsLinkLocalMulticast()
34 | }
35 |
--------------------------------------------------------------------------------
/internal/db/db.go:
--------------------------------------------------------------------------------
1 | // Code generated by sqlc. DO NOT EDIT.
2 | // versions:
3 | // sqlc v1.26.0
4 |
5 | package db
6 |
7 | import (
8 | "context"
9 | "database/sql"
10 | )
11 |
12 | type DBTX interface {
13 | ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
14 | PrepareContext(context.Context, string) (*sql.Stmt, error)
15 | QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
16 | QueryRowContext(context.Context, string, ...interface{}) *sql.Row
17 | }
18 |
19 | func New(db DBTX) *Queries {
20 | return &Queries{db: db}
21 | }
22 |
23 | type Queries struct {
24 | db DBTX
25 | }
26 |
27 | func (q *Queries) WithTx(tx *sql.Tx) *Queries {
28 | return &Queries{
29 | db: tx,
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/internal/db/embed.go:
--------------------------------------------------------------------------------
1 | package db
2 |
3 | import _ "embed"
4 |
5 | //go:embed schema.sql
6 | var Schema string
7 |
--------------------------------------------------------------------------------
/internal/db/gen.go:
--------------------------------------------------------------------------------
1 | //go:build generate
2 |
3 | package db
4 |
5 | import _ "github.com/sqlc-dev/sqlc/cmd/sqlc"
6 |
7 | //go:generate go run github.com/sqlc-dev/sqlc/cmd/sqlc generate
8 |
--------------------------------------------------------------------------------
/internal/db/models.go:
--------------------------------------------------------------------------------
1 | // Code generated by sqlc. DO NOT EDIT.
2 | // versions:
3 | // sqlc v1.26.0
4 |
5 | package db
6 |
7 | import (
8 | "database/sql"
9 | )
10 |
11 | type Node struct {
12 | ID int64
13 | CreatedAt Time
14 | LastSeenAt Time
15 | LastInfoReqAt Time
16 | LastInfoResAt Time
17 | PublicKey *PublicKey
18 | Fqdn sql.NullString
19 | Motd sql.NullString
20 | Version sql.NullInt64
21 | }
22 |
23 | type NodeAddress struct {
24 | ID int64
25 | CreatedAt Time
26 | LastSeenAt Time
27 | LastPingAt Time
28 | LastPongAt Time
29 | NodeID int64
30 | Net string
31 | Ip string
32 | Port int64
33 | Ptr sql.NullString
34 | }
35 |
--------------------------------------------------------------------------------
/internal/db/open.go:
--------------------------------------------------------------------------------
1 | package db
2 |
3 | import (
4 | "context"
5 | "database/sql"
6 | "fmt"
7 | "net/url"
8 | "runtime"
9 |
10 | "github.com/mattn/go-sqlite3"
11 | )
12 |
13 | type OpenOptions struct {
14 | Params map[string]string
15 | }
16 |
17 | func RegisterPragmaHook(cacheSize int) {
18 | sql.Register("toxstatus_sqlite3", &sqlite3.SQLiteDriver{
19 | ConnectHook: func(c *sqlite3.SQLiteConn) error {
20 | fmt.Println("Executing pragmas")
21 | pragmas := fmt.Sprintf(`
22 | PRAGMA journal_mode = WAL;
23 | PRAGMA busy_timeout = 5000;
24 | PRAGMA synchronous = NORMAL;
25 | PRAGMA cache_size = -%d;
26 | PRAGMA foreign_keys = true;
27 | PRAGMA temp_store = memory;
28 | `, cacheSize)
29 | _, err := c.Exec(pragmas, nil)
30 | return err
31 | },
32 | })
33 | }
34 |
35 | func OpenReadWrite(ctx context.Context, dbFile string, opts OpenOptions) (rdb *sql.DB, wdb *sql.DB, err error) {
36 | uri := &url.URL{
37 | Scheme: "file",
38 | Opaque: dbFile,
39 | }
40 | query := uri.Query()
41 | if opts.Params != nil {
42 | for k, v := range opts.Params {
43 | query.Set(k, v)
44 | }
45 | }
46 | query.Set("_txlock", "immediate")
47 | uri.RawQuery = query.Encode()
48 |
49 | readConn, err := sql.Open("toxstatus_sqlite3", uri.String())
50 | if err != nil {
51 | return nil, nil, err
52 | }
53 | defer func() {
54 | if err != nil {
55 | readConn.Close()
56 | }
57 | }()
58 | readConn.SetMaxOpenConns(max(4, runtime.NumCPU()))
59 |
60 | writeConn, err := sql.Open("toxstatus_sqlite3", uri.String())
61 | if err != nil {
62 | return nil, nil, err
63 | }
64 | defer func() {
65 | if err != nil {
66 | writeConn.Close()
67 | }
68 | }()
69 | writeConn.SetMaxOpenConns(1)
70 |
71 | if _, err = writeConn.ExecContext(ctx, Schema); err != nil {
72 | return nil, nil, fmt.Errorf("init db: %w", err)
73 | }
74 |
75 | return readConn, writeConn, nil
76 | }
77 |
--------------------------------------------------------------------------------
/internal/db/queries.sql:
--------------------------------------------------------------------------------
1 | -- name: GetNodeByPublicKey :many
2 | SELECT sqlc.embed(n), sqlc.embed(a)
3 | FROM node n
4 | JOIN node_address a ON a.node_id = n.id
5 | WHERE n.public_key = ?;
6 |
7 | -- name: HasNodeByPublicKey :one
8 | SELECT EXISTS(
9 | SELECT 1
10 | FROM node
11 | WHERE public_key = ?
12 | );
13 |
14 | -- name: GetNodeCount :one
15 | SELECT COUNT(*)
16 | FROM node;
17 |
18 | -- name: UpsertNode :one
19 | INSERT INTO node(public_key)
20 | VALUES(?)
21 | ON CONFLICT(public_key)
22 | DO UPDATE SET last_seen_at = unixepoch('subsec')
23 | RETURNING *;
24 |
25 | -- name: UpdateNodeBootstrapInfo :exec
26 | UPDATE node
27 | SET motd = ?, version = ?, last_info_res_at = unixepoch('subsec')
28 | WHERE public_key = ?;
29 |
30 | -- name: UpsertNodeAddress :one
31 | INSERT INTO node_address(node_id, net, ip, port, ptr)
32 | VALUES(?, ?, ?, ?, ?)
33 | ON CONFLICT(node_id, net, ip, port) DO UPDATE SET last_seen_at = unixepoch('subsec')
34 | RETURNING *;
35 |
36 | -- name: UpdateNodeAddress :one
37 | UPDATE node_address
38 | SET node_id = ?, net = ?, ip = ?, port = ?, ptr = ?
39 | WHERE id = ?
40 | RETURNING *;
41 |
42 | -- name: UpdateNodeInfoRequestTime :exec
43 | UPDATE node
44 | SET last_info_req_at = ?
45 | WHERE id = ?;
46 |
47 | -- name: GetNodeAddress :one
48 | SELECT a.id
49 | FROM node_address a
50 | JOIN node n ON n.id = a.node_id
51 | WHERE n.public_key = ? AND a.net = ? AND a.ip = ? AND a.port = ?;
52 |
53 | -- name: PingNodeAddress :exec
54 | UPDATE node_address
55 | SET last_ping_at = unixepoch('subsec')
56 | WHERE id = ?;
57 |
58 | -- name: PongNodeAddress :exec
59 | UPDATE node_address
60 | SET last_pong_at = unixepoch('subsec')
61 | WHERE id = ?;
62 |
63 | -- name: GetResponsiveNodes :many
64 | SELECT sqlc.embed(n), sqlc.embed(a)
65 | FROM node n
66 | JOIN node_address a ON a.node_id = n.id
67 | WHERE a.last_pong_at IS NOT NULL;
68 |
69 | -- name: GetUnresponsiveNodes :many
70 | SELECT sqlc.embed(n), sqlc.embed(a)
71 | FROM node n
72 | JOIN node_address a ON a.node_id = n.id
73 | WHERE a.last_pong_at IS NULL
74 | AND a.last_ping_at IS NOT NULL
75 | AND (unixepoch('subsec') - a.last_ping_at) >= CAST(sqlc.arg(retry_delay) AS REAL);
76 |
77 | -- name: GetNodesWithStaleBootstrapInfo :many
78 | SELECT sqlc.embed(n), sqlc.embed(a)
79 | FROM node n
80 | JOIN node_address a ON a.node_id = n.id
81 | WHERE a.last_pong_at IS NOT NULL
82 | AND a.net IN ("udp4", "udp6")
83 | AND (unixepoch('subsec') - a.last_pong_at) < CAST(sqlc.arg(node_timeout) AS REAL)
84 | AND (n.last_info_req_at IS NULL
85 | OR (unixepoch('subsec') - n.last_info_req_at) >= CAST(sqlc.arg(info_interval) AS REAL))
86 | AND (n.last_info_res_at IS NULL
87 | OR (unixepoch('subsec') - n.last_info_res_at) >= CAST(sqlc.arg(info_interval) AS REAL));
88 |
89 | -- name: GetNodeByInfoResponseAddress :one
90 | SELECT sqlc.embed(n), sqlc.embed(a)
91 | FROM node n
92 | JOIN node_address a ON a.node_id = n.id
93 | WHERE a.net = ? AND a.ip = ? AND a.port = ?
94 | AND (unixepoch('subsec') - n.last_info_req_at) < CAST(sqlc.arg(info_req_timeout) AS REAL);
95 |
--------------------------------------------------------------------------------
/internal/db/queries.sql.go:
--------------------------------------------------------------------------------
1 | // Code generated by sqlc. DO NOT EDIT.
2 | // versions:
3 | // sqlc v1.26.0
4 | // source: queries.sql
5 |
6 | package db
7 |
8 | import (
9 | "context"
10 | "database/sql"
11 | )
12 |
13 | const getNodeAddress = `-- name: GetNodeAddress :one
14 | SELECT a.id
15 | FROM node_address a
16 | JOIN node n ON n.id = a.node_id
17 | WHERE n.public_key = ? AND a.net = ? AND a.ip = ? AND a.port = ?
18 | `
19 |
20 | type GetNodeAddressParams struct {
21 | PublicKey *PublicKey
22 | Net string
23 | Ip string
24 | Port int64
25 | }
26 |
27 | func (q *Queries) GetNodeAddress(ctx context.Context, arg *GetNodeAddressParams) (int64, error) {
28 | row := q.db.QueryRowContext(ctx, getNodeAddress,
29 | arg.PublicKey,
30 | arg.Net,
31 | arg.Ip,
32 | arg.Port,
33 | )
34 | var id int64
35 | err := row.Scan(&id)
36 | return id, err
37 | }
38 |
39 | const getNodeByInfoResponseAddress = `-- name: GetNodeByInfoResponseAddress :one
40 | SELECT n.id, n.created_at, n.last_seen_at, n.last_info_req_at, n.last_info_res_at, n.public_key, n.fqdn, n.motd, n.version, a.id, a.created_at, a.last_seen_at, a.last_ping_at, a.last_pong_at, a.node_id, a.net, a.ip, a.port, a.ptr
41 | FROM node n
42 | JOIN node_address a ON a.node_id = n.id
43 | WHERE a.net = ? AND a.ip = ? AND a.port = ?
44 | AND (unixepoch('subsec') - n.last_info_req_at) < CAST(?4 AS REAL)
45 | `
46 |
47 | type GetNodeByInfoResponseAddressParams struct {
48 | Net string
49 | Ip string
50 | Port int64
51 | InfoReqTimeout float64
52 | }
53 |
54 | type GetNodeByInfoResponseAddressRow struct {
55 | Node Node
56 | NodeAddress NodeAddress
57 | }
58 |
59 | func (q *Queries) GetNodeByInfoResponseAddress(ctx context.Context, arg *GetNodeByInfoResponseAddressParams) (*GetNodeByInfoResponseAddressRow, error) {
60 | row := q.db.QueryRowContext(ctx, getNodeByInfoResponseAddress,
61 | arg.Net,
62 | arg.Ip,
63 | arg.Port,
64 | arg.InfoReqTimeout,
65 | )
66 | var i GetNodeByInfoResponseAddressRow
67 | err := row.Scan(
68 | &i.Node.ID,
69 | &i.Node.CreatedAt,
70 | &i.Node.LastSeenAt,
71 | &i.Node.LastInfoReqAt,
72 | &i.Node.LastInfoResAt,
73 | &i.Node.PublicKey,
74 | &i.Node.Fqdn,
75 | &i.Node.Motd,
76 | &i.Node.Version,
77 | &i.NodeAddress.ID,
78 | &i.NodeAddress.CreatedAt,
79 | &i.NodeAddress.LastSeenAt,
80 | &i.NodeAddress.LastPingAt,
81 | &i.NodeAddress.LastPongAt,
82 | &i.NodeAddress.NodeID,
83 | &i.NodeAddress.Net,
84 | &i.NodeAddress.Ip,
85 | &i.NodeAddress.Port,
86 | &i.NodeAddress.Ptr,
87 | )
88 | return &i, err
89 | }
90 |
91 | const getNodeByPublicKey = `-- name: GetNodeByPublicKey :many
92 | SELECT n.id, n.created_at, n.last_seen_at, n.last_info_req_at, n.last_info_res_at, n.public_key, n.fqdn, n.motd, n.version, a.id, a.created_at, a.last_seen_at, a.last_ping_at, a.last_pong_at, a.node_id, a.net, a.ip, a.port, a.ptr
93 | FROM node n
94 | JOIN node_address a ON a.node_id = n.id
95 | WHERE n.public_key = ?
96 | `
97 |
98 | type GetNodeByPublicKeyRow struct {
99 | Node Node
100 | NodeAddress NodeAddress
101 | }
102 |
103 | func (q *Queries) GetNodeByPublicKey(ctx context.Context, publicKey *PublicKey) ([]*GetNodeByPublicKeyRow, error) {
104 | rows, err := q.db.QueryContext(ctx, getNodeByPublicKey, publicKey)
105 | if err != nil {
106 | return nil, err
107 | }
108 | defer rows.Close()
109 | var items []*GetNodeByPublicKeyRow
110 | for rows.Next() {
111 | var i GetNodeByPublicKeyRow
112 | if err := rows.Scan(
113 | &i.Node.ID,
114 | &i.Node.CreatedAt,
115 | &i.Node.LastSeenAt,
116 | &i.Node.LastInfoReqAt,
117 | &i.Node.LastInfoResAt,
118 | &i.Node.PublicKey,
119 | &i.Node.Fqdn,
120 | &i.Node.Motd,
121 | &i.Node.Version,
122 | &i.NodeAddress.ID,
123 | &i.NodeAddress.CreatedAt,
124 | &i.NodeAddress.LastSeenAt,
125 | &i.NodeAddress.LastPingAt,
126 | &i.NodeAddress.LastPongAt,
127 | &i.NodeAddress.NodeID,
128 | &i.NodeAddress.Net,
129 | &i.NodeAddress.Ip,
130 | &i.NodeAddress.Port,
131 | &i.NodeAddress.Ptr,
132 | ); err != nil {
133 | return nil, err
134 | }
135 | items = append(items, &i)
136 | }
137 | if err := rows.Close(); err != nil {
138 | return nil, err
139 | }
140 | if err := rows.Err(); err != nil {
141 | return nil, err
142 | }
143 | return items, nil
144 | }
145 |
146 | const getNodeCount = `-- name: GetNodeCount :one
147 | SELECT COUNT(*)
148 | FROM node
149 | `
150 |
151 | func (q *Queries) GetNodeCount(ctx context.Context) (int64, error) {
152 | row := q.db.QueryRowContext(ctx, getNodeCount)
153 | var count int64
154 | err := row.Scan(&count)
155 | return count, err
156 | }
157 |
158 | const getNodesWithStaleBootstrapInfo = `-- name: GetNodesWithStaleBootstrapInfo :many
159 | SELECT n.id, n.created_at, n.last_seen_at, n.last_info_req_at, n.last_info_res_at, n.public_key, n.fqdn, n.motd, n.version, a.id, a.created_at, a.last_seen_at, a.last_ping_at, a.last_pong_at, a.node_id, a.net, a.ip, a.port, a.ptr
160 | FROM node n
161 | JOIN node_address a ON a.node_id = n.id
162 | WHERE a.last_pong_at IS NOT NULL
163 | AND a.net IN ("udp4", "udp6")
164 | AND (unixepoch('subsec') - a.last_pong_at) < CAST(?1 AS REAL)
165 | AND (n.last_info_req_at IS NULL
166 | OR (unixepoch('subsec') - n.last_info_req_at) >= CAST(?2 AS REAL))
167 | AND (n.last_info_res_at IS NULL
168 | OR (unixepoch('subsec') - n.last_info_res_at) >= CAST(?2 AS REAL))
169 | `
170 |
171 | type GetNodesWithStaleBootstrapInfoParams struct {
172 | NodeTimeout float64
173 | InfoInterval float64
174 | }
175 |
176 | type GetNodesWithStaleBootstrapInfoRow struct {
177 | Node Node
178 | NodeAddress NodeAddress
179 | }
180 |
181 | func (q *Queries) GetNodesWithStaleBootstrapInfo(ctx context.Context, arg *GetNodesWithStaleBootstrapInfoParams) ([]*GetNodesWithStaleBootstrapInfoRow, error) {
182 | rows, err := q.db.QueryContext(ctx, getNodesWithStaleBootstrapInfo, arg.NodeTimeout, arg.InfoInterval)
183 | if err != nil {
184 | return nil, err
185 | }
186 | defer rows.Close()
187 | var items []*GetNodesWithStaleBootstrapInfoRow
188 | for rows.Next() {
189 | var i GetNodesWithStaleBootstrapInfoRow
190 | if err := rows.Scan(
191 | &i.Node.ID,
192 | &i.Node.CreatedAt,
193 | &i.Node.LastSeenAt,
194 | &i.Node.LastInfoReqAt,
195 | &i.Node.LastInfoResAt,
196 | &i.Node.PublicKey,
197 | &i.Node.Fqdn,
198 | &i.Node.Motd,
199 | &i.Node.Version,
200 | &i.NodeAddress.ID,
201 | &i.NodeAddress.CreatedAt,
202 | &i.NodeAddress.LastSeenAt,
203 | &i.NodeAddress.LastPingAt,
204 | &i.NodeAddress.LastPongAt,
205 | &i.NodeAddress.NodeID,
206 | &i.NodeAddress.Net,
207 | &i.NodeAddress.Ip,
208 | &i.NodeAddress.Port,
209 | &i.NodeAddress.Ptr,
210 | ); err != nil {
211 | return nil, err
212 | }
213 | items = append(items, &i)
214 | }
215 | if err := rows.Close(); err != nil {
216 | return nil, err
217 | }
218 | if err := rows.Err(); err != nil {
219 | return nil, err
220 | }
221 | return items, nil
222 | }
223 |
224 | const getResponsiveNodes = `-- name: GetResponsiveNodes :many
225 | SELECT n.id, n.created_at, n.last_seen_at, n.last_info_req_at, n.last_info_res_at, n.public_key, n.fqdn, n.motd, n.version, a.id, a.created_at, a.last_seen_at, a.last_ping_at, a.last_pong_at, a.node_id, a.net, a.ip, a.port, a.ptr
226 | FROM node n
227 | JOIN node_address a ON a.node_id = n.id
228 | WHERE a.last_pong_at IS NOT NULL
229 | `
230 |
231 | type GetResponsiveNodesRow struct {
232 | Node Node
233 | NodeAddress NodeAddress
234 | }
235 |
236 | func (q *Queries) GetResponsiveNodes(ctx context.Context) ([]*GetResponsiveNodesRow, error) {
237 | rows, err := q.db.QueryContext(ctx, getResponsiveNodes)
238 | if err != nil {
239 | return nil, err
240 | }
241 | defer rows.Close()
242 | var items []*GetResponsiveNodesRow
243 | for rows.Next() {
244 | var i GetResponsiveNodesRow
245 | if err := rows.Scan(
246 | &i.Node.ID,
247 | &i.Node.CreatedAt,
248 | &i.Node.LastSeenAt,
249 | &i.Node.LastInfoReqAt,
250 | &i.Node.LastInfoResAt,
251 | &i.Node.PublicKey,
252 | &i.Node.Fqdn,
253 | &i.Node.Motd,
254 | &i.Node.Version,
255 | &i.NodeAddress.ID,
256 | &i.NodeAddress.CreatedAt,
257 | &i.NodeAddress.LastSeenAt,
258 | &i.NodeAddress.LastPingAt,
259 | &i.NodeAddress.LastPongAt,
260 | &i.NodeAddress.NodeID,
261 | &i.NodeAddress.Net,
262 | &i.NodeAddress.Ip,
263 | &i.NodeAddress.Port,
264 | &i.NodeAddress.Ptr,
265 | ); err != nil {
266 | return nil, err
267 | }
268 | items = append(items, &i)
269 | }
270 | if err := rows.Close(); err != nil {
271 | return nil, err
272 | }
273 | if err := rows.Err(); err != nil {
274 | return nil, err
275 | }
276 | return items, nil
277 | }
278 |
279 | const getUnresponsiveNodes = `-- name: GetUnresponsiveNodes :many
280 | SELECT n.id, n.created_at, n.last_seen_at, n.last_info_req_at, n.last_info_res_at, n.public_key, n.fqdn, n.motd, n.version, a.id, a.created_at, a.last_seen_at, a.last_ping_at, a.last_pong_at, a.node_id, a.net, a.ip, a.port, a.ptr
281 | FROM node n
282 | JOIN node_address a ON a.node_id = n.id
283 | WHERE a.last_pong_at IS NULL
284 | AND a.last_ping_at IS NOT NULL
285 | AND (unixepoch('subsec') - a.last_ping_at) >= CAST(?1 AS REAL)
286 | `
287 |
288 | type GetUnresponsiveNodesRow struct {
289 | Node Node
290 | NodeAddress NodeAddress
291 | }
292 |
293 | func (q *Queries) GetUnresponsiveNodes(ctx context.Context, retryDelay float64) ([]*GetUnresponsiveNodesRow, error) {
294 | rows, err := q.db.QueryContext(ctx, getUnresponsiveNodes, retryDelay)
295 | if err != nil {
296 | return nil, err
297 | }
298 | defer rows.Close()
299 | var items []*GetUnresponsiveNodesRow
300 | for rows.Next() {
301 | var i GetUnresponsiveNodesRow
302 | if err := rows.Scan(
303 | &i.Node.ID,
304 | &i.Node.CreatedAt,
305 | &i.Node.LastSeenAt,
306 | &i.Node.LastInfoReqAt,
307 | &i.Node.LastInfoResAt,
308 | &i.Node.PublicKey,
309 | &i.Node.Fqdn,
310 | &i.Node.Motd,
311 | &i.Node.Version,
312 | &i.NodeAddress.ID,
313 | &i.NodeAddress.CreatedAt,
314 | &i.NodeAddress.LastSeenAt,
315 | &i.NodeAddress.LastPingAt,
316 | &i.NodeAddress.LastPongAt,
317 | &i.NodeAddress.NodeID,
318 | &i.NodeAddress.Net,
319 | &i.NodeAddress.Ip,
320 | &i.NodeAddress.Port,
321 | &i.NodeAddress.Ptr,
322 | ); err != nil {
323 | return nil, err
324 | }
325 | items = append(items, &i)
326 | }
327 | if err := rows.Close(); err != nil {
328 | return nil, err
329 | }
330 | if err := rows.Err(); err != nil {
331 | return nil, err
332 | }
333 | return items, nil
334 | }
335 |
336 | const hasNodeByPublicKey = `-- name: HasNodeByPublicKey :one
337 | SELECT EXISTS(
338 | SELECT 1
339 | FROM node
340 | WHERE public_key = ?
341 | )
342 | `
343 |
344 | func (q *Queries) HasNodeByPublicKey(ctx context.Context, publicKey *PublicKey) (int64, error) {
345 | row := q.db.QueryRowContext(ctx, hasNodeByPublicKey, publicKey)
346 | var column_1 int64
347 | err := row.Scan(&column_1)
348 | return column_1, err
349 | }
350 |
351 | const pingNodeAddress = `-- name: PingNodeAddress :exec
352 | UPDATE node_address
353 | SET last_ping_at = unixepoch('subsec')
354 | WHERE id = ?
355 | `
356 |
357 | func (q *Queries) PingNodeAddress(ctx context.Context, id int64) error {
358 | _, err := q.db.ExecContext(ctx, pingNodeAddress, id)
359 | return err
360 | }
361 |
362 | const pongNodeAddress = `-- name: PongNodeAddress :exec
363 | UPDATE node_address
364 | SET last_pong_at = unixepoch('subsec')
365 | WHERE id = ?
366 | `
367 |
368 | func (q *Queries) PongNodeAddress(ctx context.Context, id int64) error {
369 | _, err := q.db.ExecContext(ctx, pongNodeAddress, id)
370 | return err
371 | }
372 |
373 | const updateNodeAddress = `-- name: UpdateNodeAddress :one
374 | UPDATE node_address
375 | SET node_id = ?, net = ?, ip = ?, port = ?, ptr = ?
376 | WHERE id = ?
377 | RETURNING id, created_at, last_seen_at, last_ping_at, last_pong_at, node_id, net, ip, port, ptr
378 | `
379 |
380 | type UpdateNodeAddressParams struct {
381 | NodeID int64
382 | Net string
383 | Ip string
384 | Port int64
385 | Ptr sql.NullString
386 | ID int64
387 | }
388 |
389 | func (q *Queries) UpdateNodeAddress(ctx context.Context, arg *UpdateNodeAddressParams) (*NodeAddress, error) {
390 | row := q.db.QueryRowContext(ctx, updateNodeAddress,
391 | arg.NodeID,
392 | arg.Net,
393 | arg.Ip,
394 | arg.Port,
395 | arg.Ptr,
396 | arg.ID,
397 | )
398 | var i NodeAddress
399 | err := row.Scan(
400 | &i.ID,
401 | &i.CreatedAt,
402 | &i.LastSeenAt,
403 | &i.LastPingAt,
404 | &i.LastPongAt,
405 | &i.NodeID,
406 | &i.Net,
407 | &i.Ip,
408 | &i.Port,
409 | &i.Ptr,
410 | )
411 | return &i, err
412 | }
413 |
414 | const updateNodeBootstrapInfo = `-- name: UpdateNodeBootstrapInfo :exec
415 | UPDATE node
416 | SET motd = ?, version = ?, last_info_res_at = unixepoch('subsec')
417 | WHERE public_key = ?
418 | `
419 |
420 | type UpdateNodeBootstrapInfoParams struct {
421 | Motd sql.NullString
422 | Version sql.NullInt64
423 | PublicKey *PublicKey
424 | }
425 |
426 | func (q *Queries) UpdateNodeBootstrapInfo(ctx context.Context, arg *UpdateNodeBootstrapInfoParams) error {
427 | _, err := q.db.ExecContext(ctx, updateNodeBootstrapInfo, arg.Motd, arg.Version, arg.PublicKey)
428 | return err
429 | }
430 |
431 | const updateNodeInfoRequestTime = `-- name: UpdateNodeInfoRequestTime :exec
432 | UPDATE node
433 | SET last_info_req_at = ?
434 | WHERE id = ?
435 | `
436 |
437 | type UpdateNodeInfoRequestTimeParams struct {
438 | LastInfoReqAt Time
439 | ID int64
440 | }
441 |
442 | func (q *Queries) UpdateNodeInfoRequestTime(ctx context.Context, arg *UpdateNodeInfoRequestTimeParams) error {
443 | _, err := q.db.ExecContext(ctx, updateNodeInfoRequestTime, arg.LastInfoReqAt, arg.ID)
444 | return err
445 | }
446 |
447 | const upsertNode = `-- name: UpsertNode :one
448 | INSERT INTO node(public_key)
449 | VALUES(?)
450 | ON CONFLICT(public_key)
451 | DO UPDATE SET last_seen_at = unixepoch('subsec')
452 | RETURNING id, created_at, last_seen_at, last_info_req_at, last_info_res_at, public_key, fqdn, motd, version
453 | `
454 |
455 | func (q *Queries) UpsertNode(ctx context.Context, publicKey *PublicKey) (*Node, error) {
456 | row := q.db.QueryRowContext(ctx, upsertNode, publicKey)
457 | var i Node
458 | err := row.Scan(
459 | &i.ID,
460 | &i.CreatedAt,
461 | &i.LastSeenAt,
462 | &i.LastInfoReqAt,
463 | &i.LastInfoResAt,
464 | &i.PublicKey,
465 | &i.Fqdn,
466 | &i.Motd,
467 | &i.Version,
468 | )
469 | return &i, err
470 | }
471 |
472 | const upsertNodeAddress = `-- name: UpsertNodeAddress :one
473 | INSERT INTO node_address(node_id, net, ip, port, ptr)
474 | VALUES(?, ?, ?, ?, ?)
475 | ON CONFLICT(node_id, net, ip, port) DO UPDATE SET last_seen_at = unixepoch('subsec')
476 | RETURNING id, created_at, last_seen_at, last_ping_at, last_pong_at, node_id, net, ip, port, ptr
477 | `
478 |
479 | type UpsertNodeAddressParams struct {
480 | NodeID int64
481 | Net string
482 | Ip string
483 | Port int64
484 | Ptr sql.NullString
485 | }
486 |
487 | func (q *Queries) UpsertNodeAddress(ctx context.Context, arg *UpsertNodeAddressParams) (*NodeAddress, error) {
488 | row := q.db.QueryRowContext(ctx, upsertNodeAddress,
489 | arg.NodeID,
490 | arg.Net,
491 | arg.Ip,
492 | arg.Port,
493 | arg.Ptr,
494 | )
495 | var i NodeAddress
496 | err := row.Scan(
497 | &i.ID,
498 | &i.CreatedAt,
499 | &i.LastSeenAt,
500 | &i.LastPingAt,
501 | &i.LastPongAt,
502 | &i.NodeID,
503 | &i.Net,
504 | &i.Ip,
505 | &i.Port,
506 | &i.Ptr,
507 | )
508 | return &i, err
509 | }
510 |
--------------------------------------------------------------------------------
/internal/db/schema.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE IF NOT EXISTS node (
2 | id INTEGER NOT NULL PRIMARY KEY,
3 | created_at REAL NOT NULL DEFAULT(unixepoch('subsec')),
4 | -- The last time this node's public key was seen in the DHT
5 | last_seen_at REAL NOT NULL DEFAULT(unixepoch('subsec')),
6 | -- The last time we sent a bootstrap info request to this node
7 | last_info_req_at REAL,
8 | -- The last time we received a bootstrap response from this node
9 | last_info_res_at REAL,
10 | public_key TEXT NOT NULL UNIQUE CHECK (LENGTH(public_key) == 64),
11 | fqdn TEXT,
12 | motd TEXT,
13 | version INTEGER CHECK (version > 0 AND version < 1<<32)
14 | ) STRICT;
15 |
16 | CREATE TABLE IF NOT EXISTS node_address (
17 | id INTEGER NOT NULL PRIMARY KEY,
18 | created_at REAL NOT NULL DEFAULT(unixepoch('subsec')),
19 | -- The last time this node address was seen in the DHT
20 | last_seen_at REAL NOT NULL DEFAULT(unixepoch('subsec')),
21 | -- The last time we pinged this node address with a getnodes request
22 | last_ping_at REAL,
23 | -- The last time we received a response from this node address to our getnodes request
24 | last_pong_at REAL,
25 | node_id INTEGER NOT NULL,
26 | net TEXT NOT NULL CHECK (net IN ('udp4', 'udp6', 'tcp4', 'tcp6')),
27 | ip TEXT NOT NULL,
28 | port INTEGER NOT NULL CHECK (port > 0 AND port < 1<<16),
29 | ptr TEXT,
30 | UNIQUE(node_id, net, ip, port),
31 | FOREIGN KEY (node_id) REFERENCES node (id)
32 | ) STRICT;
33 |
--------------------------------------------------------------------------------
/internal/db/sqlc.yml:
--------------------------------------------------------------------------------
1 | version: "2"
2 | sql:
3 | - engine: "sqlite"
4 | queries: "queries.sql"
5 | schema: "schema.sql"
6 | gen:
7 | go:
8 | package: "db"
9 | out: "."
10 | emit_result_struct_pointers: true
11 | emit_params_struct_pointers: true
12 | overrides:
13 | - column: "*.*_at"
14 | go_type:
15 | type: "Time"
16 | - column: "*.public_key"
17 | go_type:
18 | type: "*PublicKey"
19 |
--------------------------------------------------------------------------------
/internal/db/types.go:
--------------------------------------------------------------------------------
1 | package db
2 |
3 | import (
4 | "database/sql/driver"
5 | "encoding/hex"
6 | "fmt"
7 | "time"
8 |
9 | "github.com/alexbakker/tox4go/dht"
10 | )
11 |
12 | type Time time.Time
13 |
14 | // Scan implements the sql.Scanner interface.
15 | func (t *Time) Scan(src any) error {
16 | if src == nil {
17 | *t = Time{}
18 | return nil
19 | }
20 |
21 | f, ok := src.(float64)
22 | if !ok {
23 | return fmt.Errorf("can't scan into db.Time: %T", src)
24 | }
25 |
26 | *t = Time(time.UnixMilli(int64(f * 1000)))
27 | return nil
28 | }
29 |
30 | // Value implements the driver.Valuer interface.
31 | func (t Time) Value() (driver.Value, error) {
32 | return float64(time.Time(t).UnixNano()) / float64(time.Second), nil
33 | }
34 |
35 | type PublicKey dht.PublicKey
36 |
37 | // Scan implements the sql.Scanner interface.
38 | func (k *PublicKey) Scan(src any) error {
39 | s, ok := src.(string)
40 | if !ok {
41 | return fmt.Errorf("can't scan into db.PublicKey: %T", src)
42 | }
43 |
44 | ds, err := hex.DecodeString(s)
45 | if err != nil {
46 | return err
47 | }
48 |
49 | *k = (PublicKey)(ds)
50 | return nil
51 | }
52 |
53 | // Value implements the driver.Valuer interface.
54 | func (k *PublicKey) Value() (driver.Value, error) {
55 | return hex.EncodeToString(k[:]), nil
56 | }
57 |
--------------------------------------------------------------------------------
/internal/models/models.go:
--------------------------------------------------------------------------------
1 | package models
2 |
3 | import (
4 | "fmt"
5 | "net"
6 | "time"
7 |
8 | "github.com/alexbakker/tox4go/dht"
9 | )
10 |
11 | type Node struct {
12 | ID int64 `json:"-"`
13 | CreatedAt time.Time `json:"created_at"`
14 | LastSeenAt time.Time `json:"last_seen_at"`
15 | LastInfoReqAt time.Time `json:"last_info_req_at"`
16 | LastInfoResAt time.Time `json:"last_info_res_at"`
17 | PublicKey *dht.PublicKey `json:"public_key"`
18 | FQDN *string `json:"fqdn"`
19 | MOTD *string `json:"motd"`
20 | Version uint32 `json:"version"`
21 | Addresses []*NodeAddress `json:"addresses"`
22 | }
23 |
24 | type NodeAddress struct {
25 | Node *Node `json:"-"`
26 | ID int64 `json:"-"`
27 | CreatedAt time.Time `json:"created_at"`
28 | LastSeenAt time.Time `json:"last_seen_at"`
29 | LastPingAt time.Time `json:"last_ping_at"`
30 | LastPongAt time.Time `json:"last_pong_at"`
31 | Net string `json:"net"`
32 | IP string `json:"ip"`
33 | Port int `json:"port"`
34 | Ptr *string `json:"ptr"`
35 | }
36 |
37 | func (a *NodeAddress) DHTNode() (*dht.Node, error) {
38 | publicKey := (*dht.PublicKey)(a.Node.PublicKey)
39 |
40 | var nodeType dht.NodeType
41 | if err := nodeType.UnmarshalText([]byte(a.Net)); err != nil {
42 | return nil, fmt.Errorf("convert db node: %w", err)
43 | }
44 |
45 | ip := net.ParseIP(a.IP)
46 | if ip == nil {
47 | return nil, fmt.Errorf("bad ip: %s", a.IP)
48 | }
49 |
50 | return &dht.Node{
51 | Type: nodeType,
52 | PublicKey: publicKey,
53 | IP: ip,
54 | Port: int(a.Port),
55 | }, nil
56 | }
57 |
--------------------------------------------------------------------------------
/internal/repo/repo.go:
--------------------------------------------------------------------------------
1 | package repo
2 |
3 | import (
4 | "context"
5 | "database/sql"
6 | "errors"
7 | "fmt"
8 | "net"
9 | "time"
10 |
11 | "github.com/Tox/ToxStatus/internal/db"
12 | "github.com/Tox/ToxStatus/internal/models"
13 | "github.com/alexbakker/tox4go/dht"
14 | "golang.org/x/exp/maps"
15 | )
16 |
17 | var ErrNotFound = fmt.Errorf("not found: %w", sql.ErrNoRows)
18 |
19 | type NodesRepo struct {
20 | wdb *sql.DB
21 | rq *db.Queries
22 | wq *db.Queries
23 | }
24 |
25 | type nodeAddressCombo struct {
26 | Node db.Node
27 | NodeAddress db.NodeAddress
28 | }
29 |
30 | func New(rdb *sql.DB, wdb *sql.DB) *NodesRepo {
31 | return &NodesRepo{
32 | wdb: wdb,
33 | rq: db.New(rdb),
34 | wq: db.New(wdb),
35 | }
36 | }
37 |
38 | func (r *NodesRepo) GetNodeByPublicKey(ctx context.Context, pk *dht.PublicKey) (*models.Node, error) {
39 | rows, err := r.rq.GetNodeByPublicKey(ctx, (*db.PublicKey)(pk))
40 | if err != nil {
41 | return nil, err
42 | }
43 |
44 | if len(rows) == 0 {
45 | return nil, ErrNotFound
46 | }
47 |
48 | node := convertNode(&rows[0].Node)
49 | for _, row := range rows {
50 | addr := convertNodeAddress(node, &row.NodeAddress)
51 | node.Addresses = append(node.Addresses, addr)
52 | }
53 |
54 | return node, nil
55 | }
56 |
57 | func (r *NodesRepo) HasNodeByPublicKey(ctx context.Context, pk *dht.PublicKey) (bool, error) {
58 | res, err := r.rq.HasNodeByPublicKey(ctx, (*db.PublicKey)(pk))
59 | if err != nil {
60 | return false, err
61 | }
62 |
63 | return res == 1, nil
64 | }
65 |
66 | func (r *NodesRepo) GetNodeCount(ctx context.Context) (int64, error) {
67 | return r.rq.GetNodeCount(ctx)
68 | }
69 |
70 | func (r *NodesRepo) TrackDHTNode(ctx context.Context, node *dht.Node) (*models.Node, error) {
71 | tx, err := r.wdb.Begin()
72 | if err != nil {
73 | return nil, err
74 | }
75 | defer tx.Rollback()
76 |
77 | q := r.wq.WithTx(tx)
78 | dbNode, err := q.UpsertNode(ctx, (*db.PublicKey)(node.PublicKey))
79 | if err != nil {
80 | return nil, fmt.Errorf("upsert node: %w", err)
81 | }
82 |
83 | dbNodeAddr, err := q.UpsertNodeAddress(ctx, &db.UpsertNodeAddressParams{
84 | NodeID: dbNode.ID,
85 | Net: node.Type.Net(),
86 | Ip: node.IP.String(),
87 | Port: int64(node.Port),
88 | })
89 | if err != nil {
90 | return nil, fmt.Errorf("upsert node address: %w", err)
91 | }
92 |
93 | if err := tx.Commit(); err != nil {
94 | return nil, err
95 | }
96 |
97 | res := convertNode(dbNode)
98 | nodeAddr := convertNodeAddress(res, dbNodeAddr)
99 | res.Addresses = append(res.Addresses, nodeAddr)
100 | return res, nil
101 | }
102 |
103 | func (r *NodesRepo) getDHTNodeAddressID(ctx context.Context, node *dht.Node) (int64, error) {
104 | return r.rq.GetNodeAddress(ctx, &db.GetNodeAddressParams{
105 | PublicKey: (*db.PublicKey)(node.PublicKey),
106 | Net: node.Type.Net(),
107 | Ip: node.IP.String(),
108 | Port: int64(node.Port),
109 | })
110 | }
111 |
112 | func (r *NodesRepo) PingDHTNode(ctx context.Context, node *dht.Node) error {
113 | id, err := r.getDHTNodeAddressID(ctx, node)
114 | if err != nil {
115 | if errors.Is(err, sql.ErrNoRows) {
116 | return ErrNotFound
117 | }
118 | return err
119 | }
120 |
121 | return r.rq.PingNodeAddress(ctx, id)
122 | }
123 |
124 | func (r *NodesRepo) PongDHTNode(ctx context.Context, node *dht.Node) error {
125 | id, err := r.getDHTNodeAddressID(ctx, node)
126 | if err != nil {
127 | if errors.Is(err, sql.ErrNoRows) {
128 | return ErrNotFound
129 | }
130 | return err
131 | }
132 |
133 | return r.rq.PongNodeAddress(ctx, id)
134 | }
135 |
136 | func (r *NodesRepo) GetNodesWithStaleBootstrapInfo(ctx context.Context) ([]*models.Node, error) {
137 | rows, err := r.rq.GetNodesWithStaleBootstrapInfo(ctx, &db.GetNodesWithStaleBootstrapInfoParams{
138 | NodeTimeout: (5 * time.Minute).Seconds(),
139 | InfoInterval: (1 * time.Minute).Seconds(),
140 | })
141 | if err != nil {
142 | return nil, err
143 | }
144 |
145 | nodes := make(map[dht.PublicKey]*models.Node)
146 | for _, row := range rows {
147 | node, ok := nodes[dht.PublicKey(*row.Node.PublicKey)]
148 | if !ok {
149 | node = convertNode(&row.Node)
150 | nodes[*node.PublicKey] = node
151 | }
152 |
153 | addr := convertNodeAddress(node, &row.NodeAddress)
154 | node.Addresses = append(node.Addresses, addr)
155 | }
156 |
157 | return maps.Values(nodes), nil
158 | }
159 |
160 | func (r *NodesRepo) UpdateNodeInfoRequestTime(ctx context.Context, addrReqTimes map[int64]time.Time) error {
161 | tx, err := r.wdb.Begin()
162 | if err != nil {
163 | return err
164 | }
165 | defer tx.Rollback()
166 |
167 | q := r.wq.WithTx(tx)
168 | for id, reqTime := range addrReqTimes {
169 | if err := q.UpdateNodeInfoRequestTime(ctx, &db.UpdateNodeInfoRequestTimeParams{
170 | ID: id,
171 | LastInfoReqAt: db.Time(reqTime),
172 | }); err != nil {
173 | return err
174 | }
175 | }
176 |
177 | return tx.Commit()
178 | }
179 |
180 | func (r *NodesRepo) UpdateNodeInfo(ctx context.Context, addr *net.UDPAddr, motd string, version uint32) error {
181 | tx, err := r.wdb.Begin()
182 | if err != nil {
183 | return err
184 | }
185 | defer tx.Rollback()
186 |
187 | var nodeType dht.NodeType
188 | if addr.IP.To4() != nil {
189 | nodeType = dht.NodeTypeUDPIP4
190 | } else {
191 | nodeType = dht.NodeTypeUDPIP6
192 | }
193 |
194 | q := r.wq.WithTx(tx)
195 | node, err := q.GetNodeByInfoResponseAddress(ctx, &db.GetNodeByInfoResponseAddressParams{
196 | InfoReqTimeout: (10 * time.Second).Seconds(),
197 | Net: nodeType.Net(),
198 | Ip: addr.IP.String(),
199 | Port: int64(addr.Port),
200 | })
201 | if err != nil {
202 | return err
203 | }
204 |
205 | if err := q.UpdateNodeBootstrapInfo(ctx, &db.UpdateNodeBootstrapInfoParams{
206 | PublicKey: node.Node.PublicKey,
207 | Motd: newNullString(&motd),
208 | Version: sql.NullInt64{Valid: true, Int64: int64(version)},
209 | }); err != nil {
210 | return err
211 | }
212 |
213 | return tx.Commit()
214 | }
215 |
216 | func (r *NodesRepo) GetResponsiveDHTNodes(ctx context.Context) ([]*dht.Node, error) {
217 | rows, err := r.rq.GetResponsiveNodes(ctx)
218 | if err != nil {
219 | return nil, err
220 | }
221 |
222 | var combos []*nodeAddressCombo
223 | for _, row := range rows {
224 | combos = append(combos, &nodeAddressCombo{
225 | Node: row.Node,
226 | NodeAddress: row.NodeAddress,
227 | })
228 | }
229 |
230 | return convertNodeAddressesToDHTNodes(combos)
231 | }
232 |
233 | func (r *NodesRepo) GetUnresponsiveDHTNodes(ctx context.Context, retryDelay time.Duration) ([]*dht.Node, error) {
234 | rows, err := r.rq.GetUnresponsiveNodes(ctx, retryDelay.Seconds())
235 | if err != nil {
236 | return nil, err
237 | }
238 |
239 | var combos []*nodeAddressCombo
240 | for _, row := range rows {
241 | combos = append(combos, &nodeAddressCombo{
242 | Node: row.Node,
243 | NodeAddress: row.NodeAddress,
244 | })
245 | }
246 |
247 | return convertNodeAddressesToDHTNodes(combos)
248 | }
249 |
250 | func convertNodeAddressesToDHTNodes(rows []*nodeAddressCombo) ([]*dht.Node, error) {
251 | // Only return a single address per node for now
252 | nodes := make(map[dht.PublicKey]*dht.Node)
253 | for _, row := range rows {
254 | // TODO: Replace with models.NodeAddress.DHTNode()
255 | publicKey := (*dht.PublicKey)(row.Node.PublicKey)
256 | if _, ok := nodes[*publicKey]; ok {
257 | continue
258 | }
259 |
260 | var nodeType dht.NodeType
261 | if err := nodeType.UnmarshalText([]byte(row.NodeAddress.Net)); err != nil {
262 | return nil, fmt.Errorf("convert db node: %w", err)
263 | }
264 |
265 | ip := net.ParseIP(row.NodeAddress.Ip)
266 | if ip == nil {
267 | return nil, fmt.Errorf("bad ip: %s", row.NodeAddress.Ip)
268 | }
269 |
270 | nodes[*publicKey] = &dht.Node{
271 | Type: nodeType,
272 | PublicKey: publicKey,
273 | IP: ip,
274 | Port: int(row.NodeAddress.Port),
275 | }
276 | }
277 |
278 | return maps.Values(nodes), nil
279 | }
280 |
281 | func convertNode(dbNode *db.Node) *models.Node {
282 | return &models.Node{
283 | ID: dbNode.ID,
284 | CreatedAt: time.Time(dbNode.CreatedAt),
285 | LastSeenAt: time.Time(dbNode.LastSeenAt),
286 | LastInfoReqAt: time.Time(dbNode.LastInfoReqAt),
287 | LastInfoResAt: time.Time(dbNode.LastInfoResAt),
288 | PublicKey: (*dht.PublicKey)(dbNode.PublicKey),
289 | FQDN: convertNullString(dbNode.Fqdn),
290 | MOTD: convertNullString(dbNode.Motd),
291 | Version: uint32(dbNode.Version.Int64),
292 | }
293 | }
294 |
295 | func convertNodeAddress(node *models.Node, dbNodeAddr *db.NodeAddress) *models.NodeAddress {
296 | return &models.NodeAddress{
297 | Node: node,
298 | ID: dbNodeAddr.ID,
299 | CreatedAt: time.Time(dbNodeAddr.CreatedAt),
300 | LastSeenAt: time.Time(dbNodeAddr.LastSeenAt),
301 | LastPingAt: time.Time(dbNodeAddr.LastPingAt),
302 | LastPongAt: time.Time(dbNodeAddr.LastPongAt),
303 | Net: dbNodeAddr.Net,
304 | IP: dbNodeAddr.Ip,
305 | Port: int(dbNodeAddr.Port),
306 | Ptr: convertNullString(dbNodeAddr.Ptr),
307 | }
308 | }
309 |
310 | func convertNullString(s sql.NullString) *string {
311 | if s.Valid {
312 | return &s.String
313 | }
314 | return nil
315 | }
316 |
317 | func newNullString(s *string) sql.NullString {
318 | res := sql.NullString{Valid: s != nil}
319 | if res.Valid {
320 | res.String = *s
321 | }
322 | return res
323 | }
324 |
--------------------------------------------------------------------------------
/internal/repo/repo_test.go:
--------------------------------------------------------------------------------
1 | package repo
2 |
3 | import (
4 | "bytes"
5 | "context"
6 | "crypto/rand"
7 | "errors"
8 | "net"
9 | "testing"
10 |
11 | "github.com/Tox/ToxStatus/internal/db"
12 | "github.com/Tox/ToxStatus/internal/models"
13 | "github.com/alexbakker/tox4go/dht"
14 | _ "github.com/mattn/go-sqlite3"
15 | )
16 |
17 | var ctx = context.Background()
18 |
19 | func init() {
20 | db.RegisterPragmaHook(2000)
21 | }
22 |
23 | func initRepo(t *testing.T) (repo *NodesRepo, close func() error) {
24 | readConn, writeConn, err := db.OpenReadWrite(ctx, ":memory:", db.OpenOptions{
25 | Params: map[string]string{"cache": "shared"},
26 | })
27 | if err != nil {
28 | t.Fatal(err)
29 | }
30 |
31 | return New(readConn, writeConn), func() error {
32 | var errs []error
33 | if err := readConn.Close(); err != nil {
34 | errs = append(errs, err)
35 | }
36 | if err := writeConn.Close(); err != nil {
37 | errs = append(errs, err)
38 | }
39 | return errors.Join(errs...)
40 | }
41 | }
42 |
43 | func generateNode(t *testing.T) *models.Node {
44 | return &models.Node{
45 | PublicKey: generatePublicKey(t),
46 | }
47 | }
48 |
49 | func generateIP(t *testing.T) net.IP {
50 | bytes := make([]byte, 4)
51 | if _, err := rand.Read(bytes); err != nil {
52 | t.Fatal(err)
53 | }
54 |
55 | return net.IP(bytes)
56 | }
57 |
58 | func generateDHTNode(t *testing.T) *dht.Node {
59 | return &dht.Node{
60 | Type: dht.NodeTypeUDPIP4,
61 | PublicKey: generatePublicKey(t),
62 | IP: generateIP(t),
63 | Port: 33445,
64 | }
65 | }
66 |
67 | func generatePublicKey(t *testing.T) *dht.PublicKey {
68 | ident, err := dht.NewIdentity(dht.IdentityOptions{})
69 | if err != nil {
70 | t.Fatal(err)
71 | }
72 |
73 | return ident.PublicKey
74 | }
75 |
76 | func TestAddNode(t *testing.T) {
77 | repo, close := initRepo(t)
78 | defer close()
79 |
80 | node := generateNode(t)
81 | dbNode, err := repo.wq.UpsertNode(ctx, (*db.PublicKey)(node.PublicKey))
82 | if err != nil {
83 | t.Fatal(err)
84 | }
85 |
86 | if !bytes.Equal(node.PublicKey[:], dbNode.PublicKey[:]) {
87 | t.Fatal("public keys not equal")
88 | }
89 | }
90 |
91 | func TestGetNonExistentNode(t *testing.T) {
92 | repo, close := initRepo(t)
93 | defer close()
94 |
95 | _, err := repo.GetNodeByPublicKey(ctx, generatePublicKey(t))
96 | if !errors.Is(err, ErrNotFound) {
97 | t.Fatalf("expected error: '%v', got: %v", ErrNotFound, err)
98 | }
99 | }
100 |
101 | func TestHasNodeByPublicKey(t *testing.T) {
102 | repo, close := initRepo(t)
103 | defer close()
104 |
105 | node := generateNode(t)
106 | _, err := repo.wq.UpsertNode(ctx, (*db.PublicKey)(node.PublicKey))
107 | if err != nil {
108 | t.Fatal(err)
109 | }
110 |
111 | found, err := repo.HasNodeByPublicKey(ctx, node.PublicKey)
112 | if err != nil {
113 | t.Fatal(err)
114 | }
115 | if !found {
116 | t.Fatal("unable to find node by public key")
117 | }
118 |
119 | found, err = repo.HasNodeByPublicKey(ctx, generatePublicKey(t))
120 | if err != nil {
121 | t.Fatal(err)
122 | }
123 | if found {
124 | t.Fatal("found non-existent node by public key")
125 | }
126 | }
127 |
128 | func TestPongNonExistentNode(t *testing.T) {
129 | repo, close := initRepo(t)
130 | defer close()
131 |
132 | pk := generatePublicKey(t)
133 | _, err := repo.wq.UpsertNode(ctx, (*db.PublicKey)(pk))
134 | if err != nil {
135 | t.Fatal(err)
136 | }
137 |
138 | node := generateDHTNode(t)
139 | if err := repo.PongDHTNode(ctx, node); !errors.Is(err, ErrNotFound) {
140 | t.Fatalf("expected error: '%v', got: %v", ErrNotFound, err)
141 | }
142 | }
143 |
--------------------------------------------------------------------------------
/internal/version/version.go:
--------------------------------------------------------------------------------
1 | package version
2 |
3 | import (
4 | "fmt"
5 | "strconv"
6 | "time"
7 | )
8 |
9 | var (
10 | Number string
11 | Revision string
12 | RevisionTime string
13 | )
14 |
15 | func String() (string, error) {
16 | if Number == "" {
17 | return "toxstatus: development build", nil
18 | }
19 |
20 | return fmt.Sprintf("toxstatus: v%s-%s", Number, Revision), nil
21 | }
22 |
23 | func HumanRevisionTime() string {
24 | secs, err := strconv.ParseInt(RevisionTime, 10, 64)
25 | if err != nil {
26 | return ""
27 | }
28 |
29 | return time.Unix(secs, 0).UTC().String()
30 | }
31 |
--------------------------------------------------------------------------------