├── .gitignore ├── LICENSE ├── README.md ├── gmod-addon └── luctus_netprotect │ └── lua │ └── autorun │ └── server │ └── sv_luctus_netprotect.lua ├── performance_dos.png ├── server_iptables ├── go.mod ├── go.sum └── main.go └── server_proxy ├── go.mod ├── go.sum └── main.go /.gitignore: -------------------------------------------------------------------------------- 1 | # If you prefer the allow list template instead of the deny list, see community template: 2 | # https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore 3 | # 4 | # Binaries for programs and plugins 5 | *.exe 6 | *.exe~ 7 | *.dll 8 | *.so 9 | *.dylib 10 | 11 | # Test binary, built with `go test -c` 12 | *.test 13 | 14 | # Output of the go coverage tool, specifically when used with LiteIDE 15 | *.out 16 | 17 | # Dependency directories (remove the comment below to include it) 18 | # vendor/ 19 | 20 | # Go workspace file 21 | go.work 22 | 23 | # Compiled binaries 24 | netprotect 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gmod-netprotect 2 | 3 | DDoS protection for Garry's Mod server 4 | 5 | 6 | # About 7 | 8 | This is an anti-ddos solution for garry's mod servers. 9 | It helps against udp network floods to the gameserver port. 10 | 11 | **Warning**: It only works on linux! If you have only a gameserver and not a root-/v-server then this won't work for you! 12 | 13 | It was tested on Debian 11 and Debian 12 with a standard 32bit gmod server installed with LinuxGSM. 14 | 15 | # Versions 16 | 17 | There now exists 2 versions for netprotect: Iptables and Proxy. 18 | The garrysmod addon is the same for both versions. 19 | 20 | The `server_iptables` version has no proxy between player and gameserver. It only has firewall rules (=iptables) which protect the server. This has an advantage of being very "low cost" as the firewall does all the filtering. 21 | 22 | The `server_proxy` version has a proxy server between the player and gameserver. Every packet goes through the proxy, which means you can also easily add more rules to it. This has the advantage of being monitor-able via prometheus/grafana while also being very robust. 23 | 24 | If you can't decide between versions then use the iptables version as it has been tested more thoroughly. 25 | 26 | # Installation 27 | 28 | You need to have golang installed. 29 | 30 | It is important that your gameserver starts with the 31 | 32 | -allowlocalhttp 33 | 34 | start-parameter because without it the gmodserver can't send network messages to the locally running api webserver. 35 | 36 | Move the gmod addon folder (named `luctus_netprotect`) into your gmodserver's addons folder and restart the gameserver. 37 | 38 | 39 | ## Iptables version 40 | Basic installation steps are, as root user: 41 | 42 | ```bash 43 | # Become root if not already: 44 | sudo -i 45 | 46 | git clone https://github.com/OverlordAkise/gmod-netprotect.git 47 | cd gmod-netprotect/server_iptables 48 | go get . 49 | go build . 50 | ./netprotect & 51 | ``` 52 | 53 | To change the gameport you can use `-gameport 12345`, for more details see `./netprotect -h`. 54 | 55 | 56 | ## Proxy version 57 | Basic installation steps are, as root user: 58 | 59 | ```bash 60 | # Become root if not already: 61 | sudo -i 62 | 63 | git clone https://github.com/OverlordAkise/gmod-netprotect.git 64 | cd gmod-netprotect/server_proxy 65 | go get . 66 | go build . 67 | ./netprotect & 68 | ``` 69 | 70 | You can use `./netprotect -help` to view all the options to change tickrate, delay, ports and more. 71 | After the proxy is running you have to add the firewall rules so that the proxy is actually being used: 72 | 73 | ```bash 74 | iptables -I INPUT -p udp -m udp --dport 22000 -j ACCEPT 75 | iptables -t raw -A PREROUTING -i eth0 -p udp --dport 27015 -j MARK --set-mark 1 76 | iptables -t raw -A PREROUTING -m mark --mark 1 -j CT --zone-orig 1 77 | iptables -t nat -A PREROUTING -p udp -m mark --mark 1 -j REDIRECT --to-ports 22000 78 | ``` 79 | 80 | To disable the proxy you can use the same rules as above but replace `-A` with `-D`. You can enable or disable these redirect firewall rules whenever you want, the players will NOT notice any impact and you do NOT have to restart the gameserver. 81 | 82 | 83 | # Usage 84 | 85 | To enable the protection simply use the `!netprotect on` chat command or `netprotect on` console command. 86 | To turn the protection off use `off` in the command instead of "on". 87 | 88 | 89 | ## Results 90 | 91 | ![performance_dos.png](performance_dos.png) 92 | 93 | I sadly haven't been able to capture a distributed denial of service attack yet, but the above is a screenshot of netprotect working against a single attacker (=single ip). 94 | Because of the 33/s limit a single IP can not take down the gameserver anymore. 95 | In the above screenshot you can see how the bandwidth (from one ip only) stays the same, but the UDP errors drop immediately after activating luctus netprotect. 96 | 97 | 98 | ## Internal workings of the iptables version 99 | 100 | A Garry's Mod server has to listen on a port, by default this is 27015. 101 | This port only listens for udp and all gameplay-related traffic is only udp, even the initial connection to the gameserver. 102 | 103 | If a malicious actor wants to disrupt this gameserver all he has to do is spam udp network traffic onto this port. 104 | This way the UDP Buffer of linux will constantly be full and new udp traffic will be discarded, which creates an unplayable lag for everyone playing. 105 | (Simply increasing the UDP buffer won't help much if the incoming traffic is around 400Mbit/s) 106 | 107 | Luctus' netprotect is a small api webserver which adds firewall rules that limit the receiving network traffic for the gameserver. 108 | 109 | By default (if you exclude the multiplayer browser) you would only want to receive network traffic from the players currently on the server. 110 | This means that there should not be a lot of udp traffic to port 27015 from non-players. 111 | 112 | Luctus' netprotect does this by dynamically inserting firewall rules that limit anyone not currently playing on the server. 113 | By default it only lets enough packets through for a player to be able to connect (~4 udp packets). 114 | If the server sees these first packets as an "connection attempt" then it will whitelist that IP to play on the server via a lua addon. 115 | If the first 5 packets from an IP are not a connection attempt it will be rate limited by the above 5/min rule, because the gameserver hasn't allowed the higher rate. 116 | 117 | By default it also limits the packets per second to ~35/s to match the gameserver tickrate. This makes it impossible for a single player with a lot of upload bandwidth to attack the server while he is playing on it. 118 | 119 | The activation of the protection has to be done from within the gmodserver because the players already on the server have to be added to the whitelist immediately, and only the gmodserver knows these players and their IP. 120 | 121 | The rules look something like this if the protection is active: 122 | 123 | ```bash 124 | $ iptables -S 125 | #[...] 126 | -A INPUT -p udp -m udp --dport 27015 -j GMOD 127 | #[...] 128 | -A GMOD -p udp -m udp --sport 1900 -j DROP 129 | -A GMOD -p udp -m udp --sport 123 -j DROP 130 | -A GMOD -p udp -m udp --sport 53 -j DROP 131 | -A GMOD -p udp -m udp --dport 27015 -m hashlimit --hashlimit-above 35/sec --hashlimit-burst 5 --hashlimit-mode srcip --hashlimit-name mainmain -j DROP 132 | -A GMOD -s 1.2.3.4/32 -p udp -m udp --dport 27015 -j ACCEPT 133 | -A GMOD -p udp -m udp --dport 27015 -m hashlimit --hashlimit-above 1/min --hashlimit-burst 5 --hashlimit-mode srcip --hashlimit-name main -j DROP 134 | #[...] 135 | ``` 136 | 137 | The first rule redirects requests coming to port 27015 to our new chain which protects our gmodserver. 138 | The first 3 rules in the GMOD chain disable a commonly used "performance testing" service which can be used to DDoS servers. They only send traffic from these 3 ports (1900/123/53) and we disable that for our gmod port. I have seen this happen live before and simply disabling these 3 source ports made the attack not work anymore. 139 | The next one is the 35/second limit that no player should exceed on a 33 ticks per second server. 140 | Between the 2 hashlimit rules are the whitelist rules for players. Here, Index 5 in the iptables chain, will all the players reside. 141 | Last but not least is the anti-ddos rule itself. If you are not a player and not already exceeding 40packets/second then you will be blocked by this 1 per minute rule. 142 | 143 | I highly recommend to not let this protection be always on. 144 | Multiplayer queries arrive at port 27015 via udp too and I am unsure, even after a lot of testing, if these protecting firewall rules break anything. 145 | 146 | 147 | ## Internal workings of the proxy version 148 | 149 | This is a single core application but it still is able to forward 1000 packets per second easily. 150 | 151 | The average delay of forwarding (seen by prometheus) is ~100µs for 1-3 packets per second, ~80µs for 66 packets per second and when benchmarking it ~20µs for 10.000 packets per second. 152 | 153 | The go proxyserver rebuilds the packets of the clients from scratch and resends it to the actual gameserver via loopback interface. The rebuilding uses google's "gopacket" module and works very quickly. We need root privilege because we send custom network packages. We need to set the source IP to the clients IP so that the gameserver doesn't send the response through the proxy but instead directly to the client/player. 154 | 155 | The proxy triples the network bandwidth used for the gameserver, which is visible in grafana. It receives the data, forwards it to the gameserver and the gameserver receives the data. This means you have eth0-in -> lo0-out -> lo0-in instead of a single eth0-in. This should not be a problem thanks to the 3packets/min/ip filter of the proxy. (I also tested it without this filter and my 4core linux server was able to easily handle 500Mbit/s \* 3 for minutes on end) 156 | -------------------------------------------------------------------------------- /gmod-addon/luctus_netprotect/lua/autorun/server/sv_luctus_netprotect.lua: -------------------------------------------------------------------------------- 1 | --Luctus Netprotect 2 | --Made by OverlordAkise 3 | 4 | --Target for netprotect http calls 5 | LUCTUS_NETPROTECT_HOST = "http://localhost:3531/" 6 | 7 | 8 | -- CONFIG END 9 | 10 | LUCTUS_NETPROTECT_ACTIVE = LUCTUS_NETPROTECT_ACTIVE or false 11 | 12 | LUCTUS_NETPROTECT_CONNECT_CACHE = LUCTUS_NETPROTECT_CONNECT_CACHE or {} 13 | 14 | hook.Add("PlayerConnect","luctus_netprotect",function(name, ip) 15 | if not LUCTUS_NETPROTECT_ACTIVE then return end 16 | print("[luctus_netprotect] PlayerConnect",name,ip) 17 | ip = string.Split(ip,":")[1] 18 | LUCTUS_NETPROTECT_CONNECT_CACHE[name] = ip 19 | local ret = HTTP({ 20 | failed = function(failMessage) print("[luctus_netprotect] Error during IP adding of player! (/add)") ErrorNoHaltWithStack(failMessage) end, 21 | success = function(httpcode,body,headers) print("[luctus_netprotect] IPs of joining player added!",httpcode,body) end, 22 | method = "POST", 23 | url = LUCTUS_NETPROTECT_HOST.."add", 24 | body = util.TableToJSON({["ip"]=ip}), 25 | type = "application/json; charset=utf-8", 26 | timeout = 3 27 | }) 28 | if not ret then 29 | ErrorNoHaltWithStack("ERROR: Couldn't make http request to luctus netprotect /add!") 30 | end 31 | end) 32 | 33 | gameevent.Listen("player_disconnect") 34 | hook.Add("player_disconnect","luctus_netprotect",function(data) 35 | local name = data.name 36 | local steamid = data.networkid 37 | local id = data.userid 38 | local bot = data.bot 39 | local reason = data.reason 40 | 41 | local ip = LUCTUS_NETPROTECT_CONNECT_CACHE[name] 42 | if ip then 43 | print("[luctus_netprotect] player_disconnect",name,steamid,ip) 44 | LuctusNetprotectRemoveIP(ip) 45 | end 46 | end) 47 | 48 | hook.Add("PlayerInitialSpawn","luctus_netprotect",function(ply) 49 | ply.netprotectSpawned = true 50 | if ply.SteamName then 51 | LUCTUS_NETPROTECT_CONNECT_CACHE[ply:SteamName()] = nil 52 | end 53 | LUCTUS_NETPROTECT_CONNECT_CACHE[ply:Nick()] = nil 54 | end) 55 | 56 | hook.Add("PlayerDisconnected","luctus_netprotect",function(ply) 57 | if not LUCTUS_NETPROTECT_ACTIVE then return end 58 | local steamid = ply:SteamID() 59 | local name = ply:Nick() 60 | local ip = string.Split(ply:IPAddress(),":")[1] 61 | print("[luctus_netprotect] PlayerDisconnected",ply,steamid,name,ip) 62 | LuctusNetprotectRemoveIP(ip) 63 | end) 64 | 65 | function LuctusNetprotectRemoveIP(ip) 66 | local ret = HTTP({ 67 | failed = function(failMessage) print("[luctus_netprotect] Error during IP delete of player! (/del)") ErrorNoHaltWithStack(failMessage) end, 68 | success = function(httpcode,body,headers) print("[luctus_netprotect] IPs of leaving player removed!",httpcode,body) end, 69 | method = "POST", 70 | url = LUCTUS_NETPROTECT_HOST.."del", 71 | body = util.TableToJSON({["ip"]=ip}), 72 | type = "application/json; charset=utf-8", 73 | timeout = 3 74 | }) 75 | if not ret then 76 | ErrorNoHaltWithStack("ERROR: Couldn't make http request to luctus netprotect /del!") 77 | end 78 | end 79 | 80 | function LuctusNetprotectActivate() 81 | if LUCTUS_NETPROTECT_ACTIVE then 82 | print("ERROR: Luctus Netprotect already active!") 83 | return 84 | end 85 | LUCTUS_NETPROTECT_ACTIVE = true 86 | LuctusNetProtectEnable(LuctusNetProtectAddLivePlayers) 87 | end 88 | 89 | function LuctusNetProtectAddLivePlayers() 90 | local ips = {} 91 | for k,ply in ipairs(player.GetHumans()) do 92 | table.insert(ips,string.Split(ply:IPAddress(),":")[1]) 93 | end 94 | 95 | local ret = HTTP({ 96 | failed = function(failMessage) print("[luctus_netprotect] Error during IP adding of current players! (/addmany)") ErrorNoHaltWithStack(failMessage) end, 97 | success = function(httpcode,body,headers) print("[luctus_netprotect] IPs of current players added!",httpcode,body) end, 98 | method = "POST", 99 | url = LUCTUS_NETPROTECT_HOST.."addmany", 100 | body = util.TableToJSON({["ips"]=ips}), 101 | type = "application/json; charset=utf-8", 102 | timeout = 3 103 | }) 104 | if not ret then 105 | ErrorNoHaltWithStack("ERROR: Couldn't make http request to luctus netprotect /addmany!") 106 | end 107 | end 108 | 109 | function LuctusNetProtectEnable(callback) 110 | local ret = HTTP({ 111 | failed = function(failMessage) print("[luctus_netprotect] Error during start! (/start)") ErrorNoHaltWithStack(failMessage) end, 112 | success = function(httpcode,body,headers) 113 | print("[luctus_netprotect] activated!",httpcode,body) 114 | if callback and isfunction(callback) then 115 | callback() 116 | end 117 | end, 118 | method = "POST", 119 | url = LUCTUS_NETPROTECT_HOST.."start", 120 | body = "", 121 | type = "application/json; charset=utf-8", 122 | timeout = 3 123 | }) 124 | if not ret then 125 | ErrorNoHaltWithStack("ERROR: Couldn't make http request to luctus netprotect /start!") 126 | end 127 | hook.Run("LuctusNetprotectActivated") 128 | end 129 | 130 | function LuctusNetprotectDisable() 131 | if not LUCTUS_NETPROTECT_ACTIVE then 132 | print("WARN: Luctus Netprotect is not active, but disable called!") 133 | end 134 | LUCTUS_NETPROTECT_ACTIVE = false 135 | local ret = HTTP({ 136 | failed = function(failMessage) print("[luctus_netprotect] Error during stop! (/stop)") ErrorNoHaltWithStack(failMessage) end, 137 | success = function(httpcode,body,headers) print("[luctus_netprotect] deactivated!",httpcode,body) end, 138 | method = "POST", 139 | url = LUCTUS_NETPROTECT_HOST.."stop", 140 | body = "", 141 | type = "application/json; charset=utf-8", 142 | timeout = 3 143 | }) 144 | if not ret then 145 | ErrorNoHaltWithStack("ERROR: Couldn't make http request to luctus netprotect /stop!") 146 | end 147 | hook.Run("LuctusNetprotectDeactivated") 148 | end 149 | 150 | hook.Add("PlayerInitialSpawn","luctus_netprotect_reset",function() 151 | print("[luctus_netprotect] Setting to off after server restart") 152 | local ret = HTTP({ 153 | failed = function(failMessage) print("[luctus_netprotect] Error during stop! (/stop)") ErrorNoHaltWithStack(failMessage) end, 154 | success = function(httpcode,body,headers) print("[luctus_netprotect] cleaned up!",httpcode,body) end, 155 | method = "POST", 156 | url = LUCTUS_NETPROTECT_HOST.."stop", 157 | body = "", 158 | type = "application/json; charset=utf-8", 159 | timeout = 3 160 | }) 161 | if not ret then 162 | ErrorNoHaltWithStack("ERROR: Couldn't make http request to luctus netprotect /stop!") 163 | end 164 | hook.Remove("PlayerInitialSpawn","luctus_netprotect_reset") 165 | end) 166 | 167 | concommand.Add("netprotect", function(ply, cmd, args, argstr) 168 | if IsValid(ply) then return end 169 | if cmd ~= "netprotect" then return end 170 | if argstr == "on" then 171 | LuctusNetprotectActivate() 172 | elseif argstr == "off" then 173 | LuctusNetprotectDisable() 174 | else 175 | print("Usage: 'netprotect on' or 'netprotect off'") 176 | end 177 | end) 178 | 179 | hook.Add("PlayerSay","luctus_netprotect",function(ply,text) 180 | if not ply:IsAdmin() then return end 181 | if text == "!netprotect on" then 182 | LuctusNetprotectActivate() 183 | end 184 | if text == "!netprotect off" then 185 | LuctusNetprotectDisable() 186 | end 187 | end) 188 | 189 | timer.Create("luctus_netprotect_auto",3,0,function() 190 | local plyCount = 0 191 | local packetLoss = 0 192 | local tickRate = math.Round(1 / engine.TickInterval()) 193 | for k,ply in ipairs(player.GetHumans()) do 194 | if not IsValid(ply) then return end 195 | if not ply.netprotectSpawned then return end 196 | packetLoss = packetLoss + ply:PacketLoss() 197 | plyCount = plyCount + 1 198 | end 199 | if (plyCount*tickRate)/2 < packetLoss and not LUCTUS_NETPROTECT_ACTIVE and plyCount > 4 then 200 | LuctusNetprotectActivate() 201 | print("[luctus_netprotect] ATTENTION: packet loss detected, netprotect enabled!") 202 | print(Format("Packetloss: %d/%d (ply:%d,tickrate:%d)",packetLoss,plyCount*tickRate,plyCount,tickRate)) 203 | end 204 | end) 205 | 206 | print("[luctus_netprotect] sv loaded") 207 | -------------------------------------------------------------------------------- /performance_dos.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OverlordAkise/gmod-netprotect/f8dcb969cbd0755c5308bed5a7b3ae31b0d5d2ce/performance_dos.png -------------------------------------------------------------------------------- /server_iptables/go.mod: -------------------------------------------------------------------------------- 1 | module luctus.at/netprotect 2 | 3 | go 1.22.4 4 | 5 | require ( 6 | github.com/coreos/go-iptables v0.8.0 7 | github.com/gin-gonic/gin v1.10.0 8 | ) 9 | 10 | require ( 11 | github.com/bytedance/sonic v1.11.6 // indirect 12 | github.com/bytedance/sonic/loader v0.1.1 // indirect 13 | github.com/cloudwego/base64x v0.1.4 // indirect 14 | github.com/cloudwego/iasm v0.2.0 // indirect 15 | github.com/gabriel-vasile/mimetype v1.4.3 // indirect 16 | github.com/gin-contrib/sse v0.1.0 // indirect 17 | github.com/go-playground/locales v0.14.1 // indirect 18 | github.com/go-playground/universal-translator v0.18.1 // indirect 19 | github.com/go-playground/validator/v10 v10.20.0 // indirect 20 | github.com/goccy/go-json v0.10.2 // indirect 21 | github.com/json-iterator/go v1.1.12 // indirect 22 | github.com/klauspost/cpuid/v2 v2.2.7 // indirect 23 | github.com/leodido/go-urn v1.4.0 // indirect 24 | github.com/mattn/go-isatty v0.0.20 // indirect 25 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect 26 | github.com/modern-go/reflect2 v1.0.2 // indirect 27 | github.com/pelletier/go-toml/v2 v2.2.2 // indirect 28 | github.com/twitchyliquid64/golang-asm v0.15.1 // indirect 29 | github.com/ugorji/go/codec v1.2.12 // indirect 30 | golang.org/x/arch v0.8.0 // indirect 31 | golang.org/x/crypto v0.23.0 // indirect 32 | golang.org/x/net v0.25.0 // indirect 33 | golang.org/x/sys v0.20.0 // indirect 34 | golang.org/x/text v0.15.0 // indirect 35 | google.golang.org/protobuf v1.34.1 // indirect 36 | gopkg.in/yaml.v3 v3.0.1 // indirect 37 | ) 38 | -------------------------------------------------------------------------------- /server_iptables/go.sum: -------------------------------------------------------------------------------- 1 | github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0= 2 | github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= 3 | github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= 4 | github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= 5 | github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= 6 | github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= 7 | github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= 8 | github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= 9 | github.com/coreos/go-iptables v0.8.0 h1:MPc2P89IhuVpLI7ETL/2tx3XZ61VeICZjYqDEgNsPRc= 10 | github.com/coreos/go-iptables v0.8.0/go.mod h1:Qe8Bv2Xik5FyTXwgIbLAnv2sWSBmvWdFETJConOQ//Q= 11 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 12 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 13 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 14 | github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= 15 | github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= 16 | github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= 17 | github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= 18 | github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= 19 | github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= 20 | github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= 21 | github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= 22 | github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= 23 | github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= 24 | github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= 25 | github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= 26 | github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8= 27 | github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= 28 | github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= 29 | github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= 30 | github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= 31 | github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 32 | github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= 33 | github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= 34 | github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= 35 | github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= 36 | github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= 37 | github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= 38 | github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= 39 | github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= 40 | github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= 41 | github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= 42 | github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= 43 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 44 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= 45 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 46 | github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= 47 | github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= 48 | github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= 49 | github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= 50 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 51 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 52 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 53 | github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= 54 | github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= 55 | github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= 56 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 57 | github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 58 | github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 59 | github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= 60 | github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= 61 | github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= 62 | github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= 63 | github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= 64 | github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= 65 | github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= 66 | github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= 67 | github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= 68 | golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= 69 | golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc= 70 | golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= 71 | golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= 72 | golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= 73 | golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= 74 | golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= 75 | golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 76 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 77 | golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= 78 | golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 79 | golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= 80 | golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= 81 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= 82 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 83 | google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= 84 | google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= 85 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 86 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 87 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 88 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 89 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 90 | nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= 91 | rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= 92 | -------------------------------------------------------------------------------- /server_iptables/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | // "fmt" 5 | "flag" 6 | "github.com/gin-gonic/gin" 7 | "log/slog" 8 | "os" 9 | "strconv" 10 | "time" 11 | //firewall 12 | "github.com/coreos/go-iptables/iptables" 13 | ) 14 | 15 | type HIP struct { 16 | IP string `json:"ip" xml:"ip" binding:"required,ip"` 17 | } 18 | 19 | type HIPs struct { 20 | IPs []string `json:"ips" xml:"ips" binding:"required,dive,ip"` 21 | } 22 | 23 | func main() { 24 | starttime := time.Now() 25 | 26 | var gameport int 27 | flag.IntVar(&gameport, "gameport", 27015, "The port where the gameserver listens, aka. the port you want to protect") 28 | var apiport int 29 | flag.IntVar(&apiport, "apiport", 3531, "The port of the http web API") 30 | var listenip string 31 | flag.StringVar(&listenip, "listenip", "127.0.0.1", "On which IP should the http web API listen on") 32 | var maxrate int 33 | flag.IntVar(&maxrate, "maxrate", 34, "How many packets are allowed per player per second, best is tickrate+1") 34 | var logpath string 35 | flag.StringVar(&logpath, "logpath", "stdout", "Where the logs should be written to, default: stdout") 36 | var preroute bool 37 | flag.BoolVar(&preroute, "preroute", false, "Use raw/PREROUTING chain instead of filter/INPUT, use this when using pterodactyl") 38 | flag.Parse() 39 | 40 | table := "filter" 41 | mainChain := "INPUT" 42 | if preroute { 43 | table = "raw" 44 | mainChain = "PREROUTING" 45 | } 46 | 47 | targetport := strconv.Itoa(gameport) 48 | ginport := strconv.Itoa(apiport) 49 | 50 | //Logger 51 | var logger *slog.Logger 52 | if logpath == "stdout" { 53 | logger = slog.New(slog.NewTextHandler(os.Stdout, nil)) 54 | } else { 55 | f, err := os.OpenFile(logpath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) 56 | if err != nil { 57 | panic(err) 58 | } 59 | defer f.Close() 60 | logger = slog.New(slog.NewTextHandler(f, nil)) 61 | } 62 | 63 | //Webserver 64 | gin.SetMode(gin.ReleaseMode) 65 | app := gin.New() 66 | app.Use(func(c *gin.Context) { 67 | start := time.Now() 68 | 69 | defer func() { 70 | if r := recover(); r != nil { 71 | c.String(500, "ERROR") 72 | logger.Error("webreq", "status", c.Writer.Status(), "method", c.Request.Method, "path", c.Request.URL.Path, "ip", c.ClientIP(), "duration", time.Since(start), "err", r) 73 | } 74 | }() 75 | 76 | c.Next() 77 | 78 | logger.Info("webreq", "status", c.Writer.Status(), "method", c.Request.Method, "path", c.Request.URL.Path, "ip", c.ClientIP(), "duration", time.Since(start)) 79 | }) 80 | 81 | app.GET("/", func(c *gin.Context) { 82 | c.String(200, "ok") 83 | }) 84 | 85 | app.POST("/add", func(c *gin.Context) { 86 | var hip HIP 87 | if err := c.ShouldBind(&hip); err != nil { 88 | panic(err) 89 | } 90 | ipt, err := iptables.New() 91 | if err != nil { 92 | panic(err) 93 | } 94 | logger.Info("adding ip", "ip", hip.IP) 95 | err = ipt.InsertUnique(table, "GMOD", 5, "-s", hip.IP+"/32", "-p", "udp", "-m", "udp", "--dport", targetport, "-j", "ACCEPT") 96 | if err != nil { 97 | panic(err) 98 | } 99 | c.String(200, "ok") 100 | }) 101 | 102 | app.POST("/addmany", func(c *gin.Context) { 103 | var hips HIPs 104 | if err := c.ShouldBind(&hips); err != nil { 105 | panic(err) 106 | } 107 | ipt, err := iptables.New() 108 | if err != nil { 109 | panic(err) 110 | } 111 | for _, ip := range hips.IPs { 112 | logger.Info("adding ip", "ip", ip) 113 | err = ipt.InsertUnique(table, "GMOD", 5, "-s", ip+"/32", "-p", "udp", "-m", "udp", "--dport", targetport, "-j", "ACCEPT") 114 | if err != nil { 115 | panic(err) 116 | } 117 | } 118 | c.String(200, "ok") 119 | }) 120 | 121 | app.POST("/del", func(c *gin.Context) { 122 | var hip HIP 123 | if err := c.ShouldBind(&hip); err != nil { 124 | panic(err) 125 | } 126 | ipt, err := iptables.New() 127 | if err != nil { 128 | panic(err) 129 | } 130 | logger.Info("removing ip", "ip", hip.IP) 131 | err = ipt.Delete(table, "GMOD", "-s", hip.IP+"/32", "-p", "udp", "-m", "udp", "--dport", targetport, "-j", "ACCEPT") 132 | if err != nil { 133 | panic(err) 134 | } 135 | c.String(200, "ok") 136 | }) 137 | 138 | app.POST("/start", func(c *gin.Context) { 139 | ipt, err := iptables.New() 140 | if err != nil { 141 | panic(err) 142 | } 143 | err = ipt.InsertUnique(table, "GMOD", 1, "-p", "udp", "--dport", targetport, "-m", "hashlimit", "--hashlimit-name", "mainmain", "--hashlimit-above", strconv.Itoa(maxrate)+"/sec", "--hashlimit-mode", "srcip", "-j", "DROP") 144 | if err != nil { 145 | panic(err) 146 | } 147 | err = ipt.AppendUnique(table, "GMOD", "-p", "udp", "-m", "udp", "--dport", targetport, "-m", "hashlimit", "--hashlimit-above", "1/min", "--hashlimit-burst", "5", "--hashlimit-mode", "srcip", "--hashlimit-name", "main", "-j", "DROP") 148 | if err != nil { 149 | panic(err) 150 | } 151 | var srcPortsBlocked = []int{53, 123, 1900} 152 | for _, port := range srcPortsBlocked { 153 | err = ipt.InsertUnique(table, "GMOD", 1, "-p", "udp", "-m", "udp", "--sport", strconv.Itoa(port), "-j", "DROP") 154 | if err != nil { 155 | panic(err) 156 | } 157 | } 158 | 159 | c.String(200, "ok") 160 | }) 161 | 162 | app.POST("/stop", func(c *gin.Context) { 163 | ipt, err := iptables.New() 164 | if err != nil { 165 | panic(err) 166 | } 167 | err = ipt.ClearChain(table, "GMOD") 168 | if err != nil { 169 | panic(err) 170 | } 171 | c.String(200, "ok") 172 | }) 173 | 174 | initipt, err := iptables.New() 175 | if err != nil { 176 | logger.Error("are you running as root?") 177 | panic(err) 178 | } 179 | 180 | //ClearChain clears a chain AND creates it if it doesnt exist yet 181 | err = initipt.ClearChain(table, "GMOD") 182 | if err != nil { 183 | panic(err) 184 | } 185 | 186 | err = initipt.InsertUnique(table, mainChain, 1, "-m", "udp", "-p", "udp", "--dport", targetport, "-j", "GMOD") 187 | if err != nil { 188 | logger.Error("error inserting main jump rule", "err", err) 189 | } else { 190 | logger.Info("added main jump rule successfully") 191 | } 192 | 193 | logger.Info("Luctus Netprotect started", "startuptime", time.Since(starttime), "listenip", listenip, "listenport", ginport) 194 | err = app.Run(listenip + ":" + ginport) 195 | if err != nil { 196 | panic(err) 197 | } 198 | } 199 | -------------------------------------------------------------------------------- /server_proxy/go.mod: -------------------------------------------------------------------------------- 1 | module luctus.at/netprotect 2 | 3 | go 1.22.2 4 | 5 | require ( 6 | github.com/gin-contrib/zap v1.1.1 7 | github.com/gin-gonic/gin v1.9.1 8 | github.com/google/gopacket v1.1.19 9 | github.com/prometheus/client_golang v1.19.0 10 | go.uber.org/zap v1.27.0 11 | ) 12 | 13 | require ( 14 | github.com/beorn7/perks v1.0.1 // indirect 15 | github.com/bytedance/sonic v1.11.3 // indirect 16 | github.com/cespare/xxhash/v2 v2.2.0 // indirect 17 | github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect 18 | github.com/chenzhuoyu/iasm v0.9.1 // indirect 19 | github.com/gabriel-vasile/mimetype v1.4.3 // indirect 20 | github.com/gin-contrib/sse v0.1.0 // indirect 21 | github.com/go-playground/locales v0.14.1 // indirect 22 | github.com/go-playground/universal-translator v0.18.1 // indirect 23 | github.com/go-playground/validator/v10 v10.19.0 // indirect 24 | github.com/goccy/go-json v0.10.2 // indirect 25 | github.com/json-iterator/go v1.1.12 // indirect 26 | github.com/klauspost/cpuid/v2 v2.2.7 // indirect 27 | github.com/leodido/go-urn v1.4.0 // indirect 28 | github.com/mattn/go-isatty v0.0.20 // indirect 29 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect 30 | github.com/modern-go/reflect2 v1.0.2 // indirect 31 | github.com/pelletier/go-toml/v2 v2.2.0 // indirect 32 | github.com/prometheus/client_model v0.5.0 // indirect 33 | github.com/prometheus/common v0.48.0 // indirect 34 | github.com/prometheus/procfs v0.12.0 // indirect 35 | github.com/twitchyliquid64/golang-asm v0.15.1 // indirect 36 | github.com/ugorji/go/codec v1.2.12 // indirect 37 | go.uber.org/multierr v1.11.0 // indirect 38 | golang.org/x/arch v0.7.0 // indirect 39 | golang.org/x/crypto v0.21.0 // indirect 40 | golang.org/x/net v0.22.0 // indirect 41 | golang.org/x/sys v0.18.0 // indirect 42 | golang.org/x/text v0.14.0 // indirect 43 | google.golang.org/protobuf v1.33.0 // indirect 44 | gopkg.in/yaml.v3 v3.0.1 // indirect 45 | ) 46 | -------------------------------------------------------------------------------- /server_proxy/go.sum: -------------------------------------------------------------------------------- 1 | github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= 2 | github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= 3 | github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= 4 | github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= 5 | github.com/bytedance/sonic v1.11.3 h1:jRN+yEjakWh8aK5FzrciUHG8OFXK+4/KrAX/ysEtHAA= 6 | github.com/bytedance/sonic v1.11.3/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4= 7 | github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= 8 | github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= 9 | github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= 10 | github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= 11 | github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0= 12 | github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA= 13 | github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= 14 | github.com/chenzhuoyu/iasm v0.9.1 h1:tUHQJXo3NhBqw6s33wkGn9SP3bvrWLdlVIJ3hQBL7P0= 15 | github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= 16 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 17 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 18 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 19 | github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= 20 | github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= 21 | github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= 22 | github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= 23 | github.com/gin-contrib/zap v1.1.1 h1:DDyIF9YQorl3gZzAabIowRywHJuohDfiLnhwvWKl6SY= 24 | github.com/gin-contrib/zap v1.1.1/go.mod h1:YW8KOko2kYLy8g6k9YgVNTj7SIcrUEzYiAd9IjiBPs0= 25 | github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= 26 | github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= 27 | github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= 28 | github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= 29 | github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= 30 | github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= 31 | github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= 32 | github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= 33 | github.com/go-playground/validator/v10 v10.19.0 h1:ol+5Fu+cSq9JD7SoSqe04GMI92cbn0+wvQ3bZ8b/AU4= 34 | github.com/go-playground/validator/v10 v10.19.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= 35 | github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= 36 | github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= 37 | github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= 38 | github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= 39 | github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= 40 | github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8= 41 | github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo= 42 | github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= 43 | github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= 44 | github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= 45 | github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= 46 | github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= 47 | github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= 48 | github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= 49 | github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= 50 | github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= 51 | github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= 52 | github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= 53 | github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= 54 | github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= 55 | github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= 56 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 57 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= 58 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 59 | github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= 60 | github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= 61 | github.com/pelletier/go-toml/v2 v2.2.0 h1:QLgLl2yMN7N+ruc31VynXs1vhMZa7CeHHejIeBAsoHo= 62 | github.com/pelletier/go-toml/v2 v2.2.0/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= 63 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 64 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 65 | github.com/prometheus/client_golang v1.19.0 h1:ygXvpU1AoN1MhdzckN+PyD9QJOSD4x7kmXYlnfbA6JU= 66 | github.com/prometheus/client_golang v1.19.0/go.mod h1:ZRM9uEAypZakd+q/x7+gmsvXdURP+DABIEIjnmDdp+k= 67 | github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw= 68 | github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI= 69 | github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE= 70 | github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc= 71 | github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo= 72 | github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= 73 | github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= 74 | github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= 75 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 76 | github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= 77 | github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= 78 | github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= 79 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 80 | github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 81 | github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 82 | github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= 83 | github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= 84 | github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= 85 | github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= 86 | github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= 87 | github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= 88 | github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= 89 | github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= 90 | github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= 91 | go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= 92 | go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= 93 | go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= 94 | go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= 95 | go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= 96 | go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= 97 | golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= 98 | golang.org/x/arch v0.7.0 h1:pskyeJh/3AmoQ8CPE95vxHLqp1G1GfGNXTmcl9NEKTc= 99 | golang.org/x/arch v0.7.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= 100 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 101 | golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= 102 | golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= 103 | golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= 104 | golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= 105 | golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= 106 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 107 | golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 108 | golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc= 109 | golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= 110 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 111 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 112 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 113 | golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 114 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 115 | golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= 116 | golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 117 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 118 | golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= 119 | golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= 120 | golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= 121 | golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 122 | google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= 123 | google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= 124 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 125 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= 126 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= 127 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 128 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 129 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 130 | nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= 131 | rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= 132 | -------------------------------------------------------------------------------- /server_proxy/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net" 6 | "os" 7 | "syscall" 8 | 9 | "github.com/google/gopacket" 10 | "github.com/google/gopacket/layers" 11 | //"os/signal" 12 | "flag" 13 | "github.com/gin-gonic/gin" 14 | "strconv" 15 | "time" 16 | //Logging 17 | ginzap "github.com/gin-contrib/zap" 18 | "go.uber.org/zap" 19 | //Metrics 20 | "github.com/prometheus/client_golang/prometheus" 21 | "github.com/prometheus/client_golang/prometheus/promhttp" 22 | //Connectionpool 23 | "sync" 24 | "sync/atomic" 25 | ) 26 | 27 | // Avg Time taken to forward a packet: 85.061µs 28 | 29 | type HIP struct { 30 | IP string `json:"ip" xml:"ip" binding:"required,ip"` 31 | } 32 | 33 | type HIPs struct { 34 | IPs []string `json:"ips" xml:"ips" binding:"required,dive,ip"` 35 | } 36 | 37 | func debugDo(adr *net.UDPAddr, size int, text string) { 38 | fmt.Println(time.Now().Format("15:04:05.00000000"), text, size, "\t", adr.String()) 39 | } 40 | 41 | func debugNoop(adr *net.UDPAddr, size int, text string) {} 42 | 43 | func createForwardSocket(ifName string) (net.PacketConn, error) { 44 | fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, syscall.IPPROTO_RAW) 45 | if err != nil { 46 | return nil, fmt.Errorf("Failed open socket(syscall.AF_INET, syscall.SOCK_RAW, syscall.IPPROTO_RAW): %s", err) 47 | } 48 | err = syscall.SetsockoptInt(fd, syscall.IPPROTO_IP, syscall.IP_HDRINCL, 1) 49 | if err != nil { 50 | return nil, err 51 | } 52 | if ifName != "" { 53 | _, err = net.InterfaceByName(ifName) 54 | if err != nil { 55 | return nil, fmt.Errorf("Failed to find interface: %s: %s", ifName, err) 56 | } 57 | err = syscall.SetsockoptString(fd, syscall.SOL_SOCKET, syscall.SO_BINDTODEVICE, ifName) 58 | if err != nil { 59 | return nil, err 60 | } 61 | } 62 | 63 | conn, err := net.FilePacketConn(os.NewFile(uintptr(fd), fmt.Sprintf("fd %d", fd))) 64 | if err != nil { 65 | return nil, err 66 | } 67 | return conn, nil 68 | } 69 | 70 | func buildUDPPacket(dst *net.UDPAddr, data []byte, src *net.UDPAddr) ([]byte, error) { 71 | buffer := gopacket.NewSerializeBuffer() 72 | payload := gopacket.Payload(data) 73 | ip := &layers.IPv4{ 74 | DstIP: dst.IP, 75 | SrcIP: src.IP, 76 | Version: 4, 77 | TTL: 64, 78 | Protocol: layers.IPProtocolUDP, 79 | } 80 | udp := &layers.UDP{ 81 | SrcPort: layers.UDPPort(src.Port), 82 | DstPort: layers.UDPPort(dst.Port), 83 | } 84 | if err := udp.SetNetworkLayerForChecksum(ip); err != nil { 85 | return nil, fmt.Errorf("Failed calc checksum: %s", err) 86 | } 87 | if err := gopacket.SerializeLayers(buffer, gopacket.SerializeOptions{ComputeChecksums: true, FixLengths: true}, ip, udp, payload); err != nil { 88 | return nil, fmt.Errorf("Failed serialize packet: %s", err) 89 | } 90 | return buffer.Bytes(), nil 91 | } 92 | 93 | func main() { 94 | var initAllowedPackets int 95 | var maxPerSecond int 96 | var initCooldown int 97 | var udpport int 98 | var tcpport int 99 | var target string 100 | var loglocation string 101 | var isDebug bool 102 | flag.IntVar(&maxPerSecond, "rate.max", 34, "Max packets per second, should be tickrate+1") 103 | flag.IntVar(&initAllowedPackets, "rate.init", 3, "How many packets to initially let through, should be 3 for gmod") 104 | flag.IntVar(&initCooldown, "rate.delay", 60, "How long to block/ban packets for, in seconds") 105 | flag.IntVar(&udpport, "port.udp", 22000, "Port for UDP proxy to listen on") 106 | flag.IntVar(&tcpport, "port.tcp", 3531, "Port for TCP webserver to listen on") 107 | flag.StringVar(&target, "target", "0", "Proxy-Target, format ':', example: 45.142.177.13:27015") 108 | flag.StringVar(&loglocation, "log", "./netprotect.log", "Location of logs from UDP and TCP servers, can use 'stdout'") 109 | flag.BoolVar(&isDebug, "debug", false, "Debug: Print packages and timestamps to stdout") 110 | flag.Parse() 111 | debugPrint := debugNoop 112 | if isDebug { 113 | debugPrint = debugDo 114 | } 115 | 116 | //Logger 117 | 118 | cfg := zap.NewProductionConfig() 119 | cfg.OutputPaths = []string{ 120 | loglocation, 121 | } 122 | flogger, err := cfg.Build() 123 | if err != nil { 124 | panic(err) 125 | } 126 | logger := flogger.Sugar() 127 | 128 | //Metrics 129 | 130 | requestCounter := prometheus.NewCounter( 131 | prometheus.CounterOpts{ 132 | Namespace: "netprotect", 133 | Name: "udp_requests_total", 134 | Help: "Number of forwarded udp messages", 135 | }, 136 | ) 137 | prometheus.MustRegister(requestCounter) 138 | 139 | sizeCounter := prometheus.NewCounter( 140 | prometheus.CounterOpts{ 141 | Namespace: "netprotect", 142 | Name: "udp_requests_size_bytes", 143 | Help: "Amount of bytes received and sent (per forward only counted once)", 144 | }, 145 | ) 146 | prometheus.MustRegister(sizeCounter) 147 | 148 | timeCounter := prometheus.NewCounter( 149 | prometheus.CounterOpts{ 150 | Namespace: "netprotect", 151 | Name: "udp_requests_seconds_sum", 152 | Help: "Time taken between 'after receiving packet' and 'sent packet out'", 153 | }, 154 | ) 155 | prometheus.MustRegister(timeCounter) 156 | 157 | //UDP proxy 158 | 159 | addr := net.UDPAddr{ 160 | Port: udpport, 161 | IP: net.ParseIP("0.0.0.0"), 162 | } 163 | svdst, err := net.ResolveUDPAddr("udp", target) 164 | if err != nil { 165 | panic(err) 166 | } 167 | 168 | sock, err := net.ListenUDP("udp", &addr) 169 | if err != nil { 170 | panic(err) 171 | } 172 | defer sock.Close() 173 | recvbuffer := make([]byte, 65000) 174 | var whitelist sync.Map 175 | var isWhitelistActive atomic.Bool 176 | isWhitelistActive.Store(false) 177 | 178 | conn, err := createForwardSocket("eth0") 179 | if err != nil { 180 | panic(err) 181 | } 182 | dstServer := &net.UDPAddr{ 183 | IP: net.ParseIP(target), 184 | Port: 27015, 185 | } 186 | dstServerIP := &net.IPAddr{IP: dstServer.IP} 187 | 188 | cooldownTime := time.Duration(initCooldown) * time.Second 189 | oneSecond := 1 * time.Second 190 | 191 | go func() { 192 | nextResetAll := time.Now() 193 | nextReset := make(map[string]time.Time) 194 | packetCount := make(map[string]int) 195 | 196 | fmt.Println("Netprotect UDP proxy listening on:", sock.LocalAddr(), "forwarding to:", target) 197 | for { 198 | n, udpaddr, err := sock.ReadFromUDP(recvbuffer) 199 | if err != nil { 200 | sock.Close() 201 | panic(err) 202 | } 203 | sCurTime := time.Now() 204 | debugPrint(udpaddr, n, strconv.FormatBool(isWhitelistActive.Load())) 205 | 206 | //whitelist, distributed dos prevention 207 | srcIP := udpaddr.IP.String() 208 | if _, ok := whitelist.Load(srcIP); !ok && isWhitelistActive.Load() { 209 | //clear packetCount every 60s 210 | if sCurTime.After(nextResetAll) { 211 | packetCount = make(map[string]int) 212 | nextResetAll = sCurTime.Add(cooldownTime) 213 | } 214 | //Do not continue if too many packets 215 | packetCount[srcIP] += 1 216 | if packetCount[srcIP] > initAllowedPackets { 217 | debugPrint(udpaddr, n, "^Above initAllowedPackets") 218 | continue 219 | } 220 | } 221 | 222 | //single dos prevention 223 | packetCount[srcIP] += 1 224 | rtime, ok := nextReset[srcIP] 225 | if !ok { 226 | nextReset[srcIP] = time.Now() 227 | } 228 | if ok && sCurTime.After(rtime) { 229 | packetCount[srcIP] = 0 230 | nextReset[srcIP] = sCurTime.Add(oneSecond) 231 | } 232 | if packetCount[srcIP] > maxPerSecond { 233 | debugPrint(udpaddr, n, "^Above maxPerSecond") 234 | continue 235 | } 236 | 237 | //Forward packet 238 | b, err := buildUDPPacket(svdst, recvbuffer[:n], udpaddr) 239 | if err != nil { 240 | panic(err) 241 | } 242 | _, err = conn.WriteTo(b, dstServerIP) 243 | if err != nil { 244 | panic(err) 245 | } 246 | requestCounter.Inc() 247 | sizeCounter.Add(float64(n)) 248 | debugPrint(svdst, n, "<-") 249 | timeCounter.Add(float64(time.Since(sCurTime))) 250 | } 251 | }() 252 | 253 | //TCP webserver 254 | 255 | gin.SetMode(gin.ReleaseMode) 256 | app := gin.New() 257 | app.Use(ginzap.RecoveryWithZap(flogger, true)) 258 | app.Use(ginzap.Ginzap(flogger, time.RFC3339, true)) 259 | 260 | app.GET("/", func(c *gin.Context) { 261 | c.String(200, "ok") 262 | }) 263 | 264 | app.GET("/metrics", gin.WrapH(promhttp.Handler())) 265 | 266 | app.POST("/add", func(c *gin.Context) { 267 | var hip HIP 268 | if err := c.ShouldBind(&hip); err != nil { 269 | panic(err) 270 | } 271 | whitelist.Store(hip.IP, true) 272 | c.String(200, "ok") 273 | }) 274 | 275 | app.POST("/addmany", func(c *gin.Context) { 276 | var hips HIPs 277 | if err := c.ShouldBind(&hips); err != nil { 278 | panic(err) 279 | } 280 | for _, ip := range hips.IPs { 281 | whitelist.Store(ip, true) 282 | } 283 | 284 | c.String(200, "ok") 285 | }) 286 | 287 | app.POST("/del", func(c *gin.Context) { 288 | var hip HIP 289 | if err := c.ShouldBind(&hip); err != nil { 290 | panic(err) 291 | } 292 | whitelist.Delete(hip.IP) 293 | c.String(200, "ok") 294 | }) 295 | 296 | app.POST("/start", func(c *gin.Context) { 297 | isWhitelistActive.Store(true) 298 | c.String(200, "ok") 299 | }) 300 | 301 | app.POST("/stop", func(c *gin.Context) { 302 | isWhitelistActive.Store(false) 303 | c.String(200, "ok") 304 | }) 305 | 306 | logger.Infow("Startup finished", "tcpport", tcpport, "udpport", udpport, "target", target, "loglocation", loglocation) 307 | fmt.Println("Netprotect TCP webserver listening on:", "0.0.0.0:"+strconv.Itoa(tcpport)) 308 | fmt.Println(app.Run("0.0.0.0:" + strconv.Itoa(tcpport))) 309 | } 310 | --------------------------------------------------------------------------------