├── .gitignore
├── LICENSE
├── README.md
├── go.work
├── go.work.sum
├── midware-dokuwiki
├── DefaultIniType.template
├── DefaultNonIni.template
├── RegBeacon.dokuwiki
├── conf.go
├── db.go
├── doku.go
├── fileio.go
├── globals.go
├── go.mod
├── go.sum
├── kml.go
├── main.go
├── output.go
└── set.go
└── server
├── auth.go
├── conf.go
├── db.go
├── fileio.go
├── globals.go
├── go.mod
├── go.sum
├── main.go
├── msg.go
└── output.go
/.gitignore:
--------------------------------------------------------------------------------
1 | # If you prefer the allow list template instead of the deny list, see community template:
2 | # https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
3 | #
4 | # Binaries for programs and plugins
5 | *.exe
6 | *.exe~
7 | *.dll
8 | *.so
9 | *.dylib
10 |
11 | # Test binary, built with `go test -c`
12 | *.test
13 |
14 | # Output of the go coverage tool, specifically when used with LiteIDE
15 | *.out
16 |
17 | # Dependency directories (remove the comment below to include it)
18 | # vendor/
19 |
20 | # Go workspace file
21 | go.work
22 |
23 | # Files for testing
24 | server/akbp.db
25 | server/testB.db
26 | server/testA.conf
27 | server/testB.conf
28 | midware-dokuwiki/example.kml
--------------------------------------------------------------------------------
/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
637 | by 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 | .
662 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
9 |
10 | # AKBP - [A]nti [K]idnapping [B]eacon [P]roject
11 |
12 | Anti Kidnapping Beacon Project是受国际搜救卫星组织的信标启发的, 可用智能手机或其他可直接或间接连接到互联网的硬件作为信标的, 可自行架设服务器的, 反对绑架/虐待型"教育学校/矫正学校"的项目.
13 | 愿每个人都可以不受绑架及虐待!
14 | 本项目的姊妹项目: [PanDefenseProject](https://github.com/FunctionSir/PanDefenseProject).
15 |
16 | ## 务必注意
17 |
18 | 现在正处于测试阶段, 仅可**非常有限地**用于生产环境.
19 |
20 | ## 约束
21 |
22 | 时间戳的单位为毫秒(ms), 若选择其他单位可能会造成时间解析错误的问题.
23 | Server ID只允许含有A\~Z,a\~z,0\~9,以及下划线\_, 且不得超过16个字符.
24 | Beacon ID只允许含有A\~Z,a\~z,0\~9,以及下划线\_, 以及用于标识来源服务器的@和点号(.), 且不得超过32个字符.
25 | 要想在DW即可解密加密信息, 那么加密方式只能为"aes-256-cbc-pbkdf2".
26 |
27 | ## 为什么有了这个项目? 以及其他
28 |
29 | 众所周知, 某些所谓"网戒中心"/"教育学校"/"问题孩子学校"(勿对号入座)实在是恶心至极, 它们通过各种反人类的手段以求可以将一个所谓"问题少年"变成傀儡.
30 | 这些孩子中有许多其实并不是什么问题少年, 而是只是有自己的思考, 没有完全听从家长的命令而已...
31 | 这些被"教育"的未成年人往往深受其害. 而一旦他们被从家中带走, 比如被冒充成警察的所谓"教官"给带走, 未来会发生什么也就可以猜个大差不离了...
32 | 如果是性少数人群, 且"家人"看不惯的话, 那么严酷的扭转"治疗"恐怕在所难免...
33 | 找的对象不符合熊"家长"的心意被送进来的? 也不是没有.
34 | 喜欢网络游戏? 只要"家长"想, 那也是会被带走的!
35 | 这类机构中的一些甚至不会核实"家长"身份的真实性, 且即使是成年人也来者不拒, 完全就是"你给钱, 我绑人".
36 | 总之, 这些机构干的事儿, 至少是把受害者从家里带到"学校"这个过程, 很难不让我联想到"Kidnapping", 也就是说绑架/诱拐/劫持/拐骗...
37 | 很多时候一些有志之士想要救援, 然而, 他们可能会绝望的发现, 根本不知道人在哪儿...
38 | 受到国际搜救卫星组织的启发, 这个信标项目便被搞出来了. 信息越多, 则诸如救援和取证, 都可能会更容易些.
39 | 且相较于手机, 或者电话手表, 这个项目提供了较为简单且完全开放的协议, 以及完全可self-host的自由软件的服务端, 且开源免费.
40 | 多亏了aes-256-cbc和pbkdf2, 您可以发送加密信息, 除了知道密钥的人可以解密之外, 他人很难知道您的位置(甚至是服务器运营者).
41 | 由于使用DokuWiki, 以及"Encrypted Passwords Plugin", 使用aes-256-cbc和pbkdf2的加密信息可以在网页解密(解密是在本地发生的, 服务器不知道解密后的内容).
42 | 不过若不使用加密, 则可以更方便服务器上有权限的人员找到您(那样的话单点的地图以及路径的KML生成就可用了).
43 |
44 | ## 依赖项及注意事项
45 |
46 | 仅部署服务器: 除了SQLite 3, 基本无更多的依赖项.
47 | 使用DokuWiki中间件成为一个Hub: 需要SQLite 3, DokuWiki, PHP, 以及DokuWiki的Encrypted Passwords Plugin, 和iframe plugin.
48 | 建议您使用Linux系统, 因为目前本项目的所有程序均是在Linux(具体到发行版的话是Arch Linux)系统中开发与测试的.
49 | 当然, Windows也是可以的, 但目前未经过任何测试.
50 | 强烈建议您在容器中运行这套程序.
51 | 本套程序中的DokuWiki中间件, 包含大量文件操作的部分, 写入量可能较大, 建议您将临时文件夹放在内存盘内, 或即使坏掉也无所谓的SSD中. 至于HDD, 也是可以的.
52 | 不建议您将临时文件夹放在SD卡中.
53 | 您也许应该及时清理您的数据库, 在保持数据库的清爽的同时, 还可以防止上述中间件的读写量过大或是运行过于缓慢.
54 | 为保证注册请求在被处理后删除, 请保证您的程序以http或其他有权限删除注册请求对应文件的用户运行.
55 | 除了封闭的, 只监听127.0.0.1的测试之外, 不建议您使用root用户运行此套程序.
56 |
57 | ## 我想给我搞一个, 防范一般意义上的绑架, 或是其他紧急情况, 可以么?
58 |
59 | 完全可以.
60 |
61 | ## 我... 需要购买特殊硬件?
62 |
63 | 不, 你完全不需要. 国际搜救卫星组织也许需要你拥有一个专用的信标, 而这个项目不同.
64 | 很多人没有钱买一个自己的信标, 也有很多人如果即使买了一个信标也到不了手里(父母可能会拦截其快件)...
65 | 所以, 这个项目的目标之一, 就是构建一套可以用智能手机当作信标的系统. 当然, 如果你想的话, 电脑也不是不可以, 但是一些信息恐怕就不能自动获取了.
66 |
67 | ## 我可以构建属于我自己的硬件信标么?
68 |
69 | 你当然可以. 这套系统是基于HTTP(S)的, 目前可以用HTTP请求来发送相关信息. 也就是说, 不管你是树莓派, 还是ESP32, 还是其他东西, 只要能进行特定请求, 就可以将信息送过来, 也就是说, 可以作为信标.
70 |
--------------------------------------------------------------------------------
/go.work:
--------------------------------------------------------------------------------
1 | go 1.23
2 |
3 | use (
4 | ./server
5 | ./midware-dokuwiki
6 | )
7 |
--------------------------------------------------------------------------------
/go.work.sum:
--------------------------------------------------------------------------------
1 | github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
2 | golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
3 | golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
4 | golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
5 |
--------------------------------------------------------------------------------
/midware-dokuwiki/DefaultIniType.template:
--------------------------------------------------------------------------------
1 | ~~NOTOC~~
2 | ====== %ROW_ID%-%BEACON_ID%-%EVENT_ID%-%TIMESTAMP_MS% ======
3 | ===== Basic Info =====
4 | BEACON: %BEACON_ID% \\
5 | EVENT: %EVENT_ID% \\
6 | TIME: %TIME% \\
7 | FROM: %ORIGIN% \\
8 | ===== Decoded Message - Basic =====
9 | ENCRYPTION: %ENC_TYPE% \\
10 | POSITION: Lat=%LAT% Lon=%LON% Asl=%ASL% \\
11 | {{url>%OSM_EMBED_WITH_MARKER%}} \\
12 | ** Rapid Ops: [[%OSM_MAP_WITH_MARKER%|[Open In OSM]]] [[%OSM_TO%|[Nav In OSM]]] [[%OSM_QUERY%|[Query Nearby In OSM]]] [[?do=edit|[View Page Source Code]]] ** \\
13 | ===== Decoded Message - Extra =====
14 | %EXTRA%
15 | ===== Raw Message =====
16 |
17 | %RAW_MSG%
18 |
19 | ===== Midware Info =====
20 | ROWID: %ROW_ID% \\
21 | VERSION: %VERSION% (%CODENAME%) \\
22 | OS: %GOOS% \\
23 | ARCH: %GOARCH% \\
24 | ----
25 | // Generated by AKBP Midware for DokuWiki //
--------------------------------------------------------------------------------
/midware-dokuwiki/DefaultNonIni.template:
--------------------------------------------------------------------------------
1 | ~~NOTOC~~
2 | ====== %ROW_ID%-%BEACON_ID%-%EVENT_ID%-%TIMESTAMP_MS% ======
3 | ===== Hint =====
4 | This is not an ini-type message. \\
5 | ===== Basic Info =====
6 | BEACON: %BEACON_ID% \\
7 | EVENT: %EVENT_ID% \\
8 | TIME: %TIME% \\
9 | FROM: %ORIGIN% \\
10 | ** Rapid Op: [[?do=edit|[View Page Source Code]]] ** \\
11 | ===== Raw Message =====
12 |
13 | %RAW_MSG%
14 |
15 | ===== Midware Info =====
16 | ROWID: %ROW_ID% \\
17 | VERSION: %VERSION% (%CODENAME%) \\
18 | OS: %GOOS% \\
19 | ARCH: %GOARCH% \\
20 | ----
21 | // Generated by AKBP Midware for DokuWiki //
--------------------------------------------------------------------------------
/midware-dokuwiki/RegBeacon.dokuwiki:
--------------------------------------------------------------------------------
1 | ====== REGISTER YOUR BEACON ======
2 |
--------------------------------------------------------------------------------
/midware-dokuwiki/conf.go:
--------------------------------------------------------------------------------
1 | /*
2 | * @Author: FunctionSir
3 | * @License: AGPLv3
4 | * @Date: 2024-09-14 00:23:17
5 | * @LastEditTime: 2024-10-07 17:01:53
6 | * @LastEditors: FunctionSir
7 | * @Description: Config related.
8 | * @FilePath: /AKBP/midware-dokuwiki/conf.go
9 | */
10 |
11 | package main
12 |
13 | import (
14 | "strconv"
15 | "strings"
16 |
17 | "gopkg.in/ini.v1"
18 | )
19 |
20 | func LoadConf() {
21 | if ConfLoaded {
22 | LogFatalln("Error: reload config might cause data races.")
23 | }
24 | conf, err := ini.Load(ConfigFile)
25 | if err != nil {
26 | LogFatalln("Error occurred when loading conf file \"" + ConfigFile + "\": " + strings.Trim(err.Error(), "\n") + ".")
27 | }
28 | if !conf.HasSection("midware-dokuwiki") {
29 | LogWarnln("Warning: config file \"" + ConfigFile + "\" has no section \"midware-dokuwiki\", default values about midware-dokuwiki will be applied for everything.")
30 | return
31 | }
32 | section := conf.Section("midware-dokuwiki")
33 | if section.HasKey("MainDB") {
34 | Db = section.Key("MainDB").String()
35 | } else {
36 | LogWarnln("Seems like you didn't specify the DB you want to use, the default value \"" + DEFAULT_DB + "\" will be applied.")
37 | }
38 | if section.HasKey("TmpDir") {
39 | TmpDir = section.Key("TmpDir").String()
40 | } else {
41 | LogWarnln("Seems like you didn't specify the tmp dir you want to use, the gened value \"" + TmpDir + "\" will be applied.")
42 | }
43 | if section.HasKey("UpdateGap") {
44 | UpdGap, err = strconv.Atoi(section.Key("UpdateGap").String())
45 | if err != nil || UpdGap <= 0 {
46 | LogFatalln("Can not convert value of key \"UpdateGap\" from string to a positive int. Is this key has a wrong value?")
47 | }
48 | } else {
49 | LogWarnln("Seems like you didn't specify the update gap, use default value 10.")
50 | }
51 | if section.HasKey("NonIniTemplate") {
52 | NonIniTemplate = section.Key("NonIniTemplate").String()
53 | } else {
54 | LogWarnln("Seems like you didn't specify template for non-ini messages, use the default one.")
55 | }
56 | if section.HasKey("IniTypeTemplate") {
57 | IniTypeTemplate = section.Key("IniTypeTemplate").String()
58 | } else {
59 | LogWarnln("Seems like you didn't specify template for ini messages, use the default one.")
60 | }
61 | if section.HasKey("NsForAll") {
62 | AllEntriesNs = section.Key("NsForAll").String()
63 | } else {
64 | LogWarnln("Seems like you didn't specify the namespace linked to tmpdir/dw-ns/all, use the default one.")
65 |
66 | }
67 | if section.HasKey("NsForEvents") {
68 | EventsNs = section.Key("NsForEvents").String()
69 | } else {
70 | LogWarnln("Seems like you didn't specify the namespace linked to tmpdir/dw-ns/evnets, use the default one.")
71 |
72 | }
73 | if section.HasKey("NsForKmls") {
74 | KmlsNs = section.Key("NsForKmls").String()
75 | } else {
76 | LogWarnln("Seems like you didn't specify the namespace linked to tmpdir/dw-m/kmls, use the default one.")
77 | }
78 | if !section.HasKey("Domain") {
79 | LogFatalln("You need to specify your domain.")
80 | }
81 | Domain = strings.TrimSpace(section.Key("Domain").String())
82 | if !section.HasKey("UsersDir") {
83 | LogFatalln("You need to specify where is users' dir.")
84 | }
85 | UsersDir = strings.TrimSpace(section.Key("UsersDir").String())
86 | if !DirExists(UsersDir) {
87 | LogFatalln("Users' dir not exists or it is not a dir.")
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/midware-dokuwiki/db.go:
--------------------------------------------------------------------------------
1 | /*
2 | * @Author: FunctionSir
3 | * @License: AGPLv3
4 | * @Date: 2024-09-14 21:33:33
5 | * @LastEditTime: 2024-10-06 19:42:51
6 | * @LastEditors: FunctionSir
7 | * @Description: DB related.
8 | * @FilePath: /AKBP/midware-dokuwiki/db.go
9 | */
10 |
11 | package main
12 |
13 | import (
14 | "crypto/sha512"
15 | "database/sql"
16 | "fmt"
17 | "strings"
18 |
19 | "github.com/google/uuid"
20 | _ "github.com/mattn/go-sqlite3"
21 | )
22 |
23 | // '0'~'9', 'A'~'Z', '_', 'a'~'z' are considered as safe chars.
24 | func chrIsSafe(ch rune) bool {
25 | return strings.ContainsRune("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz", ch)
26 | }
27 |
28 | func CalcHash(key string, salt string) string {
29 | return fmt.Sprintf("%X", sha512.Sum512([]byte(key+salt)))
30 | }
31 |
32 | // A kind of protection before SQL actions.
33 | func ChkStrWithExit(str *string) {
34 | for _, x := range *str {
35 | if !chrIsSafe(x) {
36 | LogFatalln("Unsafe char found in string \"" + *str + "\" by strict str checker for DB.")
37 | }
38 | }
39 | }
40 |
41 | // Chk str, but do not exit.
42 | func ChkStrNoExit(str *string) bool {
43 | for _, x := range *str {
44 | if !chrIsSafe(x) {
45 | return false
46 | }
47 | }
48 | return true
49 | }
50 |
51 | // Open DB using DB file specified in global var.
52 | func DbOpen() *sql.DB {
53 | db, err := sql.Open("sqlite3", Db)
54 | if err != nil {
55 | LogFatalln("Error occurred when opening the database: " + strings.Trim(err.Error(), "\n"))
56 | }
57 | return db
58 | }
59 |
60 | // Prepare a query.
61 | func DbPrepare(db *sql.DB, query string) *sql.Stmt {
62 | stmt, err := db.Prepare(query)
63 | if err != nil {
64 | LogFatalln("Error occurred when preparing the SQL statement: " + strings.Trim(err.Error(), "\n"))
65 | }
66 | return stmt
67 | }
68 |
69 | func QueryRecs() *sql.Rows {
70 | db := DbOpen()
71 | defer db.Close()
72 | stmt := DbPrepare(db, "SELECT ROWID,BID,EID,TS,MSG,ORIGIN FROM RECORDS ORDER BY TS;")
73 | rows, err := stmt.Query()
74 | if err != nil {
75 | LogWarnln("An error occurred when performing a query.")
76 | return nil
77 | }
78 | return rows
79 | }
80 |
81 | func IsBidExists(bid string) bool {
82 | db := DbOpen()
83 | defer db.Close()
84 | stmt := DbPrepare(db, "SELECT ROWID FROM BEACONS WHERE ID=?")
85 | var tmp int
86 | err := stmt.QueryRow(bid).Scan(&tmp)
87 | return err == nil
88 | }
89 |
90 | func IsSaltExists(salt string) bool {
91 | db := DbOpen()
92 | defer db.Close()
93 | stmt := DbPrepare(db, "SELECT * FROM BEACONS WHERE SALT=?")
94 | err := stmt.QueryRow(salt)
95 | return err == nil
96 | }
97 |
98 | func RegBeacon(bid string, key string, note string) bool {
99 | db := DbOpen()
100 | defer db.Close()
101 | stmt := DbPrepare(db, "INSERT INTO BEACONS VALUES(?,?,?,?)")
102 | salt := GenSalt()
103 | hash := CalcHash(key, salt)
104 | _, err := stmt.Exec(bid, salt, hash, note)
105 | return err == nil
106 | }
107 |
108 | func GenSalt() string {
109 | salt := uuid.New().String()
110 | for IsSaltExists(salt) {
111 | salt = uuid.New().String()
112 | }
113 | return salt
114 | }
115 |
--------------------------------------------------------------------------------
/midware-dokuwiki/doku.go:
--------------------------------------------------------------------------------
1 | /*
2 | * @Author: FunctionSir
3 | * @License: AGPLv3
4 | * @Date: 2024-09-30 23:07:57
5 | * @LastEditTime: 2024-10-07 18:03:46
6 | * @LastEditors: FunctionSir
7 | * @Description: -
8 | * @FilePath: /AKBP/midware-dokuwiki/doku.go
9 | */
10 | package main
11 |
12 | import (
13 | "fmt"
14 | "net/url"
15 | "os"
16 | "path/filepath"
17 | "runtime"
18 | "strconv"
19 | "strings"
20 | "time"
21 |
22 | "gopkg.in/ini.v1"
23 | )
24 |
25 | type NotIniPart struct {
26 | RowId int
27 | Bid string
28 | Eid string
29 | Ts int
30 | Msg string
31 | Origin string
32 | IsIni bool
33 | }
34 |
35 | type DecodedPart struct {
36 | Bid string
37 | Eid string
38 | EncType string
39 | Pos Coord
40 | Extra string
41 | }
42 |
43 | func coordToStrLatFirstForHuman(coord *Coord, encrypted bool) string {
44 | markL := ""
45 | markR := ""
46 | if encrypted {
47 | markL = ""
48 | markR = ""
49 | }
50 | return markL + coord.Lat + markR + ", " + markL + coord.Lon + markR + ", " + markL + coord.Asl + markR
51 | }
52 |
53 | func genBbox(point *Coord) string {
54 | tmpLat, err := strconv.ParseFloat(point.Lat, 64)
55 | if err != nil {
56 | return ""
57 | }
58 | tmpLon, err := strconv.ParseFloat(point.Lon, 64)
59 | if err != nil {
60 | return ""
61 | }
62 | return fmt.Sprintf("%f,%f,%f,%f", tmpLon-GEN_BBOX_LON_OFFSET, tmpLat-GEN_BBOX_LAT_OFFSET, tmpLon+GEN_BBOX_LON_OFFSET, tmpLat+GEN_BBOX_LAT_OFFSET)
63 | }
64 |
65 | func NonIniFillTemplate(entry *NotIniPart, template *string) string {
66 | result := *template
67 | result = strings.Replace(result, "%BEACON_ID%", entry.Bid, -1)
68 | result = strings.Replace(result, "%EVENT_ID%", entry.Eid, -1)
69 | result = strings.Replace(result, "%TIMESTAMP_MS%", strconv.Itoa(entry.Ts), -1)
70 | result = strings.Replace(result, "%TIME%", time.UnixMilli(int64(entry.Ts)).Format(TimeTemplate), -1)
71 | result = strings.Replace(result, "%ORIGIN%", entry.Origin, -1)
72 | result = strings.Replace(result, "%VERSION%", VER, -1)
73 | result = strings.Replace(result, "%CODENAME%", CODENAME, -1)
74 | result = strings.Replace(result, "%GOOS%", runtime.GOOS, -1)
75 | result = strings.Replace(result, "%GOARCH%", runtime.GOARCH, -1)
76 | result = strings.Replace(result, "%ROW_ID%", strconv.Itoa(entry.RowId), -1)
77 | result = strings.Replace(result, "%RAW_MSG%", entry.Msg, -1)
78 | return result
79 | }
80 |
81 | func IniFillTemplate(notIni *NotIniPart, decoded *DecodedPart, template *string) string {
82 | result := *template
83 | result = NonIniFillTemplate(notIni, &result)
84 | result = strings.Replace(result, "%ENC_TYPE%", decoded.EncType, -1)
85 | encMsgLabelL := ""
86 | encMsgLabelR := ""
87 | guardL := "\n"
88 | guardR := "\n"
89 | // This is why you need "Encrypted Passwords Plugin" installed on dokuwiki.
90 | if decoded.EncType == "aes-256-cbc-pbkdf2" {
91 | encMsgLabelL = ""
92 | encMsgLabelR = ""
93 | guardL = ""
94 | guardR = ""
95 | }
96 | result = strings.Replace(result, "%LON%", encMsgLabelL+decoded.Pos.Lon+encMsgLabelR, -1)
97 | result = strings.Replace(result, "%LAT%", encMsgLabelL+decoded.Pos.Lat+encMsgLabelR, -1)
98 | result = strings.Replace(result, "%ASL%", encMsgLabelL+decoded.Pos.Asl+encMsgLabelR, -1)
99 | result = strings.Replace(result, "%EXTRA%", guardL+encMsgLabelL+decoded.Extra+encMsgLabelR+guardR, -1)
100 | // If position is encrypted or illegal, replace OSM related keys with "" (Empty string).
101 | if decoded.EncType != "plaintext" || decoded.Pos.Lat == "" || decoded.Pos.Lat == "nil" || decoded.Pos.Lon == "" || decoded.Pos.Lon == "nil" {
102 | osmKeys := []string{"%OSM_TO%", "%OSM_QUERY%", "%OSM_MAP_WITH_MARKER%", "%OSM_EMBED_WITH_MARKER%"}
103 | for _, x := range osmKeys {
104 | result = strings.Replace(result, x, "", -1)
105 | }
106 | return result
107 | }
108 | result = strings.Replace(result, "%OSM_TO%", fmt.Sprintf(
109 | "https://www.openstreetmap.org/directions?to=%s%%2C%s#map=%s/%s/%s",
110 | decoded.Pos.Lat, decoded.Pos.Lon, OSM_ZOOM, decoded.Pos.Lat, decoded.Pos.Lon), -1)
111 | result = strings.Replace(result, "%OSM_QUERY%", fmt.Sprintf(
112 | "https://www.openstreetmap.org/query?lat=%s&lon=%s#map=%s/%s/%s",
113 | decoded.Pos.Lat, decoded.Pos.Lon, OSM_ZOOM, decoded.Pos.Lat, decoded.Pos.Lon), -1)
114 | result = strings.Replace(result, "%OSM_MAP_WITH_MARKER%", fmt.Sprintf(
115 | "https://www.openstreetmap.org/?mlat=%s&mlon=%s#map=%s/%s/%s",
116 | decoded.Pos.Lat, decoded.Pos.Lon, OSM_ZOOM, decoded.Pos.Lat, decoded.Pos.Lon), -1)
117 | result = strings.Replace(result, "%OSM_EMBED_WITH_MARKER%", fmt.Sprintf(
118 | "https://www.openstreetmap.org/export/embed.html?bbox=%s&marker=%s%%2C%s",
119 | url.QueryEscape(genBbox(&decoded.Pos)), decoded.Pos.Lat, decoded.Pos.Lon), -1)
120 | return result
121 | }
122 |
123 | func GenAllSummary(file string, notIniPart []NotIniPart) {
124 | content := ""
125 | content += "====== Summary Page Of All Messages The Server Stored ======\n"
126 | content += fmt.Sprintf("** Total Messages Received: %d. ** \\\\\n", len(notIniPart))
127 | content += "** The latest one is the top one in this table. ** \\\\\n"
128 | content += "| INI | Row ID | Beacon ID | Event ID | Reported Time | Received From | Details |\n"
129 | for i := len(notIniPart) - 1; i >= 0; i-- {
130 | x := ¬IniPart[i]
131 | isIniStr := "❌"
132 | if x.IsIni {
133 | isIniStr = "✔️"
134 | }
135 | content += fmt.Sprintf("| %s | %s | %s | %s | %s | %s | [[%s|GO ➡️]] |\n",
136 | isIniStr, strconv.Itoa(x.RowId), x.Bid, x.Eid,
137 | time.UnixMilli(int64(x.Ts)).Format(TimeTemplate), x.Origin,
138 | strings.ToLower(strconv.Itoa(x.RowId)+"-"+x.Bid+"-"+x.Eid+"-"+strconv.Itoa(x.Ts)))
139 | }
140 | content += "** The gap between two updates is " + strconv.Itoa(UpdGap) + " second(s). ** \\\\\n"
141 | content += "\n----\n// Generated by AKBP Midware for DokuWiki //"
142 | os.WriteFile(file, []byte(content), 0644)
143 | }
144 |
145 | func GenByEvent(file string, event string, pages []string, indexNotIni []int, indexDecoded []int, notIniPart []NotIniPart, decodedPart []DecodedPart) {
146 | content := ""
147 | content += "~~NOTOC~~\n"
148 | content += "====== Summary Page Of Event " + event + " ======\n"
149 | content += "===== Basic Info =====\n"
150 | content += fmt.Sprintf("Reported Time: From: %s To: %s \\\\\n",
151 | time.UnixMilli(int64(notIniPart[indexNotIni[0]].Ts)).Format(TimeTemplate),
152 | time.UnixMilli(int64(notIniPart[indexNotIni[len(indexDecoded)-1]].Ts)).Format(TimeTemplate))
153 | content += fmt.Sprintf("Total related pages (received messages): %d \\\\\n", len(pages))
154 | content += "===== KML Download =====\n"
155 | content += fmt.Sprintf("** -> Download KML File Of Route We Decoded And Assembled: {{%s:%s.kml| 💾 Download}} <- ** \\\\\n", KmlsNs, strings.ToLower(event))
156 | content += "P.S. KML file might be unavailable if there are no points we can decode in some cases. \\\\\n"
157 | content += "===== Related Pages =====\n"
158 | content += "** The latest one is the top one in the table. ** \\\\\n"
159 | content += "| INI | Row ID | Encrypted | Decoded Position | Reported Time | Received From | Details |\n"
160 | for i := len(pages) - 1; i >= 0; i-- {
161 | tmpNotIni := ¬IniPart[indexNotIni[i]]
162 | isIniStr := "❌"
163 | isEncrypted := "❓"
164 | decodePos := "UNABLE TO DECODE"
165 | if tmpNotIni.IsIni && indexDecoded[i] >= 0 {
166 | isIniStr = "✔️"
167 | tmpDecoded := &decodedPart[indexDecoded[i]]
168 | if tmpDecoded.EncType != "plaintext" {
169 | isEncrypted = "✔️"
170 | } else {
171 | isEncrypted = "❌"
172 | }
173 | tmpEencrypted := false
174 | if tmpDecoded.EncType == "aes-256-cbc-pbkdf2" {
175 | tmpEencrypted = true
176 | }
177 | decodePos = coordToStrLatFirstForHuman(&tmpDecoded.Pos, tmpEencrypted)
178 | }
179 | rowId := tmpNotIni.RowId
180 | reportedTime := time.UnixMilli(int64(tmpNotIni.Ts)).Format(TimeTemplate)
181 | content += fmt.Sprintf("| %s | %d | %s | %s | %s | %s | [[%s:%s|GO ➡️]] |\n",
182 | isIniStr, rowId, isEncrypted, decodePos, reportedTime, tmpNotIni.Origin, AllEntriesNs, pages[i])
183 | }
184 | content += "** The gap between two updates is " + strconv.Itoa(UpdGap) + " second(s). ** \\\\\n"
185 | content += "\n----\n// Generated by AKBP Midware for DokuWiki //"
186 | os.WriteFile(file, []byte(content), 0644)
187 | }
188 |
189 | func GenByEventSummary(file string, events []string, ts []int, msgCnt []int) {
190 | content := ""
191 | content += "====== Summary Page Of All Events The Server Stored ======\n"
192 | content += fmt.Sprintf("** Total Events Calculated: %d. ** \\\\\n", len(events))
193 | content += "** The latest one is the top one in this table. ** \\\\\n"
194 | content += "| # | Event | Last Message | Total Received | Details |\n"
195 | for i := range events {
196 | content += fmt.Sprintf("| %d | %s | %s | %d | [[%s:%s|GO ➡️]]|\n",
197 | i+1, events[i], time.UnixMilli(int64(ts[i])).Format(TimeTemplate), msgCnt[i], EventsNs, strings.ToLower(events[i]))
198 | }
199 | content += "** The gap between two updates is " + strconv.Itoa(UpdGap) + " second(s). ** \\\\\n"
200 | content += "\n----\n// Generated by AKBP Midware for DokuWiki //"
201 | os.WriteFile(file, []byte(content), 0644)
202 | }
203 |
204 | func ProcessRegRequests(dir string) (int, int) {
205 | regCnt := 0
206 | processCnt := 0
207 | files := make([]string, 0)
208 | filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
209 | if err != nil {
210 | return nil
211 | }
212 | if info.IsDir() {
213 | return nil
214 | }
215 | if info.Name() == "akbp_beacon_registration.txt" {
216 | files = append(files, path)
217 | }
218 | return nil
219 | })
220 | for _, x := range files {
221 | processCnt++
222 | conf, err := ini.Load(x)
223 | if err != nil {
224 | continue
225 | }
226 | sec := conf.Section("DEFAULT")
227 | if !sec.HasKey("BID") || !sec.HasKey("KEY") {
228 | continue
229 | }
230 | bid := strings.TrimSpace(sec.Key("BID").String())
231 | if !ChkStrNoExit(&bid) || len(bid) > 32 {
232 | continue
233 | }
234 | bid += "@" + Domain
235 | key := strings.TrimSpace(sec.Key("KEY").String())
236 | note := strings.TrimSpace(sec.Key("NOTE").String())
237 | if IsBidExists(bid) {
238 | continue
239 | }
240 | if RegBeacon(bid, key, note) {
241 | os.Remove(x)
242 | regCnt++
243 | }
244 | }
245 | return processCnt, regCnt
246 | }
247 |
--------------------------------------------------------------------------------
/midware-dokuwiki/fileio.go:
--------------------------------------------------------------------------------
1 | /*
2 | * @Author: FunctionSir
3 | * @License: AGPLv3
4 | * @Date: 2024-09-24 23:00:35
5 | * @LastEditTime: 2024-10-05 23:07:59
6 | * @LastEditors: FunctionSir
7 | * @Description: -
8 | * @FilePath: /AKBP/midware-dokuwiki/fileio.go
9 | */
10 | package main
11 |
12 | import "os"
13 |
14 | // If file exists and is not dir -> true.
15 | // If file not exists or it's a dir -> false.
16 | func FileExists(path string) bool {
17 | stat, err := os.Stat(path)
18 | if os.IsNotExist(err) || stat.IsDir() {
19 | return false
20 | }
21 | return true
22 | }
23 |
24 | func DirExists(path string) bool {
25 | stat, err := os.Stat(path)
26 | if os.IsNotExist(err) || !stat.IsDir() {
27 | return false
28 | }
29 | return true
30 | }
31 |
--------------------------------------------------------------------------------
/midware-dokuwiki/globals.go:
--------------------------------------------------------------------------------
1 | /*
2 | * @Author: FunctionSir
3 | * @License: AGPLv3
4 | * @Date: 2024-09-24 22:49:18
5 | * @LastEditTime: 2024-10-07 17:00:34
6 | * @LastEditors: FunctionSir
7 | * @Description: -
8 | * @FilePath: /AKBP/midware-dokuwiki/globals.go
9 | */
10 |
11 | package main
12 |
13 | // ABOUT //
14 |
15 | const (
16 | VER string = "0.0.1" // Version.
17 | CODENAME string = "NunotabaShinobu" // Codename of this version.
18 | )
19 |
20 | // COMMON CONSTS //
21 | const (
22 | LINUX_DEV_SHM string = "/dev/shm"
23 | OSM_ZOOM string = "18"
24 | GEN_BBOX_LAT_OFFSET float64 = 0.0005
25 | GEN_BBOX_LON_OFFSET float64 = 0.005
26 | )
27 |
28 | // DEFAULTS //
29 | const (
30 | DEFAULT_DB string = "akbp.db"
31 | DEFAULT_TMP_DIR string = "akbp-tmp"
32 | DEFAULT_TIME_TEMPLATE string = "2006-01-02 15:04:05"
33 | DEFAULT_NON_INI_TEMPLATE string = "DefaultNonIni.template"
34 | DEFAULT_INI_TYPE_TEMPLATE string = "DefaultIniType.template"
35 | DEFAULT_ALL_ENTRIE_NS string = "akbp:all"
36 | DEFAULT_EVENTS_NS string = "akbp:events"
37 | DEFAULT_KMLS_NS string = "akbp:kmls"
38 | )
39 |
40 | // COMMON VARS //
41 |
42 | var (
43 | ConfigFile string = "" // Path of the config file.
44 | )
45 |
46 | // CONFIG //
47 |
48 | var (
49 | ConfLoaded bool = false // To prevent data races, do not change it manually.
50 | TmpDir string = "" // Tmp dir.
51 | Db string = DEFAULT_DB // DB to use.
52 | UpdGap int = 10 // Update gap.
53 | TimeTemplate string = DEFAULT_TIME_TEMPLATE // Template for time.
54 | NonIniTemplate string = DEFAULT_NON_INI_TEMPLATE // NonIniTemplate.
55 | IniTypeTemplate string = DEFAULT_INI_TYPE_TEMPLATE // IniTypeTemplate.
56 | AllEntriesNs string = DEFAULT_ALL_ENTRIE_NS // Ns for all entries.
57 | EventsNs string = DEFAULT_EVENTS_NS // Ns for "by events".
58 | KmlsNs string = DEFAULT_KMLS_NS // Ns for kmls.
59 | Domain string = "" // Domain.
60 | UsersDir string = "" // Users dirs.
61 | )
62 |
--------------------------------------------------------------------------------
/midware-dokuwiki/go.mod:
--------------------------------------------------------------------------------
1 | module midware-dokuwiki
2 |
3 | go 1.23
4 |
5 | require (
6 | github.com/fatih/color v1.17.0
7 | github.com/google/uuid v1.6.0
8 | github.com/mattn/go-sqlite3 v1.14.24
9 | gopkg.in/ini.v1 v1.67.0
10 | )
11 |
12 | require (
13 | github.com/mattn/go-colorable v0.1.13 // indirect
14 | github.com/mattn/go-isatty v0.0.20 // indirect
15 | github.com/stretchr/testify v1.9.0 // indirect
16 | golang.org/x/sys v0.18.0 // indirect
17 | )
18 |
--------------------------------------------------------------------------------
/midware-dokuwiki/go.sum:
--------------------------------------------------------------------------------
1 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3 | github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
4 | github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
5 | github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
6 | github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
7 | github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
8 | github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
9 | github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
10 | github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
11 | github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
12 | github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
13 | github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
14 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
15 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
16 | github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
17 | github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
18 | golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
19 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
20 | golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
21 | golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
22 | gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
23 | gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
24 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
25 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
26 |
--------------------------------------------------------------------------------
/midware-dokuwiki/kml.go:
--------------------------------------------------------------------------------
1 | /*
2 | * @Author: FunctionSir
3 | * @License: AGPLv3
4 | * @Date: 2024-09-22 14:49:31
5 | * @LastEditTime: 2024-10-04 23:22:56
6 | * @LastEditors: FunctionSir
7 | * @Description: -
8 | * @FilePath: /AKBP/midware-dokuwiki/kml.go
9 | */
10 |
11 | package main
12 |
13 | import (
14 | "fmt"
15 | "strings"
16 | )
17 |
18 | type Coord struct {
19 | Lat string
20 | Lon string
21 | Asl string
22 | }
23 |
24 | const (
25 | KML_FMT_STR string = `` +
26 | `` +
28 | `%s%s` // Gened by Google Earth Pro. %s: Name, %s: Content.
29 | PLACEMARK_FMT_STR string = "%s%s%s" // %s: Name, %s: Description %s: Content.
30 | LINE_FMT_STR string = "%s" // %s: CoordinatesList.
31 | POINT_FMT_STR string = "%s" // %s: Coord of the point.
32 | )
33 |
34 | func mkCoordsListStr(coords []Coord) string {
35 | r := ""
36 | for _, x := range coords {
37 | asl := x.Asl
38 | if x.Asl == "nil" || x.Asl == "" {
39 | asl = "0"
40 | }
41 | r += x.Lon + "," + x.Lat + "," + asl + " "
42 | }
43 | return strings.TrimSpace(r)
44 | }
45 |
46 | func coordToStr(coord *Coord) string {
47 | asl := coord.Asl
48 | if coord.Asl == "nil" || coord.Asl == "" {
49 | asl = "0"
50 | }
51 | return coord.Lon + "," + coord.Lat + "," + asl
52 | }
53 |
54 | func KmlAssemble(name string, content string) string {
55 | return fmt.Sprintf(KML_FMT_STR, name, content)
56 | }
57 |
58 | func KmlLineString(name string, description string, coords []Coord) string {
59 | coordsList := mkCoordsListStr(coords)
60 | return fmt.Sprintf(PLACEMARK_FMT_STR,
61 | name, description, fmt.Sprintf(LINE_FMT_STR, coordsList))
62 | }
63 |
64 | func KmlPoint(name string, description string, coord Coord) string {
65 | return fmt.Sprintf(PLACEMARK_FMT_STR,
66 | name, description, fmt.Sprintf(POINT_FMT_STR, coordToStr(&coord)))
67 | }
68 |
69 | func GenRoute(name string, route []Coord, elemName string, description string) string {
70 | content := KmlLineString(elemName, description, route)
71 | content += KmlPoint("Begin", description, route[0])
72 | content += KmlPoint("End", description, route[len(route)-1])
73 | content = KmlAssemble(name, content)
74 | return content
75 | }
76 |
--------------------------------------------------------------------------------
/midware-dokuwiki/main.go:
--------------------------------------------------------------------------------
1 | /*
2 | * @Author: FunctionSir
3 | * @License: AGPLv3
4 | * @Date: 2024-09-22 14:46:17
5 | * @LastEditTime: 2024-10-07 16:59:58
6 | * @LastEditors: FunctionSir
7 | * @Description: The bridge to dokuwiki.
8 | * @FilePath: /AKBP/midware-dokuwiki/main.go
9 | */
10 |
11 | package main
12 |
13 | import (
14 | "fmt"
15 | "os"
16 | "path"
17 | "runtime"
18 | "strconv"
19 | "strings"
20 | "syscall"
21 | "time"
22 |
23 | "github.com/fatih/color"
24 | "gopkg.in/ini.v1"
25 | )
26 |
27 | // You can find global consts and vars in globals.go //
28 |
29 | func initialize() {
30 | for _, x := range os.Args {
31 | switch x {
32 | case "--no-color":
33 | color.NoColor = true
34 | }
35 | }
36 | Hello() // Say hello.
37 | if len(os.Args) <= 1 {
38 | LogFatalln("No config file specified.")
39 | }
40 | ConfigFile = os.Args[1]
41 | if !FileExists(ConfigFile) {
42 | LogFatalln("Config file specified not exists or is a dir.")
43 | }
44 | }
45 |
46 | func main() {
47 | initialize()
48 | oldUmask := syscall.Umask(022)
49 | defer syscall.Umask(oldUmask)
50 | if runtime.GOOS == "linux" {
51 | TmpDir = path.Join(LINUX_DEV_SHM, DEFAULT_TMP_DIR)
52 | } else {
53 | TmpDir = DEFAULT_TMP_DIR
54 | }
55 | LoadConf()
56 | if !FileExists(Db) {
57 | LogFatalln("Database \"" + Db + "\" not exists. Please check your config.")
58 | }
59 | if len(TmpDir) < len(LINUX_DEV_SHM) || TmpDir[0:len(LINUX_DEV_SHM)] != LINUX_DEV_SHM {
60 | LogWarnln("A large amount of write operations might will be preformed. Please make sure you are using a ram disk, or a HDD, or a SSD but without any valuable data in it for tmp dir.")
61 | LogWarnln("Please make sure that dir " + TmpDir + " is not important, the program will clean the dir.")
62 | }
63 | err := os.RemoveAll(TmpDir)
64 | if err != nil {
65 | LogFatalln("Failed to remove tmp dir, unable to clean it.")
66 | }
67 | err = os.MkdirAll(TmpDir, 0755)
68 | if err != nil {
69 | LogFatalln("Failed to make tmp dir.")
70 | }
71 | neededDirs := []string{"/dw-ns", "/dw-m", "/dw-ns/all", "/dw-ns/events", "/dw-m/kmls"}
72 | for _, x := range neededDirs {
73 | err = os.Mkdir(path.Join(TmpDir, x), 0755)
74 | if err != nil {
75 | LogFatalln("Needed dir \"" + path.Join(TmpDir, x) + "\" could not be created.")
76 | }
77 | }
78 | content, err := os.ReadFile(NonIniTemplate)
79 | if err != nil {
80 | LogFatalln("Unable to load NoIniTemplate \"" + NonIniTemplate + "\". Error: " + err.Error() + ".")
81 | }
82 | NonIniTemplate = string(content)
83 | content, err = os.ReadFile(IniTypeTemplate)
84 | if err != nil {
85 | LogFatalln("Unable to load IniTypeTemplate \"" + IniTypeTemplate + "\". Error: " + err.Error() + ".")
86 | }
87 | IniTypeTemplate = string(content)
88 | LogInfoln("The software is ready to serve.")
89 | totUpd := 0
90 | eventsDir := path.Join(TmpDir, "/dw-ns/events")
91 | allDir := path.Join(TmpDir, "/dw-ns/all")
92 | kmlsDir := path.Join(TmpDir, "/dw-m/kmls")
93 | var processed int
94 | // The main loop.
95 | for {
96 | startTime := time.Now().UnixMilli()
97 | // Handle reg requests.
98 | totRegRequests, reged := ProcessRegRequests(UsersDir)
99 | processed = 0
100 | var rowId int
101 | var bid string
102 | var eid string
103 | var ts int
104 | var msg string
105 | var origin string
106 | toGenKml := make(map[string][]Coord)
107 | byEvent := make(map[string][]string)
108 | byEventNotIniIndex := make(map[string][]int)
109 | byEventDecodedPartIndex := make(map[string][]int)
110 | notIniParts := make([]NotIniPart, 0)
111 | decodedParts := make([]DecodedPart, 0)
112 | addedToByEventSummary := make(set[string])
113 | rows := QueryRecs()
114 | // Process every rows.
115 | for rows.Next() {
116 | processed++
117 | rows.Scan(&rowId, &bid, &eid, &ts, &msg, &origin)
118 | // Gen file in "all" dir.
119 | filePath := path.Join(allDir, strings.ToLower(strconv.Itoa(rowId)+"-"+bid+"-"+eid+"-"+strconv.Itoa(ts)+".txt"))
120 | ini, err := ini.Load([]byte(msg))
121 | notIni := NotIniPart{rowId, bid, eid, ts, msg, origin, true}
122 | // Append to by event list.
123 | byEvent[bid+"-"+eid] = append(byEvent[bid+"-"+eid], strings.ToLower(strconv.Itoa(rowId)+"-"+bid+"-"+eid+"-"+strconv.Itoa(ts)))
124 | // If is not ini format.
125 | if err != nil {
126 | notIni.IsIni = false
127 | os.WriteFile(filePath, []byte(NonIniFillTemplate(¬Ini, &NonIniTemplate)), 0644)
128 | notIniParts = append(notIniParts, notIni)
129 | byEventNotIniIndex[bid+"-"+eid] = append(byEventNotIniIndex[bid+"-"+eid], len(notIniParts)-1)
130 | byEventDecodedPartIndex[bid+"-"+eid] = append(byEventDecodedPartIndex[bid+"-"+eid], -1)
131 | continue
132 | }
133 | sec := ini.Section("DEFAULT")
134 | decoded := DecodedPart{}
135 | decoded.Bid = notIni.Bid
136 | decoded.Eid = notIni.Eid
137 | if sec.HasKey("Encryption") {
138 | decoded.EncType = strings.TrimSpace(sec.Key("Encryption").String())
139 | } else {
140 | decoded.EncType = "plaintext"
141 | }
142 | if sec.HasKey("Position") {
143 | splited := strings.Split(sec.Key("Position").String(), ",")
144 | if len(splited) <= 1 {
145 | decoded.Pos = Coord{"nil", "nil", "nil"}
146 | } else {
147 | asl := "nil"
148 | if len(splited) >= 3 {
149 | asl = splited[2]
150 | }
151 | decoded.Pos = Coord{strings.TrimSpace(splited[0]), strings.TrimSpace(splited[1]), strings.TrimSpace(asl)}
152 | }
153 | } else {
154 | decoded.Pos = Coord{"nil", "nil", "nil"}
155 | }
156 | coordKeys := []string{"Lat", "Lon", "Asl"}
157 | for _, x := range coordKeys {
158 | if sec.HasKey(x) {
159 | decoded.Pos.Lon = strings.TrimSpace(sec.Key(x).String())
160 | }
161 | }
162 | if sec.HasKey("Extra") {
163 | decoded.Extra = strings.TrimSpace(sec.Key("Extra").String())
164 | } else {
165 | decoded.Extra = "nil"
166 | }
167 | notIniParts = append(notIniParts, notIni)
168 | decodedParts = append(decodedParts, decoded)
169 | byEventNotIniIndex[bid+"-"+eid] = append(byEventNotIniIndex[bid+"-"+eid], len(notIniParts)-1)
170 | byEventDecodedPartIndex[bid+"-"+eid] = append(byEventDecodedPartIndex[bid+"-"+eid], len(decodedParts)-1)
171 | os.WriteFile(filePath, []byte(IniFillTemplate(¬Ini, &decoded, &IniTypeTemplate)), 0644)
172 | }
173 | // Gen Summary for "all".
174 | GenAllSummary(path.Join(allDir, "summary.txt"), notIniParts)
175 | // Gen KMLs.
176 | for _, x := range decodedParts {
177 | if x.EncType == "plaintext" {
178 | tmp := x.Bid + "-" + x.Eid
179 | toGenKml[tmp] = append(toGenKml[tmp], x.Pos)
180 | }
181 | }
182 | for i, x := range toGenKml {
183 | os.WriteFile(path.Join(kmlsDir, strings.ToLower(i+".kml")), []byte(GenRoute(i, x, "Route of "+i, "Belongs to event "+i+".")), 0644)
184 | }
185 | // Gen "by event"
186 | for i, x := range byEvent {
187 | GenByEvent(path.Join(eventsDir, strings.ToLower(i+".txt")), i, x, byEventNotIniIndex[i], byEventDecodedPartIndex[i], notIniParts, decodedParts)
188 | }
189 | // Gen summary of "by evnent".
190 | toGenSummaryOfByEventTime := make([]int, 0)
191 | toGenSummaryOfByEventEvent := make([]string, 0)
192 | toGenSummaryOfByEventMsgCnt := make([]int, 0)
193 | for i := len(notIniParts) - 1; i >= 0; i-- {
194 | tmp := notIniParts[i].Bid + "-" + notIniParts[i].Eid
195 | if !addedToByEventSummary.Have(tmp) {
196 | toGenSummaryOfByEventEvent = append(toGenSummaryOfByEventEvent, tmp)
197 | toGenSummaryOfByEventTime = append(toGenSummaryOfByEventTime, notIniParts[i].Ts)
198 | toGenSummaryOfByEventMsgCnt = append(toGenSummaryOfByEventMsgCnt, len(byEventNotIniIndex[tmp]))
199 | addedToByEventSummary.Ins(tmp)
200 | }
201 | }
202 | GenByEventSummary(path.Join(eventsDir, "summary.txt"), toGenSummaryOfByEventEvent, toGenSummaryOfByEventTime, toGenSummaryOfByEventMsgCnt)
203 | // Calc used time.
204 | usedTime := time.Now().UnixMilli() - startTime
205 | LogInfoln(fmt.Sprintf("Update #%d done. Processed %d records, %d registration request, registered %d beacons. Used %d ms.", totUpd+1, processed, totRegRequests, reged, usedTime))
206 | if usedTime > 5000 {
207 | LogWarnln("The program is running too slow. Check your DB, or the location of your tmp dir.")
208 | }
209 | // Add 1 to update counter.
210 | totUpd = totUpd + 1
211 | // Sit back and relax.
212 | time.Sleep(time.Duration(UpdGap) * time.Second)
213 | }
214 | }
215 |
--------------------------------------------------------------------------------
/midware-dokuwiki/output.go:
--------------------------------------------------------------------------------
1 | /*
2 | * @Author: FunctionSir
3 | * @License: AGPLv3
4 | * @Date: 2024-09-12 22:47:30
5 | * @LastEditTime: 2024-10-07 17:58:59
6 | * @LastEditors: FunctionSir
7 | * @Description: Print logs, or other output things.
8 | * @FilePath: /AKBP/midware-dokuwiki/output.go
9 | */
10 |
11 | package main
12 |
13 | import (
14 | "log"
15 |
16 | "github.com/fatih/color"
17 | )
18 |
19 | func LogFatalln(s string) {
20 | c := color.New(color.FgHiRed, color.Underline)
21 | log.Fatalln(c.Sprint(s))
22 | }
23 |
24 | func LogWarnln(s string) {
25 | c := color.New(color.FgHiYellow)
26 | log.Println(c.Sprint(s))
27 | }
28 |
29 | func LogInfoln(s string) {
30 | c := color.New(color.FgHiGreen)
31 | log.Println(c.Sprint(s))
32 | }
33 |
34 | func Hello() {
35 | c := color.New(color.FgHiBlue)
36 | c.Println("[A]nti [K]idnapping [B]eacon [P]roject Midware For Dokuwiki")
37 | c.Println("Version: " + VER + " (" + CODENAME + ")")
38 | }
39 |
--------------------------------------------------------------------------------
/midware-dokuwiki/set.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | type VOID struct{}
4 |
5 | type set[Key comparable] map[Key]VOID
6 |
7 | func (x set[Key]) Have(key Key) bool {
8 | _, exists := x[key]
9 | return exists
10 | }
11 |
12 | func (x set[Key]) Ins(key Key) {
13 | x[key] = VOID{}
14 | }
15 |
16 | // func (x set[Key]) Del(key Key) {
17 | // delete(x, key)
18 | // }
19 |
--------------------------------------------------------------------------------
/server/auth.go:
--------------------------------------------------------------------------------
1 | /*
2 | * @Author: FunctionSir
3 | * @License: AGPLv3
4 | * @Date: 2024-09-16 21:23:59
5 | * @LastEditTime: 2024-09-22 00:48:36
6 | * @LastEditors: FunctionSir
7 | * @Description: -
8 | * @FilePath: /AKBP/server/auth.go
9 | */
10 | package main
11 |
12 | import (
13 | "crypto/sha512"
14 | "fmt"
15 |
16 | _ "github.com/mattn/go-sqlite3"
17 | )
18 |
19 | func CalcHash(key string, salt string) string {
20 | return fmt.Sprintf("%X", sha512.Sum512([]byte(key+salt)))
21 | }
22 |
23 | func AuthOK(table string, id string, key string) bool {
24 | ChkStrWithExit(&table)
25 | db := DbOpen()
26 | defer db.Close()
27 | stmt := DbPrepare(db, "SELECT SALT,HASH FROM \""+table+"\" WHERE ID=?")
28 | var salt string
29 | var hash string
30 | stmt.QueryRow(id).Scan(&salt, &hash)
31 | if CalcHash(key, salt) == hash {
32 | return true
33 | } else {
34 | return false
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/server/conf.go:
--------------------------------------------------------------------------------
1 | /*
2 | * @Author: FunctionSir
3 | * @License: AGPLv3
4 | * @Date: 2024-09-14 00:23:17
5 | * @LastEditTime: 2024-10-07 17:02:03
6 | * @LastEditors: FunctionSir
7 | * @Description: Config related.
8 | * @FilePath: /AKBP/server/conf.go
9 | */
10 |
11 | package main
12 |
13 | import (
14 | "strconv"
15 | "strings"
16 |
17 | "gopkg.in/ini.v1"
18 | )
19 |
20 | func LoadConf() {
21 | if ConfLoaded {
22 | LogFatalln("Error: reload config might cause data races.")
23 | }
24 | conf, err := ini.Load(ConfigFile)
25 | if err != nil {
26 | LogFatalln("Error occurred when loading conf file \"" + ConfigFile + "\": " + strings.Trim(err.Error(), "\n") + ".")
27 | }
28 | if !conf.HasSection("server") {
29 | LogWarnln("Warning: config file \"" + ConfigFile + "\" has no section \"server\", default values about server will be applied for everything.")
30 | return
31 | }
32 | section := conf.Section("server")
33 | if section.HasKey("Addr") {
34 | Addr = section.Key("Addr").String()
35 | }
36 | if section.HasKey("ServerID") {
37 | ServerId = section.Key("ServerID").String()
38 | }
39 | if section.HasKey("MainDB") {
40 | Db = section.Key("MainDB").String()
41 | }
42 | if section.HasKey("ExchangeGap") {
43 | ExchangeGap, err = strconv.Atoi(section.Key("ExchangeGap").String())
44 | if err != nil {
45 | LogFatalln("Can not covert value of key \"ExchangeGap\" to int.")
46 | }
47 | }
48 | if conf.HasSection("known-servers") {
49 | KnownServers = make(map[string]string)
50 | KeysForAuth = make(map[string]string)
51 | sec_known_servers := conf.Section("known-servers")
52 | for _, key := range sec_known_servers.KeyStrings() {
53 | tmp := strings.Split(sec_known_servers.Key(key).String(), "$")
54 | if len(tmp) == 2 {
55 | KnownServers[key] = strings.TrimSpace(tmp[0])
56 | KeysForAuth[key] = strings.TrimSpace(tmp[1])
57 | } else {
58 | LogFatalln("Wrong value \"" + sec_known_servers.Key(key).String() + "\" in section known-servers of file \"" + ConfigFile + "\".")
59 | }
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/server/db.go:
--------------------------------------------------------------------------------
1 | /*
2 | * @Author: FunctionSir
3 | * @License: AGPLv3
4 | * @Date: 2024-09-14 21:33:33
5 | * @LastEditTime: 2024-10-07 17:50:21
6 | * @LastEditors: FunctionSir
7 | * @Description: DB related.
8 | * @FilePath: /AKBP/server/db.go
9 | */
10 |
11 | package main
12 |
13 | import (
14 | "database/sql"
15 | "strings"
16 |
17 | _ "github.com/mattn/go-sqlite3"
18 | )
19 |
20 | // '0'~'9', 'A'~'Z', '_', 'a'~'z' are considered as safe chars.
21 | func chrIsSafe(ch rune, extra string) bool {
22 | return strings.ContainsRune("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"+extra, ch)
23 | }
24 |
25 | // A kind of protection before SQL actions.
26 | func ChkStrWithExit(str *string) {
27 | for _, x := range *str {
28 | if !chrIsSafe(x, "") {
29 | LogFatalln("Unsafe char found in string \"" + *str + "\" by strict str checker for DB.")
30 | }
31 | }
32 | }
33 |
34 | // Chk str, but do not exit.
35 | func ChkStrNoExit(str *string, extra string) bool {
36 | for _, x := range *str {
37 | if !chrIsSafe(x, extra) {
38 | return false
39 | }
40 | }
41 | return true
42 | }
43 |
44 | // Open DB using DB file specified in global var.
45 | func DbOpen() *sql.DB {
46 | db, err := sql.Open("sqlite3", Db)
47 | if err != nil {
48 | LogFatalln("Error occurred when opening the database: " + strings.Trim(err.Error(), "\n"))
49 | }
50 | return db
51 | }
52 |
53 | // Prepare a query.
54 | func DbPrepare(db *sql.DB, query string) *sql.Stmt {
55 | stmt, err := db.Prepare(query)
56 | if err != nil {
57 | LogFatalln("Error occurred when preparing the SQL statement: " + strings.Trim(err.Error(), "\n"))
58 | }
59 | return stmt
60 | }
61 |
62 | func QueryRecs() *sql.Rows {
63 | db := DbOpen()
64 | defer db.Close()
65 | stmt := DbPrepare(db, "SELECT BID,EID,TS,MSG,BANNED FROM RECORDS;")
66 | rows, err := stmt.Query()
67 | if err != nil {
68 | LogWarnln("An error occurred when performing a query.")
69 | return nil
70 | }
71 | return rows
72 | }
73 |
74 | // Init a new DB.
75 | func DbInit() {
76 | db := DbOpen()
77 | defer db.Close()
78 | db.Exec(DB_INIT)
79 | }
80 |
--------------------------------------------------------------------------------
/server/fileio.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import "os"
4 |
5 | // If file exists and is not dir -> true.
6 | // If file not exists or it's a dir -> false.
7 | func FileExists(path string) bool {
8 | stat, err := os.Stat(path)
9 | if os.IsNotExist(err) || stat.IsDir() {
10 | return false
11 | }
12 | return true
13 | }
14 |
--------------------------------------------------------------------------------
/server/globals.go:
--------------------------------------------------------------------------------
1 | /*
2 | * @Author: FunctionSir
3 | * @License: AGPLv3
4 | * @Date: 2024-09-12 22:19:36
5 | * @LastEditTime: 2024-10-07 18:45:53
6 | * @LastEditors: FunctionSir
7 | * @Description: Global consts and vars.
8 | * @FilePath: /AKBP/server/globals.go
9 | */
10 |
11 | package main
12 |
13 | // ABOUT //
14 |
15 | const (
16 | VER string = "0.0.1" // Version.
17 | CODENAME string = "NunotabaShinobu" // Codename of this version.
18 | )
19 |
20 | // DEFAULTS //
21 |
22 | const (
23 | DEFAULT_ADDR string = "127.0.0.1:4060" // Default listening address.
24 | DEFAULT_DB string = "akbp.db" // Default DB.
25 | DEFAULT_SID string = "" // Default server ID.
26 | DEFAULT_EXCHANGE_GAP int = 3600 // Default exchange gap.
27 | )
28 |
29 | // COMMON CONSTS //
30 |
31 | const (
32 | ADD_TTL int = 64
33 | )
34 |
35 | // COMMON VARS //
36 |
37 | var (
38 | DebugMode bool = false // Switch to true to enable debug for Gin.
39 | ConfigFile string = "" // Path of config file.
40 | ServerId string = DEFAULT_SID // Server ID.
41 | )
42 |
43 | // CONFIG //
44 |
45 | var (
46 | ConfLoaded bool = false // To prevent data races, do not change it manually.
47 | Addr string = DEFAULT_ADDR // Listening address.
48 | Db string = DEFAULT_DB // DB to use.
49 | KnownServers map[string]string = nil // Known servers.
50 | KeysForAuth map[string]string = nil // Auth info for known servers.
51 | ExchangeGap int = DEFAULT_EXCHANGE_GAP // Gap between two times of server to server exchange.
52 | )
53 |
54 | // FOR SQL //
55 |
56 | const (
57 | TABLE_BEACONS string = "BEACONS"
58 | TABLE_SERVERS string = "SERVERS"
59 | STR_BEACON string = "BEACON"
60 | STR_SERVER string = "SERVER"
61 | DB_INIT string = "CREATE TABLE BEACONS (ID TEXT PRIMARY KEY NOT NULL UNIQUE, SALT TEXT NOT NULL UNIQUE, HASH TEXT NOT NULL UNIQUE, NOTE TEXT);" +
62 | "CREATE TABLE RECORDS (BID TEXT NOT NULL, EID TEXT NOT NULL, TS INTEGER NOT NULL, MSG TEXT, ORIGIN TEXT, BANNED TEXT);" +
63 | "CREATE TABLE SERVERS (ID TEXT PRIMARY KEY NOT NULL UNIQUE, SALT TEXT NOT NULL UNIQUE, HASH TEXT NOT NULL UNIQUE);" +
64 | "CREATE TABLE RECEIVED(HASH TEXT NOT NULL UNIQUE);" // This will be exec DIRECTLY if needed!
65 | )
66 |
67 | // HEADERS //
68 |
69 | const (
70 | KEY_AKBP_MSG_TYPE string = "X-Akbp-Msg-Type"
71 | KEY_AKBP_BEACON_ID string = "X-Akbp-Beacon-Id"
72 | KEY_AKBP_AUTH string = "X-Akbp-Auth"
73 | KEY_AKBP_TIMESTAMP string = "X-Akbp-Timestamp"
74 | KEY_AKBP_EVENT_ID string = "X-Akbp-Event-Id"
75 | KEY_AKBP_DO_NOT_FORWARD_TO string = "X-Akbp-Do-Not-Forward-To"
76 | KEY_AKBP_SERVER_ID string = "X-Akbp-Server-Id"
77 | KEY_AKBP_TTL string = "X-Akbp-Ttl"
78 | KEY_CONTENT_TYPE string = "Content-Type"
79 | )
80 |
81 | // OK //
82 |
83 | const (
84 | HTTP_200_OK string = "200 OK"
85 | )
86 |
87 | // ERRORS //
88 |
89 | const (
90 | ERR_UNKNOWN_MSG_TYPE string = "400 BadRequest UnknownMsgType"
91 | ERR_BAD_TIMESTAMP string = "400 BadRequest BadTimestamp"
92 | ERR_NO_VALID_EID string = "400 BadRequest NoValidEventId"
93 | ERR_NO_VALID_AUTH_HEADER string = "401 Unauthorized NoValidAuthHeader"
94 | ERR_WRONG_BEACON_ID_OR_AUTH_INFO string = "403 Forbidden WrongBeaconIdOrAuthInfo"
95 | )
96 |
97 | // MAKE A CUP OF COFFEE //
98 |
99 | const (
100 | ALWAYS_TEAPOT bool = false
101 | FORTUNE string = "/usr/bin/fortune"
102 | I_AM_A_TEAPOT string = "418 I'm a teapot"
103 | )
104 |
--------------------------------------------------------------------------------
/server/go.mod:
--------------------------------------------------------------------------------
1 | module server
2 |
3 | go 1.23
4 |
5 | require (
6 | github.com/fatih/color v1.17.0
7 | github.com/gin-gonic/gin v1.10.0
8 | github.com/mattn/go-sqlite3 v1.14.23
9 | gopkg.in/ini.v1 v1.67.0
10 | )
11 |
12 | require (
13 | github.com/bytedance/sonic v1.11.6 // indirect
14 | github.com/bytedance/sonic/loader v0.1.1 // indirect
15 | github.com/cloudwego/base64x v0.1.4 // indirect
16 | github.com/cloudwego/iasm v0.2.0 // indirect
17 | github.com/gabriel-vasile/mimetype v1.4.3 // indirect
18 | github.com/gin-contrib/sse v0.1.0 // indirect
19 | github.com/go-playground/locales v0.14.1 // indirect
20 | github.com/go-playground/universal-translator v0.18.1 // indirect
21 | github.com/go-playground/validator/v10 v10.20.0 // indirect
22 | github.com/goccy/go-json v0.10.2 // indirect
23 | github.com/json-iterator/go v1.1.12 // indirect
24 | github.com/klauspost/cpuid/v2 v2.2.7 // indirect
25 | github.com/leodido/go-urn v1.4.0 // indirect
26 | github.com/mattn/go-colorable v0.1.13 // indirect
27 | github.com/mattn/go-isatty v0.0.20 // indirect
28 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
29 | github.com/modern-go/reflect2 v1.0.2 // indirect
30 | github.com/pelletier/go-toml/v2 v2.2.2 // indirect
31 | github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
32 | github.com/ugorji/go/codec v1.2.12 // indirect
33 | golang.org/x/arch v0.8.0 // indirect
34 | golang.org/x/crypto v0.35.0 // indirect
35 | golang.org/x/net v0.33.0 // indirect
36 | golang.org/x/sys v0.20.0 // indirect
37 | golang.org/x/text v0.15.0 // indirect
38 | google.golang.org/protobuf v1.34.1 // indirect
39 | gopkg.in/yaml.v3 v3.0.1 // indirect
40 | )
41 |
--------------------------------------------------------------------------------
/server/go.sum:
--------------------------------------------------------------------------------
1 | github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
2 | github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
3 | github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
4 | github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
5 | github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
6 | github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
7 | github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
8 | github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
9 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
11 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12 | github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
13 | github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
14 | github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
15 | github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
16 | github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
17 | github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
18 | github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
19 | github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
20 | github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
21 | github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
22 | github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
23 | github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
24 | github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
25 | github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
26 | github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
27 | github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
28 | github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
29 | github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
30 | github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
31 | github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
32 | github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
33 | github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
34 | github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
35 | github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
36 | github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
37 | github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
38 | github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
39 | github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
40 | github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
41 | github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
42 | github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
43 | github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
44 | github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
45 | github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
46 | github.com/mattn/go-sqlite3 v1.14.23 h1:gbShiuAP1W5j9UOksQ06aiiqPMxYecovVGwmTxWtuw0=
47 | github.com/mattn/go-sqlite3 v1.14.23/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
48 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
49 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
50 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
51 | github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
52 | github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
53 | github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
54 | github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
55 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
56 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
57 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
58 | github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
59 | github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
60 | github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
61 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
62 | github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
63 | github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
64 | github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
65 | github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
66 | github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
67 | github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
68 | github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
69 | github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
70 | github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
71 | github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
72 | github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
73 | golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
74 | golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
75 | golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
76 | golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
77 | golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
78 | golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
79 | golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
80 | golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
81 | golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
82 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
83 | golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
84 | golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
85 | golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
86 | golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
87 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
88 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
89 | google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
90 | google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
91 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
92 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
93 | gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
94 | gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
95 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
96 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
97 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
98 | nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
99 | rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
100 |
--------------------------------------------------------------------------------
/server/main.go:
--------------------------------------------------------------------------------
1 | /*
2 | * @Author: FunctionSir
3 | * @License: AGPLv3
4 | * @Date: 2024-09-12 22:07:34
5 | * @LastEditTime: 2024-10-07 18:07:40
6 | * @LastEditors: FunctionSir
7 | * @Description: AKBP Server, main file.
8 | * @FilePath: /AKBP/server/main.go
9 | */
10 |
11 | package main
12 |
13 | import (
14 | "fmt"
15 | "net/http"
16 | "net/url"
17 | "os"
18 | "os/exec"
19 | "runtime"
20 | "slices"
21 | "strconv"
22 | "strings"
23 | "time"
24 |
25 | "github.com/fatih/color"
26 | "github.com/gin-gonic/gin"
27 | )
28 |
29 | // You can find global consts and vars in globals.go //
30 |
31 | func initialize() {
32 | for _, x := range os.Args {
33 | switch x {
34 | case "--no-color":
35 | color.NoColor = true
36 | gin.DisableConsoleColor()
37 | case "--debug":
38 | DebugMode = true
39 | }
40 | }
41 | if !color.NoColor {
42 | gin.ForceConsoleColor()
43 | }
44 | if !DebugMode {
45 | gin.SetMode(gin.ReleaseMode)
46 | } else {
47 | gin.SetMode(gin.DebugMode)
48 | LogWarnln("You are in debug mode currently.")
49 | }
50 | Hello() // Say hello.
51 | if len(os.Args) <= 1 {
52 | LogFatalln("No config file specified.")
53 | }
54 | ConfigFile = os.Args[1]
55 | if !FileExists(ConfigFile) {
56 | LogFatalln("Config file specified not exists or is a dir.")
57 | }
58 | }
59 |
60 | func doPost(url string, headers *http.Header, kv *map[string]string) {
61 | tmpList := []string{}
62 | for k, v := range *kv {
63 | tmpList = append(tmpList, k+"="+v)
64 | }
65 | bodyStr := strings.Join(tmpList, "&")
66 | req, _ := http.NewRequest("POST", url, strings.NewReader(bodyStr))
67 | req.Header.Set(KEY_CONTENT_TYPE, "application/x-www-form-urlencoded")
68 | for k, v := range *headers {
69 | req.Header.Add(k, strings.Join(v, ","))
70 | }
71 | // Just let it alone.
72 | go http.DefaultClient.Do(req)
73 | }
74 |
75 | func reportOk(c *gin.Context) {
76 | c.Header(KEY_AKBP_MSG_TYPE, "server-report")
77 | c.String(http.StatusOK, HTTP_200_OK+" "+fmt.Sprint(time.Now().UnixMilli()))
78 | }
79 |
80 | func pong(c *gin.Context) {
81 | c.Header(KEY_AKBP_MSG_TYPE, "pong")
82 | c.String(http.StatusOK, "pong")
83 | }
84 |
85 | func srvToSrvExchange() {
86 | for {
87 | LogInfoln("Started server to server records exchange.")
88 | // If no valid server ID or no known server, do not exchange.
89 | if ServerId == "" || KnownServers == nil {
90 | return
91 | }
92 | rows := QueryRecs()
93 | for rows.Next() {
94 | var bid string
95 | var eid string
96 | var ts int
97 | var msg string
98 | var banned string
99 | rows.Scan(&bid, &eid, &ts, &msg, &banned)
100 | notForwardTo := strings.Split(banned, ",")
101 | // Make a new header.
102 | newHeader := make(http.Header)
103 | newHeader.Set(KEY_AKBP_TTL, strconv.Itoa(ADD_TTL))
104 | // Set type and server ID info.
105 | newHeader.Set(KEY_AKBP_MSG_TYPE, "forwarded")
106 | newHeader.Set(KEY_AKBP_SERVER_ID, ServerId)
107 | newHeader.Set(KEY_AKBP_DO_NOT_FORWARD_TO, banned)
108 | // BID, EID, TS
109 | newHeader.Set(KEY_AKBP_BEACON_ID, bid)
110 | newHeader.Set(KEY_AKBP_EVENT_ID, eid)
111 | newHeader.Set(KEY_AKBP_TIMESTAMP, strconv.Itoa(ts))
112 | // Encoded msg here.
113 | encodedMsg := url.QueryEscape(msg)
114 | newKv := map[string]string{"msg": encodedMsg}
115 | // Range known servers.
116 | for k, v := range KnownServers {
117 | // If the server is contained in notForwardTo.
118 | if slices.Contains(notForwardTo, k) {
119 | continue
120 | }
121 | // Set new auth info.
122 | newHeader.Set(KEY_AKBP_AUTH, KeysForAuth[k])
123 | // Post it!
124 | doPost(v, &newHeader, &newKv)
125 | }
126 | }
127 | LogInfoln("Successfully done once server to server records exchange.")
128 | // Sit back and relax.
129 | time.Sleep(time.Duration(ExchangeGap) * time.Second)
130 | }
131 | }
132 |
133 | func srvToSrvForward(c *gin.Context) {
134 | // If no valid server ID or no known server, do not forward.
135 | if ServerId == "" || KnownServers == nil {
136 | return
137 | }
138 | // Prepare a new header.
139 | newHeader := c.Request.Header.Clone()
140 | if newHeader == nil {
141 | newHeader = make(http.Header)
142 | }
143 | // Handle TTL.
144 | curTtlStr := newHeader.Get(KEY_AKBP_TTL)
145 | if curTtlStr == "" {
146 | newHeader.Add(KEY_AKBP_TTL, strconv.Itoa(ADD_TTL))
147 | } else {
148 | curTtlInt, err := strconv.Atoi(curTtlStr)
149 | if err != nil {
150 | newHeader.Set(KEY_AKBP_TTL, strconv.Itoa(ADD_TTL))
151 | } else {
152 | if curTtlInt <= 0 {
153 | return
154 | }
155 | newHeader.Set(KEY_AKBP_TTL, strconv.Itoa(curTtlInt-1))
156 | }
157 | }
158 | // Remove additional header.
159 | newHeader.Del(KEY_CONTENT_TYPE)
160 | // Add this server to chain.
161 | newHeader.Add(KEY_AKBP_DO_NOT_FORWARD_TO, ServerId)
162 | // Set type and server ID info.
163 | newHeader.Set(KEY_AKBP_MSG_TYPE, "forwarded")
164 | newHeader.Set(KEY_AKBP_SERVER_ID, ServerId)
165 | // For safety.
166 | encodedMsg := url.QueryEscape(c.PostForm("msg"))
167 | // New k-v pair.
168 | newKv := map[string]string{"msg": encodedMsg}
169 | // Get not forward to. Use new header will let the server itself be included.
170 | notForwardTo := strings.Split(newHeader.Get(KEY_AKBP_DO_NOT_FORWARD_TO), ",")
171 | // Range known servers.
172 | for k, v := range KnownServers {
173 | // If the server is contained in notForwardTo.
174 | if slices.Contains(notForwardTo, k) {
175 | continue
176 | }
177 | // Set new auth info.
178 | newHeader.Set(KEY_AKBP_AUTH, KeysForAuth[k])
179 | // Post it!
180 | doPost(v, &newHeader, &newKv)
181 | }
182 | }
183 |
184 | func beaconReportHandler(c *gin.Context) {
185 | // Get beacon ID.
186 | bid := c.GetHeader(KEY_AKBP_BEACON_ID)
187 | if len(bid) == 0 || !ChkStrNoExit(&bid, "@.") || len(bid) > 32 {
188 | c.Header(KEY_AKBP_MSG_TYPE, "error")
189 | c.String(http.StatusBadRequest, ERR_WRONG_BEACON_ID_OR_AUTH_INFO)
190 | return
191 | }
192 | // Get key.
193 | key := c.GetHeader(KEY_AKBP_AUTH)
194 | if len(key) == 0 {
195 | c.Header(KEY_AKBP_MSG_TYPE, "error")
196 | c.String(http.StatusUnauthorized, ERR_NO_VALID_AUTH_HEADER)
197 | return
198 | }
199 | // Auth here.
200 | if !AuthOK(TABLE_BEACONS, bid, key) {
201 | c.Header(KEY_AKBP_MSG_TYPE, "error")
202 | c.String(http.StatusForbidden, ERR_WRONG_BEACON_ID_OR_AUTH_INFO)
203 | return
204 | }
205 | // To process the message.
206 | msgType := c.GetHeader(KEY_AKBP_MSG_TYPE)
207 | switch msgType {
208 | case "ping": // This ping can use as a kind of auth test.
209 | pong(c)
210 | case "beacon-report":
211 | eid := c.GetHeader(KEY_AKBP_EVENT_ID)
212 | if len(eid) == 0 {
213 | c.Header(KEY_AKBP_MSG_TYPE, "error")
214 | c.String(http.StatusBadRequest, ERR_NO_VALID_EID)
215 | return
216 | }
217 | ts, err := strconv.Atoi(c.GetHeader(KEY_AKBP_TIMESTAMP))
218 | if err != nil {
219 | c.Header(KEY_AKBP_MSG_TYPE, "error")
220 | c.String(http.StatusBadRequest, ERR_BAD_TIMESTAMP)
221 | return
222 | }
223 | // Add new record.
224 | tmp := strings.Split(c.GetHeader(KEY_AKBP_DO_NOT_FORWARD_TO), ",")
225 | notForwardTo := ""
226 | for _, x := range tmp {
227 | notForwardTo += strings.TrimSpace(x) + ","
228 | }
229 | notForwardTo += ServerId
230 | if AddRecord(bid, eid, ts, c.PostForm("msg"), STR_BEACON+"-"+bid, notForwardTo) {
231 | // Forward.
232 | srvToSrvForward(c)
233 | }
234 | // Report.
235 | reportOk(c)
236 | default:
237 | c.Header(KEY_AKBP_MSG_TYPE, "error")
238 | c.String(http.StatusBadRequest, ERR_UNKNOWN_MSG_TYPE)
239 | }
240 | }
241 |
242 | func fromServerHandler(c *gin.Context) {
243 | sid := c.GetHeader(KEY_AKBP_SERVER_ID)
244 | key := c.GetHeader(KEY_AKBP_AUTH)
245 | if !ChkStrNoExit(&sid, "") || len(sid) > 16 || !AuthOK(TABLE_SERVERS, sid, key) {
246 | return
247 | }
248 | msgType := c.GetHeader(KEY_AKBP_MSG_TYPE)
249 | switch msgType {
250 | case "ping":
251 | pong(c)
252 | case "forwarded":
253 | bid := c.GetHeader(KEY_AKBP_BEACON_ID)
254 | eid := c.GetHeader(KEY_AKBP_EVENT_ID)
255 | tsStr := c.GetHeader(KEY_AKBP_TIMESTAMP)
256 | tsInt, err := strconv.Atoi(tsStr)
257 | msg := c.PostForm("msg")
258 | if bid == "" || eid == "" || tsStr == "" || err != nil || (!ChkStrNoExit(&bid, "@.")) || len(bid) > 32 {
259 | return
260 | }
261 | tmp := strings.Split(c.GetHeader(KEY_AKBP_DO_NOT_FORWARD_TO), ",")
262 | notForwardTo := ""
263 | for _, x := range tmp {
264 | notForwardTo += strings.TrimSpace(x) + ","
265 | }
266 | notForwardTo += ServerId
267 | if AddRecord(bid, eid, tsInt, msg, STR_SERVER+"-"+sid, notForwardTo) {
268 | srvToSrvForward(c)
269 | }
270 | reportOk(c)
271 | default:
272 | c.Header(KEY_AKBP_MSG_TYPE, "error")
273 | c.String(http.StatusBadRequest, ERR_UNKNOWN_MSG_TYPE)
274 | }
275 | }
276 |
277 | func main() {
278 | initialize()
279 | LoadConf()
280 | if !FileExists(Db) {
281 | LogWarnln("Specified DB not found, will create and init a new one.")
282 | DbInit()
283 | }
284 | // If no valid server ID.
285 | if ServerId == "" {
286 | LogWarnln("No valid server ID, server to server forward will be disabled.")
287 | }
288 |
289 | // If no known servers.
290 | if KnownServers == nil {
291 | LogWarnln("No known server, server to server forward will be disabled.")
292 | }
293 |
294 | ginEng := gin.Default()
295 |
296 | // FAVICON //
297 | ginEng.GET("/favicon.ico", func(c *gin.Context) {
298 | c.Header(KEY_AKBP_MSG_TYPE, "favicon")
299 | c.String(http.StatusOK, "")
300 | })
301 |
302 | // Ping //
303 | ginEng.GET("/ping", func(c *gin.Context) {
304 | pong(c)
305 | })
306 |
307 | // Fortune //
308 | if runtime.GOOS == "linux" && FileExists(FORTUNE) && !ALWAYS_TEAPOT {
309 | ginEng.GET("/fortune", func(c *gin.Context) {
310 | c.Header(KEY_AKBP_MSG_TYPE, "coffee")
311 | output, _ := exec.Command(FORTUNE).Output()
312 | c.String(http.StatusOK, strings.Trim(string(output), "\n"))
313 | })
314 | } else {
315 | ginEng.GET("/fortune", func(c *gin.Context) {
316 | c.Header(KEY_AKBP_MSG_TYPE, "teapot")
317 | c.String(418, I_AM_A_TEAPOT)
318 | })
319 | }
320 |
321 | // Beacon-Report //
322 | ginEng.POST("/beacon-report", beaconReportHandler)
323 |
324 | // From-Server //
325 | ginEng.POST("/from-server", fromServerHandler)
326 |
327 | // Start Server To Server Exchange //
328 | go srvToSrvExchange()
329 |
330 | LogInfoln("Everything is OK, ready to start the HTTP(S) service.")
331 | ginEng.Run(Addr)
332 | }
333 |
--------------------------------------------------------------------------------
/server/msg.go:
--------------------------------------------------------------------------------
1 | /*
2 | * @Author: FunctionSir
3 | * @License: AGPLv3
4 | * @Date: 2024-09-14 21:18:18
5 | * @LastEditTime: 2024-10-07 17:46:45
6 | * @LastEditors: FunctionSir
7 | * @Description: Beacons related.
8 | * @FilePath: /AKBP/server/msg.go
9 | */
10 |
11 | package main
12 |
13 | import (
14 | "crypto/sha512"
15 | "fmt"
16 | )
17 |
18 | func CalcMsgHash(bid string, eid string, ts int, msg string) string {
19 | info := fmt.Sprintf("%s\t%s\t%d\t%s", bid, eid, ts, msg)
20 | return fmt.Sprintf("%X", sha512.Sum512([]byte(info)))
21 | }
22 |
23 | func MsgExists(bid string, eid string, ts int, msg string) bool {
24 | db := DbOpen()
25 | defer db.Close()
26 | stmt := DbPrepare(db, "SELECT HASH FROM RECEIVED WHERE HASH=?")
27 | var tmp string
28 | err := stmt.QueryRow(CalcMsgHash(bid, eid, ts, msg)).Scan(&tmp)
29 | return err == nil
30 | }
31 |
32 | func AddRecord(bid string, eid string, ts int, msg string, origin string, banned string) bool {
33 | // If msg exists, do nothing.
34 | if MsgExists(bid, eid, ts, msg) {
35 | return false
36 | }
37 | db := DbOpen()
38 | defer db.Close()
39 | stmt := DbPrepare(db, "INSERT INTO RECORDS VALUES(?,?,?,?,?,?)")
40 | _, err := stmt.Exec(bid, eid, ts, msg, origin, banned)
41 | if err != nil {
42 | return false
43 | }
44 | stmt = DbPrepare(db, "INSERT INTO RECEIVED VALUES(?)")
45 | stmt.Exec(CalcMsgHash(bid, eid, ts, msg))
46 | return true
47 | }
48 |
--------------------------------------------------------------------------------
/server/output.go:
--------------------------------------------------------------------------------
1 | /*
2 | * @Author: FunctionSir
3 | * @License: AGPLv3
4 | * @Date: 2024-09-12 22:47:30
5 | * @LastEditTime: 2024-09-22 11:52:47
6 | * @LastEditors: FunctionSir
7 | * @Description: Print logs, or other output things.
8 | * @FilePath: /AKBP/server/output.go
9 | */
10 |
11 | package main
12 |
13 | import (
14 | "log"
15 |
16 | "github.com/fatih/color"
17 | )
18 |
19 | func LogFatalln(s string) {
20 | c := color.New(color.FgHiRed, color.Underline)
21 | log.Fatalln(c.Sprint(s))
22 | }
23 |
24 | func LogWarnln(s string) {
25 | c := color.New(color.FgHiYellow)
26 | log.Println(c.Sprint(s))
27 | }
28 |
29 | func LogInfoln(s string) {
30 | c := color.New(color.FgHiGreen)
31 | log.Println(c.Sprint(s))
32 | }
33 |
34 | func Hello() {
35 | c := color.New(color.FgHiBlue)
36 | c.Println("[A]nti [K]idnapping [B]eacon [P]roject Server")
37 | c.Println("Version: " + VER + " (" + CODENAME + ")")
38 | }
39 |
--------------------------------------------------------------------------------