├── LICENSE.md ├── Makefile ├── README.md ├── bin ├── sshwireguard-amd64.exe └── sshwireguard-linux-amd64.bin ├── clntConsoleHeader.go ├── clntCreateConf.go ├── clntDownloadClientConfig.go ├── clntFileExists.go ├── clntGetClientConfigs.go ├── clntMsgs.go ├── glblVars.go ├── go.mod ├── go.sum ├── gobuild.sh ├── srvCheckOutput.go ├── srvCheckStatus.go ├── srvCtrlWg.go ├── srvRunCmd.go ├── srvRunResults.go ├── sshwireguard.go ├── test ├── sshwireguard-arch-amd64-test └── sshwireguard-ubuntu-amd64-test └── wireguard-manager └── wireguard-manager.sh /LICENSE.md: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: copy-ubuntu 2 | copy-ubuntu: ## Copy the sshwireguard binary to the test on Ubuntu Linux. 3 | @lxc file push test/sshwireguard-ubuntu-amd64-test ubuntuSSHGW/home/duane/ 4 | 5 | .PHONY: ubuntu-cont 6 | ubuntu-cont: ## Drop into an ubuntu container to test the binary. 7 | @lxc exec ubuntuSSHWG -- su --login duane 8 | 9 | # Linux binaries 64-bit 10 | LINUX := env GOOS=linux GOARCH=amd64 go build -o test/sshwireguard 11 | 12 | .PHONY: test 13 | test: ## Build and create test binaries. 14 | env GOOS=windows GOARCH=amd64 go build -o test/sshwireguard-amd64-test.exe 15 | $(LINUX)-ubuntu-amd64-test 16 | $(LINUX)-arch-amd64-test 17 | 18 | 19 | .PHONY: compile 20 | compile: ## Create the final binaries for the given version. 21 | echo "Compiling for every OS and Platform" 22 | env GOOS=windows GOARCH=amd64 go build -o bin/sshwireguard-amd64.exe 23 | env GOOS=linux GOARCH=amd64 go build -o bin/sshwireguard-linux-amd64.bin 24 | 25 | .PHONY: clean 26 | clean: ## Remove test binaries. 27 | go clean 28 | rm -f test/sshwireguard-amd64-test.exe 29 | rm -f test/sshwireguard-linux-amd64-test 30 | rm -f bin/sshwireguard-amd64.exe 31 | rm -f bin/sshwireguard-linux-amd64.bin 32 | 33 | # https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html 34 | .PHONY: help 35 | help: ## This output. 36 | @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' 37 | 38 | .DEFAULT_GOAL := test 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sshwireguard 2 | 3 | **Status:** Actively used by author and actively supported. 4 | 5 | License: [GNU GPL v3.0](https://github.com/thedunston/sshwireguard/blob/master/LICENSE.md) 6 | 7 | sshwireguard is a program that distributes Wireguard VPN configuration files over SSH to clients and automatically starts their Wireguard VPN client. It has replaced the nowire application. 8 | 9 | **Ready for use.** 10 | 11 | ### Why was nowire replaced? 12 | 13 | The nowire application required PHP and Apache to distribute files to users and 3rd party One-time password modules. That was a lot of overhead for a very simple purpose. 14 | 15 | The Wireguard VPN configuration files are now distributed over SSH. Since SSH is used extensively across Linux distributions and is usually a publicly availble service, I decided to use a well-established secure communication and file transfer protocol instead having to manage web SSL certificates, updating PHP, Apache, and third-party modules. 16 | 17 | ### What does sshwireguard do? 18 | 19 | sshwireguard manages the Wireguard application for starting and stopping the service and downloading the configuration files. The SSH application scp is used to download the VPN and YAML configuration files. The YAML file contains configurations that are read by the sshwireguard client. 20 | 21 | In summary, a user runs the command: 22 | 23 | sshwireguard start 24 | 25 | and the WireGuard service is started. If they run: 26 | 27 | sshwireguard stop 28 | 29 | the WireGuard service is stopped. The status of whether or not the service is up and running can be managed with: 30 | 31 | sshwireguard status 32 | 33 | Whether it is used for remote access by roadwarriors or to setup quick peer-to-peer networks, all activity is performed over encrypted communication channels. 34 | 35 | ### Using sshwireguard 36 | 37 | The sshwireguard clients are written in Go. sshwireguard uses the native scp client on Windows 10+ Server 2016+ and Linux. Support will be provided for [pscp](https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html). 38 | 39 | The Wireguard server runs on Linux (WSL has not been tested). It uses a modified version of [ComplexOrganizations](https://github.com/complexorganizations/wireguard-manager) wireguard manager script. The customizations are based on the needs for the automation of creating new VPN configurations. 40 | 41 | Features added include 42 | 43 | - Custom expiration date option per user 44 | - Automated cron job that checks for users and removes them after a given period of time and generates a new VPN configuration 45 | - Expirations can be set for a standard period for all users such as every 48 hours 46 | - Email notification that the VPN configuration is going to expire. One 2 hour notifcation prior to deactivation and the user has to re-authenticate. 47 | - Fill in IP gaps. When a user is removed there is a gap in available IPs since their IP is free. When a new VPN configuration is created, it will use the first available free IP if there are gaps. 48 | 49 | **NOTE** 50 | Multifactor authentication is supported since the native SSH or pscp client is used interactively so no changes are required for existing multifactor authentication methods. Similarly, SSH keys will work normally, as well. 51 | 52 | Accordingly, sshwireguard doesn't manage any multifactor authentication or SSH keys. The sshwireguard client creates an interactive session that allows authentication via SSH to occur just as it did prior to using sshwireguard. 53 | 54 | ## Installation 55 | 56 | ## *Server* 57 | 58 | On the Linux server hosting the Wireguard service, copy the wireguard-manager.sh file to a directory. *Please note that the SSH server does not have to be on the same host as the WireGuard VPN*. The server running SSH is only needed to distribute the WireGuard configuration and config.yaml file for each client. The config.yaml file will have the public IP address of the WireGuard server. 59 | 60 | Make the script executable, run it and follow the prompts. 61 | 62 | chmod +x wireguard-manager.sh 63 | ./wireguard-manager.sh 64 | 65 | Please pay attention to each prompt and input the appropriate values. 66 | 67 | If you want to install without prompts then run: 68 | 69 | ./wireguard-manager.sh --install 70 | 71 | Be aware that it will install the unbound DNS server on the WireGuard VPN server. 72 | 73 | ### Add new users 74 | 75 | ./wireguard-manager.sh --add username 76 | 77 | replace "username" with the user's name. Adding a user will not disrupt existing VPN clients. 78 | 79 | ### Remove a user 80 | 81 | ./wireguard-manager.sh --remove username 82 | 83 | replace "username" with the user's name. Removing a user will not disrupt existing VPN clients. 84 | 85 | ### Add a custom expiration for a user 86 | 87 | ./wireguard-manager.sh --add 88 | 89 | Follow the prompts to create an expiration date. 90 | 91 | Expiring an account can be used in a couple of ways. One is to expire the account for temporary users, at the end of a project, end of a semester, etc. The other is to require users to change their VPN client configuration on a periodic basis such as every 24 or 48 hours, etc. 92 | 93 | TODO: Allow adding a custom expiration for existing users. 94 | 95 | ## *Client setup* 96 | 97 | 1. Download the [Wireguard client](https://www.wireguard.com/install/) for the respective OS. 98 | 2. Most Unix distributions have the scp client already installed. Windows hosts can install the OpenSSH tools via the Microsoft Programs and Features control panel. 99 | 100 | 3. Download the sshwireguard binary for the respective OS. 101 | 102 | 4. On Linux/Unix based systems make the file executable: 103 | 104 | chmod +x sshwireguard-xxx-xxxx.bin 105 | 106 | 5. On Windows, download the sshwireguard executable ending with .exe. 107 | 108 | 6. The first time you run the client, you can execute it and it will check if the *sshwireguardConf* directory exists in the user's home directory. If not, then it will start the process to download the *sshwireguardConf* directory from the WireGuard VPN server. Follow the prompts for the username, IP or domain name for the SSH server, and enter the port for the SSH server. 109 | 110 | 7. Once the files have been downloaded, sshwireguard will attempt to start the WireGuard tunnel. On Unix based systems the client runs "sudo" and on Windows, it will prompt to run with admin privileges. Elevating to admin only occurs when starting or stopping the sshwireguard Tunnel. The interface name is called "sshwireguard." 111 | 112 | TODO: Allow multiple profiles in case a user uses multiple WireGuard servers. 113 | TODO: Create a GUI for the client. 114 | 115 | 8. Managing the Wireguard Tunnel: 116 | 117 | sshwireguard init - Initializes a new VPN configuration 118 | sshwireguard stop - Stops the VPN tunnel 119 | sshwireguard start - Returns whether the sshwireguard Tunnel is running or not running 120 | sshwireguard renew - Downloads a new VPN configuration (reads config.yaml file located in the sshwireguardConf directory) 121 | 122 | ### Compiling clients 123 | 124 | The clients can be compiled from source. First install go 1.16+. 125 | 126 | run: 127 | 128 | make help 129 | 130 | for options. 131 | -------------------------------------------------------------------------------- /bin/sshwireguard-amd64.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thedunston/sshwireguard/18c035171589bc3c13b26770eae632b94bbece26/bin/sshwireguard-amd64.exe -------------------------------------------------------------------------------- /bin/sshwireguard-linux-amd64.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thedunston/sshwireguard/18c035171589bc3c13b26770eae632b94bbece26/bin/sshwireguard-linux-amd64.bin -------------------------------------------------------------------------------- /clntConsoleHeader.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/pterm/pterm" 5 | ) 6 | 7 | // Using the pterm package for cross-platform terminal colors 8 | // and status messages. 9 | func introSSHWireguardScreen() { 10 | 11 | credit := "#########################################\nSSHWireguard - Golang VPN client\nDuane Dunston - dunston@champlain.edu\n#########################################" 12 | pterm.DefaultCenter.Print(pterm.DefaultHeader.WithFullWidth().WithBackgroundStyle(pterm.NewStyle(pterm.BgLightBlue)).WithMargin(10).Sprint(credit)) 13 | 14 | } 15 | -------------------------------------------------------------------------------- /clntCreateConf.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | // Creates the sshwiregard configuration directory and default config file. 8 | // After the user authenticates to the SSH server, their admin provided 9 | // WireGuard VPN configuration file and config file will be downloaded. 10 | func createConf() { 11 | 12 | var username string 13 | var hostname string 14 | var port int 15 | 16 | infoMsg("Creating a new sshwireguard configuration...") 17 | 18 | // Username 19 | fmt.Print("Enter your username on the VPN server: ") 20 | fmt.Scan(&username) 21 | 22 | // hostname 23 | fmt.Print("Enter the IP or Hostname of the VPN server: ") 24 | fmt.Scan(&hostname) 25 | 26 | // port, if other than 22 27 | fmt.Print("Enter the port number: ") 28 | fmt.Scan(&port) 29 | 30 | if port == 0 { 31 | 32 | port = 22 33 | 34 | } 35 | // Get the VPN client configuration 36 | downloadClientConfig(username, hostname, port) 37 | 38 | } 39 | -------------------------------------------------------------------------------- /clntDownloadClientConfig.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "os/exec" 7 | "strings" 8 | ) 9 | 10 | func downloadClientConfig(username string, hostname string, hostport int) { 11 | 12 | // If the hostport is blank then set it to 22. 13 | if hostport == 0 { 14 | 15 | hostport = 22 16 | 17 | } 18 | 19 | // Start the native scp client and download the configuration files. 20 | c := fmt.Sprintf("scp -r -P %d %s@%s:%s %s", hostport, username, hostname, dirname, uHome) 21 | x := strings.Fields(c) 22 | cmd := exec.Command(x[0], x[1:]...) 23 | 24 | // Execute the command and provide an interactive shell 25 | // where users type in their password and 2fa code 26 | // https://www.reddit.com/r/golang/comments/2nd4pq/how_can_i_open_an_interactive_subprogram_from/ 27 | cmd.Stdout = os.Stdout 28 | cmd.Stdin = os.Stdin 29 | cmd.Stderr = os.Stderr 30 | _ = cmd.Run() 31 | 32 | // Check if the VPN config file download was successful 33 | fileExists(uHome+wgConfig, uHome+clientConfig, "getCheck") 34 | 35 | // Run the appropriate command based on the OS. 36 | ctrl_wg("start") 37 | 38 | } 39 | -------------------------------------------------------------------------------- /clntFileExists.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "os" 7 | ) 8 | 9 | // Function to check if file exists 10 | func fileExists(filename string, filename2 string, todo string) { 11 | 12 | // Delete the VPN config file 13 | if todo == "remove" { 14 | 15 | // Stat returns file info. It will return 16 | // nil if the file does not exist. 17 | _, err := os.Stat(filename) 18 | 19 | if err == nil { 20 | 21 | fmt.Printf("\nRemoving existing VPN Configs\n") 22 | err = os.Remove(filename) 23 | checkErr(err, "") 24 | 25 | } 26 | 27 | _, err = os.Stat(filename2) 28 | 29 | if err == nil { 30 | 31 | err = os.Remove(filename2) 32 | checkErr(err, "") 33 | 34 | } 35 | 36 | } else if todo == "getCheck" { 37 | 38 | // Stat returns file info. It will return 39 | // nil if the file does not exist. 40 | _, err := os.Stat(filename) 41 | if err == nil { 42 | 43 | fmt.Println("VPN config downloaded.") 44 | } else { 45 | 46 | fmt.Println("New VPN configuration was not received, please try again.") 47 | checkErr(err, "VPN Files were not found.") 48 | 49 | } 50 | 51 | } else { 52 | 53 | _, err := os.Stat(filename) 54 | if err != nil { 55 | 56 | fmt.Println("New VPN configuration was not received, please try again.") 57 | log.Fatal(err) 58 | 59 | } else { 60 | 61 | fmt.Println("Existing VPN config removed.") 62 | } 63 | 64 | } 65 | 66 | return 67 | } 68 | -------------------------------------------------------------------------------- /clntGetClientConfigs.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "os" 6 | "runtime" 7 | 8 | "github.com/spf13/viper" 9 | ) 10 | 11 | // Function to get the remote WireGuard configuration file 12 | // The WireGuard configuration file is read from the user's 13 | // home directory in the directory sshwireguardConf. 14 | // It is also the location of the config file provided 15 | // by the admin for the client. 16 | func getClientConfigs() { 17 | 18 | // Read in the YAML configuration file 19 | data, err := os.ReadFile(uHome + clientConfig) 20 | 21 | checkErr(err, "Could not read sshwireguard configuration file.") 22 | 23 | // Convert YAML strings to bytes 24 | var _config = []byte(data) 25 | 26 | // Set config type to YAML 27 | viper.SetConfigType("yaml") 28 | viper.ReadConfig(bytes.NewBuffer(_config)) 29 | 30 | // Get the values from the yaml file: 31 | username := viper.Get("username").(string) 32 | hostname := viper.Get("hostname").(string) 33 | hostport := viper.Get("hostport").(int) 34 | 35 | os := runtime.GOOS 36 | s := check_status(os, "status") 37 | 38 | if s == "Success" { 39 | 40 | // Stop the currently running VPN sessions 41 | ctrl_wg("stop") 42 | 43 | } 44 | // Remove the existing configurations 45 | wgConf := uHome + wgConfig 46 | clientConf := uHome + clientConfig 47 | fileExists(uHome+wgConf, uHome+clientConf, "remove") 48 | 49 | // Download the sshwireguard configuration files 50 | downloadClientConfig(username, hostname, hostport) 51 | 52 | } 53 | -------------------------------------------------------------------------------- /clntMsgs.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | // Print messages to the console for the user. 4 | // Includes a prompt so the user can see the message 5 | // and acknowledge it. 6 | 7 | import ( 8 | "bufio" 9 | "os" 10 | 11 | "github.com/pterm/pterm" 12 | ) 13 | 14 | // Prompt when messages are printed to allow the user 15 | // to see the message and acknowledge it. 16 | func toContinue() { 17 | 18 | pterm.Info.Println("Press 'Enter' to continue...") 19 | bufio.NewReader(os.Stdin).ReadBytes('\n') 20 | 21 | } 22 | 23 | // Generic check for errors 24 | func checkErr(theError error, errString string) { 25 | 26 | if theError != nil { 27 | 28 | // Print default error. 29 | pterm.Error.Println(errString) 30 | toContinue() 31 | os.Exit(1) 32 | } 33 | 34 | } 35 | 36 | // Command was not completed successfully 37 | func errorMsg(theError string) { 38 | 39 | // Print default error. 40 | pterm.Error.Println(theError) 41 | 42 | } 43 | 44 | // Command completed successfully 45 | func successMsg(theSuccess string) { 46 | 47 | // Print default Success 48 | pterm.Success.Println(theSuccess) 49 | 50 | } 51 | 52 | // General Informational message 53 | func infoMsg(theInfo string) { 54 | 55 | // Print default Info message 56 | pterm.Info.Println(theInfo) 57 | 58 | } 59 | 60 | // Unsucessful command execution returns 1 61 | func errorOne() { 62 | 63 | os.Exit(1) 64 | } 65 | 66 | // Sucessful command execution returns 0 67 | func successZero() { 68 | 69 | os.Exit(0) 70 | } 71 | -------------------------------------------------------------------------------- /glblVars.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | ) 6 | 7 | const ( 8 | dirname = "sshwireguardConf" 9 | wgConfig = "/sshwireguardConf/sshwireguard.conf" 10 | clientConfig = "/sshwireguardConf/config.yaml" 11 | ) 12 | 13 | /** Global variables */ 14 | var ( 15 | cliUsage = ` 16 | Usage: sshwireguard [init|stop|start|status|renew] 17 | sshwireguard init - Creates a new WireGuard VPN configuration directory 18 | sshwireguard start - Starts the VPN Service 19 | sshwireguard stop - Stops the VPN Service 20 | sshwireguard status - Checks if the VPN service is stopped or running 21 | sshwireguard renew - Downloads a new VPN configuration 22 | ` 23 | 24 | // sshwireguardConf 25 | uHome = userHome() 26 | 27 | // Used in ctrlWireguard.go 28 | manageWgClient string 29 | 30 | // Used in ctrlClient.go 31 | //clientTask string 32 | ) 33 | 34 | // Get user's home directory 35 | func userHome() string { 36 | 37 | // Checking if current user information is unavailable 38 | homedir, err := os.UserHomeDir() 39 | checkErr(err, "User's Home Dir environment variable is not set.") 40 | 41 | return homedir 42 | 43 | } 44 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/thedunston/sshwireguard 2 | 3 | go 1.17 4 | 5 | require ( 6 | github.com/pterm/pterm v0.12.36 7 | github.com/spf13/viper v1.10.1 8 | ) 9 | 10 | require ( 11 | github.com/atomicgo/cursor v0.0.1 // indirect 12 | github.com/fsnotify/fsnotify v1.5.1 // indirect 13 | github.com/gookit/color v1.4.2 // indirect 14 | github.com/hashicorp/hcl v1.0.0 // indirect 15 | github.com/magiconair/properties v1.8.5 // indirect 16 | github.com/mattn/go-runewidth v0.0.13 // indirect 17 | github.com/mitchellh/mapstructure v1.4.3 // indirect 18 | github.com/pelletier/go-toml v1.9.4 // indirect 19 | github.com/rivo/uniseg v0.2.0 // indirect 20 | github.com/spf13/afero v1.6.0 // indirect 21 | github.com/spf13/cast v1.4.1 // indirect 22 | github.com/spf13/jwalterweatherman v1.1.0 // indirect 23 | github.com/spf13/pflag v1.0.5 // indirect 24 | github.com/subosito/gotenv v1.2.0 // indirect 25 | github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect 26 | github.com/yuin/goldmark v1.4.1 // indirect 27 | golang.org/x/mod v0.5.1 // indirect 28 | golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect 29 | golang.org/x/sys v0.0.0-20211210111614-af8b64212486 // indirect 30 | golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect 31 | golang.org/x/text v0.3.7 // indirect 32 | golang.org/x/tools v0.1.9 // indirect 33 | golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect 34 | gopkg.in/ini.v1 v1.66.2 // indirect 35 | gopkg.in/yaml.v2 v2.4.0 // indirect 36 | ) 37 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/MarvinJWendt/testza v0.1.0/go.mod h1:7AxNvlfeHP7Z/hDQ5JtE3OKYT3XFUeLCDE2DQninSqs= 2 | github.com/MarvinJWendt/testza v0.2.1/go.mod h1:God7bhG8n6uQxwdScay+gjm9/LnO4D3kkcZX4hv9Rp8= 3 | github.com/MarvinJWendt/testza v0.2.8/go.mod h1:nwIcjmr0Zz+Rcwfh3/4UhBp7ePKVhuBExvZqnKYWlII= 4 | github.com/MarvinJWendt/testza v0.2.10/go.mod h1:pd+VWsoGUiFtq+hRKSU1Bktnn+DMCSrDrXDpX2bG66k= 5 | github.com/MarvinJWendt/testza v0.2.12 h1:/PRp/BF+27t2ZxynTiqj0nyND5PbOtfJS0SuTuxmgeg= 6 | github.com/MarvinJWendt/testza v0.2.12/go.mod h1:JOIegYyV7rX+7VZ9r77L/eH6CfJHHzXjB69adAhzZkI= 7 | github.com/atomicgo/cursor v0.0.1 h1:xdogsqa6YYlLfM+GyClC/Lchf7aiMerFiZQn7soTOoU= 8 | github.com/atomicgo/cursor v0.0.1/go.mod h1:cBON2QmmrysudxNBFthvMtN32r3jxVRIvzkUiF/RuIk= 9 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 10 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 11 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 12 | github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI= 13 | github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU= 14 | github.com/gookit/color v1.4.2 h1:tXy44JFSFkKnELV6WaMo/lLfu/meqITX3iAV52do7lk= 15 | github.com/gookit/color v1.4.2/go.mod h1:fqRyamkC1W8uxl+lxCQxOT09l/vYfZ+QeiX3rKQHCoQ= 16 | github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= 17 | github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= 18 | github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4= 19 | github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= 20 | github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= 21 | github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls= 22 | github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= 23 | github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= 24 | github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= 25 | github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs= 26 | github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= 27 | github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM= 28 | github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= 29 | github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 30 | github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI= 31 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 32 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 33 | github.com/pterm/pterm v0.12.27/go.mod h1:PhQ89w4i95rhgE+xedAoqous6K9X+r6aSOI2eFF7DZI= 34 | github.com/pterm/pterm v0.12.29/go.mod h1:WI3qxgvoQFFGKGjGnJR849gU0TsEOvKn5Q8LlY1U7lg= 35 | github.com/pterm/pterm v0.12.30/go.mod h1:MOqLIyMOgmTDz9yorcYbcw+HsgoZo3BQfg2wtl3HEFE= 36 | github.com/pterm/pterm v0.12.31/go.mod h1:32ZAWZVXD7ZfG0s8qqHXePte42kdz8ECtRyEejaWgXU= 37 | github.com/pterm/pterm v0.12.33/go.mod h1:x+h2uL+n7CP/rel9+bImHD5lF3nM9vJj80k9ybiiTTE= 38 | github.com/pterm/pterm v0.12.36 h1:Ui5zZj7xA8lXR0CxWXlKGCQMW1cZVUMOS8jEXs6ur/g= 39 | github.com/pterm/pterm v0.12.36/go.mod h1:NjiL09hFhT/vWjQHSj1athJpx6H8cjpHXNAK5bUw8T8= 40 | github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= 41 | github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= 42 | github.com/spf13/afero v1.6.0 h1:xoax2sJ2DT8S8xA2paPFjDCScCNeWsg75VG0DLRreiY= 43 | github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I= 44 | github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA= 45 | github.com/spf13/cast v1.4.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= 46 | github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk= 47 | github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo= 48 | github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= 49 | github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= 50 | github.com/spf13/viper v1.10.1 h1:nuJZuYpG7gTj/XqiUwg8bA0cp1+M2mC3J4g5luUYBKk= 51 | github.com/spf13/viper v1.10.1/go.mod h1:IGlFPqhNAPKRxohIzWpI5QEy4kuI7tcl5WvR+8qy1rU= 52 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 53 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 54 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 55 | github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 56 | github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= 57 | github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 58 | github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= 59 | github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= 60 | github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8= 61 | github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= 62 | github.com/yuin/goldmark v1.4.1 h1:/vn0k+RBvwlxEmP5E7SZMqNxPhfMVFEJiykr15/0XKM= 63 | github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= 64 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 65 | golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= 66 | golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38= 67 | golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= 68 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 69 | golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f h1:OfiFi4JbukWwe3lzw+xunroH1mnC1e2Gy5cxNJApiSY= 70 | golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= 71 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 72 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 73 | golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 74 | golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 75 | golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 76 | golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 77 | golang.org/x/sys v0.0.0-20211013075003-97ac67df715c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 78 | golang.org/x/sys v0.0.0-20211210111614-af8b64212486 h1:5hpz5aRr+W1erYCL5JRhSUBJRph7l9XkNveoExlrKYk= 79 | golang.org/x/sys v0.0.0-20211210111614-af8b64212486/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 80 | golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= 81 | golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= 82 | golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY= 83 | golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= 84 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 85 | golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 86 | golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= 87 | golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= 88 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 89 | golang.org/x/tools v0.1.9 h1:j9KsMiaP1c3B0OTQGth0/k+miLGTgLsAFUCrF2vLcF8= 90 | golang.org/x/tools v0.1.9/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU= 91 | golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= 92 | golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 93 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 94 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 95 | gopkg.in/ini.v1 v1.66.2 h1:XfR1dOYubytKy4Shzc2LHrrGhU0lDCfDGG1yLPmpgsI= 96 | gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= 97 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 98 | gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= 99 | gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= 100 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 101 | gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= 102 | gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 103 | -------------------------------------------------------------------------------- /gobuild.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Based on https://gist.github.com/eduncan911/68775dba9d3c028181e4 4 | # but improved to use the `go` command so it never goes out of date. 5 | 6 | type setopt >/dev/null 2>&1 7 | 8 | contains() { 9 | # Source: https://stackoverflow.com/a/8063398/7361270 10 | [[ $1 =~ (^|[[:space:]])$2($|[[:space:]]) ]] 11 | } 12 | 13 | SOURCE_FILE=$(echo "$@" | sed 's/\.go//') 14 | CURRENT_DIRECTORY="${PWD##*/}" 15 | OUTPUT=${SOURCE_FILE:-$CURRENT_DIRECTORY} # if no src file given, use current dir name 16 | FAILURES="" 17 | 18 | # You can set your own flags on the command line 19 | FLAGS=${FLAGS:-"-ldflags=\"-s -w\""} 20 | 21 | # A list of OSes to not build for, space-separated 22 | # It can be set from the command line when the script is called. 23 | NOT_ALLOWED_OS=${NOT_ALLOWED_OS:-"js android ios solaris illumos aix"} 24 | 25 | # Get all targets 26 | while IFS= read -r target; do 27 | GOOS=${target%/*} 28 | GOARCH=${target#*/} 29 | BIN_FILENAME="${OUTPUT}-${GOOS}-${GOARCH}" 30 | 31 | if contains "$NOT_ALLOWED_OS" "$GOOS" ; then 32 | continue 33 | fi 34 | 35 | # Check for arm and set arm version 36 | if [[ $GOARCH == "arm" ]]; then 37 | # Set what arm versions each platform supports 38 | if [[ $GOOS == "darwin" ]]; then 39 | arms="7" 40 | elif [[ $GOOS == "windows" ]]; then 41 | # This is a guess, it's not clear what Windows supports from the docs 42 | # But I was able to build all these on my machine 43 | arms="5 6 7" 44 | elif [[ $GOOS == *"bsd" ]]; then 45 | arms="6 7" 46 | else 47 | # Linux goes here 48 | arms="5 6 7" 49 | fi 50 | 51 | # Now do the arm build 52 | for GOARM in $arms; do 53 | BIN_FILENAME="${OUTPUT}-${GOOS}-${GOARCH}${GOARM}" 54 | if [[ "${GOOS}" == "windows" ]]; then BIN_FILENAME="${BIN_FILENAME}.exe"; fi 55 | CMD="GOARM=${GOARM} GOOS=${GOOS} GOARCH=${GOARCH} go build $FLAGS -o ${BIN_FILENAME} $@" 56 | echo "${CMD}" 57 | eval "${CMD}" || FAILURES="${FAILURES} ${GOOS}/${GOARCH}${GOARM}" 58 | done 59 | else 60 | # Build non-arm here 61 | if [[ "${GOOS}" == "windows" ]]; then BIN_FILENAME="${BIN_FILENAME}.exe"; fi 62 | CMD="GOOS=${GOOS} GOARCH=${GOARCH} go build $FLAGS -o ${BIN_FILENAME} $@" 63 | echo "${CMD}" 64 | eval "${CMD}" || FAILURES="${FAILURES} ${GOOS}/${GOARCH}" 65 | fi 66 | done <<< "$(go tool dist list)" 67 | 68 | if [[ "${FAILURES}" != "" ]]; then 69 | echo "" 70 | echo "${SCRIPT_NAME} failed on: ${FAILURES}" 71 | exit 1 72 | fi 73 | -------------------------------------------------------------------------------- /srvCheckOutput.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "regexp" 6 | "runtime" 7 | ) 8 | 9 | func check_output(o string, taskMsg string) string { 10 | 11 | // Search string 12 | toSearch := "" 13 | 14 | // Check OS for expected output 15 | // Windows 16 | if runtime.GOOS == "windows" { 17 | 18 | switch taskMsg { 19 | 20 | case "started": 21 | 22 | toSearch = "RUNNING" 23 | case "stopped": 24 | 25 | toSearch = "STOPPED" 26 | 27 | case "status": 28 | 29 | fmt.Println(0) 30 | matched, _ := regexp.MatchString("RUNNING", o) 31 | 32 | // Check if STOPPED or RUNNING is in the output 33 | if matched { 34 | 35 | taskMsg = "Success" 36 | 37 | } else { 38 | 39 | taskMsg = "Error" 40 | 41 | } 42 | 43 | return taskMsg 44 | 45 | default: 46 | 47 | infoMsg("Nothing to do!") 48 | errorOne() 49 | 50 | } 51 | 52 | // *nix 53 | } else { 54 | 55 | if taskMsg == "status" { 56 | 57 | fmt.Println(o) 58 | 59 | matched, _ := regexp.MatchString(" sshwireguard: ", o) 60 | 61 | // Check if STOPPED or RUNNING is in the output 62 | if matched { 63 | 64 | taskMsg = "Success" 65 | 66 | } else { 67 | 68 | taskMsg = "Error" 69 | 70 | } 71 | 72 | return taskMsg 73 | } 74 | // Output from the ip command has a colon at the end of the interface 75 | toSearch = "sshwireguard:" 76 | 77 | } 78 | 79 | // Search for the task being performed STOPPED or RUNNING 80 | matched, _ := regexp.MatchString(toSearch, o) 81 | 82 | allGood := "" 83 | // Check if the task is in the output. 84 | if matched { 85 | 86 | // The printed message is the same for checking on the status. 87 | // Ran successfully 88 | allGood = "Success" 89 | 90 | } else { 91 | 92 | // The printed message is the same for checking on the status. 93 | // Ran successfully 94 | allGood = "Error" 95 | 96 | } 97 | 98 | return allGood 99 | 100 | } 101 | -------------------------------------------------------------------------------- /srvCheckStatus.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os/exec" 5 | "strings" 6 | ) 7 | 8 | // Function checks the status to determine if the sshwireguard Tunnel is running 9 | // or not running. 10 | func check_status(os string, taskMsg string) string { 11 | 12 | wg_run := "" 13 | 14 | if os == "windows" { 15 | 16 | // Command to check on status of sshwireguard service 17 | wg_run = "sc query WireGuardTunnel$sshwireguard" 18 | 19 | // *nix 20 | } else { 21 | 22 | // Command to check on status of sshwireguard service 23 | wg_run = "ip a show dev sshwireguard" 24 | 25 | } 26 | 27 | // Split the command for execution 28 | args := strings.Fields(wg_run) 29 | 30 | // Format the command to run 31 | cmd := exec.Command(args[0], args[1:]...) 32 | 33 | // Execute the command 34 | results, _ := cmd.CombinedOutput() 35 | 36 | // Get the results 37 | cmdResults := check_output(string(results), taskMsg) 38 | 39 | return cmdResults 40 | 41 | } 42 | -------------------------------------------------------------------------------- /srvCtrlWg.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | /** Checks the OS and runs the respective command to manage wireguard. */ 4 | 5 | import ( 6 | "fmt" 7 | "runtime" 8 | ) 9 | 10 | // Function is used to manage the sshwireguard service. 11 | // The arguments can have one or two parameters. 12 | // If no pipe is needed to check for the service running set the arg_2 parameter to "None" 13 | // 14 | // Example: srvStart(ip addr show dev sshwireguard, "", wg-quick up sshwireguardConf/sshwireguard.conf) 15 | // 16 | // If the service requires a Pipe then don't include the pipe, but the text after the pipe. 17 | // 18 | // Example: srvStart("sc query", "findstr sshwireguard", "wireguard.exe /uninstalltunnelservice ireguard.exe /installtunnelservice " + uHome + "\\sshwireguardConf\\sshwireguard.conf"") 19 | // 20 | // would be the same as: 21 | // 22 | // sc query | findstr sshwireguard 23 | // 24 | // The above line will return output and that is checked to determine if the service is running. 25 | // Manage the Wireguard service 26 | func ctrl_wg(runit string) { 27 | 28 | os := runtime.GOOS 29 | 30 | switch os { 31 | 32 | case "windows": 33 | 34 | // Invoke Powershell to start and stop the sshwireguard Tunnel and elevate to admin. 35 | if runit == "start" { 36 | 37 | run_cmd(os, "powershell -command start-process 'C:\\Windows\\System32\\sc.exe' -ArgumentList 'start WireGuardTunnel$sshwireguard' -Verb RunAs", "started") 38 | 39 | } else if runit == "stop" { 40 | 41 | run_cmd(os, "powershell -command start-process 'C:\\Windows\\System32\\sc.exe' -ArgumentList 'stop WireGuardTunnel$sshwireguard' -Verb RunAs", "stopped") 42 | 43 | } else if runit == "status" { 44 | 45 | s := check_status(os, "status") 46 | 47 | run_results(s, "status") 48 | 49 | } else { 50 | 51 | errorMsg("Nothing to do") 52 | errorOne() 53 | } 54 | 55 | case "darwin": 56 | fmt.Println("MAC operating system") 57 | 58 | case "linux": 59 | 60 | if runit == "start" { 61 | 62 | // Sudo is used to start and stop the service 63 | run_cmd(os, "sudo wg-quick up "+uHome+wgConfig, "started") 64 | 65 | } else if runit == "stop" { 66 | 67 | run_cmd(os, "sudo wg-quick down "+uHome+wgConfig, "stopped") 68 | 69 | } else if runit == "status" { 70 | 71 | s := check_status(os, "status") 72 | 73 | run_results(s, "status") 74 | 75 | } else { 76 | 77 | errorMsg("Nothing to do!") 78 | 79 | } 80 | 81 | default: 82 | 83 | errorMsg("Nothing to do") 84 | errorOne() 85 | 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /srvRunCmd.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | /** Executes the command to manage the Wireguard service or check running status */ 4 | 5 | import ( 6 | "os/exec" 7 | "strings" 8 | "time" 9 | ) 10 | 11 | // Function executes the respective command to start or stop 12 | // the service. 13 | // wg_run: Command to start or stop the sshwireguard 14 | // service. 15 | // taskMsg: String is "start" or "stop" 16 | func run_cmd(os string, wg_run string, taskMsg string) { 17 | 18 | // Split the command using spaces to manage the service 19 | args := strings.Fields(wg_run) 20 | 21 | // Run the wireguard command 22 | cmd := exec.Command(args[0], args[1:]...) 23 | 24 | // Execute the command 25 | _, err := cmd.CombinedOutput() 26 | checkErr(err, "Error running command: "+wg_run) 27 | 28 | // Pause to give service time to start/stop 29 | infoMsg("Please wait...") 30 | 31 | // Sleep for 5 seconds 32 | time.Sleep(5 * time.Second) 33 | 34 | // Check if service is stopped or stopped 35 | s := check_status(os, taskMsg) 36 | 37 | run_results(s, taskMsg) 38 | 39 | } 40 | -------------------------------------------------------------------------------- /srvRunResults.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | // Function displays whether the service task was pwerformed 4 | // successfully or unsuccessfully 5 | func run_results(taskRunMsg string, theTask string) { 6 | 7 | // Debug: fmt.Println(taskRunMsg + " " + theTask) 8 | // Print message based on the status of running the command 9 | switch taskRunMsg { 10 | case "Success": 11 | 12 | if theTask == "status" { 13 | 14 | infoMsg("sshwireguard is running") 15 | 16 | } else { 17 | 18 | successMsg("sshwireguard was " + theTask) 19 | 20 | } 21 | 22 | case "Error": 23 | 24 | if theTask == "status" { 25 | 26 | infoMsg("sshwireguard is not running") 27 | 28 | // Message when manually stopping the VPN service 29 | } else if theTask == "stopped" { 30 | 31 | successMsg("sshwireguard was stopped") 32 | 33 | // Message when the VPN service was running, but did not stop 34 | } else { 35 | 36 | errorMsg("sshwireguard was not " + theTask + ". Please try again.") 37 | 38 | } 39 | 40 | default: 41 | 42 | infoMsg("Nothing to do!rr") 43 | errorOne() 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /sshwireguard.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | // SCP to a remote user using 2FA 4 | 5 | import ( 6 | "os" 7 | ) 8 | 9 | func main() { 10 | 11 | // Default terminal window header 12 | introSSHWireguardScreen() 13 | 14 | // Check if the sshwireguardConf folder exists in the user's 15 | // home directory. 16 | _, err := os.Stat(uHome + "/" + dirname) 17 | if os.IsNotExist(err) { 18 | 19 | createConf() 20 | 21 | } else { 22 | 23 | // Check that there is an argument provided 24 | if len(os.Args) > 2 || len(os.Args) == 1 { 25 | 26 | infoMsg(cliUsage) 27 | 28 | } else if len(os.Args) == 1 { 29 | 30 | infoMsg(cliUsage) 31 | 32 | } else { 33 | 34 | task := os.Args[1] 35 | 36 | switch task { 37 | 38 | case "init": 39 | 40 | createConf() 41 | 42 | case "stop": 43 | 44 | ctrl_wg("stop") 45 | 46 | case "start": 47 | 48 | ctrl_wg("start") 49 | 50 | case "renew": 51 | 52 | getClientConfigs() 53 | 54 | case "status": 55 | 56 | ctrl_wg("status") 57 | 58 | default: 59 | 60 | infoMsg(cliUsage) 61 | os.Exit(1) 62 | 63 | } 64 | 65 | } 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /test/sshwireguard-arch-amd64-test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thedunston/sshwireguard/18c035171589bc3c13b26770eae632b94bbece26/test/sshwireguard-arch-amd64-test -------------------------------------------------------------------------------- /test/sshwireguard-ubuntu-amd64-test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thedunston/sshwireguard/18c035171589bc3c13b26770eae632b94bbece26/test/sshwireguard-ubuntu-amd64-test -------------------------------------------------------------------------------- /wireguard-manager/wireguard-manager.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # https://github.com/complexorganizations/wireguard-manager 3 | 4 | # Require script to be run as root 5 | function super-user-check() { 6 | if [ "${EUID}" -ne 0 ]; then 7 | echo "Error: You need to run this script as administrator." 8 | exit 9 | fi 10 | } 11 | 12 | # Check for root 13 | super-user-check 14 | 15 | # User on the CLI 16 | CLIENT_USER="${2}" 17 | 18 | # Custom expiration 19 | CUSTOM_EXPIRE="${3}" 20 | 21 | # Get the current system information 22 | function system-information() { 23 | if [ -f /etc/os-release ]; then 24 | # shellcheck disable=SC1091 25 | source /etc/os-release 26 | CURRENT_DISTRO=${ID} 27 | CURRENT_DISTRO_VERSION=${VERSION_ID} 28 | fi 29 | } 30 | 31 | # Get the current system information 32 | system-information 33 | 34 | # Pre-Checks system requirements 35 | function installing-system-requirements() { 36 | if { [ "${CURRENT_DISTRO}" == "ubuntu" ] || [ "${CURRENT_DISTRO}" == "debian" ] || [ "${CURRENT_DISTRO}" == "raspbian" ] || [ "${CURRENT_DISTRO}" == "pop" ] || [ "${CURRENT_DISTRO}" == "kali" ] || [ "${CURRENT_DISTRO}" == "linuxmint" ] || [ "${CURRENT_DISTRO}" == "neon" ] || [ "${CURRENT_DISTRO}" == "fedora" ] || [ "${CURRENT_DISTRO}" == "centos" ] || [ "${CURRENT_DISTRO}" == "rhel" ] || [ "${CURRENT_DISTRO}" == "almalinux" ] || [ "${CURRENT_DISTRO}" == "rocky" ] || [ "${CURRENT_DISTRO}" == "arch" ] || [ "${CURRENT_DISTRO}" == "archarm" ] || [ "${CURRENT_DISTRO}" == "manjaro" ] || [ "${CURRENT_DISTRO}" == "alpine" ] || [ "${CURRENT_DISTRO}" == "freebsd" ]; }; then 37 | if { [ ! -x "$(command -v curl)" ] || [ ! -x "$(command -v cut)" ] || [ ! -x "$(command -v jq)" ] || [ ! -x "$(command -v ip)" ] || [ ! -x "$(command -v lsof)" ] || [ ! -x "$(command -v cron)" ] || [ ! -x "$(command -v awk)" ] || [ ! -x "$(command -v pgrep)" ] || [ ! -x "$(command -v grep)" ] || [ ! -x "$(command -v qrencode)" ] || [ ! -x "$(command -v sed)" ] || [ ! -x "$(command -v zip)" ] || [ ! -x "$(command -v unzip)" ] || [ ! -x "$(command -v openssl)" ] || [ ! -x "$(command -v iptables)" ] || [ ! -x "$(command -v bc)" ] || [ ! -x "$(command -v ifup)" ] || [ ! -x "$(command -v chattr)" ] || [ ! -x "$(command -v gpg)" ] || [ ! -x "$(command -v systemd-detect-virt)" ]; }; then 38 | if { [ "${CURRENT_DISTRO}" == "ubuntu" ] || [ "${CURRENT_DISTRO}" == "debian" ] || [ "${CURRENT_DISTRO}" == "raspbian" ] || [ "${CURRENT_DISTRO}" == "pop" ] || [ "${CURRENT_DISTRO}" == "kali" ] || [ "${CURRENT_DISTRO}" == "linuxmint" ] || [ "${CURRENT_DISTRO}" == "neon" ]; }; then 39 | apt-get update 40 | apt-get install curl coreutils jq iproute2 lsof cron gawk procps grep qrencode sed zip unzip openssl iptables bc ifupdown e2fsprogs gnupg systemd -y 41 | elif { [ "${CURRENT_DISTRO}" == "fedora" ] || [ "${CURRENT_DISTRO}" == "centos" ] || [ "${CURRENT_DISTRO}" == "rhel" ] || [ "${CURRENT_DISTRO}" == "almalinux" ] || [ "${CURRENT_DISTRO}" == "rocky" ]; }; then 42 | yum update -y 43 | yum install epel-release elrepo-release -y 44 | yum install curl coreutils jq iproute lsof cronie gawk procps-ng grep qrencode sed zip unzip openssl iptables bc NetworkManager e2fsprogs gnupg systemd -y 45 | elif { [ "${CURRENT_DISTRO}" == "arch" ] || [ "${CURRENT_DISTRO}" == "archarm" ] || [ "${CURRENT_DISTRO}" == "manjaro" ]; }; then 46 | pacman -Syu --noconfirm --needed curl coreutils jq iproute2 lsof cronie gawk procps-ng grep qrencode sed zip unzip openssl iptables bc ifupdown e2fsprogs gnupg systemd 47 | elif [ "${CURRENT_DISTRO}" == "alpine" ]; then 48 | apk update 49 | apk add curl coreutils jq iproute2 lsof cronie gawk procps grep qrencode sed zip unzip openssl iptables bc ifupdown e2fsprogs gnupg systemd 50 | elif [ "${CURRENT_DISTRO}" == "freebsd" ]; then 51 | pkg update 52 | pkg install curl coreutils jq iproute2 lsof cronie gawk procps grep qrencode sed zip unzip openssl iptables bc ifupdown e2fsprogs gnupg systemd 53 | fi 54 | fi 55 | else 56 | echo "Error: ${CURRENT_DISTRO} ${CURRENT_DISTRO_VERSION} is not supported." 57 | exit 58 | fi 59 | } 60 | 61 | # check for requirements 62 | installing-system-requirements 63 | 64 | # Checking For Virtualization 65 | function virt-check() { 66 | # Deny certain virtualization 67 | case $(systemd-detect-virt) in 68 | "kvm" | "none" | "qemu") ;; 69 | *) 70 | echo "$(systemd-detect-virt) virtualization is not supported (yet)." 71 | exit 72 | ;; 73 | esac 74 | } 75 | 76 | # Virtualization Check 77 | virt-check 78 | 79 | # Lets check the kernel version 80 | function kernel-check() { 81 | CURRENT_KERNEL_VERSION=$(uname -r | cut -d'.' -f1-2) 82 | ALLOWED_KERNEL_VERSION="3.1" 83 | if (($(echo "${CURRENT_KERNEL_VERSION} <= ${ALLOWED_KERNEL_VERSION}" | bc -l))); then 84 | echo "Error: Kernel ${CURRENT_KERNEL_VERSION} not supported, please update to ${ALLOWED_KERNEL_VERSION}." 85 | exit 86 | fi 87 | } 88 | 89 | kernel-check 90 | 91 | # Global variables 92 | CURRENT_FILE_PATH=$(realpath "${0}") 93 | WIREGUARD_WEBSITE_URL="https://www.wireguard.com" 94 | WIREGUARD_PATH="/etc/wireguard" 95 | WIREGUARD_CLIENT_PATH="${WIREGUARD_PATH}/clients" 96 | WIREGUARD_PUB_NIC="wg0" 97 | WIREGUARD_CONFIG="${WIREGUARD_PATH}/${WIREGUARD_PUB_NIC}.conf" 98 | WIREGUARD_ADD_PEER_CONFIG="${WIREGUARD_PATH}/${WIREGUARD_PUB_NIC}-add-peer.conf" 99 | SYSTEM_BACKUP_PATH="/var/backups" 100 | WIREGUARD_CONFIG_BACKUP="${SYSTEM_BACKUP_PATH}/wireguard-manager.zip" 101 | WIREGUARD_BACKUP_PASSWORD_PATH="${HOME}/.wireguard-manager" 102 | WIREGUARD_IP_FORWARDING_CONFIG="/etc/sysctl.d/wireguard.conf" 103 | RESOLV_CONFIG="/etc/resolv.conf" 104 | RESOLV_CONFIG_OLD="${RESOLV_CONFIG}.old" 105 | UNBOUND_ROOT="/etc/unbound" 106 | UNBOUND_MANAGER="${UNBOUND_ROOT}/wireguard-manager" 107 | UNBOUND_CONFIG="${UNBOUND_ROOT}/unbound.conf" 108 | UNBOUND_ROOT_HINTS="${UNBOUND_ROOT}/root.hints" 109 | UNBOUND_ANCHOR="/var/lib/unbound/root.key" 110 | UNBOUND_CONFIG_HOST="${UNBOUND_ROOT}/unbound.conf.d/hosts.conf" 111 | case $(shuf -i1-6 -n1) in 112 | 1) 113 | UNBOUND_ROOT_SERVER_CONFIG_URL="https://raw.githubusercontent.com/complexorganizations/wireguard-manager/main/assets/named.cache" 114 | ;; 115 | 2) 116 | UNBOUND_ROOT_SERVER_CONFIG_URL="https://cdn.statically.io/gh/complexorganizations/wireguard-manager/main/assets/named.cache" 117 | ;; 118 | 3) 119 | UNBOUND_ROOT_SERVER_CONFIG_URL="https://cdn.jsdelivr.net/gh/complexorganizations/wireguard-manager/assets/named.cache" 120 | ;; 121 | 4) 122 | UNBOUND_ROOT_SERVER_CONFIG_URL="https://combinatronics.io/complexorganizations/wireguard-manager/main/assets/named.cache" 123 | ;; 124 | 5) 125 | UNBOUND_ROOT_SERVER_CONFIG_URL="https://www.internic.net/domain/named.cache" 126 | ;; 127 | 6) 128 | UNBOUND_ROOT_SERVER_CONFIG_URL="https://gitlab.com/prajwal-koirala/wireguard-manager/-/raw/main/assets/named.cache" 129 | ;; 130 | esac 131 | case $(shuf -i1-5 -n1) in 132 | 1) 133 | UNBOUND_CONFIG_HOST_URL="https://raw.githubusercontent.com/complexorganizations/content-blocker/main/assets/hosts" 134 | ;; 135 | 2) 136 | UNBOUND_CONFIG_HOST_URL="https://cdn.statically.io/gh/complexorganizations/content-blocker/main/assets/hosts" 137 | ;; 138 | 3) 139 | UNBOUND_CONFIG_HOST_URL="https://cdn.jsdelivr.net/gh/complexorganizations/content-blocker/assets/hosts" 140 | ;; 141 | 4) 142 | UNBOUND_CONFIG_HOST_URL="https://combinatronics.io/complexorganizations/content-blocker/main/assets/hosts" 143 | ;; 144 | 5) 145 | UNBOUND_CONFIG_HOST_URL="https://gitlab.com/prajwal-koirala/content-blocker/-/raw/main/assets/hosts" 146 | ;; 147 | esac 148 | case $(shuf -i1-1 -n1) in 149 | 1) 150 | WIREGUARD_MANAGER_UPDATE="https://wireguard-manager.sh" 151 | ;; 152 | esac 153 | 154 | # Usage Guide of the application 155 | function usage-guide() { 156 | echo "usage: ./$(basename "${0}") " 157 | echo " --install Install WireGuard Interface" 158 | echo " --start Start WireGuard Interface" 159 | echo " --stop Stop WireGuard Interface" 160 | echo " --restart Restart WireGuard Interface" 161 | echo " --list Show WireGuard Peer(s)" 162 | echo " --add Add WireGuard Peer" 163 | echo " --remove Remove WireGuard Peer" 164 | echo " --reinstall Reinstall WireGuard Interface" 165 | echo " --uninstall Uninstall WireGuard Interface" 166 | echo " --update Update WireGuard Manager" 167 | echo " --ddns Update WireGuard IP Address" 168 | echo " --backup Backup WireGuard" 169 | echo " --restore Restore WireGuard" 170 | echo " --purge Purge WireGuard Peer(s)" 171 | echo " --help Show Usage Guide" 172 | } 173 | 174 | # The usage of the script 175 | function usage() { 176 | while [ $# -ne 0 ]; do 177 | case ${1} in 178 | --install) 179 | shift 180 | HEADLESS_INSTALL=${HEADLESS_INSTALL:-true} 181 | ;; 182 | --start) 183 | shift 184 | WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-2} 185 | ;; 186 | --stop) 187 | shift 188 | WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-3} 189 | ;; 190 | --restart) 191 | shift 192 | WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-4} 193 | ;; 194 | --list) 195 | shift 196 | WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-1} 197 | ;; 198 | --add) 199 | shift 200 | WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-5} 201 | ;; 202 | --remove) 203 | shift 204 | WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-6} 205 | ;; 206 | --reinstall) 207 | shift 208 | WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-7} 209 | ;; 210 | --uninstall) 211 | shift 212 | WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-8} 213 | ;; 214 | --update) 215 | shift 216 | WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-9} 217 | ;; 218 | --backup) 219 | shift 220 | WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-10} 221 | ;; 222 | --restore) 223 | shift 224 | WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-11} 225 | ;; 226 | --ddns) 227 | shift 228 | WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-12} 229 | ;; 230 | --purge) 231 | shift 232 | WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-14} 233 | ;; 234 | --help) 235 | shift 236 | usage-guide 237 | ;; 238 | *) 239 | echo "Invalid argument: ${1}" 240 | usage-guide 241 | exit 242 | ;; 243 | esac 244 | done 245 | } 246 | 247 | 248 | # If the user name is on the CLI and the option is to remove the user 249 | if [[ "${CLIENT_USER}" != "" && "${1}" == "--remove" ]]; then 250 | 251 | WIREGUARD_OPTIONS=6 252 | 253 | # If the user name is on the CLI and the option is to add the user 254 | elif [[ "${CLIENT_USER}" != "" && "${1}" == "--add" ]]; then 255 | 256 | WIREGUARD_OPTIONS=5 257 | 258 | else 259 | 260 | usage "$@" 261 | 262 | fi 263 | 264 | # All questions are skipped, and wireguard is installed and a configuration is generated. 265 | function headless-install() { 266 | if [ "${HEADLESS_INSTALL}" == true ]; then 267 | INTERFACE_OR_PEER=${INTERFACE_OR_PEER:-1} 268 | IPV4_SUBNET_SETTINGS=${IPV4_SUBNET_SETTINGS:-1} 269 | IPV6_SUBNET_SETTINGS=${IPV6_SUBNET_SETTINGS:-1} 270 | SERVER_HOST_V4_SETTINGS=${SERVER_HOST_V4_SETTINGS:-1} 271 | SERVER_HOST_V6_SETTINGS=${SERVER_HOST_V6_SETTINGS:-1} 272 | SERVER_PUB_NIC_SETTINGS=${SERVER_PUB_NIC_SETTINGS:-1} 273 | SERVER_PORT_SETTINGS=${SERVER_PORT_SETTINGS:-1} 274 | NAT_CHOICE_SETTINGS=${NAT_CHOICE_SETTINGS:-1} 275 | MTU_CHOICE_SETTINGS=${MTU_CHOICE_SETTINGS:-1} 276 | SERVER_HOST_SETTINGS=${SERVER_HOST_SETTINGS:-1} 277 | DISABLE_HOST_SETTINGS=${DISABLE_HOST_SETTINGS:-1} 278 | CLIENT_ALLOWED_IP_SETTINGS=${CLIENT_ALLOWED_IP_SETTINGS:-1} 279 | AUTOMATIC_UPDATES_SETTINGS=${AUTOMATIC_UPDATES_SETTINGS:-1} 280 | AUTOMATIC_BACKUP_SETTINGS=${AUTOMATIC_BACKUP_SETTINGS:-1} 281 | DNS_PROVIDER_SETTINGS=${DNS_PROVIDER_SETTINGS:-1} 282 | CONTENT_BLOCKER_SETTINGS=${CONTENT_BLOCKER_SETTINGS:-1} 283 | CLIENT_NAME=${CLIENT_NAME:-$(openssl rand -hex 50)} 284 | AUTOMATIC_CONFIG_REMOVER=${AUTOMATIC_CONFIG_REMOVER:-1} 285 | fi 286 | } 287 | 288 | # No GUI 289 | headless-install 290 | 291 | # Set up the wireguard, if config it isn't already there. 292 | if [ ! -f "${WIREGUARD_CONFIG}" ]; then 293 | 294 | # Custom IPv4 subnet 295 | function set-ipv4-subnet() { 296 | echo "What IPv4 subnet do you want to use?" 297 | echo " 1) 10.0.0.0/8 (Recommended)" 298 | echo " 2) Custom (Advanced)" 299 | until [[ "${IPV4_SUBNET_SETTINGS}" =~ ^[1-2]$ ]]; do 300 | read -rp "Subnet Choice [1-2]:" -e -i 1 IPV4_SUBNET_SETTINGS 301 | done 302 | case ${IPV4_SUBNET_SETTINGS} in 303 | 1) 304 | IPV4_SUBNET="10.0.0.0/8" 305 | ;; 306 | 2) 307 | read -rp "Custom IPv4 Subnet:" IPV4_SUBNET 308 | if [ -z "${IPV4_SUBNET}" ]; then 309 | IPV4_SUBNET="10.0.0.0/8" 310 | fi 311 | ;; 312 | esac 313 | } 314 | 315 | # Custom IPv4 Subnet 316 | set-ipv4-subnet 317 | 318 | # Custom IPv6 subnet 319 | function set-ipv6-subnet() { 320 | echo "What IPv6 subnet do you want to use?" 321 | echo " 1) fd00:00:00::0/8 (Recommended)" 322 | echo " 2) Custom (Advanced)" 323 | until [[ "${IPV6_SUBNET_SETTINGS}" =~ ^[1-2]$ ]]; do 324 | read -rp "Subnet Choice [1-2]:" -e -i 1 IPV6_SUBNET_SETTINGS 325 | done 326 | case ${IPV6_SUBNET_SETTINGS} in 327 | 1) 328 | IPV6_SUBNET="fd00:00:00::0/8" 329 | ;; 330 | 2) 331 | read -rp "Custom IPv6 Subnet:" IPV6_SUBNET 332 | if [ -z "${IPV6_SUBNET}" ]; then 333 | IPV6_SUBNET="fd00:00:00::0/8" 334 | fi 335 | ;; 336 | esac 337 | } 338 | 339 | # Custom IPv6 Subnet 340 | set-ipv6-subnet 341 | 342 | # Private Subnet Ipv4 343 | PRIVATE_SUBNET_V4=${PRIVATE_SUBNET_V4:-"${IPV4_SUBNET}"} 344 | # Private Subnet Mask IPv4 345 | PRIVATE_SUBNET_MASK_V4=$(echo "${PRIVATE_SUBNET_V4}" | cut -d "/" -f 2) 346 | # IPv4 Getaway 347 | GATEWAY_ADDRESS_V4=$(echo "${PRIVATE_SUBNET_V4}" | cut -d'.' -f1-3).1 348 | # Private Subnet Ipv6 349 | PRIVATE_SUBNET_V6=${PRIVATE_SUBNET_V6:-"${IPV6_SUBNET}"} 350 | # Private Subnet Mask IPv6 351 | PRIVATE_SUBNET_MASK_V6=$(echo "${PRIVATE_SUBNET_V6}" | cut -d "/" -f 2) 352 | # IPv6 Getaway 353 | GATEWAY_ADDRESS_V6="${PRIVATE_SUBNET_V6::-3}1" 354 | 355 | # Get the IPv4 356 | function test-connectivity-v4() { 357 | echo "How would you like to detect IPv4?" 358 | echo " 1) Curl (Recommended)" 359 | echo " 2) Custom (Advanced)" 360 | until [[ "${SERVER_HOST_V4_SETTINGS}" =~ ^[1-2]$ ]]; do 361 | read -rp "IPv4 Choice [1-2]:" -e -i 1 SERVER_HOST_V4_SETTINGS 362 | done 363 | case ${SERVER_HOST_V4_SETTINGS} in 364 | 1) 365 | SERVER_HOST_V4="$(curl -4 --connect-timeout 5 --tlsv1.3 -s 'https://api.ipengine.dev' | jq -r '.network.ip')" 366 | if [ -z "${SERVER_HOST_V4}" ]; then 367 | SERVER_HOST_V4="$(curl -4 --connect-timeout 5 --tlsv1.3 -s 'https://checkip.amazonaws.com')" 368 | fi 369 | ;; 370 | 2) 371 | read -rp "Custom IPv4:" SERVER_HOST_V4 372 | if [ -z "${SERVER_HOST_V4}" ]; then 373 | SERVER_HOST_V4="$(curl -4 --connect-timeout 5 --tlsv1.3 -s 'https://api.ipengine.dev' | jq -r '.network.ip')" 374 | fi 375 | if [ -z "${SERVER_HOST_V4}" ]; then 376 | SERVER_HOST_V4="$(curl -4 --connect-timeout 5 --tlsv1.3 -s 'https://checkip.amazonaws.com')" 377 | fi 378 | ;; 379 | esac 380 | } 381 | 382 | # Get the IPv4 383 | test-connectivity-v4 384 | 385 | # Determine IPv6 386 | function test-connectivity-v6() { 387 | echo "How would you like to detect IPv6?" 388 | echo " 1) Curl (Recommended)" 389 | echo " 2) Custom (Advanced)" 390 | until [[ "${SERVER_HOST_V6_SETTINGS}" =~ ^[1-2]$ ]]; do 391 | read -rp "IPv6 Choice [1-2]:" -e -i 1 SERVER_HOST_V6_SETTINGS 392 | done 393 | case ${SERVER_HOST_V6_SETTINGS} in 394 | 1) 395 | SERVER_HOST_V6="$(curl -6 --connect-timeout 5 --tlsv1.3 -s 'https://api.ipengine.dev' | jq -r '.network.ip')" 396 | if [ -z "${SERVER_HOST_V6}" ]; then 397 | SERVER_HOST_V6="$(curl -6 --connect-timeout 5 --tlsv1.3 -s 'https://checkip.amazonaws.com')" 398 | fi 399 | ;; 400 | 2) 401 | read -rp "Custom IPv6:" SERVER_HOST_V6 402 | if [ -z "${SERVER_HOST_V6}" ]; then 403 | SERVER_HOST_V6="$(curl -6 --connect-timeout 5 --tlsv1.3 -s 'https://api.ipengine.dev' | jq -r '.network.ip')" 404 | fi 405 | if [ -z "${SERVER_HOST_V6}" ]; then 406 | SERVER_HOST_V6="$(curl -6 --connect-timeout 5 --tlsv1.3 -s 'https://checkip.amazonaws.com')" 407 | fi 408 | ;; 409 | esac 410 | } 411 | 412 | # Get the IPv6 413 | test-connectivity-v6 414 | 415 | # Determine public NIC 416 | function server-pub-nic() { 417 | echo "How would you like to detect NIC?" 418 | echo " 1) IP (Recommended)" 419 | echo " 2) Custom (Advanced)" 420 | until [[ "${SERVER_PUB_NIC_SETTINGS}" =~ ^[1-2]$ ]]; do 421 | read -rp "Nic Choice [1-2]:" -e -i 1 SERVER_PUB_NIC_SETTINGS 422 | done 423 | case ${SERVER_PUB_NIC_SETTINGS} in 424 | 1) 425 | SERVER_PUB_NIC="$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)' | head -1)" 426 | if [ -z "${SERVER_PUB_NIC}" ]; then 427 | echo "Error: Your server's public network interface could not be found." 428 | fi 429 | ;; 430 | 2) 431 | read -rp "Custom NAT:" SERVER_PUB_NIC 432 | if [ -z "${SERVER_PUB_NIC}" ]; then 433 | SERVER_PUB_NIC="$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)' | head -1)" 434 | fi 435 | ;; 436 | esac 437 | } 438 | 439 | # Determine public NIC 440 | server-pub-nic 441 | 442 | # Determine host port 443 | function set-port() { 444 | echo "What port do you want WireGuard server to listen to?" 445 | echo " 1) 51820 (Recommended)" 446 | echo " 2) Custom (Advanced)" 447 | until [[ "${SERVER_PORT_SETTINGS}" =~ ^[1-2]$ ]]; do 448 | read -rp "Port Choice [1-2]:" -e -i 1 SERVER_PORT_SETTINGS 449 | done 450 | case ${SERVER_PORT_SETTINGS} in 451 | 1) 452 | SERVER_PORT="51820" 453 | if [ "$(lsof -i UDP:"${SERVER_PORT}")" ]; then 454 | echo "Error: Please use a different port because ${SERVER_PORT} is already in use." 455 | fi 456 | ;; 457 | 2) 458 | until [[ "${SERVER_PORT}" =~ ^[0-9]+$ ]] && [ "${SERVER_PORT}" -ge 1 ] && [ "${SERVER_PORT}" -le 65535 ]; do 459 | read -rp "Custom port [1-65535]:" SERVER_PORT 460 | done 461 | if [ "$(lsof -i UDP:"${SERVER_PORT}")" ]; then 462 | echo "Error: The port ${SERVER_PORT} is already used by a different application, please use a different port." 463 | fi 464 | ;; 465 | esac 466 | } 467 | 468 | # Set port 469 | set-port 470 | 471 | # Determine Keepalive interval. 472 | function nat-keepalive() { 473 | echo "What do you want your keepalive interval to be?" 474 | echo " 1) 25 (Default)" 475 | echo " 2) Custom (Advanced)" 476 | until [[ "${NAT_CHOICE_SETTINGS}" =~ ^[1-2]$ ]]; do 477 | read -rp "Nat Choice [1-2]:" -e -i 1 NAT_CHOICE_SETTINGS 478 | done 479 | case ${NAT_CHOICE_SETTINGS} in 480 | 1) 481 | NAT_CHOICE="25" 482 | ;; 483 | 2) 484 | until [[ "${NAT_CHOICE}" =~ ^[0-9]+$ ]] && [ "${NAT_CHOICE}" -ge 1 ] && [ "${NAT_CHOICE}" -le 65535 ]; do 485 | read -rp "Custom NAT [1-65535]:" NAT_CHOICE 486 | done 487 | ;; 488 | esac 489 | } 490 | 491 | # Keepalive interval 492 | nat-keepalive 493 | 494 | # Custom MTU or default settings 495 | function mtu-set() { 496 | echo "What MTU do you want to use?" 497 | echo " 1) 1280 (Recommended)" 498 | echo " 2) Custom (Advanced)" 499 | until [[ "${MTU_CHOICE_SETTINGS}" =~ ^[1-2]$ ]]; do 500 | read -rp "MTU Choice [1-2]:" -e -i 1 MTU_CHOICE_SETTINGS 501 | done 502 | case ${MTU_CHOICE_SETTINGS} in 503 | 1) 504 | MTU_CHOICE="1280" 505 | ;; 506 | 2) 507 | until [[ "${MTU_CHOICE}" =~ ^[0-9]+$ ]] && [ "${MTU_CHOICE}" -ge 1 ] && [ "${MTU_CHOICE}" -le 65535 ]; do 508 | read -rp "Custom MTU [1-65535]:" MTU_CHOICE 509 | done 510 | ;; 511 | esac 512 | } 513 | 514 | # Set MTU 515 | mtu-set 516 | 517 | # What IP version would you like to be available on this WireGuard server? 518 | function ipvx-select() { 519 | echo "What IPv do you want to use to connect to the WireGuard server?" 520 | echo " 1) IPv4 (Recommended)" 521 | echo " 2) IPv6" 522 | until [[ "${SERVER_HOST_SETTINGS}" =~ ^[1-2]$ ]]; do 523 | read -rp "IP Choice [1-2]:" -e -i 1 SERVER_HOST_SETTINGS 524 | done 525 | case ${SERVER_HOST_SETTINGS} in 526 | 1) 527 | if [ -n "${SERVER_HOST_V4}" ]; then 528 | SERVER_HOST="${SERVER_HOST_V4}" 529 | else 530 | SERVER_HOST="[${SERVER_HOST_V6}]" 531 | fi 532 | ;; 533 | 2) 534 | if [ -n "${SERVER_HOST_V6}" ]; then 535 | SERVER_HOST="[${SERVER_HOST_V6}]" 536 | else 537 | SERVER_HOST="${SERVER_HOST_V4}" 538 | fi 539 | ;; 540 | esac 541 | } 542 | 543 | # IPv4 or IPv6 Selector 544 | ipvx-select 545 | 546 | # Do you want to disable IPv4 or IPv6 or leave them both enabled? 547 | function disable-ipvx() { 548 | echo "Do you want to disable IPv4 or IPv6 on the server?" 549 | echo " 1) No (Recommended)" 550 | echo " 2) Disable IPv4" 551 | echo " 3) Disable IPv6" 552 | until [[ "${DISABLE_HOST_SETTINGS}" =~ ^[1-3]$ ]]; do 553 | read -rp "Disable Host Choice [1-3]:" -e -i 1 DISABLE_HOST_SETTINGS 554 | done 555 | case ${DISABLE_HOST_SETTINGS} in 556 | 1) 557 | echo "net.ipv4.ip_forward=1" >${WIREGUARD_IP_FORWARDING_CONFIG} 558 | echo "net.ipv6.conf.all.forwarding=1" >>${WIREGUARD_IP_FORWARDING_CONFIG} 559 | ;; 560 | 2) 561 | echo "net.ipv6.conf.all.forwarding=1" >${WIREGUARD_IP_FORWARDING_CONFIG} 562 | ;; 563 | 3) 564 | echo "net.ipv4.ip_forward=1" >${WIREGUARD_IP_FORWARDING_CONFIG} 565 | ;; 566 | esac 567 | sysctl -p ${WIREGUARD_IP_FORWARDING_CONFIG} 568 | } 569 | 570 | # Disable IPv4 or IPv6 571 | disable-ipvx 572 | 573 | # Would you like to allow connections to your LAN neighbors? 574 | function client-allowed-ip() { 575 | echo "What traffic do you want the client to forward through WireGuard?" 576 | echo " 1) Everything (Recommended)" 577 | echo " 2) Custom (Advanced)" 578 | until [[ "${CLIENT_ALLOWED_IP_SETTINGS}" =~ ^[1-2]$ ]]; do 579 | read -rp "Client Allowed IP Choice [1-2]:" -e -i 1 CLIENT_ALLOWED_IP_SETTINGS 580 | done 581 | case ${CLIENT_ALLOWED_IP_SETTINGS} in 582 | 1) 583 | CLIENT_ALLOWED_IP="0.0.0.0/0,::/0" 584 | ;; 585 | 2) 586 | read -rp "Custom IPs:" CLIENT_ALLOWED_IP 587 | if [ -z "${CLIENT_ALLOWED_IP}" ]; then 588 | CLIENT_ALLOWED_IP="0.0.0.0/0,::/0" 589 | fi 590 | ;; 591 | esac 592 | } 593 | 594 | # Traffic Forwarding 595 | client-allowed-ip 596 | 597 | # real-time updates 598 | function enable-automatic-updates() { 599 | echo "Would you like to setup real-time updates?" 600 | echo " 1) Yes (Recommended)" 601 | echo " 2) No (Advanced)" 602 | until [[ "${AUTOMATIC_UPDATES_SETTINGS}" =~ ^[1-2]$ ]]; do 603 | read -rp "Automatic Updates [1-2]:" -e -i 1 AUTOMATIC_UPDATES_SETTINGS 604 | done 605 | case ${AUTOMATIC_UPDATES_SETTINGS} in 606 | 1) 607 | crontab -l | { 608 | cat 609 | echo "0 0 * * * ${CURRENT_FILE_PATH} --update" 610 | } | crontab - 611 | if pgrep systemd-journal; then 612 | systemctl enable cron 613 | systemctl start cron 614 | else 615 | service cron enable 616 | service cron start 617 | fi 618 | ;; 619 | 2) 620 | echo "Real-time Updates Disabled" 621 | ;; 622 | esac 623 | } 624 | 625 | # real-time updates 626 | enable-automatic-updates 627 | 628 | # real-time backup 629 | function enable-automatic-backup() { 630 | echo "Would you like to setup real-time backup?" 631 | echo " 1) Yes (Recommended)" 632 | echo " 2) No (Advanced)" 633 | until [[ "${AUTOMATIC_BACKUP_SETTINGS}" =~ ^[1-2]$ ]]; do 634 | read -rp "Automatic Backup [1-2]:" -e -i 1 AUTOMATIC_BACKUP_SETTINGS 635 | done 636 | case ${AUTOMATIC_BACKUP_SETTINGS} in 637 | 1) 638 | crontab -l | { 639 | cat 640 | echo "0 0 * * * ${CURRENT_FILE_PATH} --backup" 641 | } | crontab - 642 | if pgrep systemd-journal; then 643 | systemctl enable cron 644 | systemctl start cron 645 | else 646 | service cron enable 647 | service cron start 648 | fi 649 | ;; 650 | 2) 651 | echo "Real-time Backup Disabled" 652 | ;; 653 | esac 654 | } 655 | 656 | # real-time backup 657 | enable-automatic-backup 658 | 659 | # Would you like to install unbound. 660 | function ask-install-dns() { 661 | echo "Which DNS provider would you like to use?" 662 | echo " 1) Unbound (Recommended)" 663 | echo " 2) Custom (Advanced)" 664 | until [[ "${DNS_PROVIDER_SETTINGS}" =~ ^[1-2]$ ]]; do 665 | read -rp "DNS provider [1-2]:" -e -i 1 DNS_PROVIDER_SETTINGS 666 | done 667 | case ${DNS_PROVIDER_SETTINGS} in 668 | 1) 669 | INSTALL_UNBOUND=true 670 | echo "Do you want to prevent advertisements, tracking, malware, and phishing using the content-blocker?" 671 | echo " 1) Yes (Recommended)" 672 | echo " 2) No" 673 | until [[ "${CONTENT_BLOCKER_SETTINGS}" =~ ^[1-2]$ ]]; do 674 | read -rp "Content Blocker Choice [1-2]:" -e -i 1 CONTENT_BLOCKER_SETTINGS 675 | done 676 | case ${CONTENT_BLOCKER_SETTINGS} in 677 | 1) 678 | INSTALL_BLOCK_LIST=true 679 | ;; 680 | 2) 681 | INSTALL_BLOCK_LIST=false 682 | ;; 683 | esac 684 | ;; 685 | 2) 686 | CUSTOM_DNS=true 687 | ;; 688 | esac 689 | } 690 | 691 | # Ask To Install DNS 692 | ask-install-dns 693 | 694 | # Let the users choose their custom dns provider. 695 | function custom-dns() { 696 | if [ "${CUSTOM_DNS}" == true ]; then 697 | echo "Which DNS do you want to use with the WireGuard connection?" 698 | echo " 1) Cloudflare (Recommended)" 699 | echo " 2) AdGuard" 700 | echo " 3) NextDNS" 701 | echo " 4) OpenDNS" 702 | echo " 5) Google" 703 | echo " 6) Verisign" 704 | echo " 7) Quad9" 705 | echo " 8) FDN" 706 | echo " 9) Custom (Advanced)" 707 | until [[ "${CLIENT_DNS_SETTINGS}" =~ ^[0-9]+$ ]] && [ "${CLIENT_DNS_SETTINGS}" -ge 1 ] && [ "${CLIENT_DNS_SETTINGS}" -le 9 ]; do 708 | read -rp "DNS [1-9]:" -e -i 1 CLIENT_DNS_SETTINGS 709 | done 710 | case ${CLIENT_DNS_SETTINGS} in 711 | 1) 712 | CLIENT_DNS="1.1.1.1,1.0.0.1,2606:4700:4700::1111,2606:4700:4700::1001" 713 | ;; 714 | 2) 715 | CLIENT_DNS="94.140.14.14,94.140.15.15,2a10:50c0::ad1:ff,2a10:50c0::ad2:ff" 716 | ;; 717 | 3) 718 | CLIENT_DNS="45.90.28.167,45.90.30.167,2a07:a8c0::12:cf53,2a07:a8c1::12:cf53" 719 | ;; 720 | 4) 721 | CLIENT_DNS="208.67.222.222,208.67.220.220,2620:119:35::35,2620:119:53::53" 722 | ;; 723 | 5) 724 | CLIENT_DNS="8.8.8.8,8.8.4.4,2001:4860:4860::8888,2001:4860:4860::8844" 725 | ;; 726 | 6) 727 | CLIENT_DNS="64.6.64.6,64.6.65.6,2620:74:1b::1:1,2620:74:1c::2:2" 728 | ;; 729 | 7) 730 | CLIENT_DNS="9.9.9.9,149.112.112.112,2620:fe::fe,2620:fe::9" 731 | ;; 732 | 8) 733 | CLIENT_DNS="80.67.169.40,80.67.169.12,2001:910:800::40,2001:910:800::12" 734 | ;; 735 | 9) 736 | read -rp "Custom DNS:" CLIENT_DNS 737 | if [ -z "${CLIENT_DNS}" ]; then 738 | CLIENT_DNS="8.8.8.8,8.8.4.4,2001:4860:4860::8888,2001:4860:4860::8844" 739 | fi 740 | ;; 741 | esac 742 | fi 743 | } 744 | 745 | # use custom dns 746 | custom-dns 747 | 748 | # What would you like to name your first WireGuard peer? 749 | function client-name() { 750 | if [ -z "${CLIENT_NAME}" ]; then 751 | echo "Let's name the WireGuard Peer. Use one word only, no special characters, no spaces." 752 | read -rp "Client name:" -e -i "$(openssl rand -hex 50)" CLIENT_NAME 753 | fi 754 | if [ -z "${CLIENT_NAME}" ]; then 755 | CLIENT_NAME="$(openssl rand -hex 50)" 756 | fi 757 | } 758 | 759 | # Client Name 760 | client-name 761 | 762 | # Automatically remove wireguard peers after a period of time. 763 | function auto-remove-confg() { 764 | echo "Would you like to expire the peer after a certain period of time?" 765 | echo " 1) Every Year (Recommended)" 766 | echo " 2) No" 767 | until [[ "${AUTOMATIC_CONFIG_REMOVER}" =~ ^[1-2]$ ]]; do 768 | read -rp "Automatic config expire [1-2]:" -e -i 1 AUTOMATIC_CONFIG_REMOVER 769 | done 770 | case ${AUTOMATIC_CONFIG_REMOVER} in 771 | 1) 772 | AUTOMATIC_WIREGUARD_EXPIRATION=true 773 | ;; 774 | 2) 775 | AUTOMATIC_WIREGUARD_EXPIRATION=false 776 | ;; 777 | esac 778 | } 779 | 780 | # Automatic Remove Config 781 | auto-remove-confg 782 | 783 | # Lets check the kernel version and check if headers are required 784 | function install-kernel-headers() { 785 | ALLOWED_KERNEL_VERSION="5.6" 786 | if (($(echo "${CURRENT_KERNEL_VERSION} <= ${ALLOWED_KERNEL_VERSION}" | bc -l))); then 787 | if { [ "${CURRENT_DISTRO}" == "ubuntu" ] || [ "${CURRENT_DISTRO}" == "debian" ] || [ "${CURRENT_DISTRO}" == "pop" ] || [ "${CURRENT_DISTRO}" == "kali" ] || [ "${CURRENT_DISTRO}" == "linuxmint" ] || [ "${CURRENT_DISTRO}" == "neon" ]; }; then 788 | apt-get update 789 | apt-get install linux-headers-"$(uname -r)" -y 790 | elif [ "${CURRENT_DISTRO}" == "raspbian" ]; then 791 | apt-get update 792 | apt-get install raspberrypi-kernel-headers -y 793 | elif { [ "${CURRENT_DISTRO}" == "arch" ] || [ "${CURRENT_DISTRO}" == "archarm" ] || [ "${CURRENT_DISTRO}" == "manjaro" ]; }; then 794 | pacman -Syu --noconfirm --needed linux-headers 795 | elif [ "${CURRENT_DISTRO}" == "fedora" ]; then 796 | dnf update -y 797 | dnf install kernel-headers-"$(uname -r)" kernel-devel-"$(uname -r)" -y 798 | elif { [ "${CURRENT_DISTRO}" == "centos" ] || [ "${CURRENT_DISTRO}" == "rhel" ] || [ "${CURRENT_DISTRO}" == "almalinux" ] || [ "${CURRENT_DISTRO}" == "rocky" ]; }; then 799 | yum update -y 800 | yum install kernel-headers-"$(uname -r)" kernel-devel-"$(uname -r)" -y 801 | fi 802 | else 803 | echo "Correct: You do not need kernel headers." >>/dev/null 804 | fi 805 | } 806 | 807 | # Kernel Version 808 | install-kernel-headers 809 | 810 | # Install WireGuard Server 811 | function install-wireguard-server() { 812 | if [ ! -x "$(command -v wg)" ]; then 813 | if [ "${CURRENT_DISTRO}" == "ubuntu" ] && [ "${CURRENT_DISTRO_VERSION%.*}" -ge 21 ]; then 814 | apt-get update 815 | apt-get install wireguard -y 816 | elif [ "${CURRENT_DISTRO}" == "ubuntu" ] && [ "${CURRENT_DISTRO_VERSION%.*}" -le 20 ]; then 817 | apt-get update 818 | apt-get install software-properties-common -y 819 | add-apt-repository ppa:wireguard/wireguard -y 820 | apt-get update 821 | apt-get install wireguard -y 822 | elif { [ "${CURRENT_DISTRO}" == "pop" ] || [ "${CURRENT_DISTRO}" == "linuxmint" ] || [ "${CURRENT_DISTRO}" == "neon" ]; }; then 823 | apt-get update 824 | apt-get install wireguard -y 825 | elif { [ "${CURRENT_DISTRO}" == "debian" ] || [ "${CURRENT_DISTRO}" == "kali" ]; }; then 826 | apt-get update 827 | if { [ "${CURRENT_DISTRO}" == "debian" ] && [ "${CURRENT_DISTRO_VERSION%.*}" -le 11 ]; }; then 828 | if [ ! -f "/etc/apt/sources.list.d/backports.list" ]; then 829 | echo "deb http://deb.debian.org/debian buster-backports main" >>/etc/apt/sources.list.d/backports.list 830 | apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 648ACFD622F3D138 831 | apt-get update 832 | fi 833 | fi 834 | apt-get install wireguard -y 835 | elif [ "${CURRENT_DISTRO}" == "raspbian" ]; then 836 | apt-get update 837 | if [ ! -f "/etc/apt/sources.list.d/backports.list" ]; then 838 | echo "deb http://deb.debian.org/debian buster-backports main" >>/etc/apt/sources.list.d/backports.list 839 | apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 648ACFD622F3D138 840 | apt-get update 841 | fi 842 | apt-get install wireguard -y 843 | elif { [ "${CURRENT_DISTRO}" == "arch" ] || [ "${CURRENT_DISTRO}" == "archarm" ] || [ "${CURRENT_DISTRO}" == "manjaro" ]; }; then 844 | pacman -Syu --noconfirm --needed wireguard-tools 845 | elif [ "${CURRENT_DISTRO}" = "fedora" ] && [ "${CURRENT_DISTRO_VERSION%.*}" -ge 32 ]; then 846 | dnf update -y 847 | dnf install wireguard-tools -y 848 | elif [ "${CURRENT_DISTRO}" = "fedora" ] && [ "${CURRENT_DISTRO_VERSION%.*}" -le 31 ]; then 849 | dnf update -y 850 | dnf copr enable jdoss/wireguard -y 851 | dnf install wireguard-dkms wireguard-tools -y 852 | elif [ "${CURRENT_DISTRO}" == "centos" ] && [ "${CURRENT_DISTRO_VERSION%.*}" -ge 8 ]; then 853 | yum update -y 854 | yum install kmod-wireguard wireguard-tools -y 855 | elif [ "${CURRENT_DISTRO}" == "centos" ] && [ "${CURRENT_DISTRO_VERSION%.*}" -le 7 ]; then 856 | yum update -y 857 | yum install yum-plugin-elrepo -y 858 | yum install kmod-wireguard wireguard-tools -y 859 | elif [ "${CURRENT_DISTRO}" == "rhel" ] && [ "${CURRENT_DISTRO_VERSION%.*}" == 8 ]; then 860 | yum update -y 861 | yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm https://www.elrepo.org/elrepo-release-8.el8.elrepo.noarch.rpm 862 | yum update -y 863 | yum install kmod-wireguard wireguard-tools -y 864 | elif [ "${CURRENT_DISTRO}" == "rhel" ] && [ "${CURRENT_DISTRO_VERSION%.*}" == 7 ]; then 865 | yum update -y 866 | yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm 867 | yum install kmod-wireguard wireguard-tools -y 868 | elif [ "${CURRENT_DISTRO}" == "alpine" ]; then 869 | apk update 870 | apk add wireguard-tools 871 | elif [ "${CURRENT_DISTRO}" == "freebsd" ]; then 872 | pkg update 873 | pkg install wireguard 874 | elif { [ "${CURRENT_DISTRO}" == "almalinux" ] || [ "${CURRENT_DISTRO}" == "rocky" ]; }; then 875 | yum update -y 876 | yum install kmod-wireguard wireguard-tools -y 877 | fi 878 | fi 879 | } 880 | 881 | # Install WireGuard Server 882 | install-wireguard-server 883 | 884 | # Function to install Unbound 885 | function install-unbound() { 886 | if [ "${INSTALL_UNBOUND}" == true ]; then 887 | if { [ ! -x "$(command -v unbound)" ] || [ ! -x "$(command -v resolvconf)" ]; }; then 888 | if { [ "${CURRENT_DISTRO}" == "debian" ] || [ "${CURRENT_DISTRO}" == "ubuntu" ] || [ "${CURRENT_DISTRO}" == "raspbian" ] || [ "${CURRENT_DISTRO}" == "pop" ] || [ "${CURRENT_DISTRO}" == "kali" ] || [ "${CURRENT_DISTRO}" == "linuxmint" ] || [ "${CURRENT_DISTRO}" == "neon" ]; }; then 889 | apt-get install unbound resolvconf -y 890 | if [ "${CURRENT_DISTRO}" == "ubuntu" ]; then 891 | if pgrep systemd-journal; then 892 | systemctl stop systemd-resolved 893 | systemctl disable systemd-resolved 894 | else 895 | service systemd-resolved stop 896 | service systemd-resolved disable 897 | fi 898 | fi 899 | elif { [ "${CURRENT_DISTRO}" == "centos" ] || [ "${CURRENT_DISTRO}" == "rhel" ] || [ "${CURRENT_DISTRO}" == "almalinux" ] || [ "${CURRENT_DISTRO}" == "rocky" ]; }; then 900 | yum install unbound -y 901 | elif [ "${CURRENT_DISTRO}" == "fedora" ]; then 902 | dnf install unbound resolvconf -y 903 | elif { [ "${CURRENT_DISTRO}" == "arch" ] || [ "${CURRENT_DISTRO}" == "archarm" ] || [ "${CURRENT_DISTRO}" == "manjaro" ]; }; then 904 | pacman -Syu --noconfirm unbound resolvconf 905 | elif [ "${CURRENT_DISTRO}" == "alpine" ]; then 906 | apk add unbound resolvconf 907 | elif [ "${CURRENT_DISTRO}" == "freebsd" ]; then 908 | pkg install unbound resolvconf 909 | fi 910 | fi 911 | unbound-anchor -a ${UNBOUND_ANCHOR} 912 | curl "${UNBOUND_ROOT_SERVER_CONFIG_URL}" --create-dirs -o ${UNBOUND_ROOT_HINTS} 913 | NPROC=$(nproc) 914 | echo "server: 915 | num-threads: ${NPROC} 916 | verbosity: 1 917 | root-hints: ${UNBOUND_ROOT_HINTS} 918 | auto-trust-anchor-file: ${UNBOUND_ANCHOR} 919 | interface: 0.0.0.0 920 | interface: ::0 921 | max-udp-size: 3072 922 | access-control: 0.0.0.0/0 refuse 923 | access-control: ::0 refuse 924 | access-control: ${PRIVATE_SUBNET_V4} allow 925 | access-control: ${PRIVATE_SUBNET_V6} allow 926 | access-control: 127.0.0.1 allow 927 | access-control: ::1 allow 928 | private-address: ${PRIVATE_SUBNET_V4} 929 | private-address: ${PRIVATE_SUBNET_V6} 930 | do-tcp: no 931 | hide-identity: yes 932 | hide-version: yes 933 | harden-glue: yes 934 | harden-dnssec-stripped: yes 935 | harden-referral-path: yes 936 | unwanted-reply-threshold: 10000000 937 | cache-min-ttl: 1800 938 | cache-max-ttl: 14400 939 | prefetch: yes 940 | qname-minimisation: yes 941 | prefetch-key: yes" >${UNBOUND_CONFIG} 942 | if [ -f "${RESOLV_CONFIG_OLD}" ]; then 943 | rm -f ${RESOLV_CONFIG_OLD} 944 | fi 945 | if [ -f "${RESOLV_CONFIG}" ]; then 946 | chattr -i ${RESOLV_CONFIG} 947 | mv ${RESOLV_CONFIG} ${RESOLV_CONFIG_OLD} 948 | fi 949 | echo "nameserver 127.0.0.1" >>${RESOLV_CONFIG} 950 | echo "nameserver ::1" >>${RESOLV_CONFIG} 951 | chattr +i ${RESOLV_CONFIG} 952 | echo "Unbound: true" >>${UNBOUND_MANAGER} 953 | if [ "${INSTALL_BLOCK_LIST}" == true ]; then 954 | echo "include: ${UNBOUND_CONFIG_HOST}" >>${UNBOUND_CONFIG} 955 | curl "${UNBOUND_CONFIG_HOST_URL}" | awk '$1' | awk '{print "local-zone: \""$1"\" always_refuse"}' >${UNBOUND_CONFIG_HOST} 956 | fi 957 | # restart unbound 958 | if pgrep systemd-journal; then 959 | systemctl reenable unbound 960 | systemctl restart unbound 961 | else 962 | service unbound enable 963 | service unbound restart 964 | fi 965 | CLIENT_DNS="${GATEWAY_ADDRESS_V4},${GATEWAY_ADDRESS_V6}" 966 | fi 967 | } 968 | 969 | # Running Install Unbound 970 | install-unbound 971 | 972 | # WireGuard Set Config 973 | function wireguard-setconf() { 974 | SERVER_PRIVKEY=$(wg genkey) 975 | SERVER_PUBKEY=$(echo "${SERVER_PRIVKEY}" | wg pubkey) 976 | CLIENT_PRIVKEY=$(wg genkey) 977 | CLIENT_PUBKEY=$(echo "${CLIENT_PRIVKEY}" | wg pubkey) 978 | CLIENT_ADDRESS_V4=$(echo "${PRIVATE_SUBNET_V4}" | cut -d'.' -f1-3).2 979 | CLIENT_ADDRESS_V6="${PRIVATE_SUBNET_V6::-3}2" 980 | PRESHARED_KEY=$(wg genpsk) 981 | PEER_PORT=$(shuf -i1024-65535 -n1) 982 | mkdir -p ${WIREGUARD_CLIENT_PATH} 983 | if [ "${INSTALL_UNBOUND}" == true ]; then 984 | IPTABLES_POSTUP="iptables -A FORWARD -i ${WIREGUARD_PUB_NIC} -j ACCEPT; iptables -t nat -A POSTROUTING -o ${SERVER_PUB_NIC} -j MASQUERADE; ip6tables -A FORWARD -i ${WIREGUARD_PUB_NIC} -j ACCEPT; ip6tables -t nat -A POSTROUTING -o ${SERVER_PUB_NIC} -j MASQUERADE; iptables -A INPUT -s ${PRIVATE_SUBNET_V4} -p udp -m udp --dport 53 -m conntrack --ctstate NEW -j ACCEPT; ip6tables -A INPUT -s ${PRIVATE_SUBNET_V6} -p udp -m udp --dport 53 -m conntrack --ctstate NEW -j ACCEPT" 985 | IPTABLES_POSTDOWN="iptables -D FORWARD -i ${WIREGUARD_PUB_NIC} -j ACCEPT; iptables -t nat -D POSTROUTING -o ${SERVER_PUB_NIC} -j MASQUERADE; ip6tables -D FORWARD -i ${WIREGUARD_PUB_NIC} -j ACCEPT; ip6tables -t nat -D POSTROUTING -o ${SERVER_PUB_NIC} -j MASQUERADE; iptables -D INPUT -s ${PRIVATE_SUBNET_V4} -p udp -m udp --dport 53 -m conntrack --ctstate NEW -j ACCEPT; ip6tables -D INPUT -s ${PRIVATE_SUBNET_V6} -p udp -m udp --dport 53 -m conntrack --ctstate NEW -j ACCEPT" 986 | else 987 | IPTABLES_POSTUP="iptables -A FORWARD -i ${WIREGUARD_PUB_NIC} -j ACCEPT; iptables -t nat -A POSTROUTING -o ${SERVER_PUB_NIC} -j MASQUERADE; ip6tables -A FORWARD -i ${WIREGUARD_PUB_NIC} -j ACCEPT; ip6tables -t nat -A POSTROUTING -o ${SERVER_PUB_NIC} -j MASQUERADE" 988 | IPTABLES_POSTDOWN="iptables -D FORWARD -i ${WIREGUARD_PUB_NIC} -j ACCEPT; iptables -t nat -D POSTROUTING -o ${SERVER_PUB_NIC} -j MASQUERADE; ip6tables -D FORWARD -i ${WIREGUARD_PUB_NIC} -j ACCEPT; ip6tables -t nat -D POSTROUTING -o ${SERVER_PUB_NIC} -j MASQUERADE" 989 | fi 990 | # Set WireGuard settings for this host and first peer. 991 | echo "# ${PRIVATE_SUBNET_V4} ${PRIVATE_SUBNET_V6} ${SERVER_HOST}:${SERVER_PORT} ${SERVER_PUBKEY} ${CLIENT_DNS} ${MTU_CHOICE} ${NAT_CHOICE} ${CLIENT_ALLOWED_IP} 992 | [Interface] 993 | Address = ${GATEWAY_ADDRESS_V4}/${PRIVATE_SUBNET_MASK_V4},${GATEWAY_ADDRESS_V6}/${PRIVATE_SUBNET_MASK_V6} 994 | DNS = ${CLIENT_DNS} 995 | ListenPort = ${SERVER_PORT} 996 | MTU = ${MTU_CHOICE} 997 | PrivateKey = ${SERVER_PRIVKEY} 998 | PostUp = ${IPTABLES_POSTUP} 999 | PostDown = ${IPTABLES_POSTDOWN} 1000 | SaveConfig = false 1001 | # ${CLIENT_NAME} start 1002 | [Peer] 1003 | PublicKey = ${CLIENT_PUBKEY} 1004 | PresharedKey = ${PRESHARED_KEY} 1005 | AllowedIPs = ${CLIENT_ADDRESS_V4}/32,${CLIENT_ADDRESS_V6}/128 1006 | # ${CLIENT_NAME} end" >>${WIREGUARD_CONFIG} 1007 | 1008 | echo "# ${WIREGUARD_WEBSITE_URL} 1009 | [Interface] 1010 | Address = ${CLIENT_ADDRESS_V4}/${PRIVATE_SUBNET_MASK_V4},${CLIENT_ADDRESS_V6}/${PRIVATE_SUBNET_MASK_V6} 1011 | DNS = ${CLIENT_DNS} 1012 | ListenPort = ${PEER_PORT} 1013 | MTU = ${MTU_CHOICE} 1014 | PrivateKey = ${CLIENT_PRIVKEY} 1015 | [Peer] 1016 | AllowedIPs = ${CLIENT_ALLOWED_IP} 1017 | Endpoint = ${SERVER_HOST}:${SERVER_PORT} 1018 | PersistentKeepalive = ${NAT_CHOICE} 1019 | PresharedKey = ${PRESHARED_KEY} 1020 | PublicKey = ${SERVER_PUBKEY}" >>${WIREGUARD_CLIENT_PATH}/"${CLIENT_NAME}"-${WIREGUARD_PUB_NIC}.conf 1021 | # If automaic wireguard expiration is enabled than set the expiration date. 1022 | if [ ${AUTOMATIC_WIREGUARD_EXPIRATION} == true ]; then 1023 | crontab -l | { 1024 | cat 1025 | echo "$(date +%M) $(date +%H) $(date +%d) $(date +%m) * echo -e \"${CLIENT_NAME}\" | ${CURRENT_FILE_PATH} --remove" 1026 | } | crontab - 1027 | if pgrep systemd-journal; then 1028 | systemctl enable cron 1029 | systemctl start cron 1030 | else 1031 | service cron enable 1032 | service cron start 1033 | fi 1034 | fi 1035 | # Service Restart 1036 | if pgrep systemd-journal; then 1037 | systemctl reenable wg-quick@${WIREGUARD_PUB_NIC} 1038 | systemctl restart wg-quick@${WIREGUARD_PUB_NIC} 1039 | else 1040 | service wg-quick@${WIREGUARD_PUB_NIC} enable 1041 | service wg-quick@${WIREGUARD_PUB_NIC} restart 1042 | fi 1043 | # Generate QR Code 1044 | qrencode -t ansiutf8 -r ${WIREGUARD_CLIENT_PATH}/"${CLIENT_NAME}"-${WIREGUARD_PUB_NIC}.conf 1045 | echo "Client Config --> ${WIREGUARD_CLIENT_PATH}/${CLIENT_NAME}-${WIREGUARD_PUB_NIC}.conf" 1046 | } 1047 | 1048 | # Setting Up WireGuard Config 1049 | wireguard-setconf 1050 | 1051 | # After WireGuard Install 1052 | else 1053 | 1054 | # Already installed what next? 1055 | function wireguard-next-questions-interface() { 1056 | echo "What do you want to do?" 1057 | echo " 1) Show WireGuard" 1058 | echo " 2) Start WireGuard" 1059 | echo " 3) Stop WireGuard" 1060 | echo " 4) Restart WireGuard" 1061 | echo " 5) Add WireGuard Peer (client)" 1062 | echo " 6) Remove WireGuard Peer (client)" 1063 | echo " 7) Reinstall WireGuard" 1064 | echo " 8) Uninstall WireGuard" 1065 | echo " 9) Update this script" 1066 | echo " 10) Backup WireGuard" 1067 | echo " 11) Restore WireGuard" 1068 | echo " 12) Update Interface IP" 1069 | echo " 13) Update Interface Port" 1070 | echo " 14) Purge WireGuard Peers" 1071 | echo " 15) Generate QR Code" 1072 | until [[ "${WIREGUARD_OPTIONS}" =~ ^[0-9]+$ ]] && [ "${WIREGUARD_OPTIONS}" -ge 1 ] && [ "${WIREGUARD_OPTIONS}" -le 15 ]; do 1073 | read -rp "Select an Option [1-15]:" -e -i 0 WIREGUARD_OPTIONS 1074 | done 1075 | case ${WIREGUARD_OPTIONS} in 1076 | 1) # WG Show 1077 | wg show ${WIREGUARD_PUB_NIC} 1078 | ;; 1079 | 2) # Enable & Start WireGuard 1080 | if pgrep systemd-journal; then 1081 | systemctl enable wg-quick@${WIREGUARD_PUB_NIC} 1082 | systemctl start wg-quick@${WIREGUARD_PUB_NIC} 1083 | else 1084 | service wg-quick@${WIREGUARD_PUB_NIC} enable 1085 | service wg-quick@${WIREGUARD_PUB_NIC} start 1086 | fi 1087 | ;; 1088 | 3) # Disable & Stop WireGuard 1089 | if pgrep systemd-journal; then 1090 | systemctl disable wg-quick@${WIREGUARD_PUB_NIC} 1091 | systemctl stop wg-quick@${WIREGUARD_PUB_NIC} 1092 | else 1093 | service wg-quick@${WIREGUARD_PUB_NIC} disable 1094 | service wg-quick@${WIREGUARD_PUB_NIC} stop 1095 | fi 1096 | ;; 1097 | 4) # Restart WireGuard 1098 | if pgrep systemd-journal; then 1099 | systemctl restart wg-quick@${WIREGUARD_PUB_NIC} 1100 | else 1101 | service wg-quick@${WIREGUARD_PUB_NIC} restart 1102 | fi 1103 | ;; 1104 | 5) # WireGuard add Peer 1105 | # If the client name is set then assign it to the NEW_CLIENT_NAME 1106 | if [[ "${CLIENT_USER}" != "" ]]; then 1107 | NEW_CLIENT_NAME=${CLIENT_USER} 1108 | fi 1109 | if [ -z "${NEW_CLIENT_NAME}" ]; then 1110 | echo "Let's name the WireGuard Peer. Use one word only, no special characters, no spaces." 1111 | read -rp "New client peer:" -e -i "$(openssl rand -hex 50)" NEW_CLIENT_NAME 1112 | fi 1113 | if [ -z "${NEW_CLIENT_NAME}" ]; then 1114 | NEW_CLIENT_NAME="$(openssl rand -hex 50)" 1115 | fi 1116 | # Check if user exists 1117 | if [[ "$(egrep ${NEW_CLIENT_NAME} ${WIREGUARD_CONFIG})" != "" ]]; then 1118 | 1119 | echo "User already exists." 1120 | exit 1 1121 | fi 1122 | 1123 | if [[ "${CUSTOM_EXPIRE}" != "" ]]; then 1124 | 1125 | # Custom Expiration date for the user 1126 | read -p "Do you want to set a custom expiration date for the user? [y|N]" set_expire 1127 | if [[ "${set_expire}" =~ ^[Yy]$ ]]; then 1128 | 1129 | function custom_exp() { 1130 | 1131 | read -p "Enter the month (MM) (e.g. 01 for January ): " the_month 1132 | read -p "Enter the day (DD) (e.g. 05 or 25): " the_day 1133 | read -p "Enter the year (YYYY) (e.g. 2022): " the_year 1134 | 1135 | # Format the date YYYYMMDD 1136 | REMOVE_DATE="${the_year}${the_month}${the_day}" 1137 | 1138 | # Current date 1139 | CURR_DATE=$(date +"%Y%m%d") 1140 | 1141 | # Check if date is current or in the future 1142 | if [[ ${REMOVE_DATE} -le ${CURR_DATE} ]]; then 1143 | 1144 | echo "Date must be greater than or equal to today's date...please wait" 1145 | read -p "Hit ENTER to continue" 1146 | custom_exp 1147 | 1148 | fi 1149 | 1150 | # Check if the entered date is valid 1151 | # Catches leap year invalid dates too 1152 | date -d "${REMOVE_DATE}" > /dev/null 2>&1 1153 | 1154 | # If the date is valid, it will return 0 1155 | if [[ $? != 0 ]]; then 1156 | 1157 | echo "Invalid Date provided." 1158 | read -p "Press ENTER to continue" 1159 | custom_exp 1160 | 1161 | fi 1162 | 1163 | # Current Hour and minute 1164 | THE_HOUR_MIN=$(date '+%H%M' -d '1 hour ago') 1165 | 1166 | # Removal Date 1167 | THE_REMOVE_DATE="${REMOVE_DATE}${THE_HOUR_MIN}" 1168 | 1169 | } 1170 | 1171 | custom_exp 1172 | 1173 | fi 1174 | fi 1175 | # See if there are any gaps in IPs. Removing a user leaves an available IP. 1176 | # https://stackoverflow.com/questions/38491676/how-can-i-find-the-missing-integers-in-a-unique-and-sequential-list-one-per-lin 1177 | UNUSED_IPS=$(egrep -o "^AllowedIPs = $(head -n1 ${WIREGUARD_CONFIG} | awk ' { print $2 } ' | cut -d\. -f1,2,3)\.[0-9]{1,3}" ${WIREGUARD_CONFIG} | awk ' { print $3 } ' | cut -d\. -f4 | sort -n | awk '{for(i=p+1; i<$1; i++) print i} {p=$1}' | egrep -v '^1$') 1178 | # If there is an unused IP. Get the first one (especially, if there are more than one). 1179 | if [[ "${UNUSED_IPS}" != "" ]]; then 1180 | LASTIPV4=$(echo "${UNUSED_IPS}" | head -n 1) 1181 | LASTIPV6=$(echo "${UNUSED_IPS}" | head -n 1) 1182 | else 1183 | # Else use the standard method 1184 | LASTIPV4=$(grep "/32" ${WIREGUARD_CONFIG} | awk '{print $3}' | cut -d "/" -f 1 | cut -d "." -f 4 | sort | tail -n1) 1185 | LASTIPV6=$(grep "/128" ${WIREGUARD_CONFIG} | awk '{print $3}' | cut -d ":" -f 5 | cut -d "/" -f 1 | sort | tail -n1) 1186 | 1187 | fi 1188 | #LASTIPV4=$(grep "/32" ${WIREGUARD_CONFIG} | tail -n1 | awk '{print $3}' | cut -d "/" -f 1 | cut -d "." -f 4) 1189 | #LASTIPV6=$(grep "/128" ${WIREGUARD_CONFIG} | tail -n1 | awk '{print $3}' | cut -d ":" -f 5 | cut -d "/" -f 1) 1190 | if { [ -z "${LASTIPV4}" ] && [ -z "${LASTIPV6}" ]; }; then 1191 | LASTIPV4="1" 1192 | LASTIPV6="1" 1193 | fi 1194 | if { [ "${LASTIPV4}" -ge 255 ] && [ "${LASTIPV6}" -ge 255 ]; }; then 1195 | CURRENT_IPV4_RANGE=$(head -n1 ${WIREGUARD_CONFIG} | awk '{print $2}') 1196 | CURRENT_IPV6_RANGE=$(head -n1 ${WIREGUARD_CONFIG} | awk '{print $3}') 1197 | IPV4_BEFORE_BACKSLASH=$(echo "${CURRENT_IPV4_RANGE}" | cut -d "/" -f 1 | cut -d "." -f 4) 1198 | IPV6_BEFORE_BACKSLASH=$(echo "${CURRENT_IPV6_RANGE}" | cut -d "/" -f 1 | cut -d ":" -f 5) 1199 | IPV4_AFTER_FIRST=$(echo "${CURRENT_IPV4_RANGE}" | cut -d "/" -f 1 | cut -d "." -f 2) 1200 | IPV6_AFTER_FIRST=$(echo "${CURRENT_IPV6_RANGE}" | cut -d "/" -f 1 | cut -d ":" -f 2) 1201 | SECOND_IPV4_IN_RANGE=$(head -n1 ${WIREGUARD_CONFIG} | awk '{print $2}' | cut -d "/" -f 1 | cut -d "." -f 2) 1202 | SECOND_IPV6_IN_RANGE=$(head -n1 ${WIREGUARD_CONFIG} | awk '{print $3}' | cut -d "/" -f 1 | cut -d ":" -f 2) 1203 | THIRD_IPV4_IN_RANGE=$(head -n1 ${WIREGUARD_CONFIG} | awk '{print $2}' | cut -d "/" -f 1 | cut -d "." -f 3) 1204 | THIRD_IPV6_IN_RANGE=$(head -n1 ${WIREGUARD_CONFIG} | awk '{print $3}' | cut -d "/" -f 1 | cut -d ":" -f 3) 1205 | NEXT_IPV4_RANGE=$((THIRD_IPV4_IN_RANGE + 1)) 1206 | NEXT_IPV6_RANGE=$((THIRD_IPV6_IN_RANGE + 1)) 1207 | CURRENT_IPV4_RANGE_CIDR=$(head -n1 ${WIREGUARD_CONFIG} | awk '{print $2}' | cut -d "/" -f 2) 1208 | CURRENT_IPV6_RANGE_CIDR=$(head -n1 ${WIREGUARD_CONFIG} | awk '{print $3}' | cut -d "/" -f 2) 1209 | FINAL_IPV4_RANGE=$(echo "${CURRENT_IPV4_RANGE}" | cut -d "/" -f 1 | cut -d "." -f 1,2)".${NEXT_IPV4_RANGE}.${IPV4_BEFORE_BACKSLASH}/${CURRENT_IPV4_RANGE_CIDR}" 1210 | FINAL_IPV6_RANGE=$(echo "${CURRENT_IPV6_RANGE}" | cut -d "/" -f 1 | cut -d ":" -f 1,2)":${NEXT_IPV6_RANGE}::${IPV6_BEFORE_BACKSLASH}/${CURRENT_IPV6_RANGE_CIDR}" 1211 | if { [ "${THIRD_IPV4_IN_RANGE}" -ge 255 ] && [ "${THIRD_IPV6_IN_RANGE}" -ge 255 ]; }; then 1212 | if { [ "${SECOND_IPV4_IN_RANGE}" -ge 255 ] && [ "${SECOND_IPV6_IN_RANGE}" -ge 255 ] && [ "${THIRD_IPV4_IN_RANGE}" -ge 255 ] && [ "${THIRD_IPV6_IN_RANGE}" -ge 255 ] && [ "${LASTIPV4}" -ge 255 ] && [ "${LASTIPV6}" -ge 255 ]; }; then 1213 | echo "Error: You are unable to add any more peers." 1214 | exit 1215 | fi 1216 | NEXT_IPV4_RANGE=$((SECOND_IPV4_IN_RANGE + 1)) 1217 | NEXT_IPV6_RANGE=$((SECOND_IPV6_IN_RANGE + 1)) 1218 | FINAL_IPV4_RANGE=$(echo "${CURRENT_IPV4_RANGE}" | cut -d "/" -f 1 | cut -d "." -f 1)".${NEXT_IPV4_RANGE}.${IPV4_AFTER_FIRST}.${IPV4_BEFORE_BACKSLASH}/${CURRENT_IPV4_RANGE_CIDR}" 1219 | FINAL_IPV6_RANGE=$(echo "${CURRENT_IPV6_RANGE}" | cut -d "/" -f 1 | cut -d ":" -f 1)":${NEXT_IPV6_RANGE}:${IPV6_AFTER_FIRST}::${IPV6_BEFORE_BACKSLASH}/${CURRENT_IPV6_RANGE_CIDR}" 1220 | fi 1221 | sed -i "1s|${CURRENT_IPV4_RANGE}|${FINAL_IPV4_RANGE}|" ${WIREGUARD_CONFIG} 1222 | sed -i "1s|${CURRENT_IPV6_RANGE}|${FINAL_IPV6_RANGE}|" ${WIREGUARD_CONFIG} 1223 | LASTIPV4="2" 1224 | LASTIPV6="2" 1225 | fi 1226 | CLIENT_PRIVKEY=$(wg genkey) 1227 | CLIENT_PUBKEY=$(echo "${CLIENT_PRIVKEY}" | wg pubkey) 1228 | PRESHARED_KEY=$(wg genpsk) 1229 | PEER_PORT=$(shuf -i1024-65535 -n1) 1230 | PRIVATE_SUBNET_V4=$(head -n1 ${WIREGUARD_CONFIG} | awk '{print $2}') 1231 | PRIVATE_SUBNET_MASK_V4=$(echo "${PRIVATE_SUBNET_V4}" | cut -d "/" -f 2) 1232 | PRIVATE_SUBNET_V6=$(head -n1 ${WIREGUARD_CONFIG} | awk '{print $3}') 1233 | PRIVATE_SUBNET_MASK_V6=$(echo "${PRIVATE_SUBNET_V6}" | cut -d "/" -f 2) 1234 | SERVER_HOST=$(head -n1 ${WIREGUARD_CONFIG} | awk '{print $4}') 1235 | SERVER_PUBKEY=$(head -n1 ${WIREGUARD_CONFIG} | awk '{print $5}') 1236 | CLIENT_DNS=$(head -n1 ${WIREGUARD_CONFIG} | awk '{print $6}') 1237 | MTU_CHOICE=$(head -n1 ${WIREGUARD_CONFIG} | awk '{print $7}') 1238 | NAT_CHOICE=$(head -n1 ${WIREGUARD_CONFIG} | awk '{print $8}') 1239 | CLIENT_ALLOWED_IP=$(head -n1 ${WIREGUARD_CONFIG} | awk '{print $9}') 1240 | # Check if unused IPs. Prevents assigning duplicate IPs 1241 | if [[ "${UNUSED_IPS}" == "" ]]; then 1242 | CLIENT_ADDRESS_V4=$(echo "${PRIVATE_SUBNET_V4}" | cut -d'.' -f1-3).$((LASTIPV4 + 1)) 1243 | CLIENT_ADDRESS_V6="${PRIVATE_SUBNET_V6::-3}$((LASTIPV6 + 1))" 1244 | # Remove the leading zero in octets such as 01, 03, etc. http://drewgraybeal.blogspot.com/2015/07/remove-leading-zeros-from-ip-address.html 1245 | CLIENT_ADDRESS_V4=$(echo ${CLIENT_ADDRESS_V4} | sed -r 's/^0*([0-9]+)\.0*([0-9]+)\.0*([0-9]+)\.0*([0-9]+)$/\1.\2.\3.\4/') 1246 | CLIENT_ADDRESS_V6=$(echo ${CLIENT_ADDRESS_V6} | sed -r 's/^0*([0-9]+)\.0*([0-9]+)\.0*([0-9]+)\.0*([0-9]+)$/\1.\2.\3.\4/') 1247 | LASTIPV6=$(echo ${LASTIPV4} | sed -r 's/^0*([0-9]+)\.0*([0-9]+)\.0*([0-9]+)\.0*([0-9]+)$/\1.\2.\3.\4/') 1248 | else 1249 | CLIENT_ADDRESS_V4="${PRIVATE_SUBNET_V4::-3}${LASTIPV4}" 1250 | CLIENT_ADDRESS_V6="${PRIVATE_SUBNET_V6::-3}${LASTIPV6}" 1251 | # Remove the leading zero in octets such as 01, 03, etc. 1252 | CLIENT_ADDRESS_V4=$(echo ${CLIENT_ADDRESS_V4} | sed -r 's/^0*([0-9]+)\.0*([0-9]+)\.0*([0-9]+)\.0*([0-9]+)$/\1.\2.\3.\4/') 1253 | CLIENT_ADDRESS_V6=$(echo ${CLIENT_ADDRESS_V6} | sed -r 's/^0*([0-9]+)\.0*([0-9]+)\.0*([0-9]+)\.0*([0-9]+)$/\1.\2.\3.\4/') 1254 | fi 1255 | # Check if THE_REMOVE_DATE 1256 | if [[ "${THE_REMOVE_DATE}" == "" ]]; then 1257 | 1258 | TODO="${NEW_CLIENT_NAME} start" 1259 | 1260 | else 1261 | 1262 | TODO="${NEW_CLIENT_NAME} start ${THE_REMOVE_DATE}" 1263 | 1264 | fi 1265 | echo "# ${TODO} 1266 | [Peer] 1267 | PublicKey = ${CLIENT_PUBKEY} 1268 | PresharedKey = ${PRESHARED_KEY} 1269 | AllowedIPs = ${CLIENT_ADDRESS_V4}/32,${CLIENT_ADDRESS_V6}/128 1270 | # ${NEW_CLIENT_NAME} end" >${WIREGUARD_ADD_PEER_CONFIG} 1271 | wg addconf ${WIREGUARD_PUB_NIC} ${WIREGUARD_ADD_PEER_CONFIG} 1272 | cat ${WIREGUARD_ADD_PEER_CONFIG} >>${WIREGUARD_CONFIG} 1273 | rm -f ${WIREGUARD_ADD_PEER_CONFIG} 1274 | echo "# ${WIREGUARD_WEBSITE_URL} 1275 | [Interface] 1276 | Address = ${CLIENT_ADDRESS_V4}/${PRIVATE_SUBNET_MASK_V4},${CLIENT_ADDRESS_V6}/${PRIVATE_SUBNET_MASK_V6} 1277 | DNS = ${CLIENT_DNS} 1278 | ListenPort = ${PEER_PORT} 1279 | MTU = ${MTU_CHOICE} 1280 | PrivateKey = ${CLIENT_PRIVKEY} 1281 | [Peer] 1282 | AllowedIPs = ${CLIENT_ALLOWED_IP} 1283 | Endpoint = ${SERVER_HOST}${SERVER_PORT} 1284 | PersistentKeepalive = ${NAT_CHOICE} 1285 | PresharedKey = ${PRESHARED_KEY} 1286 | PublicKey = ${SERVER_PUBKEY}" >>${WIREGUARD_CLIENT_PATH}/"${NEW_CLIENT_NAME}"-${WIREGUARD_PUB_NIC}.conf 1287 | # If the ssh configurationf diectory doesn't exist, then create it. 1288 | if [[ ! -d "/home/${NEW_CLIENT_NAME}/sshwireguardConf/" ]]; then 1289 | mkdir "/home/${NEW_CLIENT_NAME}/sshwireguardConf/" 1290 | fi 1291 | # Copy the VPN configuration to user's home directory sshwireguardConf/ folder 1292 | cp ${WIREGUARD_CLIENT_PATH}/"${NEW_CLIENT_NAME}"-${WIREGUARD_PUB_NIC}.conf /home/${NEW_CLIENT_NAME}/sshwireguardConf/sshwireguard.conf 1293 | echo " 1294 | ############################################ 1295 | # These settings are generated server-side # 1296 | # and should not be manually changed. # 1297 | # ########################################## 1298 | # Username to authenticate. 1299 | username: duane 1300 | # Remote SSH server. 1301 | hostname: ${SERVER_HOST} 1302 | # SSH port number 1303 | hostport: 22" > /home/${NEW_CLIENT_NAME}/sshwireguardConf/config.yaml 1304 | wg addconf ${WIREGUARD_PUB_NIC} <(wg-quick strip ${WIREGUARD_PUB_NIC}) 1305 | #qrencode -t ansiutf8 -r ${WIREGUARD_CLIENT_PATH}/"${NEW_CLIENT_NAME}"-${WIREGUARD_PUB_NIC}.conf 1306 | echo "Client config --> ${WIREGUARD_CLIENT_PATH}/${NEW_CLIENT_NAME}-${WIREGUARD_PUB_NIC}.conf" 1307 | ;; 1308 | 6) # Remove WireGuard Peer 1309 | echo "Which WireGuard peer would you like to remove?" 1310 | grep start ${WIREGUARD_CONFIG} | awk '{print $2}' 1311 | if [[ "${CLIENT_USER}" == "" ]]; then 1312 | read -rp "Peer's name:" REMOVECLIENT 1313 | else 1314 | REMOVECLIENT=${CLIENT_USER} 1315 | fi 1316 | if [[ "$(egrep "start [0-9]{12}" ${WIREGUARD_CONFIG})" != "" ]]; then 1317 | 1318 | CLIENTKEY=$(sed -n "/\# ${REMOVECLIENT} start [0-9]\{12\}/,/\# ${REMOVECLIENT} end/p" ${WIREGUARD_CONFIG} | grep PublicKey | awk '{print $3}') 1319 | else 1320 | CLIENTKEY=$(sed -n "/\# ${REMOVECLIENT} start/,/\# ${REMOVECLIENT} end/p" ${WIREGUARD_CONFIG} | grep PublicKey | awk '{print $3}') 1321 | fi 1322 | wg set ${WIREGUARD_PUB_NIC} peer "${CLIENTKEY}" remove 1323 | sed -i "/\# ${REMOVECLIENT} start/,/\# ${REMOVECLIENT} end/d" ${WIREGUARD_CONFIG} 1324 | if [ -f "${WIREGUARD_CLIENT_PATH}/${REMOVECLIENT}-${WIREGUARD_PUB_NIC}.conf" ]; then 1325 | rm -f ${WIREGUARD_CLIENT_PATH}/"${REMOVECLIENT}"-${WIREGUARD_PUB_NIC}.conf 1326 | fi 1327 | wg addconf ${WIREGUARD_PUB_NIC} <(wg-quick strip ${WIREGUARD_PUB_NIC}) 1328 | crontab -l | grep -v "${REMOVECLIENT}" | crontab - 1329 | ;; 1330 | 7) # Reinstall WireGuard 1331 | if { [ "${CURRENT_DISTRO}" == "ubuntu" ] || [ "${CURRENT_DISTRO}" == "debian" ] || [ "${CURRENT_DISTRO}" == "raspbian" ] || [ "${CURRENT_DISTRO}" == "pop" ] || [ "${CURRENT_DISTRO}" == "kali" ] || [ "${CURRENT_DISTRO}" == "linuxmint" ] || [ "${CURRENT_DISTRO}" == "neon" ]; }; then 1332 | dpkg-reconfigure wireguard-dkms 1333 | modprobe wireguard 1334 | systemctl reenable wg-quick@${WIREGUARD_PUB_NIC} 1335 | systemctl restart wg-quick@${WIREGUARD_PUB_NIC} 1336 | elif { [ "${CURRENT_DISTRO}" == "fedora" ] || [ "${CURRENT_DISTRO}" == "centos" ] || [ "${CURRENT_DISTRO}" == "rhel" ] || [ "${CURRENT_DISTRO}" == "almalinux" ] || [ "${CURRENT_DISTRO}" == "rocky" ]; }; then 1337 | yum reinstall wireguard-tools -y 1338 | service wg-quick@${WIREGUARD_PUB_NIC} restart 1339 | elif { [ "${CURRENT_DISTRO}" == "arch" ] || [ "${CURRENT_DISTRO}" == "archarm" ] || [ "${CURRENT_DISTRO}" == "manjaro" ]; }; then 1340 | pacman -S --noconfirm wireguard-tools 1341 | systemctl reenable wg-quick@${WIREGUARD_PUB_NIC} 1342 | systemctl restart wg-quick@${WIREGUARD_PUB_NIC} 1343 | elif [ "${CURRENT_DISTRO}" == "alpine" ]; then 1344 | apk fix wireguard-tools 1345 | elif [ "${CURRENT_DISTRO}" == "freebsd" ]; then 1346 | pkg check wireguard 1347 | fi 1348 | ;; 1349 | 8) # Uninstall WireGuard and purging files 1350 | if pgrep systemd-journal; then 1351 | systemctl disable wg-quick@${WIREGUARD_PUB_NIC} 1352 | systemctl stop wg-quick@${WIREGUARD_PUB_NIC} 1353 | wg-quick down ${WIREGUARD_PUB_NIC} 1354 | else 1355 | service wg-quick@${WIREGUARD_PUB_NIC} disable 1356 | service wg-quick@${WIREGUARD_PUB_NIC} stop 1357 | wg-quick down ${WIREGUARD_PUB_NIC} 1358 | fi 1359 | # Removing Wireguard Files 1360 | if [ -d "${WIREGUARD_PATH}" ]; then 1361 | rm -rf ${WIREGUARD_PATH} 1362 | fi 1363 | if [ -d "${WIREGUARD_CLIENT_PATH}" ]; then 1364 | rm -rf ${WIREGUARD_CLIENT_PATH} 1365 | fi 1366 | if [ -f "${WIREGUARD_CONFIG}" ]; then 1367 | rm -f ${WIREGUARD_CONFIG} 1368 | fi 1369 | if [ -f "${WIREGUARD_IP_FORWARDING_CONFIG}" ]; then 1370 | rm -f ${WIREGUARD_IP_FORWARDING_CONFIG} 1371 | fi 1372 | if { [ "${CURRENT_DISTRO}" == "centos" ] || [ "${CURRENT_DISTRO}" == "almalinux" ] || [ "${CURRENT_DISTRO}" == "rocky" ]; }; then 1373 | yum remove wireguard qrencode haveged -y 1374 | elif { [ "${CURRENT_DISTRO}" == "debian" ] || [ "${CURRENT_DISTRO}" == "kali" ]; }; then 1375 | apt-get remove --purge wireguard qrencode -y 1376 | if [ -f "/etc/apt/sources.list.d/backports.list" ]; then 1377 | rm -f /etc/apt/sources.list.d/backports.list 1378 | fi 1379 | elif { [ "${CURRENT_DISTRO}" == "pop" ] || [ "${CURRENT_DISTRO}" == "linuxmint" ] || [ "${CURRENT_DISTRO}" == "neon" ]; }; then 1380 | apt-get remove --purge wireguard qrencode haveged -y 1381 | elif [ "${CURRENT_DISTRO}" == "ubuntu" ]; then 1382 | apt-get remove --purge wireguard qrencode haveged -y 1383 | if pgrep systemd-journal; then 1384 | systemctl reenable systemd-resolved 1385 | systemctl restart systemd-resolved 1386 | else 1387 | service systemd-resolved enable 1388 | service systemd-resolved restart 1389 | fi 1390 | elif [ "${CURRENT_DISTRO}" == "raspbian" ]; then 1391 | apt-key del 04EE7237B7D453EC 1392 | apt-get remove --purge wireguard qrencode haveged -y 1393 | if [ -f "/etc/apt/sources.list.d/backports.list" ]; then 1394 | rm -f /etc/apt/sources.list.d/backports.list 1395 | fi 1396 | elif { [ "${CURRENT_DISTRO}" == "arch" ] || [ "${CURRENT_DISTRO}" == "archarm" ] || [ "${CURRENT_DISTRO}" == "manjaro" ]; }; then 1397 | pacman -Rs --noconfirm wireguard-tools qrencode haveged 1398 | elif [ "${CURRENT_DISTRO}" == "fedora" ]; then 1399 | dnf remove wireguard qrencode haveged -y 1400 | if [ -f "/etc/yum.repos.d/wireguard.repo" ]; then 1401 | rm -f /etc/yum.repos.d/wireguard.repo 1402 | fi 1403 | elif [ "${CURRENT_DISTRO}" == "rhel" ]; then 1404 | yum remove wireguard qrencode haveged -y 1405 | if [ -f "/etc/yum.repos.d/wireguard.repo" ]; then 1406 | rm -f /etc/yum.repos.d/wireguard.repo 1407 | fi 1408 | elif [ "${CURRENT_DISTRO}" == "alpine" ]; then 1409 | apk del wireguard-tools libqrencode haveged 1410 | elif [ "${CURRENT_DISTRO}" == "freebsd" ]; then 1411 | pkg delete wireguard libqrencode 1412 | fi 1413 | # Delete WireGuard backup 1414 | if [ -f "${WIREGUARD_CONFIG_BACKUP}" ]; then 1415 | rm -f ${WIREGUARD_CONFIG_BACKUP} 1416 | if [ -f "${WIREGUARD_BACKUP_PASSWORD_PATH}" ]; then 1417 | rm -f "${WIREGUARD_BACKUP_PASSWORD_PATH}" 1418 | fi 1419 | fi 1420 | # Uninstall unbound 1421 | if [ -x "$(command -v unbound)" ]; then 1422 | if pgrep systemd-journal; then 1423 | systemctl disable unbound 1424 | systemctl stop unbound 1425 | else 1426 | service unbound disable 1427 | service unbound stop 1428 | fi 1429 | if [ -f "${RESOLV_CONFIG_OLD}" ]; then 1430 | chattr -i ${RESOLV_CONFIG} 1431 | rm -f ${RESOLV_CONFIG} 1432 | mv ${RESOLV_CONFIG_OLD} ${RESOLV_CONFIG} 1433 | chattr +i ${RESOLV_CONFIG} 1434 | fi 1435 | if { [ "${CURRENT_DISTRO}" == "centos" ] || [ "${CURRENT_DISTRO}" == "rhel" ] || [ "${CURRENT_DISTRO}" == "almalinux" ] || [ "${CURRENT_DISTRO}" == "rocky" ]; }; then 1436 | yum remove unbound -y 1437 | elif { [ "${CURRENT_DISTRO}" == "debian" ] || [ "${CURRENT_DISTRO}" == "pop" ] || [ "${CURRENT_DISTRO}" == "ubuntu" ] || [ "${CURRENT_DISTRO}" == "raspbian" ] || [ "${CURRENT_DISTRO}" == "kali" ] || [ "${CURRENT_DISTRO}" == "linuxmint" ] || [ "${CURRENT_DISTRO}" == "neon" ]; }; then 1438 | apt-get remove --purge unbound -y 1439 | elif { [ "${CURRENT_DISTRO}" == "arch" ] || [ "${CURRENT_DISTRO}" == "archarm" ] || [ "${CURRENT_DISTRO}" == "manjaro" ]; }; then 1440 | pacman -Rs --noconfirm unbound 1441 | elif [ "${CURRENT_DISTRO}" == "fedora" ]; then 1442 | dnf remove unbound -y 1443 | elif [ "${CURRENT_DISTRO}" == "alpine" ]; then 1444 | apk del unbound 1445 | elif [ "${CURRENT_DISTRO}" == "freebsd" ]; then 1446 | pkg delete unbound 1447 | fi 1448 | if [ -d "${UNBOUND_ROOT}" ]; then 1449 | rm -rf ${UNBOUND_ROOT} 1450 | fi 1451 | if [ -f "${UNBOUND_ANCHOR}" ]; then 1452 | rm -f ${UNBOUND_ANCHOR} 1453 | fi 1454 | if [ -f "${UNBOUND_ROOT_HINTS}" ]; then 1455 | rm -f ${UNBOUND_ROOT_HINTS} 1456 | fi 1457 | if [ -f "${UNBOUND_CONFIG}" ]; then 1458 | rm -f ${UNBOUND_CONFIG} 1459 | fi 1460 | fi 1461 | # If any cronjobs are identified, they should be removed. 1462 | crontab -l | grep -v "${CURRENT_FILE_PATH}" | crontab - 1463 | ;; 1464 | 9) # Update the script 1465 | curl ${WIREGUARD_MANAGER_UPDATE} -o "${CURRENT_FILE_PATH}" 1466 | chmod +x "${CURRENT_FILE_PATH}" 1467 | # Update the unbound configs 1468 | if [ -x "$(command -v unbound)" ]; then 1469 | # Refresh the root hints 1470 | if [ -f "${UNBOUND_ROOT_HINTS}" ]; then 1471 | curl ${UNBOUND_ROOT_SERVER_CONFIG_URL} -o ${UNBOUND_ROOT_HINTS} 1472 | fi 1473 | # The block list should be updated. 1474 | if [ -f "${UNBOUND_CONFIG_HOST}" ]; then 1475 | curl "${UNBOUND_CONFIG_HOST_URL}" | awk '$1' | awk '{print "local-zone: \""$1"\" always_refuse"}' >${UNBOUND_CONFIG_HOST} 1476 | fi 1477 | # Once everything is completed, restart the service. 1478 | if pgrep systemd-journal; then 1479 | systemctl restart unbound 1480 | else 1481 | service unbound restart 1482 | fi 1483 | fi 1484 | ;; 1485 | 10) # Backup WireGuard Config 1486 | if [ -f "${WIREGUARD_CONFIG_BACKUP}" ]; then 1487 | rm -f ${WIREGUARD_CONFIG_BACKUP} 1488 | fi 1489 | if [ -d "${WIREGUARD_PATH}" ]; then 1490 | BACKUP_PASSWORD="$(openssl rand -hex 50)" 1491 | echo "${BACKUP_PASSWORD}" >"${WIREGUARD_BACKUP_PASSWORD_PATH}" 1492 | zip -P "${BACKUP_PASSWORD}" -rj ${WIREGUARD_CONFIG_BACKUP} ${WIREGUARD_CONFIG} 1493 | fi 1494 | ;; 1495 | 11) # Restore WireGuard Config 1496 | if [ -d "${WIREGUARD_PATH}" ]; then 1497 | rm -rf ${WIREGUARD_PATH} 1498 | fi 1499 | unzip ${WIREGUARD_CONFIG_BACKUP} -d ${WIREGUARD_PATH} 1500 | # Restart WireGuard 1501 | if pgrep systemd-journal; then 1502 | systemctl enable wg-quick@${WIREGUARD_PUB_NIC} 1503 | systemctl restart wg-quick@${WIREGUARD_PUB_NIC} 1504 | else 1505 | service wg-quick@${WIREGUARD_PUB_NIC} enable 1506 | service wg-quick@${WIREGUARD_PUB_NIC} restart 1507 | fi 1508 | ;; 1509 | 12) # Change the IP address of your wireguard interface. 1510 | OLD_SERVER_HOST=$(head -n1 ${WIREGUARD_CONFIG} | awk '{print $4}' | awk -F: '{print $1}') 1511 | NEW_SERVER_HOST="$(curl -4 --connect-timeout 5 --tlsv1.3 -s 'https://api.ipengine.dev' | jq -r '.network.ip')" 1512 | if [ -z "${NEW_SERVER_HOST}" ]; then 1513 | NEW_SERVER_HOST="$(curl -4 --connect-timeout 5 --tlsv1.3 -s 'https://checkip.amazonaws.com')" 1514 | fi 1515 | sed -i "1s/${OLD_SERVER_HOST}/${NEW_SERVER_HOST}/" ${WIREGUARD_CONFIG} 1516 | ;; 1517 | 13) # Change the wireguard interface's port number. 1518 | OLD_SERVER_PORT=$(head -n1 ${WIREGUARD_CONFIG} | awk '{print $4}' | awk -F: '{print $2}') 1519 | until [[ "${NEW_SERVER_PORT}" =~ ^[0-9]+$ ]] && [ "${NEW_SERVER_PORT}" -ge 1 ] && [ "${NEW_SERVER_PORT}" -le 65535 ]; do 1520 | read -rp "Custom port [1-65535]: " -e -i 51820 NEW_SERVER_PORT 1521 | done 1522 | if [ "$(lsof -i UDP:"${NEW_SERVER_PORT}")" ]; then 1523 | echo "Error: The port ${NEW_SERVER_PORT} is already used by a different application, please use a different port." 1524 | fi 1525 | sed -i "s/${OLD_SERVER_PORT}/${NEW_SERVER_PORT}/g" ${WIREGUARD_CONFIG} 1526 | ;; 1527 | 14) # All wireguard peers should be removed from your interface 1528 | COMPLETE_CLIENT_LIST=$(grep start ${WIREGUARD_CONFIG} | awk '{print $2}') 1529 | for CLIENT_LIST_ARRAY in ${COMPLETE_CLIENT_LIST}; do 1530 | USER_LIST[${ADD_CONTENT}]=${CLIENT_LIST_ARRAY} 1531 | ADD_CONTENT=$(("${ADD_CONTENT}" + 1)) 1532 | done 1533 | for CLIENT_NAME in "${USER_LIST[@]}"; do 1534 | CLIENTKEY=$(sed -n "/\# ${CLIENT_NAME} start/,/\# ${CLIENT_NAME} end/p" ${WIREGUARD_CONFIG} | grep PublicKey | awk '{print $3}') 1535 | wg set ${WIREGUARD_PUB_NIC} peer "${CLIENTKEY}" remove 1536 | sed -i "/\# ${CLIENT_NAME} start/,/\# ${CLIENT_NAME} end/d" ${WIREGUARD_CONFIG} 1537 | if [ -f "${WIREGUARD_CLIENT_PATH}/${CLIENT_NAME}-${WIREGUARD_PUB_NIC}.conf" ]; then 1538 | rm -f ${WIREGUARD_CLIENT_PATH}/"${CLIENT_NAME}"-${WIREGUARD_PUB_NIC}.conf 1539 | fi 1540 | wg addconf ${WIREGUARD_PUB_NIC} <(wg-quick strip ${WIREGUARD_PUB_NIC}) 1541 | done 1542 | ;; 1543 | 15) # Generate QR code. 1544 | echo "Which WireGuard peer would you like to generate a QR code for?" 1545 | grep start ${WIREGUARD_CONFIG} | awk '{print $2}' 1546 | read -rp "Peer's name:" VIEW_CLIENT_INFO 1547 | if [ -f "${WIREGUARD_CLIENT_PATH}/${VIEW_CLIENT_INFO}-${WIREGUARD_PUB_NIC}.conf" ]; then 1548 | #qrencode -t ansiutf8 -r ${WIREGUARD_CLIENT_PATH}/"${VIEW_CLIENT_INFO}"-${WIREGUARD_PUB_NIC}.conf 1549 | echo "Peer's config --> ${WIREGUARD_CLIENT_PATH}/${VIEW_CLIENT_INFO}-${WIREGUARD_PUB_NIC}.conf" 1550 | fi 1551 | ;; 1552 | esac 1553 | } 1554 | 1555 | # Running Questions Command 1556 | wireguard-next-questions-interface 1557 | 1558 | fi 1559 | --------------------------------------------------------------------------------