├── LICENSE ├── README.md ├── RouterSCRIPTS_Installer.rsc ├── RouterSCRIPTS_Uninstaller.rsc ├── dist.cfg └── src ├── mod-dyndns.txt ├── mod-failover.txt ├── mod-gateway.txt ├── mod-livestream.txt ├── mod-provision.txt └── mod-resolvefqdn.txt /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RouterSCRIPTS 2 | 3 | ## I. DESCRIPTION: 4 | 5 | A collection of scripts for RouterBOARD devices 6 | 7 | Copyright (c) 2019 Frostbyte 8 | 9 | ## II. CREDITS: 10 | 11 | * Marisb @ MikroTik wiki - original file parser code 12 | * pablo @ MikroTik forum - original FQDN resolver code 13 | * hacki @ MikroTik forum - original DynDNS updater code 14 | 15 | ## III. FEATURES: 16 | 17 | * Configuration is read from external file 18 | * Supports N amount of default gateways 19 | * Detailed error messages for easy troubleshooting 20 | * Easy install and uninstall 21 | 22 | ## IV. AVAILABLE MODULES: 23 | 24 | * **Provision:** This is the core module responsible for the configuration and initialization of all other modules in this set 25 | * **Gateway Selector:** A module that simplifies on-demand load balancing or default gateway selection, for devices handling multiple gateways 26 | * **Failover:** A module for automatic default gateway swapping upon failure, for devices handling multiple gateways. Fully interoperable with the Gateway Selector module 27 | * **DynDNS Updater:** A module that simplifies dynamic DNS service updates 28 | * **FQDN Resolver:** A module that resolves fully qualified domain names to address lists 29 | * **Live Streaming:** A module that toggles policy routing or traffic prioritization/shaping rules to assist with live streaming 30 | 31 | **NOTE:** Firewall mangle rules pertaining to load balancing or live streaming are not included, as everyone's configuration and needs may differ 32 | 33 | ## V. EXAMPLE CONFIGURATION FILE: 34 | 35 | [Gateways] 36 | WANNames=Vodafone,Verizon 37 | WANGateways=192.168.1.1,192.168.2.1 38 | WANGatewayPrefix=Default route 39 | BalancingRulePrefix=LDBLNCR 40 | 41 | [Failover] 42 | FailoverTarget=8.8.8.8 43 | FailoverThreshold=3 44 | FailoverInterval=5 45 | 46 | [DynDNS] 47 | DDNSService=NoIP 48 | DDNSInterval=3600 49 | DDNSUsername=username 50 | DDNSPassword=password 51 | DDNSHostname=hostname.no-ip.org 52 | 53 | [Livestream] 54 | LVStreamList=TWITCH 55 | LVStreamFQDN=live-ams.twitch.tv 56 | LVStreamRulePrefix=LVSTRM 57 | 58 | **This configuration is an example to familiarize you with the required format** 59 | **Please use the supplied dist.cfg in the actual files, when creating your own** 60 | 61 | ## VI. EXPLANATION OF THE CONFIGURATION FILE 62 | 63 | **WANNames:** Comma separated list of default gateway names. Cannot contain spaces or duplicate values 64 | **WANGateways:** Comma separated list of default gateway addresses, as defined in your routing table. Must contain the same amount of entries as WANNames. Cannot contain spaces or duplicate values 65 | **WANGatewayPrefix:** The comment prefix to use for identifying default gateway routes in the routing table 66 | **BalancingRulePrefix:** The comment prefix to use for identifying load balancing rules in the mangle table. Must be defined even if no load balancing rules are present 67 | **FailoverTarget:** The IP address used to determine gateway availability status (via ping) during WAN availability checks 68 | **FailoverThreshold:** The number of consecutive times a gateway can fail the WAN availability checks, before getting flagged as non-operational 69 | **FailoverInterval:** The time (in seconds) between each WAN availability check, when automatic WAN availability checks are enabled 70 | **DDNSService:** The dynamic DNS service provider. Can be configured as NoIP or DynDNS only 71 | **DDNSInterval:** The time (in seconds) between each dynamic DNS service update attempt, when automatic dynamic DNS service updates are enabled. No update will be performed if WAN address hasn't changed 72 | **DDNSUsername:** The username for the dynamic DNS service 73 | **DDNSPassword:** The password (NoIP) or secret (DynDNS) for the dynamic DNS service 74 | **DDNSHostname:** The hostname for the dynamic DNS service 75 | **LVStreamList:** The firewall address list name to put the resolved live streaming service ingest server addresses under 76 | **LVStreamFQDN:** The fully qualified domain name of the live streaming service ingest servers 77 | **LVStreamRulePrefix:** The comment prefix to use for identifying live streaming rules in the mangle table 78 | 79 | ## VII. RESTRICTIONS AND IMPORTANT NOTES: 80 | 81 | * **Take regular backups of your device**, including one prior the installation of these scripts 82 | * Everything (configuration filename, commands, command arguments and so forth) is **case sensitive** 83 | * While there is no limit on how many gateways one can define, the maximum route distance of your default gateway routes **must not exceed** the number of gateways defined in the configuration file 84 | - For example, if we assume the above configuration: the default gateway routes must have a distance of 1 and 2 respectively. Gateways with distances higher than 2 will be considered non-operational by the scripts 85 | * You can only have one gateway per subnet. Multiple gateways over the same bridge/master-interface are **not supported** 86 | * Characters like braces, brackets and parentheses **must be avoided** on the prefix configuration values. Prefixes are used to locate comments and can be interpreted as regular expressions 87 | * You, or any other script/scheduler installed on the device, **must not edit or remove** any of the variables and routes managed by these scripts 88 | * Should you need to make any changes to your default routes and/or gateway subnets, please do the following, in order: 89 | - If currently active, toggle the failover module off 90 | - Proceed with making any changes that you desire 91 | - Update and re-upload your configuration file 92 | - Reprovision with: **$provision auto** 93 | - Toggle the failover module back on, if previously enabled 94 | 95 | ## VIII. INSTALLATION/UNINSTALLATION: 96 | 97 | 1. Upload the configuration file on the device, created with the restrictions mentioned above and the following guidelines in mind: 98 | - Configuration filename **must contain** the hostname of the device and **end with** a cfg extension (i.e. if MikroTik is the hostname then your configuration file should be MikroTik.cfg) 99 | - For each of the installed modules (residing in /system scripts) the configuration file **must contain** the corresponding section **fully populated** 100 | - If you wish to remove any module(s) during the installation, please also refer to sections **IX** and **X (points 3 and 4)** of this documentation 101 | 2. Upload the installation script on the device and execute: 102 | - **/import RouterSCRIPTS_Installer.rsc** 103 | - **$provision auto** 104 | 3. Upload the uninstallation script on the device and execute: 105 | - **/import RouterSCRIPTS_Uninstaller.rsc** 106 | 107 | ## IX. MODULE DEPENDENCIES: 108 | 109 | The following contains a list of the modules which are required to be present and fully configured, for each module 110 | 111 | **Provision:** none 112 | **Gateway Selector:** Provision 113 | **Failover:** Provision, Gateway Selector 114 | **DynDNS:** Provision 115 | **ResolveFQDN:** Provision 116 | **Livestream:** Provision, ResolveFQDN 117 | 118 | ## X. UNINSTALLING SPECIFIC MODULES: 119 | 120 | 1. If you wish to uninstall a module that you have no use for, first and foremost **consult** the aforementioned dependency list 121 | 2. Then, you can remove the module(s) from /system scripts and run: **$provision purge** 122 | 3. The same can be done during the initial installation, by removing the module(s) before running **$provision auto** 123 | 4. For any module(s) not present in /system scripts the corresponding configuration section(s) may be omitted 124 | 5. You should always follow this procedure for module(s) removal, as any changes made to the device by modules that contain an interval setting, will be gracefully rolled back during the module(s) purge 125 | 126 | ## XI. USAGE/COMMANDS: 127 | 128 | * $provision auto|purge|failover|gateways|dyndns|resolver|livestream 129 | - **auto:** automatically provisions/re-provisions every module found under /system scripts 130 | - **purge:** automatically deprovisions every module missing from /system scripts 131 | - **all others:** provision/re-provision the requested module 132 | * $gateway \>|status 133 | - **switch:** switch to load balancing mode or to one of the gateways defined in the configuration 134 | - **status:** display the current default gateway, not necessarily the one you have selected (if non-operational due to failover) 135 | - For example, if we assume the above configuration, possible commands would be: 136 | $gateway switch Balancer, $gateway switch Vodafone, $gateway switch Verison, $gateway status 137 | * $failover check|toggle|status 138 | - **check:** trigger a manual WAN availability check (useful for troubleshooting) 139 | - **toggle:** enable or disable the automatic WAN availability checks 140 | - **status:** display detailed information about the operational status for each gateway and their approximate up/down time 141 | * $dyndns update|toggle 142 | - **update:** trigger a manual dynamic DNS service update (if WAN address has changed) 143 | - **toggle:** enable or disable the automatic dynamic DNS service updates 144 | * $resolvefqdn \ \
145 | - **\:** the fully qualified domain name to resolve 146 | - **\
:** the address list name to put all resulting addresses under 147 | * $livestream toggle|status 148 | - **toggle:** enable or disable the live streaming module 149 | - **status:** display whether the live streaming module is active or not 150 | 151 | ## XII. REPORTING BUGS/REQUESTING FEATURES 152 | 153 | If you want to report a behavior that the scripts shouldn't exhibit, or simply request a new feature: 154 | Please use [this thread over on the MikroTik forums](https://forum.mikrotik.com/viewtopic.php?f=9&t=143511) -------------------------------------------------------------------------------- /RouterSCRIPTS_Installer.rsc: -------------------------------------------------------------------------------- 1 | # RouterSCRIPTS Installer 2 | # Version 1.0.1 3 | 4 | #Error handling functions 5 | local notify do={local msg "[RouterSCRIPTS][Info]: $1";put $msg;log info $msg} 6 | local execStatus 0 7 | 8 | #Install the scripts 9 | /system script add dont-require-permissions=no name=mod-provision policy=read,write source="#Provision module script\r\ 10 | \n#Script permissions: read, write\r\ 11 | \n#Script dependencies: none\r\ 12 | \n\r\ 13 | \n#Declare scripts version\r\ 14 | \nglobal scriptsVersion \"1.0.1\"\r\ 15 | \n\r\ 16 | \n#If the scheduler entry for the initialization of the provision module doesn't exist\r\ 17 | \nif ([/system scheduler find name=init-provision]=\"\") do={\r\ 18 | \n\t#Add it\r\ 19 | \n\t/system scheduler add name=init-provision start-time=startup on-event={delay 10;/system script run mod-provision;global provision;\$provision auto}\r\ 20 | \n}\r\ 21 | \n\r\ 22 | \n#Provision function\r\ 23 | \nglobal provision do={\r\ 24 | \n\t#Configuration parser subfunction\r\ 25 | \n\t#Inputs: \r\ 26 | \n\t#Output: \r\ 27 | \n\tlocal parseConfig do={\r\ 28 | \n\t\t#Pull the configuration file contents\r\ 29 | \n\t\tlocal cfgFileContent [/file get value-name=contents [find name~([/system identity get value-name=name].\".cfg\")]]\r\ 30 | \n\r\ 31 | \n\t\t#Detect and adjust the configuration EOL sequence\r\ 32 | \n\t\tlocal cfgEOLSequence \"\\n\"\r\ 33 | \n\t\tif ([typeof [find \$cfgFileContent \"\\r\\n\" 0]]=\"num\") do={set cfgEOLSequence \"\\r\\n\"}\r\ 34 | \n\r\ 35 | \n\t\t#Declare helper pointers\r\ 36 | \n\t\tlocal cfgLineStart 0\r\ 37 | \n\t\tlocal cfgLineEnd 0\r\ 38 | \n\r\ 39 | \n\t\t#Iterate through the configuration file contents\r\ 40 | \n\t\tdo {\r\ 41 | \n\t\t\t#Find out where the line ends\r\ 42 | \n\t\t\tset cfgLineEnd [find \$cfgFileContent \$cfgEOLSequence \$cfgLineStart]\r\ 43 | \n\r\ 44 | \n\t\t\t#If an EOL sequence cannot be found\r\ 45 | \n\t\t\tif ([typeof \$cfgLineEnd]!=\"num\") do={\r\ 46 | \n\t\t\t\t#Adjust the line end to the end of contents\r\ 47 | \n\t\t\t\tset cfgLineEnd [len \$cfgFileContent]\r\ 48 | \n\t\t\t}\r\ 49 | \n\r\ 50 | \n\t\t\t#Fetch the line\r\ 51 | \n\t\t\tlocal cfgLine [pick \$cfgFileContent \$cfgLineStart \$cfgLineEnd]\r\ 52 | \n\r\ 53 | \n\t\t\t#Pull the configuration attribute of the line\r\ 54 | \n\t\t\tlocal cfgAttribute [pick \$cfgLine 0 [find \$cfgLine \"=\" 0]]\r\ 55 | \n\r\ 56 | \n\t\t\t#If the attribute matches the request\r\ 57 | \n\t\t\tif (\$cfgAttribute=\$1) do={\r\ 58 | \n\t\t\t\t#Initialize the configuration value\r\ 59 | \n\t\t\t\tlocal cfgValue \"\"\r\ 60 | \n\r\ 61 | \n\t\t\t\t#If the value is populated\r\ 62 | \n\t\t\t\tif ([find \$cfgLine \"=\" 0]!=[len \$cfgLine]) do={\r\ 63 | \n\t\t\t\t\t#Set the configuration value\r\ 64 | \n\t\t\t\t\tset cfgValue [pick \$cfgLine ([find \$cfgLine \"=\" 0]+1) [len \$cfgLine]]\r\ 65 | \n\r\ 66 | \n\t\t\t\t\t#If the value is a boolean, properly convert it\r\ 67 | \n\t\t\t\t\tif (\$cfgValue=\"true\") do={set cfgValue true}\r\ 68 | \n\t\t\t\t\tif (\$cfgValue=\"false\") do={set cfgValue false}\r\ 69 | \n\t\t\t\t}\r\ 70 | \n\r\ 71 | \n\t\t\t\t#Return the requested configuration value\r\ 72 | \n\t\t\t\treturn \$cfgValue\r\ 73 | \n\t\t\t}\r\ 74 | \n\r\ 75 | \n\t\t\t#Advance to the next line\r\ 76 | \n\t\t\tset cfgLineStart (\$cfgLineEnd+[len \$cfgEOLSequence])\r\ 77 | \n\t\t} while (\$cfgLineStart<[len \$cfgFileContent])\r\ 78 | \n\r\ 79 | \n\t\t#If this part has been reached, it means that the requested attribute was not found\r\ 80 | \n\t\t#Exit\r\ 81 | \n\t\treturn \"\"\r\ 82 | \n\t}\r\ 83 | \n\r\ 84 | \n\t#Non-empty variable validator subfunction\r\ 85 | \n\t#Inputs: \r\ 86 | \n\t#Output: \r\ 87 | \n\tlocal validateVars do={\r\ 88 | \n\t\t#Error handling functions\r\ 89 | \n\t\tlocal error do={local msg \"[Provision][Error]: \$1\";put \$msg;log error \$msg;return -1}\r\ 90 | \n\t\tlocal execStatus 0\r\ 91 | \n\r\ 92 | \n\t\t#Iterate through the requested variables\r\ 93 | \n\t\tforeach varIndex,cfgVar in=\$1 do={\r\ 94 | \n\t\t\t#If any of them are not specified or invalid\r\ 95 | \n\t\t\tif ((\$2->\$varIndex)=\"\") do={\r\ 96 | \n\t\t\t\t#Throw error and adjust execution status\r\ 97 | \n\t\t\t\tset execStatus [\$error (\"The configuration variable \$cfgVar cannot be left empty or contain an invalid value.\")]\r\ 98 | \n\t\t\t}\r\ 99 | \n\t\t}\r\ 100 | \n\r\ 101 | \n\t\t#Exit\r\ 102 | \n\t\treturn \$execStatus\r\ 103 | \n\t}\r\ 104 | \n\r\ 105 | \n\t#Error handling functions\r\ 106 | \n\tlocal notify do={local msg \"[Provision][Info]: \$1\";put \$msg;log info \$msg}\r\ 107 | \n\tlocal error do={local msg \"[Provision][Error]: \$1\";put \$msg;log error \$msg;return -1}\r\ 108 | \n\tlocal execStatus 0\r\ 109 | \n\r\ 110 | \n\t#If the configuration file does not exist\r\ 111 | \n\tif ([/file find name~([/system identity get value-name=name].\".cfg\")]=\"\") do={\r\ 112 | \n\t\t#Throw error and exit\r\ 113 | \n\t\tset execStatus [\$error (\"Unable to locate the config file, please upload it to the router and try again.\")]\r\ 114 | \n\t\treturn \$execStatus\r\ 115 | \n\t}\r\ 116 | \n\r\ 117 | \n\t#If function was called with argument \"auto\"\r\ 118 | \n\tif (\$1=\"auto\" && [typeof \$2]=\"nothing\") do={\r\ 119 | \n\t\t#Notify\r\ 120 | \n\t\t\$notify (\"Provisioning all available modules.\")\r\ 121 | \n\r\ 122 | \n\t\t#Gain function access\r\ 123 | \n\t\tglobal provision\r\ 124 | \n\r\ 125 | \n\t\t#Iterate through the module inventory\r\ 126 | \n\t\tlocal availCommands \"\"\r\ 127 | \n\t\tlocal systemScripts {\"mod-gateway\";\"mod-failover\";\"mod-dyndns\";\"mod-resolvefqdn\";\"mod-livestream\"}\r\ 128 | \n\t\tforeach cmdIndex,moduleName in={\"gateways\";\"failover\";\"dyndns\";\"resolver\";\"livestream\"} do={\r\ 129 | \n\t\t\t#If any of the corresponding scripts are installed\r\ 130 | \n\t\t\tif ([/system script find name=(\$systemScripts->\$cmdIndex)]!=\"\") do={\r\ 131 | \n\t\t\t\t#Append their corresponding command to the provision commands string\r\ 132 | \n\t\t\t\tset availCommands \"\$availCommands,\$moduleName\"\r\ 133 | \n\t\t\t}\r\ 134 | \n\t\t}\r\ 135 | \n\r\ 136 | \n\t\t#Iterate through the available commands\r\ 137 | \n\t\tforeach command in=[toarray \$availCommands] do={\r\ 138 | \n\t\t\t#Execute each command and if any error was encountered\r\ 139 | \n\t\t\tif ([\$provision \$command]<0) do={\r\ 140 | \n\t\t\t\t#Adjust the execution status\r\ 141 | \n\t\t\t\tset execStatus -1\r\ 142 | \n\t\t\t}\r\ 143 | \n\t\t}\r\ 144 | \n\r\ 145 | \n\t\t#If any errors occured\r\ 146 | \n\t\tif (\$execStatus<0) do={\r\ 147 | \n\t\t\t#Throw error\r\ 148 | \n\t\t\t\$error (\"Completed with errors.\")\r\ 149 | \n\t\t} else {\r\ 150 | \n\t\t\t#Notify success\r\ 151 | \n\t\t\t\$notify (\"Completed successfully.\")\r\ 152 | \n\t\t}\r\ 153 | \n\r\ 154 | \n\t\t#Exit\r\ 155 | \n\t\treturn \$execStatus\r\ 156 | \n\t}\r\ 157 | \n\r\ 158 | \n\t#If function was called with argument \"purge\"\r\ 159 | \n\tif (\$1=\"purge\" && [typeof \$2]=\"nothing\") do={\r\ 160 | \n\t\t#Notify\r\ 161 | \n\t\t\$notify (\"Automatically purging provisioned configuration.\")\r\ 162 | \n\r\ 163 | \n\t\t#Construct an inventory of variable names belonging to each module\r\ 164 | \n\t\tlocal varsLiveStream {\"livestream\";\"LVStreamList\";\"LVStreamFQDN\";\"LVStreamRulePrefix\"}\r\ 165 | \n\t\tlocal varsResolver {\"resolvefqdn\"}\r\ 166 | \n\t\tlocal varsDynDNS {\"dyndns\";\"WANAddress\";\"DDNSService\";\"DDNSInterval\";\"DDNSUsername\";\"DDNSPassword\";\"DDNSHostname\"}\r\ 167 | \n\t\tlocal varsFailover {\"failover\";\"FailoverCounters\";\"FailoverTarget\";\"FailoverThreshold\";\"FailoverInterval\"}\r\ 168 | \n\t\tlocal varsGateway {\"gateway\";\"WANNames\";\"WANGateways\";\"WANGatewayPrefix\";\"BalancingRulePrefix\"}\r\ 169 | \n\t\tlocal moduleVars {\$varsLiveStream;\$varsResolver;\$varsDynDNS;\$varsFailover;\$varsGateway}\r\ 170 | \n\r\ 171 | \n\t\t#Iterate through the system scripts\r\ 172 | \n\t\tforeach modIndex,systemScript in={\"mod-livestream\";\"mod-resolvefqdn\";\"mod-dyndns\";\"mod-failover\";\"mod-gateway\"} do={\r\ 173 | \n\t\t\t#If any of them is not installed\r\ 174 | \n\t\t\tif ([/system script find name=\$systemScript]=\"\") do={\r\ 175 | \n\t\t\t\t#Iterate through its corresponding variables\r\ 176 | \n\t\t\t\tforeach modVar in=(\$moduleVars->\$modIndex) do={\r\ 177 | \n\t\t\t\t\t#If it's a function variable that possesses a cron job\r\ 178 | \n\t\t\t\t\tforeach varWithCron in={\"dyndns\";\"failover\"} do={\r\ 179 | \n\t\t\t\t\t\tif (\$modVar=\$varWithCron) do={\r\ 180 | \n\t\t\t\t\t\t\t#If it's currently active\r\ 181 | \n\t\t\t\t\t\t\tif ([/system scheduler find disabled=no name=\"cron-\$modVar\"]!=\"\") do={\r\ 182 | \n\t\t\t\t\t\t\t\t#Disable it\r\ 183 | \n\t\t\t\t\t\t\t\texecute (\"global \$modVar;\\\$\$modVar toggle\")\r\ 184 | \n\r\ 185 | \n\t\t\t\t\t\t\t\t#Allow enough time for any additional actions to complete\r\ 186 | \n\t\t\t\t\t\t\t\tdelay 1\r\ 187 | \n\t\t\t\t\t\t\t}\r\ 188 | \n\r\ 189 | \n\t\t\t\t\t\t\t#Remove it\r\ 190 | \n\t\t\t\t\t\t\t/system scheduler remove [find name=\"cron-\$modVar\"]\r\ 191 | \n\t\t\t\t\t\t}\r\ 192 | \n\t\t\t\t\t}\r\ 193 | \n\t\t\t\t\t\r\ 194 | \n\t\t\t\t\t#Clear their corresponding variables\r\ 195 | \n\t\t\t\t\texecute (\"global \".\$modVar.\"; set \".\$modVar)\r\ 196 | \n\t\t\t\t}\r\ 197 | \n\t\t\t}\r\ 198 | \n\t\t}\r\ 199 | \n\r\ 200 | \n\t\t#Exit\r\ 201 | \n\t\treturn \$execStatus\r\ 202 | \n\t}\r\ 203 | \n\r\ 204 | \n\t#If function was called with argument \"gateways\"\r\ 205 | \n\tif (\$1=\"gateways\" && [typeof \$2]=\"nothing\") do={\r\ 206 | \n\t\t#Array duplicate checker subfunction\r\ 207 | \n\t\t#Inputs: \r\ 208 | \n\t\t#Output: \r\ 209 | \n\t\tlocal containsDuplicates do={\r\ 210 | \n\t\t\t#Iterate through the requested arrays\r\ 211 | \n\t\t\tforeach array in=\$1 do={\r\ 212 | \n\t\t\t\t#Iterate through each array element\r\ 213 | \n\t\t\t\tforeach primaryElement in=\$array do={\r\ 214 | \n\t\t\t\t\t#Declare helper counter\r\ 215 | \n\t\t\t\t\tlocal elementOccurences 0\r\ 216 | \n\r\ 217 | \n\t\t\t\t\t#Iterate through each array element a second time\r\ 218 | \n\t\t\t\t\tforeach secondaryElement in=\$array do={\r\ 219 | \n\t\t\t\t\t\t#If the element is found\r\ 220 | \n\t\t\t\t\t\tif (\$primaryElement=\$secondaryElement) do={\r\ 221 | \n\t\t\t\t\t\t\t#Adjust the helper counter\r\ 222 | \n\t\t\t\t\t\t\tset elementOccurences (\$elementOccurences+1)\r\ 223 | \n\t\t\t\t\t\t}\r\ 224 | \n\t\t\t\t\t}\r\ 225 | \n\r\ 226 | \n\t\t\t\t\t#If an element has been found more than once\r\ 227 | \n\t\t\t\t\tif (\$elementOccurences>1) do={\r\ 228 | \n\t\t\t\t\t\t#Exit\r\ 229 | \n\t\t\t\t\t\treturn true\r\ 230 | \n\t\t\t\t\t}\r\ 231 | \n\t\t\t\t}\r\ 232 | \n\t\t\t}\r\ 233 | \n\r\ 234 | \n\t\t\t#Exit\r\ 235 | \n\t\t\treturn false\r\ 236 | \n\t\t}\r\ 237 | \n\r\ 238 | \n\t\t#Notify\r\ 239 | \n\t\t\$notify (\"Provisioning \$1.\")\r\ 240 | \n\r\ 241 | \n\t\t#Request variables from config\r\ 242 | \n\t\tglobal WANNames [toarray [\$parseConfig \"WANNames\"]]\r\ 243 | \n\t\tglobal WANGateways [toarray [\$parseConfig \"WANGateways\"]]\r\ 244 | \n\t\tglobal WANGatewayPrefix [\$parseConfig \"WANGatewayPrefix\"]\r\ 245 | \n\t\tglobal BalancingRulePrefix [\$parseConfig \"BalancingRulePrefix\"]\r\ 246 | \n\r\ 247 | \n\t\t#Validate that the mandatory variables are not empty\r\ 248 | \n\t\tlocal cfgMandatoryVars {\"WANNames\";\"WANGateways\";\"WANGatewayPrefix\";\"BalancingRulePrefix\"}\r\ 249 | \n\t\tlocal cfgMandatoryValues {\$WANNames;\$WANGateways;\$WANGatewayPrefix;\$BalancingRulePrefix}\r\ 250 | \n\t\tset execStatus [\$validateVars \$cfgMandatoryVars \$cfgMandatoryValues]\r\ 251 | \n\r\ 252 | \n\t\t#If the WAN names or gateways contain duplicate values\r\ 253 | \n\t\tlocal arrays {\$WANNames;\$WANGateways}\r\ 254 | \n\t\tif ([\$containsDuplicates \$arrays]) do={\r\ 255 | \n\t\t\t#Throw error and adjust execution status\r\ 256 | \n\t\t\tset execStatus [\$error (\"Gateways configuration contains duplicate values.\")]\r\ 257 | \n\t\t}\r\ 258 | \n\r\ 259 | \n\t\t#If the WAN name and gateway pairings are even\r\ 260 | \n\t\tif ([len \$WANNames]=[len \$WANGateways]) do={\r\ 261 | \n\t\t\t#Iterate through the gateways\r\ 262 | \n\t\t\tforeach wanIndex,WANName in=\$WANNames do={\r\ 263 | \n\t\t\t\t#If it's assigned with a proper IP\r\ 264 | \n\t\t\t\tif ([typeof [toip (\$WANGateways->\$wanIndex)]]=\"ip\") do={\r\ 265 | \n\t\t\t\t\t#If it does not have a corresponding default route\r\ 266 | \n\t\t\t\t\tif ([/ip route find gateway=(\$WANGateways->\$wanIndex) dst-address=0.0.0.0/0 comment~\$WANGatewayPrefix]=\"\") do={\r\ 267 | \n\t\t\t\t\t\t#Throw error and adjust execution status\r\ 268 | \n\t\t\t\t\t\tset execStatus [\$error (\"Gateways configuration for \$WANName does not have a corresponding default route.\")]\r\ 269 | \n\t\t\t\t\t}\r\ 270 | \n\t\t\t\t} else {\r\ 271 | \n\t\t\t\t\t#Throw error and adjust execution status\r\ 272 | \n\t\t\t\t\tset execStatus [\$error (\"Gateways configuration for \$WANName contains an invalid IP address: \".(\$WANGateways->\$wanIndex).\".\")]\r\ 273 | \n\t\t\t\t}\r\ 274 | \n\t\t\t}\r\ 275 | \n\t\t} else {\r\ 276 | \n\t\t\t#Throw error and adjust execution status\r\ 277 | \n\t\t\tset execStatus [\$error (\"Gateways configuration is incomplete. Declared items are not evenly populated or may be enclosed in quotes.\")]\r\ 278 | \n\t\t}\r\ 279 | \n\r\ 280 | \n\t\t#If any errors occured\r\ 281 | \n\t\tif (\$execStatus<0) do={\r\ 282 | \n\t\t\t#Gain variable access\r\ 283 | \n\t\t\tglobal gateway\r\ 284 | \n\r\ 285 | \n\t\t\t#Clear all corresponding variables\r\ 286 | \n\t\t\tset gateway\r\ 287 | \n\t\t\tset WANNames\r\ 288 | \n\t\t\tset WANGateways\r\ 289 | \n\t\t\tset WANGatewayPrefix\r\ 290 | \n\t\t\tset BalancingRulePrefix\r\ 291 | \n\r\ 292 | \n\t\t\t#Throw error and adjust execution status\r\ 293 | \n\t\t\tset execStatus [\$error (\"The \$1 configuration is invalid, please check the config file and try again.\")]\r\ 294 | \n\t\t} else {\r\ 295 | \n\t\t\t#If the function script is not installed\r\ 296 | \n\t\t\tif ([/system script find name=mod-gateway]=\"\") do={\r\ 297 | \n\t\t\t\t#Throw error and adjust execution status\r\ 298 | \n\t\t\t\tset execStatus [\$error (\"The gateway selector module script is missing, please install it and try again.\")]\r\ 299 | \n\t\t\t} else {\r\ 300 | \n\t\t\t\t#Initialize function\r\ 301 | \n\t\t\t\t/system script run mod-gateway\r\ 302 | \n\t\t\t}\r\ 303 | \n\t\t}\r\ 304 | \n\r\ 305 | \n\t\t#Exit\r\ 306 | \n\t\treturn \$execStatus\r\ 307 | \n\t}\r\ 308 | \n\r\ 309 | \n\t#If function was called with argument \"failover\"\r\ 310 | \n\tif (\$1=\"failover\" && [typeof \$2]=\"nothing\") do={\r\ 311 | \n\t\t#Notify\r\ 312 | \n\t\t\$notify (\"Provisioning \$1.\")\r\ 313 | \n\r\ 314 | \n\t\t#Request variables from config\r\ 315 | \n\t\tglobal FailoverTarget [toip [\$parseConfig \"FailoverTarget\"]]\r\ 316 | \n\t\tglobal FailoverThreshold [tonum [\$parseConfig \"FailoverThreshold\"]]\r\ 317 | \n\t\tglobal FailoverInterval [tonum [\$parseConfig \"FailoverInterval\"]]\r\ 318 | \n\r\ 319 | \n\t\t#Validate that the mandatory variables are not empty\r\ 320 | \n\t\tlocal cfgMandatoryVars {\"FailoverTarget\";\"FailoverThreshold\";\"FailoverInterval\"}\r\ 321 | \n\t\tlocal cfgMandatoryValues {\$FailoverTarget;\$FailoverThreshold;\$FailoverInterval}\r\ 322 | \n\t\tset execStatus [\$validateVars \$cfgMandatoryVars \$cfgMandatoryValues]\r\ 323 | \n\r\ 324 | \n\t\t#If any errors occured\r\ 325 | \n\t\tif (\$execStatus<0) do={\r\ 326 | \n\t\t\t#Gain variable access\r\ 327 | \n\t\t\tglobal failover\r\ 328 | \n\r\ 329 | \n\t\t\t#Clear all corresponding variables\r\ 330 | \n\t\t\tset failover\r\ 331 | \n\t\t\tset FailoverTarget\r\ 332 | \n\t\t\tset FailoverThreshold\r\ 333 | \n\t\t\tset FailoverInterval\r\ 334 | \n\r\ 335 | \n\t\t\t#Throw error and adjust execution status\r\ 336 | \n\t\t\tset execStatus [\$error (\"The \$1 configuration is invalid, please check the config file and try again.\")]\r\ 337 | \n\t\t} else {\r\ 338 | \n\t\t\t#If the function script is not installed\r\ 339 | \n\t\t\tif ([/system script find name=mod-failover]=\"\") do={\r\ 340 | \n\t\t\t\t#Throw error and adjust execution status\r\ 341 | \n\t\t\t\tset execStatus [\$error (\"The failover module script is missing, please install it and try again.\")]\r\ 342 | \n\t\t\t} else {\r\ 343 | \n\t\t\t\t#Gain variable access\r\ 344 | \n\t\t\t\tglobal gateway\r\ 345 | \n\r\ 346 | \n\t\t\t\t#If the gateway function is not present\r\ 347 | \n\t\t\t\tif ([typeof \$gateway]=\"nothing\") do={\r\ 348 | \n\t\t\t\t\t#Throw error and adjust execution status\r\ 349 | \n\t\t\t\t\tset execStatus [\$error (\"The failover module depends on the gateway selector module, please provision it and try again.\")]\r\ 350 | \n\t\t\t\t} else {\r\ 351 | \n\t\t\t\t\t#Gain variable access\r\ 352 | \n\t\t\t\t\tglobal WANNames\r\ 353 | \n\r\ 354 | \n\t\t\t\t\t#Initialize the failover counters string\r\ 355 | \n\t\t\t\t\tglobal FailoverCounters \"\"\r\ 356 | \n\r\ 357 | \n\t\t\t\t\t#For every gateway\r\ 358 | \n\t\t\t\t\tforeach WANName in=\$WANNames do={\r\ 359 | \n\t\t\t\t\t\t#Append an initialization value to it\r\ 360 | \n\t\t\t\t\t\tset FailoverCounters \"\$FailoverCounters,0\"\r\ 361 | \n\t\t\t\t\t}\r\ 362 | \n\r\ 363 | \n\t\t\t\t\t#Convert the failover counters string to an array\r\ 364 | \n\t\t\t\t\tset FailoverCounters [toarray \$FailoverCounters]\r\ 365 | \n\r\ 366 | \n\t\t\t\t\t#Declare helper flag\r\ 367 | \n\t\t\t\t\tlocal cronStatus true\r\ 368 | \n\r\ 369 | \n\t\t\t\t\t#If the scheduler entry for the periodic failover checks exists\r\ 370 | \n\t\t\t\t\tif ([/system scheduler find name=cron-failover]!=\"\") do={\r\ 371 | \n\t\t\t\t\t\t#Fetch its status\r\ 372 | \n\t\t\t\t\t\tset cronStatus [/system scheduler get value-name=disabled [find name=cron-failover]]\r\ 373 | \n\r\ 374 | \n\t\t\t\t\t\t#Remove it\r\ 375 | \n\t\t\t\t\t\t/system scheduler remove [find name=\"cron-failover\"]\r\ 376 | \n\t\t\t\t\t}\r\ 377 | \n\r\ 378 | \n\t\t\t\t\t#Re-add the scheduler entry\r\ 379 | \n\t\t\t\t\t/system scheduler add name=\"cron-failover\" interval=\$FailoverInterval policy=read,write,policy,test disabled=\$cronStatus on-event={global failover;\$failover check}\r\ 380 | \n\r\ 381 | \n\t\t\t\t\t#Initialize function\r\ 382 | \n\t\t\t\t\t/system script run mod-failover\r\ 383 | \n\t\t\t\t}\r\ 384 | \n\t\t\t}\r\ 385 | \n\t\t}\r\ 386 | \n\r\ 387 | \n\t\t#Exit\r\ 388 | \n\t\treturn \$execStatus\r\ 389 | \n\t}\r\ 390 | \n\r\ 391 | \n\t#If function was called with argument \"dyndns\"\r\ 392 | \n\tif (\$1=\"dyndns\" && [typeof \$2]=\"nothing\") do={\r\ 393 | \n\t\t#Notify\r\ 394 | \n\t\t\$notify (\"Provisioning \$1.\")\r\ 395 | \n\r\ 396 | \n\t\t#Request variables from config\r\ 397 | \n\t\tglobal DDNSService [\$parseConfig \"DDNSService\"]\r\ 398 | \n\t\tglobal DDNSInterval [tonum [\$parseConfig \"DDNSInterval\"]]\r\ 399 | \n\t\tglobal DDNSUsername [\$parseConfig \"DDNSUsername\"]\r\ 400 | \n\t\tglobal DDNSPassword [\$parseConfig \"DDNSPassword\"]\r\ 401 | \n\t\tglobal DDNSHostname [\$parseConfig \"DDNSHostname\"]\r\ 402 | \n\r\ 403 | \n\t\t#Validate that the mandatory variables are not empty\r\ 404 | \n\t\tlocal cfgMandatoryVars {\"DDNSService\";\"DDNSInterval\";\"DDNSUsername\";\"DDNSPassword\";\"DDNSHostname\"}\r\ 405 | \n\t\tlocal cfgMandatoryValues {\$DDNSService;\$DDNSInterval;\$DDNSUsername;\$DDNSPassword;\$DDNSHostname}\r\ 406 | \n\t\tset execStatus [\$validateVars \$cfgMandatoryVars \$cfgMandatoryValues]\r\ 407 | \n\r\ 408 | \n\t\t#If any errors occured\r\ 409 | \n\t\tif (\$execStatus<0) do={\r\ 410 | \n\t\t\t#Gain variable access\r\ 411 | \n\t\t\tglobal dyndns\r\ 412 | \n\r\ 413 | \n\t\t\t#Clear all corresponding variables\r\ 414 | \n\t\t\tset dyndns\r\ 415 | \n\t\t\tset DDNSService\r\ 416 | \n\t\t\tset DDNSInterval\r\ 417 | \n\t\t\tset DDNSUsername\r\ 418 | \n\t\t\tset DDNSPassword\r\ 419 | \n\t\t\tset DDNSHostname\r\ 420 | \n\r\ 421 | \n\t\t\t#Throw error and adjust execution status\r\ 422 | \n\t\t\tset execStatus [\$error (\"The \$1 configuration is invalid, please check the config file and try again.\")]\r\ 423 | \n\t\t} else {\r\ 424 | \n\t\t\t#If the function script is not installed\r\ 425 | \n\t\t\tif ([/system script find name=mod-dyndns]=\"\") do={\r\ 426 | \n\t\t\t\t#Throw error and adjust execution status\r\ 427 | \n\t\t\t\tset execStatus [\$error (\"The DynDNS updater module script is missing, please install it and try again.\")]\r\ 428 | \n\t\t\t} else {\r\ 429 | \n\t\t\t\t#Declare helper flag\r\ 430 | \n\t\t\t\tlocal cronStatus true\r\ 431 | \n\r\ 432 | \n\t\t\t\t#If the scheduler entry for the periodic DynDNS updates exists\r\ 433 | \n\t\t\t\tif ([/system scheduler find name=cron-dyndns]!=\"\") do={\r\ 434 | \n\t\t\t\t\t#Fetch its status\r\ 435 | \n\t\t\t\t\tset cronStatus [/system scheduler get value-name=disabled [find name=cron-dyndns]]\r\ 436 | \n\r\ 437 | \n\t\t\t\t\t#Remove it\r\ 438 | \n\t\t\t\t\t/system scheduler remove [find name=\"cron-dyndns\"]\r\ 439 | \n\t\t\t\t}\r\ 440 | \n\r\ 441 | \n\t\t\t\t#Re-add the scheduler entry\r\ 442 | \n\t\t\t\t/system scheduler add name=\"cron-dyndns\" interval=\$DDNSInterval disabled=\$cronStatus on-event={global dyndns;\$dyndns update}\r\ 443 | \n\r\ 444 | \n\t\t\t\t#Initialize function\r\ 445 | \n\t\t\t\t/system script run mod-dyndns\r\ 446 | \n\t\t\t}\r\ 447 | \n\t\t}\r\ 448 | \n\r\ 449 | \n\t\t#Exit\r\ 450 | \n\t\treturn \$execStatus\r\ 451 | \n\t}\r\ 452 | \n\r\ 453 | \n\t#If function was called with argument \"resolver\"\r\ 454 | \n\tif (\$1=\"resolver\" && [typeof \$2]=\"nothing\") do={\r\ 455 | \n\t\t#Notify\r\ 456 | \n\t\t\$notify (\"Provisioning \$1.\")\r\ 457 | \n\r\ 458 | \n\t\t#If the function script is not installed\r\ 459 | \n\t\tif ([/system script find name=mod-resolvefqdn]=\"\") do={\r\ 460 | \n\t\t\t#Throw error and adjust execution status\r\ 461 | \n\t\t\tset execStatus [\$error (\"The FQDN resolver module script is missing, please install it and try again.\")]\r\ 462 | \n\t\t} else {\r\ 463 | \n\t\t\t#Initialize function\r\ 464 | \n\t\t\t/system script run mod-resolvefqdn\r\ 465 | \n\t\t}\r\ 466 | \n\r\ 467 | \n\t\t#Exit\r\ 468 | \n\t\treturn \$execStatus\r\ 469 | \n\t}\r\ 470 | \n\r\ 471 | \n\t#If function was called with argument \"livestream\"\r\ 472 | \n\tif (\$1=\"livestream\" && [typeof \$2]=\"nothing\") do={\r\ 473 | \n\t\t#Notify\r\ 474 | \n\t\t\$notify (\"Provisioning \$1.\")\r\ 475 | \n\r\ 476 | \n\t\t#Request variables from config\r\ 477 | \n\t\tglobal LVStreamList [\$parseConfig \"LVStreamList\"]\r\ 478 | \n\t\tglobal LVStreamFQDN [\$parseConfig \"LVStreamFQDN\"]\r\ 479 | \n\t\tglobal LVStreamRulePrefix [\$parseConfig \"LVStreamRulePrefix\"]\r\ 480 | \n\r\ 481 | \n\t\t#Validate that the mandatory variables are not empty\r\ 482 | \n\t\tlocal cfgMandatoryVars {\"LVStreamList\";\"LVStreamFQDN\";\"LVStreamRulePrefix\"}\r\ 483 | \n\t\tlocal cfgMandatoryValues {\$LVStreamList;\$LVStreamFQDN;\$LVStreamRulePrefix}\r\ 484 | \n\t\tset execStatus [\$validateVars \$cfgMandatoryVars \$cfgMandatoryValues]\r\ 485 | \n\r\ 486 | \n\t\t#If any errors occured\r\ 487 | \n\t\tif (\$execStatus<0) do={\r\ 488 | \n\t\t\t#Gain variable access\r\ 489 | \n\t\t\tglobal livestream\r\ 490 | \n\r\ 491 | \n\t\t\t#Clear all corresponding variables\r\ 492 | \n\t\t\tset livestream\r\ 493 | \n\t\t\tset LVStreamList\r\ 494 | \n\t\t\tset LVStreamFQDN\r\ 495 | \n\t\t\tset LVStreamRulePrefix\r\ 496 | \n\r\ 497 | \n\t\t\t#Throw error and adjust execution status\r\ 498 | \n\t\t\tset execStatus [\$error (\"The \$1 configuration is invalid, please check the config file and try again.\")]\r\ 499 | \n\t\t} else {\r\ 500 | \n\t\t\t#If the function script is not installed\r\ 501 | \n\t\t\tif ([/system script find name=mod-livestream]=\"\") do={\r\ 502 | \n\t\t\t\t#Throw error and adjust execution status\r\ 503 | \n\t\t\t\tset execStatus [\$error (\"The live streaming module script is missing, please install it and try again.\")]\r\ 504 | \n\t\t\t} else {\r\ 505 | \n\t\t\t\t#Gain variable access\r\ 506 | \n\t\t\t\tglobal resolvefqdn\r\ 507 | \n\r\ 508 | \n\t\t\t\t#If the resolvefqdn function is not present\r\ 509 | \n\t\t\t\tif ([typeof \$resolvefqdn]=\"nothing\") do={\r\ 510 | \n\t\t\t\t\t#Throw error and adjust execution status\r\ 511 | \n\t\t\t\t\tset execStatus [\$error (\"The live streaming module depends on the FQDN resolver module, please provision it and try again.\")]\r\ 512 | \n\t\t\t\t} else {\r\ 513 | \n\t\t\t\t\t#Initialize function\r\ 514 | \n\t\t\t\t\t/system script run mod-livestream\r\ 515 | \n\t\t\t\t}\r\ 516 | \n\t\t\t}\r\ 517 | \n\t\t}\r\ 518 | \n\r\ 519 | \n\t\t#Exit\r\ 520 | \n\t\treturn \$execStatus\r\ 521 | \n\t}\r\ 522 | \n\r\ 523 | \n\t#If this part has been reached, it means that no valid arguments were caught\r\ 524 | \n\t#Respond on console with help message\r\ 525 | \n\tput (\"[Provision][Info]: incorrect arguments, try:\")\r\ 526 | \n\tput (\"\\\$provision auto|purge|failover|gateways|dyndns|resolver|livestream\")\r\ 527 | \n\r\ 528 | \n\t#Exit with error\r\ 529 | \n\tset execStatus -1\r\ 530 | \n\treturn \$execStatus\r\ 531 | \n}" 532 | /system script add dont-require-permissions=no name=mod-gateway policy=read,write source="#Gateway selector module script\r\ 533 | \n#Script permissions: read, write\r\ 534 | \n#Script dependencies: mod-provision\r\ 535 | \n\r\ 536 | \n#Function declaration\r\ 537 | \nglobal gateway do={\r\ 538 | \n\t#Error handling functions\r\ 539 | \n\tlocal notify do={local msg \"[Gateway Selector][Info]: \$1\";put \$msg}\r\ 540 | \n\tlocal warn do={local msg \"[Gateway Selector][Warn]: \$1\";put \$msg;log warn \$msg}\r\ 541 | \n\tlocal error do={local msg \"[Gateway Selector][Error]: \$1\";put \$msg;return -1}\r\ 542 | \n\tlocal execStatus 0\r\ 543 | \n\t\r\ 544 | \n\t#Gain variable access\r\ 545 | \n\tglobal WANNames\r\ 546 | \n\tglobal WANGateways\r\ 547 | \n\tglobal WANGatewayPrefix\r\ 548 | \n\tglobal BalancingRulePrefix\r\ 549 | \n\r\ 550 | \n\t#If function was called with argument \"switch\"\r\ 551 | \n\tif (\$1=\"switch\" && [typeof \$2]!=\"nothing\" && [typeof \$3]=\"nothing\") do={\r\ 552 | \n\t\tif (\$2=\"Balancer\") do={\r\ 553 | \n\t\t\t#If there are no load balancing rules present\r\ 554 | \n\t\t\tif ([/ip firewall mangle find comment~\$BalancingRulePrefix]=\"\") do={\r\ 555 | \n\t\t\t\t#Throw error and exit\r\ 556 | \n\t\t\t\tset execStatus [\$error (\"Cannot activate load balancing because there are no corresponding rules present.\")]\r\ 557 | \n\t\t\t\treturn \$execStatus\r\ 558 | \n\t\t\t}\r\ 559 | \n\r\ 560 | \n\t\t\t#Declare helper flag\r\ 561 | \n\t\t\tlocal WANOutage false\r\ 562 | \n\r\ 563 | \n\t\t\t#Iterate through the gateways\r\ 564 | \n\t\t\tforeach WANGateway in=\$WANGateways do={\r\ 565 | \n\t\t\t\t#If there's at least one non-operational gateway\r\ 566 | \n\t\t\t\tif ([/ip route get value-name=distance [find gateway=\$WANGateway comment~\$WANGatewayPrefix]]>[len \$WANGateways]) do={\r\ 567 | \n\t\t\t\t\t#Adjust the helper flag\r\ 568 | \n\t\t\t\t\tset WANOutage true\r\ 569 | \n\t\t\t\t}\r\ 570 | \n\t\t\t}\r\ 571 | \n\r\ 572 | \n\t\t\t#If there's a WAN outage\r\ 573 | \n\t\t\tif (\$WANOutage) do={\r\ 574 | \n\t\t\t\t#If load balancing was not active prior the outage\r\ 575 | \n\t\t\t\tif ([/ip firewall mangle find action=passthrough content=\"Failover\" comment~\$BalancingRulePrefix]=\"\") do={\r\ 576 | \n\t\t\t\t\t#Create semaphore rule\r\ 577 | \n\t\t\t\t\t/ip firewall mangle add chain=input action=passthrough disabled=yes content=\"Failover\" comment=\"\$BalancingRulePrefix\"\r\ 578 | \n\r\ 579 | \n\t\t\t\t\t#Throw warning\r\ 580 | \n\t\t\t\t\t\$warn (\"Load balancing has been selected and will be activated once WAN outage has been resolved.\")\r\ 581 | \n\t\t\t\t} else {\r\ 582 | \n\t\t\t\t\t#Throw warning\r\ 583 | \n\t\t\t\t\t\$warn (\"Load balancing is already selected, but will be activated once the WAN outage has been resolved.\")\r\ 584 | \n\t\t\t\t}\r\ 585 | \n\t\t\t} else {\r\ 586 | \n\t\t\t\t#If load balancing is currently active\r\ 587 | \n\t\t\t\tif ([len [/ip firewall mangle find disabled=no comment~\$BalancingRulePrefix]]>0) do={\r\ 588 | \n\t\t\t\t\t#Notify\r\ 589 | \n\t\t\t\t\t\$notify (\"Load balancing is already active.\")\r\ 590 | \n\t\t\t\t} else {\r\ 591 | \n\t\t\t\t\t#Activate the load balancing rules\r\ 592 | \n\t\t\t\t\t/ip firewall mangle set disabled=no [find comment~\$BalancingRulePrefix]\r\ 593 | \n\r\ 594 | \n\t\t\t\t\t#Notify\r\ 595 | \n\t\t\t\t\t\$notify (\"Switched to: Load balancing\")\r\ 596 | \n\t\t\t\t}\r\ 597 | \n\t\t\t}\r\ 598 | \n\r\ 599 | \n\t\t\t#Exit\r\ 600 | \n\t\t\treturn \$execStatus\r\ 601 | \n\t\t}\r\ 602 | \n\r\ 603 | \n\t\t#Iterate through the gateways\r\ 604 | \n\t\tforeach wanIndex,WANName in=\$WANNames do={\r\ 605 | \n\t\t\t#If the requested gateway exists\r\ 606 | \n\t\t\tif (\$2=\$WANName) do={\r\ 607 | \n\t\t\t\t#Declare helper flags\r\ 608 | \n\t\t\t\tlocal gatewayOperational true\r\ 609 | \n\t\t\t\tlocal disabledBalancer false\r\ 610 | \n\r\ 611 | \n\t\t\t\t#Fetch its distance\r\ 612 | \n\t\t\t\tlocal requestedGatewayDistance [/ip route get value-name=distance [find gateway=(\$WANGateways->\$wanIndex) comment~\$WANGatewayPrefix]]\r\ 613 | \n\r\ 614 | \n\t\t\t\t#If it's non-operational\r\ 615 | \n\t\t\t\tif (\$requestedGatewayDistance>[len \$WANGateways]) do={\r\ 616 | \n\t\t\t\t\t#Adjust for real distance\r\ 617 | \n\t\t\t\t\tset requestedGatewayDistance (\$requestedGatewayDistance-[len \$WANGateways])\r\ 618 | \n\r\ 619 | \n\t\t\t\t\t#Adjust helper flag\r\ 620 | \n\t\t\t\t\tset gatewayOperational false\r\ 621 | \n\t\t\t\t}\r\ 622 | \n\r\ 623 | \n\t\t\t\t#If load balancing is currently active\r\ 624 | \n\t\t\t\tif ([len [/ip firewall mangle find disabled=no comment~\$BalancingRulePrefix]]>0) do={\r\ 625 | \n\t\t\t\t\t#Deactivate the load balancing rules\r\ 626 | \n\t\t\t\t\t/ip firewall mangle set disabled=yes [find comment~\$BalancingRulePrefix]\r\ 627 | \n\r\ 628 | \n\t\t\t\t\t#Adjust helper flag\r\ 629 | \n\t\t\t\t\tset disabledBalancer true\r\ 630 | \n\t\t\t\t}\r\ 631 | \n\r\ 632 | \n\t\t\t\t#If load balancing was active prior a WAN outage\r\ 633 | \n\t\t\t\tif ([/ip firewall mangle find action=passthrough content=\"Failover\" comment~\$BalancingRulePrefix]!=\"\") do={\r\ 634 | \n\t\t\t\t\t#Clean up semaphore rule\r\ 635 | \n\t\t\t\t\t/ip firewall mangle remove [find action=passthrough content=\"Failover\" comment~\$BalancingRulePrefix]\r\ 636 | \n\r\ 637 | \n\t\t\t\t\t#Adjust helper flag\r\ 638 | \n\t\t\t\t\tset disabledBalancer true\r\ 639 | \n\t\t\t\t}\r\ 640 | \n\r\ 641 | \n\t\t\t\t#If it's already the default gateway\r\ 642 | \n\t\t\t\tif (\$requestedGatewayDistance=1) do={\r\ 643 | \n\t\t\t\t\t#If load balancing was just disabled\r\ 644 | \n\t\t\t\t\tif (\$disabledBalancer) do={\r\ 645 | \n\t\t\t\t\t\t#If it's operational\r\ 646 | \n\t\t\t\t\t\tif (\$gatewayOperational) do={\r\ 647 | \n\t\t\t\t\t\t\t#Notify\r\ 648 | \n\t\t\t\t\t\t\t\$notify (\"Switched to: \$WANName\")\r\ 649 | \n\t\t\t\t\t\t} else {\r\ 650 | \n\t\t\t\t\t\t\t#Throw warning\r\ 651 | \n\t\t\t\t\t\t\t\$warn (\"\$WANName has been selected and will be available once it regains WAN access.\")\r\ 652 | \n\t\t\t\t\t\t}\r\ 653 | \n\t\t\t\t\t} else {\r\ 654 | \n\t\t\t\t\t\t#If it's operational\r\ 655 | \n\t\t\t\t\t\tif (\$gatewayOperational) do={\r\ 656 | \n\t\t\t\t\t\t\t#Notify\r\ 657 | \n\t\t\t\t\t\t\t\$notify (\"\$WANName is already the default gateway.\")\r\ 658 | \n\t\t\t\t\t\t} else {\r\ 659 | \n\t\t\t\t\t\t\t#Throw warning\r\ 660 | \n\t\t\t\t\t\t\t\$warn (\"\$WANName is already selected, but will be available once it regains WAN access.\")\r\ 661 | \n\t\t\t\t\t\t}\r\ 662 | \n\t\t\t\t\t}\r\ 663 | \n\r\ 664 | \n\t\t\t\t\t#Exit\r\ 665 | \n\t\t\t\t\treturn \$execStatus\r\ 666 | \n\t\t\t\t}\r\ 667 | \n\r\ 668 | \n\t\t\t\t#Iterate through the gateways\r\ 669 | \n\t\t\t\tforeach WANGateway in=\$WANGateways do={\r\ 670 | \n\t\t\t\t\t#Fetch their distance information\r\ 671 | \n\t\t\t\t\tlocal gatewayDistance [/ip route get value-name=distance [find gateway=\$WANGateway comment~\$WANGatewayPrefix]]\r\ 672 | \n\r\ 673 | \n\t\t\t\t\t#If there are any non-operational ones\r\ 674 | \n\t\t\t\t\tif (\$gatewayDistance>[len \$WANGateways]) do={\r\ 675 | \n\t\t\t\t\t\t#Adjust for real distance\r\ 676 | \n\t\t\t\t\t\tset gatewayDistance (\$gatewayDistance-[len \$WANGateways])\r\ 677 | \n\t\t\t\t\t}\r\ 678 | \n\r\ 679 | \n\t\t\t\t\t#For every gateway's default route that has a distance less than requested one\r\ 680 | \n\t\t\t\t\tif (\$gatewayDistance<\$requestedGatewayDistance) do={\r\ 681 | \n\t\t\t\t\t\t#Increase their distance by one\r\ 682 | \n\t\t\t\t\t\t/ip route set distance=([/ip route get value-name=distance [find gateway=\$WANGateway comment~\$WANGatewayPrefix]]+1) [find gateway=\$WANGateway comment~\$WANGatewayPrefix]\r\ 683 | \n\t\t\t\t\t}\r\ 684 | \n\t\t\t\t}\r\ 685 | \n\r\ 686 | \n\t\t\t\t#Set the minimum distance to one\r\ 687 | \n\t\t\t\tlocal minDistance 1\r\ 688 | \n\r\ 689 | \n\t\t\t\t#If it's non-operational\r\ 690 | \n\t\t\t\tif (!\$gatewayOperational) do={\r\ 691 | \n\t\t\t\t\t#Set the minimum distance to the total number of gateways plus one\r\ 692 | \n\t\t\t\t\tset minDistance ([len \$WANGateways]+1)\r\ 693 | \n\t\t\t\t}\r\ 694 | \n\r\ 695 | \n\t\t\t\t#Set it as the default gateway\r\ 696 | \n\t\t\t\t/ip route set distance=\$minDistance [find gateway=(\$WANGateways->\$wanIndex) comment~\$WANGatewayPrefix]\r\ 697 | \n\r\ 698 | \n\t\t\t\t#If it's operational\r\ 699 | \n\t\t\t\tif (\$gatewayOperational) do={\r\ 700 | \n\t\t\t\t\t#Notify\r\ 701 | \n\t\t\t\t\t\$notify (\"Switched to: \$WANName\")\r\ 702 | \n\t\t\t\t} else {\r\ 703 | \n\t\t\t\t\t#Throw warning\r\ 704 | \n\t\t\t\t\t\$warn (\"\$WANName has been selected and will be available once it regains WAN access.\")\r\ 705 | \n\t\t\t\t}\r\ 706 | \n\r\ 707 | \n\t\t\t\t#Exit\r\ 708 | \n\t\t\t\treturn \$execStatus\r\ 709 | \n\t\t\t}\r\ 710 | \n\t\t}\r\ 711 | \n\r\ 712 | \n\t\t#If this part has been reached, it means that no valid gateway was caught\r\ 713 | \n\t\t#Throw error and exit\r\ 714 | \n\t\tset execStatus [\$error (\"Cannot switch to \$2 because there's no such gateway declared in the config file.\")]\r\ 715 | \n\t\treturn \$execStatus\r\ 716 | \n\t}\r\ 717 | \n\r\ 718 | \n\t#If function was called with argument \"status\"\r\ 719 | \n\tif (\$1=\"status\" && [typeof \$2]=\"nothing\") do={\r\ 720 | \n\t\t#Find the gateway of least distance\r\ 721 | \n\t\tlocal currGateway \"\"\r\ 722 | \n\t\tlocal currMinDistance ([len \$WANGateways]+1)\r\ 723 | \n\t\tforeach wanIndex,WANName in=\$WANNames do={\r\ 724 | \n\t\t\tif ([/ip route get value-name=distance [find gateway=(\$WANGateways->\$wanIndex) comment~\$WANGatewayPrefix]]<\$currMinDistance) do={\r\ 725 | \n\t\t\t\tset currGateway \$WANName\r\ 726 | \n\t\t\t\tset currMinDistance [/ip route get value-name=distance [find gateway=(\$WANGateways->\$wanIndex) comment~\$WANGatewayPrefix]]\r\ 727 | \n\t\t\t}\r\ 728 | \n\t\t}\r\ 729 | \n\r\ 730 | \n\t\t#If the gateway of least distance is non-operational\r\ 731 | \n\t\tif (\$currMinDistance>[len \$WANGateways]) do={\r\ 732 | \n\t\t\t#Throw warning\r\ 733 | \n\t\t\t\$warn (\"All gateways are non-operational.\")\r\ 734 | \n\t\t} else {\r\ 735 | \n\t\t\t#If load balancing is currently active\r\ 736 | \n\t\t\tif ([len [/ip firewall mangle find disabled=no comment~\$BalancingRulePrefix]]>0) do={\r\ 737 | \n\t\t\t\t#Notify\r\ 738 | \n\t\t\t\t\$notify (\"Currently via: Load balancing\")\r\ 739 | \n\t\t\t} else {\r\ 740 | \n\t\t\t\t#Notify\r\ 741 | \n\t\t\t\t\$notify (\"Currently via: \$currGateway\")\r\ 742 | \n\t\t\t}\r\ 743 | \n\t\t}\r\ 744 | \n\r\ 745 | \n\t\t#Exit\r\ 746 | \n\t\treturn \$execStatus\r\ 747 | \n\t}\r\ 748 | \n\r\ 749 | \n\t#If this part has been reached, it means that no valid arguments were caught\r\ 750 | \n\t#Respond on console with help message\r\ 751 | \n\tput (\"[Gateway Selector][Info]: incorrect arguments, try:\")\r\ 752 | \n\tput (\"\\\$gateway >|status\")\r\ 753 | \n\r\ 754 | \n\t#Exit with error\r\ 755 | \n\tset execStatus -1\r\ 756 | \n\treturn \$execStatus\r\ 757 | \n}" 758 | /system script add dont-require-permissions=no name=mod-failover policy=read,write source="#Failover module script\r\ 759 | \n#Script permissions: read, write\r\ 760 | \n#Script dependencies: mod-provision, mod-gateway\r\ 761 | \n\r\ 762 | \n#Function declaration\r\ 763 | \nglobal failover do={\r\ 764 | \n\t#Error handling functions\r\ 765 | \n\tlocal notify do={local msg \"[Failover][Info]: \$1\";put \$msg;log info \$msg}\r\ 766 | \n\tlocal warn do={local msg \"[Failover][Warn]: \$1\";put \$msg;log warn \$msg}\r\ 767 | \n\tlocal error do={local msg \"[Failover][Error]: \$1\";put \$msg;log error \$msg;return -1}\r\ 768 | \n\tlocal execStatus 0\r\ 769 | \n\r\ 770 | \n\t#Gain variable access\r\ 771 | \n\tglobal gateway\r\ 772 | \n\tglobal WANNames\r\ 773 | \n\tglobal WANGateways\r\ 774 | \n\tglobal WANGatewayPrefix\r\ 775 | \n\tglobal BalancingRulePrefix\r\ 776 | \n\tglobal FailoverCounters\r\ 777 | \n\tglobal FailoverTarget\r\ 778 | \n\tglobal FailoverThreshold\r\ 779 | \n\r\ 780 | \n\t#If the gateway selector function is not present\r\ 781 | \n\tif ([typeof \$gateway]=\"nothing\") do={\r\ 782 | \n\t\t#Throw error and exit\r\ 783 | \n\t\tset execStatus [\$error (\"The gateway selector function is missing. Please ensure that the module is installed and properly provisioned.\")]\r\ 784 | \n\t\treturn \$execStatus\r\ 785 | \n\t}\r\ 786 | \n\r\ 787 | \n\t#If function was called with argument \"check\"\r\ 788 | \n\tif (\$1=\"check\" && [typeof \$2]=\"nothing\") do={\r\ 789 | \n\t\t#WAN availability evaluator subfunction\r\ 790 | \n\t\t#Inputs: \r\ 791 | \n\t\tlocal evaluateAvailability do={\r\ 792 | \n\t\t\t#WAN interface locator subfunction\r\ 793 | \n\t\t\t#Inputs: \r\ 794 | \n\t\t\tlocal locateWANInterface do={\r\ 795 | \n\t\t\t\t#Iterate through the ip address entries\r\ 796 | \n\t\t\t\tforeach entry in=[/ip address find] do={\r\ 797 | \n\t\t\t\t\t#Fetch the address information\r\ 798 | \n\t\t\t\t\tlocal entryAddress [/ip address get value-name=address \$entry]\r\ 799 | \n\r\ 800 | \n\t\t\t\t\t#If it contains a subnet mask\r\ 801 | \n\t\t\t\t\tif ([typeof [find \$entryAddress \"/\"]]=\"num\") do={\r\ 802 | \n\t\t\t\t\t\t#Convert the mask to octet format\r\ 803 | \n\t\t\t\t\t\tlocal entryMask [toip (255.255.255.255<<(32-[pick \$entryAddress ([find \$entryAddress \"/\"]+1) [len \$entryAddress]]))]\r\ 804 | \n\r\ 805 | \n\t\t\t\t\t\t#Calculate the host range\r\ 806 | \n\t\t\t\t\t\tlocal firstHost ([toip ([pick \$entryAddress 0 ([find \$entryAddress \"/\"])]&(\$entryMask))]+1)\r\ 807 | \n\t\t\t\t\t\tlocal lastHost ([toip ([pick \$entryAddress 0 ([find \$entryAddress \"/\"])]|(~\$entryMask))]-1)\r\ 808 | \n\r\ 809 | \n\t\t\t\t\t\t#If the requested address exists within the host range\r\ 810 | \n\t\t\t\t\t\tif (\$1>=\$firstHost && \$1<=\$lastHost) do={\r\ 811 | \n\t\t\t\t\t\t\t#Return the name of the corresponding interface\r\ 812 | \n\t\t\t\t\t\t\treturn [/ip address get value-name=interface \$entry]\r\ 813 | \n\t\t\t\t\t\t}\r\ 814 | \n\t\t\t\t\t}\r\ 815 | \n\t\t\t\t}\r\ 816 | \n\r\ 817 | \n\t\t\t\t#If this part has been reached, it means that no valid interface was caught\r\ 818 | \n\t\t\t\t#Exit\r\ 819 | \n\t\t\t\treturn \"\"\r\ 820 | \n\t\t\t}\r\ 821 | \n\r\ 822 | \n\t\t\t#Error handling functions\r\ 823 | \n\t\t\tlocal notify do={local msg \"[Failover][Info]: \$1\";put \$msg;log info \$msg}\r\ 824 | \n\t\t\tlocal warn do={local msg \"[Failover][Warn]: \$1\";put \$msg;log warn \$msg}\r\ 825 | \n\r\ 826 | \n\t\t\t#Gain variable access\r\ 827 | \n\t\t\tglobal WANNames\r\ 828 | \n\t\t\tglobal WANGateways\r\ 829 | \n\t\t\tglobal WANGatewayPrefix\r\ 830 | \n\t\t\tglobal BalancingRulePrefix\r\ 831 | \n\t\t\tglobal FailoverCounters\r\ 832 | \n\t\t\tglobal FailoverTarget\r\ 833 | \n\t\t\tglobal FailoverThreshold\r\ 834 | \n\r\ 835 | \n\t\t\t#Declare helper flag\r\ 836 | \n\t\t\tlocal pingSuccessful false\r\ 837 | \n\r\ 838 | \n\t\t\t#If the gateway points to a valid interface\r\ 839 | \n\t\t\tlocal WANInterface [\$locateWANInterface (\$WANGateways->\$1)]\r\ 840 | \n\t\t\tif (\$WANInterface!=\"\") do={\r\ 841 | \n\t\t\t\t#Ping the target through that interface and if it succeeds\r\ 842 | \n\t\t\t\tif ([ping \$FailoverTarget count=1 interface=\$WANInterface]>0) do={\r\ 843 | \n\t\t\t\t\t#Adjust helper flag\r\ 844 | \n\t\t\t\t\tset pingSuccessful true\r\ 845 | \n\t\t\t\t}\r\ 846 | \n\t\t\t}\r\ 847 | \n\r\ 848 | \n\t\t\t#If the gateway is operational\r\ 849 | \n\t\t\tif ([/ip route get value-name=distance [find gateway=(\$WANGateways->\$1) comment~\$WANGatewayPrefix]]<=[len \$WANGateways]) do={\r\ 850 | \n\t\t\t\t#If the ping was successful\r\ 851 | \n\t\t\t\tif (\$pingSuccessful) do={\r\ 852 | \n\t\t\t\t\t#If its counter had picked up any failures prior\r\ 853 | \n\t\t\t\t\tif ((\$FailoverCounters->\$1)>0) do={\r\ 854 | \n\t\t\t\t\t\t#Set it to minus one\r\ 855 | \n\t\t\t\t\t\tset (\$FailoverCounters->\$1) -1\r\ 856 | \n\t\t\t\t\t} else {\r\ 857 | \n\t\t\t\t\t\t#Reduce it by one\r\ 858 | \n\t\t\t\t\t\tset (\$FailoverCounters->\$1) ((\$FailoverCounters->\$1)-1)\r\ 859 | \n\t\t\t\t\t}\r\ 860 | \n\t\t\t\t} else {\r\ 861 | \n\t\t\t\t\t#If its counter had not picked up any failures prior\r\ 862 | \n\t\t\t\t\tif ((\$FailoverCounters->\$1)<=0) do={\r\ 863 | \n\t\t\t\t\t\t#Set it to one\r\ 864 | \n\t\t\t\t\t\tset (\$FailoverCounters->\$1) 1\r\ 865 | \n\t\t\t\t\t} else {\r\ 866 | \n\t\t\t\t\t\t#Increase it by one\r\ 867 | \n\t\t\t\t\t\tset (\$FailoverCounters->\$1) ((\$FailoverCounters->\$1)+1)\r\ 868 | \n\t\t\t\t\t}\r\ 869 | \n\r\ 870 | \n\t\t\t\t\t#If its counter met the threshold\r\ 871 | \n\t\t\t\t\tif ((\$FailoverCounters->\$1)=\$FailoverThreshold) do={\r\ 872 | \n\t\t\t\t\t\t#Throw a warning\r\ 873 | \n\t\t\t\t\t\t\$warn (\"WAN outage detected on \".(\$WANNames->\$1).\".\")\r\ 874 | \n\r\ 875 | \n\t\t\t\t\t\t#Increase its default route distance\r\ 876 | \n\t\t\t\t\t\t/ip route set distance=([/ip route get value-name=distance [find gateway=(\$WANGateways->\$1) comment~\$WANGatewayPrefix]]+[/len \$WANGateways]) [find gateway=(\$WANGateways->\$1) comment~\$WANGatewayPrefix]\r\ 877 | \n\r\ 878 | \n\t\t\t\t\t\t#If load balancing is currently active\r\ 879 | \n\t\t\t\t\t\tif ([len [/ip firewall mangle find disabled=no comment~\$BalancingRulePrefix]]>0) do={\r\ 880 | \n\t\t\t\t\t\t\t#Deactivate its rules\r\ 881 | \n\t\t\t\t\t\t\t/ip firewall mangle set disabled=yes [find comment~\$BalancingRulePrefix]\r\ 882 | \n\r\ 883 | \n\t\t\t\t\t\t\t#Create semaphore rule\r\ 884 | \n\t\t\t\t\t\t\t/ip firewall mangle add chain=input action=passthrough disabled=yes content=\"Failover\" comment=\"\$BalancingRulePrefix\"\r\ 885 | \n\r\ 886 | \n\t\t\t\t\t\t\t#Throw a warning\r\ 887 | \n\t\t\t\t\t\t\t\$warn (\"Load balancing has been temporarily disabled until all gateways regain WAN access.\")\r\ 888 | \n\t\t\t\t\t\t}\r\ 889 | \n\r\ 890 | \n\t\t\t\t\t\t#Declare helper flag\r\ 891 | \n\t\t\t\t\t\tlocal WANOutage true\r\ 892 | \n\r\ 893 | \n\t\t\t\t\t\t#Iterate through the gateways\r\ 894 | \n\t\t\t\t\t\tforeach WANGateway in=\$WANGateways do={\r\ 895 | \n\t\t\t\t\t\t\t#If there's at least one that's operational\r\ 896 | \n\t\t\t\t\t\t\tif ([/ip route get value-name=distance [find gateway=\$WANGateway comment~\$WANGatewayPrefix]]<=[len \$WANGateways]) do={\r\ 897 | \n\t\t\t\t\t\t\t\t#Adjust the helper flag\r\ 898 | \n\t\t\t\t\t\t\t\tset WANOutage false\r\ 899 | \n\t\t\t\t\t\t\t}\r\ 900 | \n\t\t\t\t\t\t}\r\ 901 | \n\r\ 902 | \n\t\t\t\t\t\t#If all gateways are non-operational\r\ 903 | \n\t\t\t\t\t\tif (\$WANOutage) do={\r\ 904 | \n\t\t\t\t\t\t\t#Throw a warning\r\ 905 | \n\t\t\t\t\t\t\t\$warn (\"WAN outage detected on all gateways.\")\r\ 906 | \n\t\t\t\t\t\t}\r\ 907 | \n\t\t\t\t\t}\r\ 908 | \n\t\t\t\t\t\r\ 909 | \n\t\t\t\t}\r\ 910 | \n\t\t\t} else {\r\ 911 | \n\t\t\t\t#If the ping was successful\r\ 912 | \n\t\t\t\tif (\$pingSuccessful) do={\r\ 913 | \n\t\t\t\t\t#Reset its counter to zero\r\ 914 | \n\t\t\t\t\tset (\$FailoverCounters->\$1) 0\r\ 915 | \n\r\ 916 | \n\t\t\t\t\t#Decrease its default route distance\r\ 917 | \n\t\t\t\t\t/ip route set distance=([/ip route get value-name=distance [find gateway=(\$WANGateways->\$1) comment~\$WANGatewayPrefix]]-[/len \$WANGateways]) [find gateway=(\$WANGateways->\$1) comment~\$WANGatewayPrefix]\r\ 918 | \n\r\ 919 | \n\t\t\t\t\t#Notify\r\ 920 | \n\t\t\t\t\t\$notify (\"Gateway \".(\$WANNames->\$1).\" regained WAN access.\")\r\ 921 | \n\r\ 922 | \n\t\t\t\t\t#Declare helper flag\r\ 923 | \n\t\t\t\t\tlocal WANOutage false\r\ 924 | \n\r\ 925 | \n\t\t\t\t\t#Iterate through the gateways\r\ 926 | \n\t\t\t\t\tforeach WANGateway in=\$WANGateways do={\r\ 927 | \n\t\t\t\t\t\t#If there's at least one that's non-operational\r\ 928 | \n\t\t\t\t\t\tif ([/ip route get value-name=distance [find gateway=\$WANGateway comment~\$WANGatewayPrefix]]>[len \$WANGateways]) do={\r\ 929 | \n\t\t\t\t\t\t\t#Adjust the helper flag\r\ 930 | \n\t\t\t\t\t\t\tset WANOutage true\r\ 931 | \n\t\t\t\t\t\t}\r\ 932 | \n\t\t\t\t\t}\r\ 933 | \n\r\ 934 | \n\t\t\t\t\t#If all gateways are operational\r\ 935 | \n\t\t\t\t\tif (!\$WANOutage) do={\r\ 936 | \n\t\t\t\t\t\t#Notify\r\ 937 | \n\t\t\t\t\t\t\$notify (\"All gateways have regained WAN access.\")\r\ 938 | \n\r\ 939 | \n\t\t\t\t\t\t#If load balancing was enabled prior the outage\r\ 940 | \n\t\t\t\t\t\tif ([/ip firewall mangle find action=passthrough content=\"Failover\" comment~\$BalancingRulePrefix]!=\"\") do={\r\ 941 | \n\t\t\t\t\t\t\t#Clean up semaphore rule\r\ 942 | \n\t\t\t\t\t\t\t/ip firewall mangle remove [find action=passthrough content=\"Failover\" comment~\$BalancingRulePrefix]\r\ 943 | \n\r\ 944 | \n\t\t\t\t\t\t\t#Activate its rules\r\ 945 | \n\t\t\t\t\t\t\t/ip firewall mangle set disabled=no [find comment~\$BalancingRulePrefix]\r\ 946 | \n\r\ 947 | \n\t\t\t\t\t\t\t#Notify\r\ 948 | \n\t\t\t\t\t\t\t\$notify (\"Load balancing has been reactivated because it was previously enabled.\")\r\ 949 | \n\t\t\t\t\t\t}\r\ 950 | \n\t\t\t\t\t}\r\ 951 | \n\t\t\t\t} else {\r\ 952 | \n\t\t\t\t\t#In case the device is coming off of a reboot and the failed gateway's counter has not yet met or surpassed the threshold\r\ 953 | \n\t\t\t\t\tif ((\$FailoverCounters->\$1)<\$FailoverThreshold) do={\r\ 954 | \n\t\t\t\t\t\t#Set it to the threshold\r\ 955 | \n\t\t\t\t\t\tset (\$FailoverCounters->\$1) \$FailoverThreshold\r\ 956 | \n\t\t\t\t\t} else {\r\ 957 | \n\t\t\t\t\t\t#Increase it by one\r\ 958 | \n\t\t\t\t\t\tset (\$FailoverCounters->\$1) ((\$FailoverCounters->\$1)+1)\r\ 959 | \n\t\t\t\t\t}\r\ 960 | \n\t\t\t\t}\r\ 961 | \n\t\t\t}\r\ 962 | \n\t\t}\r\ 963 | \n\r\ 964 | \n\t\t#For every gateway\r\ 965 | \n\t\tforeach wanIndex,WANName in=\$WANNames do={\r\ 966 | \n\t\t\t#Evaluate its availability\r\ 967 | \n\t\t\t\$evaluateAvailability \$wanIndex\r\ 968 | \n\t\t}\r\ 969 | \n\r\ 970 | \n\t\t#Exit\r\ 971 | \n\t\treturn \$execStatus\r\ 972 | \n\t}\r\ 973 | \n\r\ 974 | \n\t#If function was called with argument \"toggle\"\r\ 975 | \n\tif (\$1=\"toggle\" && [typeof \$2]=\"nothing\") do={\r\ 976 | \n\t\t#If the scheduler entry for the periodic failover checks is not enabled\r\ 977 | \n\t\tif ([/system scheduler get value-name=disabled [find name=cron-failover]]) do={\r\ 978 | \n\t\t\t#Enable it\r\ 979 | \n\t\t\t/system scheduler set disabled=no [find name=cron-failover]\r\ 980 | \n\r\ 981 | \n\t\t\t#Notify\r\ 982 | \n\t\t\t\$notify (\"Enabled.\")\r\ 983 | \n\t\t} else {\r\ 984 | \n\t\t\t#For every gateway\r\ 985 | \n\t\t\tforeach WANGateway in=\$WANGateways do={\r\ 986 | \n\t\t\t\t#Which is non-operational\r\ 987 | \n\t\t\t\tif ([/ip route get value-name=distance [find gateway=\$WANGateway comment~\$WANGatewayPrefix]]>[len \$WANGateways]) do={\r\ 988 | \n\t\t\t\t\t#Restore its distance\r\ 989 | \n\t\t\t\t\t/ip route set distance=([/ip route get value-name=distance [find gateway=\$WANGateway comment~\$WANGatewayPrefix]]-[/len \$WANGateways]) [find gateway=\$WANGateway comment~\$WANGatewayPrefix]\r\ 990 | \n\t\t\t\t}\r\ 991 | \n\t\t\t}\r\ 992 | \n\r\ 993 | \n\t\t\t#If load balancing was enabled prior the outage\r\ 994 | \n\t\t\tif ([/ip firewall mangle find content=\"Failover\" comment~\$BalancingRulePrefix]!=\"\") do={\r\ 995 | \n\t\t\t\t#Clean up semaphore rule\r\ 996 | \n\t\t\t\t/ip firewall mangle remove [find action=passthrough content=\"Failover\" comment~\$BalancingRulePrefix]\r\ 997 | \n\r\ 998 | \n\t\t\t\t#Activate its rules\r\ 999 | \n\t\t\t\t/ip firewall mangle set disabled=no [find comment~\$BalancingRulePrefix]\t\t\t\t\r\ 1000 | \n\t\t\t}\r\ 1001 | \n\r\ 1002 | \n\t\t\t#Disable it\r\ 1003 | \n\t\t\t/system scheduler set disabled=yes [find name=cron-failover]\r\ 1004 | \n\r\ 1005 | \n\t\t\t#Notify\r\ 1006 | \n\t\t\t\$notify (\"Disabled.\")\r\ 1007 | \n\t\t}\r\ 1008 | \n\r\ 1009 | \n\t\t#Exit\r\ 1010 | \n\t\treturn \$execStatus\r\ 1011 | \n\t}\r\ 1012 | \n\r\ 1013 | \n\t#If function was called with argument \"status\"\r\ 1014 | \n\tif (\$1=\"status\" && [typeof \$2]=\"nothing\") do={\r\ 1015 | \n\t\t#Error handling functions\r\ 1016 | \n\t\tlocal notifyCon do={local msg \"[Failover][Info]: \$1\";put \$msg}\r\ 1017 | \n\r\ 1018 | \n\t\t#If the scheduler entry for the periodic failover checks is not enabled\r\ 1019 | \n\t\tif ([/system scheduler get value-name=disabled [find name=cron-failover]]) do={\r\ 1020 | \n\t\t\t#Notify\r\ 1021 | \n\t\t\t\$notifyCon (\"Failover status: Inactive.\")\r\ 1022 | \n\t\t} else {\r\ 1023 | \n\t\t\t#Notify\r\ 1024 | \n\t\t\t\$notifyCon (\"Failover status: Active.\")\r\ 1025 | \n\t\t}\r\ 1026 | \n\r\ 1027 | \n\t\t#Declare helper string\r\ 1028 | \n\t\tlocal WANStatus\r\ 1029 | \n\r\ 1030 | \n\t\t#Iterate through the failover counters\r\ 1031 | \n\t\tforeach wanIndex,WANName in=\$WANNames do={\r\ 1032 | \n\t\t\t#If the counter value greater than zero\r\ 1033 | \n\t\t\tif ((\$FailoverCounters->\$wanIndex)>0) do={\r\ 1034 | \n\t\t\t\t#Append the downtime estimation to the message\r\ 1035 | \n\t\t\t\tset WANStatus (\"\$WANName: Offline\\t(down for at least \".((\$FailoverCounters->\$wanIndex)*[/system scheduler get value-name=interval [find name=cron-failover]]).\")\")\r\ 1036 | \n\t\t\t} else {\r\ 1037 | \n\t\t\t\tset WANStatus (\"\$WANName: Online\\t(up for at least \".((\$FailoverCounters->\$wanIndex)*(-1)*[/system scheduler get value-name=interval [find name=cron-failover]]).\")\")\r\ 1038 | \n\t\t\t}\r\ 1039 | \n\r\ 1040 | \n\t\t\t#Notify\r\ 1041 | \n\t\t\t\$notifyCon \$WANStatus\r\ 1042 | \n\t\t}\r\ 1043 | \n\r\ 1044 | \n\t\t#Exit\r\ 1045 | \n\t\treturn \$execStatus\r\ 1046 | \n\t}\r\ 1047 | \n\r\ 1048 | \n\t#If this part has been reached, it means that no valid arguments were caught\r\ 1049 | \n\t#Respond on console with help message\r\ 1050 | \n\tput (\"[Failover][Info]: incorrect arguments, try:\")\r\ 1051 | \n\tput (\"\\\$failover check|toggle|status\")\r\ 1052 | \n\r\ 1053 | \n\t#Exit with error\r\ 1054 | \n\tset execStatus -1\r\ 1055 | \n\treturn \$execStatus\r\ 1056 | \n}" 1057 | /system script add dont-require-permissions=no name=mod-dyndns policy=read,write source="#DynDNS updater module script\r\ 1058 | \n#Script permissions: read, write\r\ 1059 | \n#Script dependencies: mod-provision\r\ 1060 | \n#Extra requirements: at least one DNS server must be configured\r\ 1061 | \n\r\ 1062 | \n#Function declaration\r\ 1063 | \nglobal dyndns do={\r\ 1064 | \n\t#Error handling functions\r\ 1065 | \n\tlocal notify do={local msg \"[DynDNS Updater][Info]: \$1\";put \$msg;log info \$msg}\r\ 1066 | \n\tlocal error do={local msg \"[DynDNS Updater][Error]: \$1\";put \$msg;log error \$msg;return -1}\r\ 1067 | \n\tlocal execStatus 0\r\ 1068 | \n\r\ 1069 | \n\t#If function was called with argument \"update\"\r\ 1070 | \n\tif (\$1=\"update\" && [typeof \$2]=\"nothing\") do={\r\ 1071 | \n\t\t#If there are no DNS servers configured\r\ 1072 | \n\t\tif ([/ip dns get value-name=servers]=\"\") do={\r\ 1073 | \n\t\t\t#Throw error and exit\r\ 1074 | \n\t\t\tset execStatus [\$error (\"At least one DNS server must be configured.\")]\r\ 1075 | \n\t\t\treturn \$execStatus\r\ 1076 | \n\t\t}\r\ 1077 | \n\r\ 1078 | \n\t\t#Gain variable access\r\ 1079 | \n\t\tglobal DDNSService\r\ 1080 | \n\t\tglobal DDNSUsername\r\ 1081 | \n\t\tglobal DDNSPassword\r\ 1082 | \n\t\tglobal DDNSHostname\r\ 1083 | \n\t\tglobal WANAddress\r\ 1084 | \n\r\ 1085 | \n\t\t#Declare helper string\r\ 1086 | \n\t\tlocal updateURL\r\ 1087 | \n\r\ 1088 | \n\t\t#If the service is DynDNS\r\ 1089 | \n\t\tif (\$DDNSService=\"DynDNS\") do={\r\ 1090 | \n\t\t\t#Adjust the helper string\r\ 1091 | \n\t\t\tset updateURL \"https://members.dyndns.org/v3/update\"\t\t\t\r\ 1092 | \n\t\t}\r\ 1093 | \n\r\ 1094 | \n\t\t#If the service is NoIP\r\ 1095 | \n\t\tif (\$DDNSService=\"NoIP\") do={\r\ 1096 | \n\t\t\t#Adjust the helper string\r\ 1097 | \n\t\t\tset updateURL \"https://dynupdate.no-ip.com/nic/update\"\r\ 1098 | \n\t\t}\r\ 1099 | \n\r\ 1100 | \n\t\t#If no valid service was specified\r\ 1101 | \n\t\tif ([typeof \$updateURL]=\"nothing\") do={\r\ 1102 | \n\t\t\t#Throw error and exit\r\ 1103 | \n\t\t\tset execStatus [\$error (\"Invalid service configured. Please use either DynDNS or NoIP in the config and try again.\")]\r\ 1104 | \n\t\t\treturn \$execStatus\r\ 1105 | \n\t\t}\r\ 1106 | \n\r\ 1107 | \n\t\t#Poll opendns.com to discover the current WAN address\r\ 1108 | \n\t\tlocal currWANAddress [toip [resolve myip.opendns.com server=resolver1.opendns.com]]\r\ 1109 | \n\r\ 1110 | \n\t\t#If the stored WAN address is different from the one that was just fetched\r\ 1111 | \n\t\tif (\$WANAddress!=\$currWANAddress) do={\r\ 1112 | \n\t\t\t#Update the stored WAN address\r\ 1113 | \n\t\t\tset WANAddress \$currWANAddress\r\ 1114 | \n\r\ 1115 | \n\t\t\t#Notify\r\ 1116 | \n\t\t\t\$notify (\"New WAN Address detected: \$WANAddress - Updating \$DDNSService.\")\r\ 1117 | \n\r\ 1118 | \n\t\t\t#Update over HTTP\r\ 1119 | \n\t\t\t/tool fetch url=(\"\$updateURL\?hostname=\$DDNSHostname&myip=\$WANAddress\") user=\$DDNSUsername password=\$DDNSPassword mode=https keep-result=no\r\ 1120 | \n\t\t}\r\ 1121 | \n\r\ 1122 | \n\t\t#Exit\r\ 1123 | \n\t\treturn \$execStatus\r\ 1124 | \n\t}\r\ 1125 | \n\r\ 1126 | \n\t#If function was called with argument \"toggle\"\r\ 1127 | \n\tif (\$1=\"toggle\" && [typeof \$2]=\"nothing\") do={\r\ 1128 | \n\t\t#If the scheduler entry for the periodic DynDNS updates is not enabled\r\ 1129 | \n\t\tif ([/system scheduler get value-name=disabled [find name=cron-dyndns]]) do={\r\ 1130 | \n\t\t\t#Enable it\r\ 1131 | \n\t\t\t/system scheduler set disabled=no [find name=cron-dyndns]\r\ 1132 | \n\r\ 1133 | \n\t\t\t#Notify\r\ 1134 | \n\t\t\t\$notify (\"Enabled.\")\r\ 1135 | \n\t\t} else {\r\ 1136 | \n\t\t\t#Disable it\r\ 1137 | \n\t\t\t/system scheduler set disabled=yes [find name=cron-dyndns]\r\ 1138 | \n\r\ 1139 | \n\t\t\t#Notify\r\ 1140 | \n\t\t\t\$notify (\"Disabled.\")\r\ 1141 | \n\t\t}\r\ 1142 | \n\r\ 1143 | \n\t\t#Exit\r\ 1144 | \n\t\treturn \$execStatus\r\ 1145 | \n\t}\r\ 1146 | \n\r\ 1147 | \n\t#If this part has been reached, it means that no valid arguments were caught\r\ 1148 | \n\t#Respond on console with help message\r\ 1149 | \n\tput (\"[DynDNS][Info]: incorrect arguments, try:\")\r\ 1150 | \n\tput (\"\\\$dyndns update|toggle\")\r\ 1151 | \n\r\ 1152 | \n\t#Exit with error\r\ 1153 | \n\tset execStatus -1\r\ 1154 | \n\treturn \$execStatus\r\ 1155 | \n}" 1156 | /system script add dont-require-permissions=no name=mod-resolvefqdn policy=read,write source="#FQDN resolver module script\r\ 1157 | \n#Script permissions: read, write\r\ 1158 | \n#Script dependencies: none\r\ 1159 | \n#Extra requirements: at least one DNS server must be configured\r\ 1160 | \n\r\ 1161 | \n#Function declaration\r\ 1162 | \n#Inputs:
\r\ 1163 | \nglobal resolvefqdn do={\r\ 1164 | \n\t#Error handling functions\r\ 1165 | \n\tlocal error do={local msg \"[FQDN Resolver][Error]: \$1\";put \$msg;log error \$msg;return -1}\r\ 1166 | \n\tlocal execStatus 0\r\ 1167 | \n\r\ 1168 | \n\t#If there are no DNS servers configured\r\ 1169 | \n\tif ([/ip dns get value-name=servers]=\"\") do={\r\ 1170 | \n\t\t#Throw error and exit\r\ 1171 | \n\t\tset execStatus [\$error (\"At least one DNS server must be configured.\")]\r\ 1172 | \n\t\treturn \$execStatus\r\ 1173 | \n\t}\r\ 1174 | \n\r\ 1175 | \n\t#If there are any existing address list entries\r\ 1176 | \n\tif ([len [/ip firewall address-list find list=\$2]]>0) do={\r\ 1177 | \n\t\t#Remove them\r\ 1178 | \n\t\t/ip firewall address-list remove [find list=\$2]\r\ 1179 | \n\t}\r\ 1180 | \n\r\ 1181 | \n\t#Resolve the requested FQDN\r\ 1182 | \n\tresolve \$1\r\ 1183 | \n\r\ 1184 | \n\t#For every produced DNS record that was cached\r\ 1185 | \n\tforeach cachedRecord in=[/ip dns cache all find name=\$1] do={\r\ 1186 | \n\t\t#If its an A record\r\ 1187 | \n\t\tif ([/ip dns cache all get value-name=type \$cachedRecord]=\"A\") do={\r\ 1188 | \n\t\t\t#Add it to the requested address list\r\ 1189 | \n\t\t\t/ip firewall address-list add list=\$2 address=[/ip dns cache all get value-name=data \$cachedRecord] comment=\$1\r\ 1190 | \n\t\t}\r\ 1191 | \n\r\ 1192 | \n\t\t#If its a CNAME record\r\ 1193 | \n\t\tif ([/ip dns cache all get value-name=type \$cachedRecord]=\"CNAME\") do={\r\ 1194 | \n\t\t\t#Declare helper pointers\r\ 1195 | \n\t\t\tlocal currCNAME [/ip dns cache all find name=\$1 type=\"CNAME\"]\r\ 1196 | \n\t\t\tlocal nextCNAME [/ip dns cache all find name=[/ip dns cache all get value-name=data \$currCNAME] type=\"CNAME\"]\r\ 1197 | \n\r\ 1198 | \n\t\t\t#Keep navigating to the next node, until there are no more CNAME records\r\ 1199 | \n\t\t\twhile (\$nextCNAME!=\"\") do={\r\ 1200 | \n\t\t\t\tset currCNAME \$nextCNAME\r\ 1201 | \n\t\t\t\tset nextCNAME [/ip dns cache all find name=[/ip dns cache all get value-name=data \$currCNAME] type=\"CNAME\"]\r\ 1202 | \n\t\t\t}\r\ 1203 | \n\r\ 1204 | \n\t\t\t#For every underlying A record\r\ 1205 | \n\t\t\tforeach ARecord in=[/ip dns cache all find name=[/ip dns cache all get value-name=data \$currCNAME] type=\"A\"] do={\r\ 1206 | \n\t\t\t\t#Add it to the requested address list\r\ 1207 | \n\t\t\t\t/ip firewall address-list add list=\$2 address=[/ip dns cache all get value-name=data \$ARecord] comment=\$1\r\ 1208 | \n\t\t\t}\r\ 1209 | \n\t\t}\r\ 1210 | \n\t}\r\ 1211 | \n\r\ 1212 | \n\t#Exit\r\ 1213 | \n\treturn \$execStatus\r\ 1214 | \n}" 1215 | /system script add dont-require-permissions=no name=mod-livestream policy=read,write source="#Live streaming module script\r\ 1216 | \n#Script permissions: read, write\r\ 1217 | \n#Script dependencies: mod-provision, mod-resolvefqdn\r\ 1218 | \n\r\ 1219 | \n#Function declaration\r\ 1220 | \nglobal livestream do={\r\ 1221 | \n\t#Error handling functions\r\ 1222 | \n\tlocal notify do={local msg \"[Live Streaming][Info]: \$1\";put \$msg}\r\ 1223 | \n\tlocal error do={local msg \"[Live Streaming][Error]: \$1\";put \$msg;return -1}\r\ 1224 | \n\tlocal execStatus 0\r\ 1225 | \n\r\ 1226 | \n\t#Gain variable access\r\ 1227 | \n\tglobal LVStreamList\r\ 1228 | \n\tglobal LVStreamFQDN\r\ 1229 | \n\tglobal LVStreamRulePrefix\r\ 1230 | \n\tglobal resolvefqdn\r\ 1231 | \n\r\ 1232 | \n\t#If the FQDN resolver function is not present\r\ 1233 | \n\tif ([typeof \$resolvefqdn]=\"nothing\") do={\r\ 1234 | \n\t\t#Throw error and exit\r\ 1235 | \n\t\tset execStatus [\$error (\"The FQDN resolver function is missing. Please ensure that the module is installed and properly provisioned.\")]\r\ 1236 | \n\t\treturn \$execStatus\r\ 1237 | \n\t}\r\ 1238 | \n\r\ 1239 | \n\t#If there are no livestreaming rules present\r\ 1240 | \n\tif ([/ip firewall mangle find comment~\$LVStreamRulePrefix]=\"\") do={\r\ 1241 | \n\t\t#Throw error and exit\r\ 1242 | \n\t\tset execStatus [\$error (\"There are no corresponding rules present.\")]\r\ 1243 | \n\t\treturn \$execStatus\r\ 1244 | \n\t}\r\ 1245 | \n\r\ 1246 | \n\t#If function was called with \"toggle\" argument\r\ 1247 | \n\tif (\$1=\"toggle\" && [typeof \$2]=\"nothing\") do={\r\ 1248 | \n\t\t#If livestreaming mode is currently active\r\ 1249 | \n\t\tif ([len [/ip firewall mangle find disabled=no comment~\$LVStreamRulePrefix]]>0) do={\r\ 1250 | \n\t\t\t#Deactivate its rules\r\ 1251 | \n\t\t\t/ip firewall mangle set disabled=yes [find comment~\$LVStreamRulePrefix]\r\ 1252 | \n\r\ 1253 | \n\t\t\t#Clear the streaming service address list entries\r\ 1254 | \n\t\t\t/ip firewall address-list remove [find list=\$LVStreamList]\r\ 1255 | \n\r\ 1256 | \n\t\t\t#Notify\r\ 1257 | \n\t\t\t\$notify (\"Disabled.\")\r\ 1258 | \n\t\t} else {\r\ 1259 | \n\t\t\t#Resolve the streaming service FQDN into the corresponding address list\r\ 1260 | \n\t\t\t\$resolvefqdn \$LVStreamFQDN \$LVStreamList\r\ 1261 | \n\r\ 1262 | \n\t\t\t#Activate its rules\r\ 1263 | \n\t\t\t/ip firewall mangle set disabled=no [find comment~\$LVStreamRulePrefix]\r\ 1264 | \n\r\ 1265 | \n\t\t\t#Notify\r\ 1266 | \n\t\t\t\$notify (\"Enabled.\")\r\ 1267 | \n\t\t}\r\ 1268 | \n\r\ 1269 | \n\t\t#Exit\r\ 1270 | \n\t\treturn \$execStatus\r\ 1271 | \n\t}\r\ 1272 | \n\r\ 1273 | \n\t#If function was called with argument \"status\"\r\ 1274 | \n\tif (\$1=\"status\" && [typeof \$2]=\"nothing\") do={\r\ 1275 | \n\t\t#If livestreaming mode is currently active\r\ 1276 | \n\t\tif ([len [/ip firewall mangle find disabled=no comment~\$LVStreamRulePrefix]]>0) do={\r\ 1277 | \n\t\t\t#Notify\r\ 1278 | \n\t\t\t\$notify (\"Active.\")\r\ 1279 | \n\t\t} else {\r\ 1280 | \n\t\t\t#Notify\r\ 1281 | \n\t\t\t\$notify (\"Inactive.\")\r\ 1282 | \n\t\t}\r\ 1283 | \n\r\ 1284 | \n\t\t#Exit\r\ 1285 | \n\t\treturn \$execStatus\r\ 1286 | \n\t}\r\ 1287 | \n\r\ 1288 | \n\t#If this part has been reached, it means that no valid arguments were caught\r\ 1289 | \n\t#Respond on console with help message\r\ 1290 | \n\tput (\"[Live Streaming][Info]: incorrect arguments, try:\")\r\ 1291 | \n\tput (\"\\\$livestream toggle|status\")\r\ 1292 | \n\r\ 1293 | \n\t#Exit with error\r\ 1294 | \n\tset execStatus -1\r\ 1295 | \n\treturn \$execStatus\r\ 1296 | \n}" 1297 | 1298 | #Provision the router 1299 | /system script run mod-provision 1300 | 1301 | #Cleanup 1302 | /file remove [find name="RouterSCRIPTS_Installer.rsc"] 1303 | 1304 | #Notify 1305 | $notify ("Install finished. Assuming you have read through the supplied readme file, you may now remove any modules that you have no use for, then run: \$provision auto") -------------------------------------------------------------------------------- /RouterSCRIPTS_Uninstaller.rsc: -------------------------------------------------------------------------------- 1 | # RouterSCRIPTS Uninstaller 2 | # Version 1.0.1 3 | 4 | #Error handling functions 5 | local notify do={local msg "[RouterSCRIPTS][Info]: $1";put $msg;log info $msg} 6 | local execStatus 0 7 | 8 | #Remove the scripts 9 | foreach systemScript in={"mod-gateway";"mod-failover";"mod-dyndns";"mod-resolvefqdn";"mod-livestream"} do={ 10 | /system script remove [find name=$systemScript] 11 | } 12 | 13 | #Deprovision the router 14 | execute ("global provision;\$provision purge;set provision;global scriptsVersion;set scriptsVersion") 15 | 16 | #Cleanup 17 | /system script remove [find name="mod-provision"] 18 | /system scheduler remove [find name="init-provision"] 19 | /file remove [find name="RouterSCRIPTS_Uninstaller.rsc"] 20 | 21 | #Notify 22 | $notify ("Uninstall finished.") -------------------------------------------------------------------------------- /dist.cfg: -------------------------------------------------------------------------------- 1 | [Gateways] 2 | WANNames= 3 | WANGateways= 4 | WANGatewayPrefix= 5 | BalancingRulePrefix= 6 | 7 | [Failover] 8 | FailoverTarget= 9 | FailoverThreshold= 10 | FailoverInterval= 11 | 12 | [DynDNS] 13 | DDNSService= 14 | DDNSInterval= 15 | DDNSUsername= 16 | DDNSPassword= 17 | DDNSHostname= 18 | 19 | [Livestream] 20 | LVStreamList= 21 | LVStreamFQDN= 22 | LVStreamRulePrefix= -------------------------------------------------------------------------------- /src/mod-dyndns.txt: -------------------------------------------------------------------------------- 1 | #DynDNS updater module script 2 | #Script permissions: read, write 3 | #Script dependencies: mod-provision 4 | #Extra requirements: at least one DNS server must be configured 5 | 6 | #Function declaration 7 | global dyndns do={ 8 | #Error handling functions 9 | local notify do={local msg "[DynDNS Updater][Info]: $1";put $msg;log info $msg} 10 | local error do={local msg "[DynDNS Updater][Error]: $1";put $msg;log error $msg;return -1} 11 | local execStatus 0 12 | 13 | #If function was called with argument "update" 14 | if ($1="update" && [typeof $2]="nothing") do={ 15 | #If there are no DNS servers configured 16 | if ([/ip dns get value-name=servers]="") do={ 17 | #Throw error and exit 18 | set execStatus [$error ("At least one DNS server must be configured.")] 19 | return $execStatus 20 | } 21 | 22 | #Gain variable access 23 | global DDNSService 24 | global DDNSUsername 25 | global DDNSPassword 26 | global DDNSHostname 27 | global WANAddress 28 | 29 | #Declare helper string 30 | local updateURL 31 | 32 | #If the service is DynDNS 33 | if ($DDNSService="DynDNS") do={ 34 | #Adjust the helper string 35 | set updateURL "https://members.dyndns.org/v3/update" 36 | } 37 | 38 | #If the service is NoIP 39 | if ($DDNSService="NoIP") do={ 40 | #Adjust the helper string 41 | set updateURL "https://dynupdate.no-ip.com/nic/update" 42 | } 43 | 44 | #If no valid service was specified 45 | if ([typeof $updateURL]="nothing") do={ 46 | #Throw error and exit 47 | set execStatus [$error ("Invalid service configured. Please use either DynDNS or NoIP in the config and try again.")] 48 | return $execStatus 49 | } 50 | 51 | #Poll opendns.com to discover the current WAN address 52 | local currWANAddress [toip [resolve myip.opendns.com server=resolver1.opendns.com]] 53 | 54 | #If the stored WAN address is different from the one that was just fetched 55 | if ($WANAddress!=$currWANAddress) do={ 56 | #Update the stored WAN address 57 | set WANAddress $currWANAddress 58 | 59 | #Notify 60 | $notify ("New WAN Address detected: $WANAddress - Updating $DDNSService.") 61 | 62 | #Update over HTTP 63 | /tool fetch url=("$updateURL?hostname=$DDNSHostname&myip=$WANAddress") user=$DDNSUsername password=$DDNSPassword mode=https keep-result=no 64 | } 65 | 66 | #Exit 67 | return $execStatus 68 | } 69 | 70 | #If function was called with argument "toggle" 71 | if ($1="toggle" && [typeof $2]="nothing") do={ 72 | #If the scheduler entry for the periodic DynDNS updates is not enabled 73 | if ([/system scheduler get value-name=disabled [find name=cron-dyndns]]) do={ 74 | #Enable it 75 | /system scheduler set disabled=no [find name=cron-dyndns] 76 | 77 | #Notify 78 | $notify ("Enabled.") 79 | } else { 80 | #Disable it 81 | /system scheduler set disabled=yes [find name=cron-dyndns] 82 | 83 | #Notify 84 | $notify ("Disabled.") 85 | } 86 | 87 | #Exit 88 | return $execStatus 89 | } 90 | 91 | #If this part has been reached, it means that no valid arguments were caught 92 | #Respond on console with help message 93 | put ("[DynDNS][Info]: incorrect arguments, try:") 94 | put ("\$dyndns update|toggle") 95 | 96 | #Exit with error 97 | set execStatus -1 98 | return $execStatus 99 | } -------------------------------------------------------------------------------- /src/mod-failover.txt: -------------------------------------------------------------------------------- 1 | #Failover module script 2 | #Script permissions: read, write 3 | #Script dependencies: mod-provision, mod-gateway 4 | 5 | #Function declaration 6 | global failover do={ 7 | #Error handling functions 8 | local notify do={local msg "[Failover][Info]: $1";put $msg;log info $msg} 9 | local warn do={local msg "[Failover][Warn]: $1";put $msg;log warn $msg} 10 | local error do={local msg "[Failover][Error]: $1";put $msg;log error $msg;return -1} 11 | local execStatus 0 12 | 13 | #Gain variable access 14 | global gateway 15 | global WANNames 16 | global WANGateways 17 | global WANGatewayPrefix 18 | global BalancingRulePrefix 19 | global FailoverCounters 20 | global FailoverTarget 21 | global FailoverThreshold 22 | 23 | #If the gateway selector function is not present 24 | if ([typeof $gateway]="nothing") do={ 25 | #Throw error and exit 26 | set execStatus [$error ("The gateway selector function is missing. Please ensure that the module is installed and properly provisioned.")] 27 | return $execStatus 28 | } 29 | 30 | #If function was called with argument "check" 31 | if ($1="check" && [typeof $2]="nothing") do={ 32 | #WAN availability evaluator subfunction 33 | #Inputs: 34 | local evaluateAvailability do={ 35 | #WAN interface locator subfunction 36 | #Inputs: 37 | local locateWANInterface do={ 38 | #Iterate through the ip address entries 39 | foreach entry in=[/ip address find] do={ 40 | #Fetch the address information 41 | local entryAddress [/ip address get value-name=address $entry] 42 | 43 | #If it contains a subnet mask 44 | if ([typeof [find $entryAddress "/"]]="num") do={ 45 | #Convert the mask to octet format 46 | local entryMask [toip (255.255.255.255<<(32-[pick $entryAddress ([find $entryAddress "/"]+1) [len $entryAddress]]))] 47 | 48 | #Calculate the host range 49 | local firstHost ([toip ([pick $entryAddress 0 ([find $entryAddress "/"])]&($entryMask))]+1) 50 | local lastHost ([toip ([pick $entryAddress 0 ([find $entryAddress "/"])]|(~$entryMask))]-1) 51 | 52 | #If the requested address exists within the host range 53 | if ($1>=$firstHost && $1<=$lastHost) do={ 54 | #Return the name of the corresponding interface 55 | return [/ip address get value-name=interface $entry] 56 | } 57 | } 58 | } 59 | 60 | #If this part has been reached, it means that no valid interface was caught 61 | #Exit 62 | return "" 63 | } 64 | 65 | #Error handling functions 66 | local notify do={local msg "[Failover][Info]: $1";put $msg;log info $msg} 67 | local warn do={local msg "[Failover][Warn]: $1";put $msg;log warn $msg} 68 | 69 | #Gain variable access 70 | global WANNames 71 | global WANGateways 72 | global WANGatewayPrefix 73 | global BalancingRulePrefix 74 | global FailoverCounters 75 | global FailoverTarget 76 | global FailoverThreshold 77 | 78 | #Declare helper flag 79 | local pingSuccessful false 80 | 81 | #If the gateway points to a valid interface 82 | local WANInterface [$locateWANInterface ($WANGateways->$1)] 83 | if ($WANInterface!="") do={ 84 | #Ping the target through that interface and if it succeeds 85 | if ([ping $FailoverTarget count=1 interface=$WANInterface]>0) do={ 86 | #Adjust helper flag 87 | set pingSuccessful true 88 | } 89 | } 90 | 91 | #If the gateway is operational 92 | if ([/ip route get value-name=distance [find gateway=($WANGateways->$1) comment~$WANGatewayPrefix]]<=[len $WANGateways]) do={ 93 | #If the ping was successful 94 | if ($pingSuccessful) do={ 95 | #If its counter had picked up any failures prior 96 | if (($FailoverCounters->$1)>0) do={ 97 | #Set it to minus one 98 | set ($FailoverCounters->$1) -1 99 | } else { 100 | #Reduce it by one 101 | set ($FailoverCounters->$1) (($FailoverCounters->$1)-1) 102 | } 103 | } else { 104 | #If its counter had not picked up any failures prior 105 | if (($FailoverCounters->$1)<=0) do={ 106 | #Set it to one 107 | set ($FailoverCounters->$1) 1 108 | } else { 109 | #Increase it by one 110 | set ($FailoverCounters->$1) (($FailoverCounters->$1)+1) 111 | } 112 | 113 | #If its counter met the threshold 114 | if (($FailoverCounters->$1)=$FailoverThreshold) do={ 115 | #Throw a warning 116 | $warn ("WAN outage detected on ".($WANNames->$1).".") 117 | 118 | #Increase its default route distance 119 | /ip route set distance=([/ip route get value-name=distance [find gateway=($WANGateways->$1) comment~$WANGatewayPrefix]]+[/len $WANGateways]) [find gateway=($WANGateways->$1) comment~$WANGatewayPrefix] 120 | 121 | #If load balancing is currently active 122 | if ([len [/ip firewall mangle find disabled=no comment~$BalancingRulePrefix]]>0) do={ 123 | #Deactivate its rules 124 | /ip firewall mangle set disabled=yes [find comment~$BalancingRulePrefix] 125 | 126 | #Create semaphore rule 127 | /ip firewall mangle add chain=input action=passthrough disabled=yes content="Failover" comment="$BalancingRulePrefix" 128 | 129 | #Throw a warning 130 | $warn ("Load balancing has been temporarily disabled until all gateways regain WAN access.") 131 | } 132 | 133 | #Declare helper flag 134 | local WANOutage true 135 | 136 | #Iterate through the gateways 137 | foreach WANGateway in=$WANGateways do={ 138 | #If there's at least one that's operational 139 | if ([/ip route get value-name=distance [find gateway=$WANGateway comment~$WANGatewayPrefix]]<=[len $WANGateways]) do={ 140 | #Adjust the helper flag 141 | set WANOutage false 142 | } 143 | } 144 | 145 | #If all gateways are non-operational 146 | if ($WANOutage) do={ 147 | #Throw a warning 148 | $warn ("WAN outage detected on all gateways.") 149 | } 150 | } 151 | 152 | } 153 | } else { 154 | #If the ping was successful 155 | if ($pingSuccessful) do={ 156 | #Reset its counter to zero 157 | set ($FailoverCounters->$1) 0 158 | 159 | #Decrease its default route distance 160 | /ip route set distance=([/ip route get value-name=distance [find gateway=($WANGateways->$1) comment~$WANGatewayPrefix]]-[/len $WANGateways]) [find gateway=($WANGateways->$1) comment~$WANGatewayPrefix] 161 | 162 | #Notify 163 | $notify ("Gateway ".($WANNames->$1)." regained WAN access.") 164 | 165 | #Declare helper flag 166 | local WANOutage false 167 | 168 | #Iterate through the gateways 169 | foreach WANGateway in=$WANGateways do={ 170 | #If there's at least one that's non-operational 171 | if ([/ip route get value-name=distance [find gateway=$WANGateway comment~$WANGatewayPrefix]]>[len $WANGateways]) do={ 172 | #Adjust the helper flag 173 | set WANOutage true 174 | } 175 | } 176 | 177 | #If all gateways are operational 178 | if (!$WANOutage) do={ 179 | #Notify 180 | $notify ("All gateways have regained WAN access.") 181 | 182 | #If load balancing was enabled prior the outage 183 | if ([/ip firewall mangle find action=passthrough content="Failover" comment~$BalancingRulePrefix]!="") do={ 184 | #Clean up semaphore rule 185 | /ip firewall mangle remove [find action=passthrough content="Failover" comment~$BalancingRulePrefix] 186 | 187 | #Activate its rules 188 | /ip firewall mangle set disabled=no [find comment~$BalancingRulePrefix] 189 | 190 | #Notify 191 | $notify ("Load balancing has been reactivated because it was previously enabled.") 192 | } 193 | } 194 | } else { 195 | #In case the device is coming off of a reboot and the failed gateway's counter has not yet met or surpassed the threshold 196 | if (($FailoverCounters->$1)<$FailoverThreshold) do={ 197 | #Set it to the threshold 198 | set ($FailoverCounters->$1) $FailoverThreshold 199 | } else { 200 | #Increase it by one 201 | set ($FailoverCounters->$1) (($FailoverCounters->$1)+1) 202 | } 203 | } 204 | } 205 | } 206 | 207 | #For every gateway 208 | foreach wanIndex,WANName in=$WANNames do={ 209 | #Evaluate its availability 210 | $evaluateAvailability $wanIndex 211 | } 212 | 213 | #Exit 214 | return $execStatus 215 | } 216 | 217 | #If function was called with argument "toggle" 218 | if ($1="toggle" && [typeof $2]="nothing") do={ 219 | #If the scheduler entry for the periodic failover checks is not enabled 220 | if ([/system scheduler get value-name=disabled [find name=cron-failover]]) do={ 221 | #Enable it 222 | /system scheduler set disabled=no [find name=cron-failover] 223 | 224 | #Notify 225 | $notify ("Enabled.") 226 | } else { 227 | #For every gateway 228 | foreach WANGateway in=$WANGateways do={ 229 | #Which is non-operational 230 | if ([/ip route get value-name=distance [find gateway=$WANGateway comment~$WANGatewayPrefix]]>[len $WANGateways]) do={ 231 | #Restore its distance 232 | /ip route set distance=([/ip route get value-name=distance [find gateway=$WANGateway comment~$WANGatewayPrefix]]-[/len $WANGateways]) [find gateway=$WANGateway comment~$WANGatewayPrefix] 233 | } 234 | } 235 | 236 | #If load balancing was enabled prior the outage 237 | if ([/ip firewall mangle find content="Failover" comment~$BalancingRulePrefix]!="") do={ 238 | #Clean up semaphore rule 239 | /ip firewall mangle remove [find action=passthrough content="Failover" comment~$BalancingRulePrefix] 240 | 241 | #Activate its rules 242 | /ip firewall mangle set disabled=no [find comment~$BalancingRulePrefix] 243 | } 244 | 245 | #Disable it 246 | /system scheduler set disabled=yes [find name=cron-failover] 247 | 248 | #Notify 249 | $notify ("Disabled.") 250 | } 251 | 252 | #Exit 253 | return $execStatus 254 | } 255 | 256 | #If function was called with argument "status" 257 | if ($1="status" && [typeof $2]="nothing") do={ 258 | #Error handling functions 259 | local notifyCon do={local msg "[Failover][Info]: $1";put $msg} 260 | 261 | #If the scheduler entry for the periodic failover checks is not enabled 262 | if ([/system scheduler get value-name=disabled [find name=cron-failover]]) do={ 263 | #Notify 264 | $notifyCon ("Failover status: Inactive.") 265 | } else { 266 | #Notify 267 | $notifyCon ("Failover status: Active.") 268 | } 269 | 270 | #Declare helper string 271 | local WANStatus 272 | 273 | #Iterate through the failover counters 274 | foreach wanIndex,WANName in=$WANNames do={ 275 | #If the counter value greater than zero 276 | if (($FailoverCounters->$wanIndex)>0) do={ 277 | #Append the downtime estimation to the message 278 | set WANStatus ("$WANName: Offline\t(down for at least ".(($FailoverCounters->$wanIndex)*[/system scheduler get value-name=interval [find name=cron-failover]]).")") 279 | } else { 280 | set WANStatus ("$WANName: Online\t(up for at least ".(($FailoverCounters->$wanIndex)*(-1)*[/system scheduler get value-name=interval [find name=cron-failover]]).")") 281 | } 282 | 283 | #Notify 284 | $notifyCon $WANStatus 285 | } 286 | 287 | #Exit 288 | return $execStatus 289 | } 290 | 291 | #If this part has been reached, it means that no valid arguments were caught 292 | #Respond on console with help message 293 | put ("[Failover][Info]: incorrect arguments, try:") 294 | put ("\$failover check|toggle|status") 295 | 296 | #Exit with error 297 | set execStatus -1 298 | return $execStatus 299 | } -------------------------------------------------------------------------------- /src/mod-gateway.txt: -------------------------------------------------------------------------------- 1 | #Gateway selector module script 2 | #Script permissions: read, write 3 | #Script dependencies: mod-provision 4 | 5 | #Function declaration 6 | global gateway do={ 7 | #Error handling functions 8 | local notify do={local msg "[Gateway Selector][Info]: $1";put $msg} 9 | local warn do={local msg "[Gateway Selector][Warn]: $1";put $msg;log warn $msg} 10 | local error do={local msg "[Gateway Selector][Error]: $1";put $msg;return -1} 11 | local execStatus 0 12 | 13 | #Gain variable access 14 | global WANNames 15 | global WANGateways 16 | global WANGatewayPrefix 17 | global BalancingRulePrefix 18 | 19 | #If function was called with argument "switch" 20 | if ($1="switch" && [typeof $2]!="nothing" && [typeof $3]="nothing") do={ 21 | if ($2="Balancer") do={ 22 | #If there are no load balancing rules present 23 | if ([/ip firewall mangle find comment~$BalancingRulePrefix]="") do={ 24 | #Throw error and exit 25 | set execStatus [$error ("Cannot activate load balancing because there are no corresponding rules present.")] 26 | return $execStatus 27 | } 28 | 29 | #Declare helper flag 30 | local WANOutage false 31 | 32 | #Iterate through the gateways 33 | foreach WANGateway in=$WANGateways do={ 34 | #If there's at least one non-operational gateway 35 | if ([/ip route get value-name=distance [find gateway=$WANGateway comment~$WANGatewayPrefix]]>[len $WANGateways]) do={ 36 | #Adjust the helper flag 37 | set WANOutage true 38 | } 39 | } 40 | 41 | #If there's a WAN outage 42 | if ($WANOutage) do={ 43 | #If load balancing was not active prior the outage 44 | if ([/ip firewall mangle find action=passthrough content="Failover" comment~$BalancingRulePrefix]="") do={ 45 | #Create semaphore rule 46 | /ip firewall mangle add chain=input action=passthrough disabled=yes content="Failover" comment="$BalancingRulePrefix" 47 | 48 | #Throw warning 49 | $warn ("Load balancing has been selected and will be activated once WAN outage has been resolved.") 50 | } else { 51 | #Throw warning 52 | $warn ("Load balancing is already selected, but will be activated once the WAN outage has been resolved.") 53 | } 54 | } else { 55 | #If load balancing is currently active 56 | if ([len [/ip firewall mangle find disabled=no comment~$BalancingRulePrefix]]>0) do={ 57 | #Notify 58 | $notify ("Load balancing is already active.") 59 | } else { 60 | #Activate the load balancing rules 61 | /ip firewall mangle set disabled=no [find comment~$BalancingRulePrefix] 62 | 63 | #Notify 64 | $notify ("Switched to: Load balancing") 65 | } 66 | } 67 | 68 | #Exit 69 | return $execStatus 70 | } 71 | 72 | #Iterate through the gateways 73 | foreach wanIndex,WANName in=$WANNames do={ 74 | #If the requested gateway exists 75 | if ($2=$WANName) do={ 76 | #Declare helper flags 77 | local gatewayOperational true 78 | local disabledBalancer false 79 | 80 | #Fetch its distance 81 | local requestedGatewayDistance [/ip route get value-name=distance [find gateway=($WANGateways->$wanIndex) comment~$WANGatewayPrefix]] 82 | 83 | #If it's non-operational 84 | if ($requestedGatewayDistance>[len $WANGateways]) do={ 85 | #Adjust for real distance 86 | set requestedGatewayDistance ($requestedGatewayDistance-[len $WANGateways]) 87 | 88 | #Adjust helper flag 89 | set gatewayOperational false 90 | } 91 | 92 | #If load balancing is currently active 93 | if ([len [/ip firewall mangle find disabled=no comment~$BalancingRulePrefix]]>0) do={ 94 | #Deactivate the load balancing rules 95 | /ip firewall mangle set disabled=yes [find comment~$BalancingRulePrefix] 96 | 97 | #Adjust helper flag 98 | set disabledBalancer true 99 | } 100 | 101 | #If load balancing was active prior a WAN outage 102 | if ([/ip firewall mangle find action=passthrough content="Failover" comment~$BalancingRulePrefix]!="") do={ 103 | #Clean up semaphore rule 104 | /ip firewall mangle remove [find action=passthrough content="Failover" comment~$BalancingRulePrefix] 105 | 106 | #Adjust helper flag 107 | set disabledBalancer true 108 | } 109 | 110 | #If it's already the default gateway 111 | if ($requestedGatewayDistance=1) do={ 112 | #If load balancing was just disabled 113 | if ($disabledBalancer) do={ 114 | #If it's operational 115 | if ($gatewayOperational) do={ 116 | #Notify 117 | $notify ("Switched to: $WANName") 118 | } else { 119 | #Throw warning 120 | $warn ("$WANName has been selected and will be available once it regains WAN access.") 121 | } 122 | } else { 123 | #If it's operational 124 | if ($gatewayOperational) do={ 125 | #Notify 126 | $notify ("$WANName is already the default gateway.") 127 | } else { 128 | #Throw warning 129 | $warn ("$WANName is already selected, but will be available once it regains WAN access.") 130 | } 131 | } 132 | 133 | #Exit 134 | return $execStatus 135 | } 136 | 137 | #Iterate through the gateways 138 | foreach WANGateway in=$WANGateways do={ 139 | #Fetch their distance information 140 | local gatewayDistance [/ip route get value-name=distance [find gateway=$WANGateway comment~$WANGatewayPrefix]] 141 | 142 | #If there are any non-operational ones 143 | if ($gatewayDistance>[len $WANGateways]) do={ 144 | #Adjust for real distance 145 | set gatewayDistance ($gatewayDistance-[len $WANGateways]) 146 | } 147 | 148 | #For every gateway's default route that has a distance less than requested one 149 | if ($gatewayDistance<$requestedGatewayDistance) do={ 150 | #Increase their distance by one 151 | /ip route set distance=([/ip route get value-name=distance [find gateway=$WANGateway comment~$WANGatewayPrefix]]+1) [find gateway=$WANGateway comment~$WANGatewayPrefix] 152 | } 153 | } 154 | 155 | #Set the minimum distance to one 156 | local minDistance 1 157 | 158 | #If it's non-operational 159 | if (!$gatewayOperational) do={ 160 | #Set the minimum distance to the total number of gateways plus one 161 | set minDistance ([len $WANGateways]+1) 162 | } 163 | 164 | #Set it as the default gateway 165 | /ip route set distance=$minDistance [find gateway=($WANGateways->$wanIndex) comment~$WANGatewayPrefix] 166 | 167 | #If it's operational 168 | if ($gatewayOperational) do={ 169 | #Notify 170 | $notify ("Switched to: $WANName") 171 | } else { 172 | #Throw warning 173 | $warn ("$WANName has been selected and will be available once it regains WAN access.") 174 | } 175 | 176 | #Exit 177 | return $execStatus 178 | } 179 | } 180 | 181 | #If this part has been reached, it means that no valid gateway was caught 182 | #Throw error and exit 183 | set execStatus [$error ("Cannot switch to $2 because there's no such gateway declared in the config file.")] 184 | return $execStatus 185 | } 186 | 187 | #If function was called with argument "status" 188 | if ($1="status" && [typeof $2]="nothing") do={ 189 | #Find the gateway of least distance 190 | local currGateway "" 191 | local currMinDistance ([len $WANGateways]+1) 192 | foreach wanIndex,WANName in=$WANNames do={ 193 | if ([/ip route get value-name=distance [find gateway=($WANGateways->$wanIndex) comment~$WANGatewayPrefix]]<$currMinDistance) do={ 194 | set currGateway $WANName 195 | set currMinDistance [/ip route get value-name=distance [find gateway=($WANGateways->$wanIndex) comment~$WANGatewayPrefix]] 196 | } 197 | } 198 | 199 | #If the gateway of least distance is non-operational 200 | if ($currMinDistance>[len $WANGateways]) do={ 201 | #Throw warning 202 | $warn ("All gateways are non-operational.") 203 | } else { 204 | #If load balancing is currently active 205 | if ([len [/ip firewall mangle find disabled=no comment~$BalancingRulePrefix]]>0) do={ 206 | #Notify 207 | $notify ("Currently via: Load balancing") 208 | } else { 209 | #Notify 210 | $notify ("Currently via: $currGateway") 211 | } 212 | } 213 | 214 | #Exit 215 | return $execStatus 216 | } 217 | 218 | #If this part has been reached, it means that no valid arguments were caught 219 | #Respond on console with help message 220 | put ("[Gateway Selector][Info]: incorrect arguments, try:") 221 | put ("\$gateway >|status") 222 | 223 | #Exit with error 224 | set execStatus -1 225 | return $execStatus 226 | } -------------------------------------------------------------------------------- /src/mod-livestream.txt: -------------------------------------------------------------------------------- 1 | #Live streaming module script 2 | #Script permissions: read, write 3 | #Script dependencies: mod-provision, mod-resolvefqdn 4 | 5 | #Function declaration 6 | global livestream do={ 7 | #Error handling functions 8 | local notify do={local msg "[Live Streaming][Info]: $1";put $msg} 9 | local error do={local msg "[Live Streaming][Error]: $1";put $msg;return -1} 10 | local execStatus 0 11 | 12 | #Gain variable access 13 | global LVStreamList 14 | global LVStreamFQDN 15 | global LVStreamRulePrefix 16 | global resolvefqdn 17 | 18 | #If the FQDN resolver function is not present 19 | if ([typeof $resolvefqdn]="nothing") do={ 20 | #Throw error and exit 21 | set execStatus [$error ("The FQDN resolver function is missing. Please ensure that the module is installed and properly provisioned.")] 22 | return $execStatus 23 | } 24 | 25 | #If there are no livestreaming rules present 26 | if ([/ip firewall mangle find comment~$LVStreamRulePrefix]="") do={ 27 | #Throw error and exit 28 | set execStatus [$error ("There are no corresponding rules present.")] 29 | return $execStatus 30 | } 31 | 32 | #If function was called with "toggle" argument 33 | if ($1="toggle" && [typeof $2]="nothing") do={ 34 | #If livestreaming mode is currently active 35 | if ([len [/ip firewall mangle find disabled=no comment~$LVStreamRulePrefix]]>0) do={ 36 | #Deactivate its rules 37 | /ip firewall mangle set disabled=yes [find comment~$LVStreamRulePrefix] 38 | 39 | #Clear the streaming service address list entries 40 | /ip firewall address-list remove [find list=$LVStreamList] 41 | 42 | #Notify 43 | $notify ("Disabled.") 44 | } else { 45 | #Resolve the streaming service FQDN into the corresponding address list 46 | $resolvefqdn $LVStreamFQDN $LVStreamList 47 | 48 | #Activate its rules 49 | /ip firewall mangle set disabled=no [find comment~$LVStreamRulePrefix] 50 | 51 | #Notify 52 | $notify ("Enabled.") 53 | } 54 | 55 | #Exit 56 | return $execStatus 57 | } 58 | 59 | #If function was called with argument "status" 60 | if ($1="status" && [typeof $2]="nothing") do={ 61 | #If livestreaming mode is currently active 62 | if ([len [/ip firewall mangle find disabled=no comment~$LVStreamRulePrefix]]>0) do={ 63 | #Notify 64 | $notify ("Active.") 65 | } else { 66 | #Notify 67 | $notify ("Inactive.") 68 | } 69 | 70 | #Exit 71 | return $execStatus 72 | } 73 | 74 | #If this part has been reached, it means that no valid arguments were caught 75 | #Respond on console with help message 76 | put ("[Live Streaming][Info]: incorrect arguments, try:") 77 | put ("\$livestream toggle|status") 78 | 79 | #Exit with error 80 | set execStatus -1 81 | return $execStatus 82 | } -------------------------------------------------------------------------------- /src/mod-provision.txt: -------------------------------------------------------------------------------- 1 | #Provision module script 2 | #Script permissions: read, write 3 | #Script dependencies: none 4 | 5 | #Declare scripts version 6 | global scriptsVersion "1.0.1" 7 | 8 | #If the scheduler entry for the initialization of the provision module doesn't exist 9 | if ([/system scheduler find name=init-provision]="") do={ 10 | #Add it 11 | /system scheduler add name=init-provision start-time=startup on-event={delay 10;/system script run mod-provision;global provision;$provision auto} 12 | } 13 | 14 | #Provision function 15 | global provision do={ 16 | #Configuration parser subfunction 17 | #Inputs: 18 | #Output: 19 | local parseConfig do={ 20 | #Pull the configuration file contents 21 | local cfgFileContent [/file get value-name=contents [find name~([/system identity get value-name=name].".cfg")]] 22 | 23 | #Detect and adjust the configuration EOL sequence 24 | local cfgEOLSequence "\n" 25 | if ([typeof [find $cfgFileContent "\r\n" 0]]="num") do={set cfgEOLSequence "\r\n"} 26 | 27 | #Declare helper pointers 28 | local cfgLineStart 0 29 | local cfgLineEnd 0 30 | 31 | #Iterate through the configuration file contents 32 | do { 33 | #Find out where the line ends 34 | set cfgLineEnd [find $cfgFileContent $cfgEOLSequence $cfgLineStart] 35 | 36 | #If an EOL sequence cannot be found 37 | if ([typeof $cfgLineEnd]!="num") do={ 38 | #Adjust the line end to the end of contents 39 | set cfgLineEnd [len $cfgFileContent] 40 | } 41 | 42 | #Fetch the line 43 | local cfgLine [pick $cfgFileContent $cfgLineStart $cfgLineEnd] 44 | 45 | #Pull the configuration attribute of the line 46 | local cfgAttribute [pick $cfgLine 0 [find $cfgLine "=" 0]] 47 | 48 | #If the attribute matches the request 49 | if ($cfgAttribute=$1) do={ 50 | #Initialize the configuration value 51 | local cfgValue "" 52 | 53 | #If the value is populated 54 | if ([find $cfgLine "=" 0]!=[len $cfgLine]) do={ 55 | #Set the configuration value 56 | set cfgValue [pick $cfgLine ([find $cfgLine "=" 0]+1) [len $cfgLine]] 57 | 58 | #If the value is a boolean, properly convert it 59 | if ($cfgValue="true") do={set cfgValue true} 60 | if ($cfgValue="false") do={set cfgValue false} 61 | } 62 | 63 | #Return the requested configuration value 64 | return $cfgValue 65 | } 66 | 67 | #Advance to the next line 68 | set cfgLineStart ($cfgLineEnd+[len $cfgEOLSequence]) 69 | } while ($cfgLineStart<[len $cfgFileContent]) 70 | 71 | #If this part has been reached, it means that the requested attribute was not found 72 | #Exit 73 | return "" 74 | } 75 | 76 | #Non-empty variable validator subfunction 77 | #Inputs: 78 | #Output: 79 | local validateVars do={ 80 | #Error handling functions 81 | local error do={local msg "[Provision][Error]: $1";put $msg;log error $msg;return -1} 82 | local execStatus 0 83 | 84 | #Iterate through the requested variables 85 | foreach varIndex,cfgVar in=$1 do={ 86 | #If any of them are not specified or invalid 87 | if (($2->$varIndex)="") do={ 88 | #Throw error and adjust execution status 89 | set execStatus [$error ("The configuration variable $cfgVar cannot be left empty or contain an invalid value.")] 90 | } 91 | } 92 | 93 | #Exit 94 | return $execStatus 95 | } 96 | 97 | #Error handling functions 98 | local notify do={local msg "[Provision][Info]: $1";put $msg;log info $msg} 99 | local error do={local msg "[Provision][Error]: $1";put $msg;log error $msg;return -1} 100 | local execStatus 0 101 | 102 | #If the configuration file does not exist 103 | if ([/file find name~([/system identity get value-name=name].".cfg")]="") do={ 104 | #Throw error and exit 105 | set execStatus [$error ("Unable to locate the config file, please upload it to the router and try again.")] 106 | return $execStatus 107 | } 108 | 109 | #If function was called with argument "auto" 110 | if ($1="auto" && [typeof $2]="nothing") do={ 111 | #Notify 112 | $notify ("Provisioning all available modules.") 113 | 114 | #Gain function access 115 | global provision 116 | 117 | #Iterate through the module inventory 118 | local availCommands "" 119 | local systemScripts {"mod-gateway";"mod-failover";"mod-dyndns";"mod-resolvefqdn";"mod-livestream"} 120 | foreach cmdIndex,moduleName in={"gateways";"failover";"dyndns";"resolver";"livestream"} do={ 121 | #If any of the corresponding scripts are installed 122 | if ([/system script find name=($systemScripts->$cmdIndex)]!="") do={ 123 | #Append their corresponding command to the provision commands string 124 | set availCommands "$availCommands,$moduleName" 125 | } 126 | } 127 | 128 | #Iterate through the available commands 129 | foreach command in=[toarray $availCommands] do={ 130 | #Execute each command and if any error was encountered 131 | if ([$provision $command]<0) do={ 132 | #Adjust the execution status 133 | set execStatus -1 134 | } 135 | } 136 | 137 | #If any errors occured 138 | if ($execStatus<0) do={ 139 | #Throw error 140 | $error ("Completed with errors.") 141 | } else { 142 | #Notify success 143 | $notify ("Completed successfully.") 144 | } 145 | 146 | #Exit 147 | return $execStatus 148 | } 149 | 150 | #If function was called with argument "purge" 151 | if ($1="purge" && [typeof $2]="nothing") do={ 152 | #Notify 153 | $notify ("Automatically purging provisioned configuration.") 154 | 155 | #Construct an inventory of variable names belonging to each module 156 | local varsLiveStream {"livestream";"LVStreamList";"LVStreamFQDN";"LVStreamRulePrefix"} 157 | local varsResolver {"resolvefqdn"} 158 | local varsDynDNS {"dyndns";"WANAddress";"DDNSService";"DDNSInterval";"DDNSUsername";"DDNSPassword";"DDNSHostname"} 159 | local varsFailover {"failover";"FailoverCounters";"FailoverTarget";"FailoverThreshold";"FailoverInterval"} 160 | local varsGateway {"gateway";"WANNames";"WANGateways";"WANGatewayPrefix";"BalancingRulePrefix"} 161 | local moduleVars {$varsLiveStream;$varsResolver;$varsDynDNS;$varsFailover;$varsGateway} 162 | 163 | #Iterate through the system scripts 164 | foreach modIndex,systemScript in={"mod-livestream";"mod-resolvefqdn";"mod-dyndns";"mod-failover";"mod-gateway"} do={ 165 | #If any of them is not installed 166 | if ([/system script find name=$systemScript]="") do={ 167 | #Iterate through its corresponding variables 168 | foreach modVar in=($moduleVars->$modIndex) do={ 169 | #If it's a function variable that possesses a cron job 170 | foreach varWithCron in={"dyndns";"failover"} do={ 171 | if ($modVar=$varWithCron) do={ 172 | #If it's currently active 173 | if ([/system scheduler find disabled=no name="cron-$modVar"]!="") do={ 174 | #Disable it 175 | execute ("global $modVar;\$$modVar toggle") 176 | 177 | #Allow enough time for any additional actions to complete 178 | delay 1 179 | } 180 | 181 | #Remove it 182 | /system scheduler remove [find name="cron-$modVar"] 183 | } 184 | } 185 | 186 | #Clear their corresponding variables 187 | execute ("global ".$modVar."; set ".$modVar) 188 | } 189 | } 190 | } 191 | 192 | #Exit 193 | return $execStatus 194 | } 195 | 196 | #If function was called with argument "gateways" 197 | if ($1="gateways" && [typeof $2]="nothing") do={ 198 | #Array duplicate checker subfunction 199 | #Inputs: 200 | #Output: 201 | local containsDuplicates do={ 202 | #Iterate through the requested arrays 203 | foreach array in=$1 do={ 204 | #Iterate through each array element 205 | foreach primaryElement in=$array do={ 206 | #Declare helper counter 207 | local elementOccurences 0 208 | 209 | #Iterate through each array element a second time 210 | foreach secondaryElement in=$array do={ 211 | #If the element is found 212 | if ($primaryElement=$secondaryElement) do={ 213 | #Adjust the helper counter 214 | set elementOccurences ($elementOccurences+1) 215 | } 216 | } 217 | 218 | #If an element has been found more than once 219 | if ($elementOccurences>1) do={ 220 | #Exit 221 | return true 222 | } 223 | } 224 | } 225 | 226 | #Exit 227 | return false 228 | } 229 | 230 | #Notify 231 | $notify ("Provisioning $1.") 232 | 233 | #Request variables from config 234 | global WANNames [toarray [$parseConfig "WANNames"]] 235 | global WANGateways [toarray [$parseConfig "WANGateways"]] 236 | global WANGatewayPrefix [$parseConfig "WANGatewayPrefix"] 237 | global BalancingRulePrefix [$parseConfig "BalancingRulePrefix"] 238 | 239 | #Validate that the mandatory variables are not empty 240 | local cfgMandatoryVars {"WANNames";"WANGateways";"WANGatewayPrefix";"BalancingRulePrefix"} 241 | local cfgMandatoryValues {$WANNames;$WANGateways;$WANGatewayPrefix;$BalancingRulePrefix} 242 | set execStatus [$validateVars $cfgMandatoryVars $cfgMandatoryValues] 243 | 244 | #If the WAN names or gateways contain duplicate values 245 | local arrays {$WANNames;$WANGateways} 246 | if ([$containsDuplicates $arrays]) do={ 247 | #Throw error and adjust execution status 248 | set execStatus [$error ("Gateways configuration contains duplicate values.")] 249 | } 250 | 251 | #If the WAN name and gateway pairings are even 252 | if ([len $WANNames]=[len $WANGateways]) do={ 253 | #Iterate through the gateways 254 | foreach wanIndex,WANName in=$WANNames do={ 255 | #If it's assigned with a proper IP 256 | if ([typeof [toip ($WANGateways->$wanIndex)]]="ip") do={ 257 | #If it does not have a corresponding default route 258 | if ([/ip route find gateway=($WANGateways->$wanIndex) dst-address=0.0.0.0/0 comment~$WANGatewayPrefix]="") do={ 259 | #Throw error and adjust execution status 260 | set execStatus [$error ("Gateways configuration for $WANName does not have a corresponding default route.")] 261 | } 262 | } else { 263 | #Throw error and adjust execution status 264 | set execStatus [$error ("Gateways configuration for $WANName contains an invalid IP address: ".($WANGateways->$wanIndex).".")] 265 | } 266 | } 267 | } else { 268 | #Throw error and adjust execution status 269 | set execStatus [$error ("Gateways configuration is incomplete. Declared items are not evenly populated or may be enclosed in quotes.")] 270 | } 271 | 272 | #If any errors occured 273 | if ($execStatus<0) do={ 274 | #Gain variable access 275 | global gateway 276 | 277 | #Clear all corresponding variables 278 | set gateway 279 | set WANNames 280 | set WANGateways 281 | set WANGatewayPrefix 282 | set BalancingRulePrefix 283 | 284 | #Throw error and adjust execution status 285 | set execStatus [$error ("The $1 configuration is invalid, please check the config file and try again.")] 286 | } else { 287 | #If the function script is not installed 288 | if ([/system script find name=mod-gateway]="") do={ 289 | #Throw error and adjust execution status 290 | set execStatus [$error ("The gateway selector module script is missing, please install it and try again.")] 291 | } else { 292 | #Initialize function 293 | /system script run mod-gateway 294 | } 295 | } 296 | 297 | #Exit 298 | return $execStatus 299 | } 300 | 301 | #If function was called with argument "failover" 302 | if ($1="failover" && [typeof $2]="nothing") do={ 303 | #Notify 304 | $notify ("Provisioning $1.") 305 | 306 | #Request variables from config 307 | global FailoverTarget [toip [$parseConfig "FailoverTarget"]] 308 | global FailoverThreshold [tonum [$parseConfig "FailoverThreshold"]] 309 | global FailoverInterval [tonum [$parseConfig "FailoverInterval"]] 310 | 311 | #Validate that the mandatory variables are not empty 312 | local cfgMandatoryVars {"FailoverTarget";"FailoverThreshold";"FailoverInterval"} 313 | local cfgMandatoryValues {$FailoverTarget;$FailoverThreshold;$FailoverInterval} 314 | set execStatus [$validateVars $cfgMandatoryVars $cfgMandatoryValues] 315 | 316 | #If any errors occured 317 | if ($execStatus<0) do={ 318 | #Gain variable access 319 | global failover 320 | 321 | #Clear all corresponding variables 322 | set failover 323 | set FailoverTarget 324 | set FailoverThreshold 325 | set FailoverInterval 326 | 327 | #Throw error and adjust execution status 328 | set execStatus [$error ("The $1 configuration is invalid, please check the config file and try again.")] 329 | } else { 330 | #If the function script is not installed 331 | if ([/system script find name=mod-failover]="") do={ 332 | #Throw error and adjust execution status 333 | set execStatus [$error ("The failover module script is missing, please install it and try again.")] 334 | } else { 335 | #Gain variable access 336 | global gateway 337 | 338 | #If the gateway function is not present 339 | if ([typeof $gateway]="nothing") do={ 340 | #Throw error and adjust execution status 341 | set execStatus [$error ("The failover module depends on the gateway selector module, please provision it and try again.")] 342 | } else { 343 | #Gain variable access 344 | global WANNames 345 | 346 | #Initialize the failover counters string 347 | global FailoverCounters "" 348 | 349 | #For every gateway 350 | foreach WANName in=$WANNames do={ 351 | #Append an initialization value to it 352 | set FailoverCounters "$FailoverCounters,0" 353 | } 354 | 355 | #Convert the failover counters string to an array 356 | set FailoverCounters [toarray $FailoverCounters] 357 | 358 | #Declare helper flag 359 | local cronStatus true 360 | 361 | #If the scheduler entry for the periodic failover checks exists 362 | if ([/system scheduler find name=cron-failover]!="") do={ 363 | #Fetch its status 364 | set cronStatus [/system scheduler get value-name=disabled [find name=cron-failover]] 365 | 366 | #Remove it 367 | /system scheduler remove [find name="cron-failover"] 368 | } 369 | 370 | #Re-add the scheduler entry 371 | /system scheduler add name="cron-failover" interval=$FailoverInterval policy=read,write,policy,test disabled=$cronStatus on-event={global failover;$failover check} 372 | 373 | #Initialize function 374 | /system script run mod-failover 375 | } 376 | } 377 | } 378 | 379 | #Exit 380 | return $execStatus 381 | } 382 | 383 | #If function was called with argument "dyndns" 384 | if ($1="dyndns" && [typeof $2]="nothing") do={ 385 | #Notify 386 | $notify ("Provisioning $1.") 387 | 388 | #Request variables from config 389 | global DDNSService [$parseConfig "DDNSService"] 390 | global DDNSInterval [tonum [$parseConfig "DDNSInterval"]] 391 | global DDNSUsername [$parseConfig "DDNSUsername"] 392 | global DDNSPassword [$parseConfig "DDNSPassword"] 393 | global DDNSHostname [$parseConfig "DDNSHostname"] 394 | 395 | #Validate that the mandatory variables are not empty 396 | local cfgMandatoryVars {"DDNSService";"DDNSInterval";"DDNSUsername";"DDNSPassword";"DDNSHostname"} 397 | local cfgMandatoryValues {$DDNSService;$DDNSInterval;$DDNSUsername;$DDNSPassword;$DDNSHostname} 398 | set execStatus [$validateVars $cfgMandatoryVars $cfgMandatoryValues] 399 | 400 | #If any errors occured 401 | if ($execStatus<0) do={ 402 | #Gain variable access 403 | global dyndns 404 | 405 | #Clear all corresponding variables 406 | set dyndns 407 | set DDNSService 408 | set DDNSInterval 409 | set DDNSUsername 410 | set DDNSPassword 411 | set DDNSHostname 412 | 413 | #Throw error and adjust execution status 414 | set execStatus [$error ("The $1 configuration is invalid, please check the config file and try again.")] 415 | } else { 416 | #If the function script is not installed 417 | if ([/system script find name=mod-dyndns]="") do={ 418 | #Throw error and adjust execution status 419 | set execStatus [$error ("The DynDNS updater module script is missing, please install it and try again.")] 420 | } else { 421 | #Declare helper flag 422 | local cronStatus true 423 | 424 | #If the scheduler entry for the periodic DynDNS updates exists 425 | if ([/system scheduler find name=cron-dyndns]!="") do={ 426 | #Fetch its status 427 | set cronStatus [/system scheduler get value-name=disabled [find name=cron-dyndns]] 428 | 429 | #Remove it 430 | /system scheduler remove [find name="cron-dyndns"] 431 | } 432 | 433 | #Re-add the scheduler entry 434 | /system scheduler add name="cron-dyndns" interval=$DDNSInterval disabled=$cronStatus on-event={global dyndns;$dyndns update} 435 | 436 | #Initialize function 437 | /system script run mod-dyndns 438 | } 439 | } 440 | 441 | #Exit 442 | return $execStatus 443 | } 444 | 445 | #If function was called with argument "resolver" 446 | if ($1="resolver" && [typeof $2]="nothing") do={ 447 | #Notify 448 | $notify ("Provisioning $1.") 449 | 450 | #If the function script is not installed 451 | if ([/system script find name=mod-resolvefqdn]="") do={ 452 | #Throw error and adjust execution status 453 | set execStatus [$error ("The FQDN resolver module script is missing, please install it and try again.")] 454 | } else { 455 | #Initialize function 456 | /system script run mod-resolvefqdn 457 | } 458 | 459 | #Exit 460 | return $execStatus 461 | } 462 | 463 | #If function was called with argument "livestream" 464 | if ($1="livestream" && [typeof $2]="nothing") do={ 465 | #Notify 466 | $notify ("Provisioning $1.") 467 | 468 | #Request variables from config 469 | global LVStreamList [$parseConfig "LVStreamList"] 470 | global LVStreamFQDN [$parseConfig "LVStreamFQDN"] 471 | global LVStreamRulePrefix [$parseConfig "LVStreamRulePrefix"] 472 | 473 | #Validate that the mandatory variables are not empty 474 | local cfgMandatoryVars {"LVStreamList";"LVStreamFQDN";"LVStreamRulePrefix"} 475 | local cfgMandatoryValues {$LVStreamList;$LVStreamFQDN;$LVStreamRulePrefix} 476 | set execStatus [$validateVars $cfgMandatoryVars $cfgMandatoryValues] 477 | 478 | #If any errors occured 479 | if ($execStatus<0) do={ 480 | #Gain variable access 481 | global livestream 482 | 483 | #Clear all corresponding variables 484 | set livestream 485 | set LVStreamList 486 | set LVStreamFQDN 487 | set LVStreamRulePrefix 488 | 489 | #Throw error and adjust execution status 490 | set execStatus [$error ("The $1 configuration is invalid, please check the config file and try again.")] 491 | } else { 492 | #If the function script is not installed 493 | if ([/system script find name=mod-livestream]="") do={ 494 | #Throw error and adjust execution status 495 | set execStatus [$error ("The live streaming module script is missing, please install it and try again.")] 496 | } else { 497 | #Gain variable access 498 | global resolvefqdn 499 | 500 | #If the resolvefqdn function is not present 501 | if ([typeof $resolvefqdn]="nothing") do={ 502 | #Throw error and adjust execution status 503 | set execStatus [$error ("The live streaming module depends on the FQDN resolver module, please provision it and try again.")] 504 | } else { 505 | #Initialize function 506 | /system script run mod-livestream 507 | } 508 | } 509 | } 510 | 511 | #Exit 512 | return $execStatus 513 | } 514 | 515 | #If this part has been reached, it means that no valid arguments were caught 516 | #Respond on console with help message 517 | put ("[Provision][Info]: incorrect arguments, try:") 518 | put ("\$provision auto|purge|failover|gateways|dyndns|resolver|livestream") 519 | 520 | #Exit with error 521 | set execStatus -1 522 | return $execStatus 523 | } -------------------------------------------------------------------------------- /src/mod-resolvefqdn.txt: -------------------------------------------------------------------------------- 1 | #FQDN resolver module script 2 | #Script permissions: read, write 3 | #Script dependencies: none 4 | #Extra requirements: at least one DNS server must be configured 5 | 6 | #Function declaration 7 | #Inputs:
8 | global resolvefqdn do={ 9 | #Error handling functions 10 | local error do={local msg "[FQDN Resolver][Error]: $1";put $msg;log error $msg;return -1} 11 | local execStatus 0 12 | 13 | #If there are no DNS servers configured 14 | if ([/ip dns get value-name=servers]="") do={ 15 | #Throw error and exit 16 | set execStatus [$error ("At least one DNS server must be configured.")] 17 | return $execStatus 18 | } 19 | 20 | #If there are any existing address list entries 21 | if ([len [/ip firewall address-list find list=$2]]>0) do={ 22 | #Remove them 23 | /ip firewall address-list remove [find list=$2] 24 | } 25 | 26 | #Resolve the requested FQDN 27 | resolve $1 28 | 29 | #For every produced DNS record that was cached 30 | foreach cachedRecord in=[/ip dns cache all find name=$1] do={ 31 | #If its an A record 32 | if ([/ip dns cache all get value-name=type $cachedRecord]="A") do={ 33 | #Add it to the requested address list 34 | /ip firewall address-list add list=$2 address=[/ip dns cache all get value-name=data $cachedRecord] comment=$1 35 | } 36 | 37 | #If its a CNAME record 38 | if ([/ip dns cache all get value-name=type $cachedRecord]="CNAME") do={ 39 | #Declare helper pointers 40 | local currCNAME [/ip dns cache all find name=$1 type="CNAME"] 41 | local nextCNAME [/ip dns cache all find name=[/ip dns cache all get value-name=data $currCNAME] type="CNAME"] 42 | 43 | #Keep navigating to the next node, until there are no more CNAME records 44 | while ($nextCNAME!="") do={ 45 | set currCNAME $nextCNAME 46 | set nextCNAME [/ip dns cache all find name=[/ip dns cache all get value-name=data $currCNAME] type="CNAME"] 47 | } 48 | 49 | #For every underlying A record 50 | foreach ARecord in=[/ip dns cache all find name=[/ip dns cache all get value-name=data $currCNAME] type="A"] do={ 51 | #Add it to the requested address list 52 | /ip firewall address-list add list=$2 address=[/ip dns cache all get value-name=data $ARecord] comment=$1 53 | } 54 | } 55 | } 56 | 57 | #Exit 58 | return $execStatus 59 | } --------------------------------------------------------------------------------