├── .gitattributes
├── .github
└── workflows
│ ├── workflow-docker-manual.yml
│ └── workflow-docker-release.yml
├── .gitignore
├── .shellcheckrc
├── Dockerfile
├── LICENSE
├── README.md
├── build
└── root
│ └── install.sh
└── run
├── local
└── tools.sh
├── nobody
├── microsocks.sh
├── preruncheck.sh
└── privoxy.sh
└── root
├── iptable-init.sh
├── iptable.sh
├── openvpn.sh
├── openvpndown.sh
├── openvpnup.sh
├── prerunget.sh
├── start.sh
├── wireguard.sh
├── wireguarddown.sh
└── wireguardup.sh
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Define the default behavior where GitHub attempts to detect the file type and set the line endings correctly
2 | * text=auto
3 |
4 | # Define line endings to be unix (LF) for GitHub files
5 | .gitattributes text eol=lf
6 | .gitignore text eol=lf
7 | .gitconfig text eol=lf
8 | LICENSE text eol=lf
9 | *.md text eol=lf
10 |
11 | # Define line endings to be unix (LF) for source code
12 | *.php text eol=lf
13 | *.css text eol=lf
14 | *.sass text eol=lf
15 | *.scss text eol=lf
16 | *.less text eol=lf
17 | *.styl text eol=lf
18 | *.js text eol=lf
19 | *.coffee text eol=lf
20 | *.json text eol=lf
21 | *.htm text eol=lf
22 | *.html text eol=lf
23 | *.xml text eol=lf
24 | *.svg text eol=lf
25 | *.txt text eol=lf
26 | *.ini text eol=lf
27 | *.inc text eol=lf
28 | *.pl text eol=lf
29 | *.rb text eol=lf
30 | *.py text eol=lf
31 | *.scm text eol=lf
32 | *.sql text eol=lf
33 | *.sh text eol=lf
34 | *.bat text eol=lf
35 | *.txt text eol=lf
36 | *.tmpl text eol=lf
37 | Dockerfile text eol=lf
38 |
39 | # Define binary files to be excluded from modification
40 | *.png binary
41 | *.jpg binary
42 | *.jpeg binary
43 | *.gif binary
44 | *.ico binary
45 | *.flv binary
46 | *.fla binary
47 | *.swf binary
48 | *.gz binary
49 | *.tar binary
50 | *.rar binary
51 | *.zip binary
52 | *.7z binary
53 | *.egg binary
54 | *.ttf binary
55 | *.eot binary
56 | *.woff binary
57 | *.pyc binary
58 | *.pdf binary
59 | *.db binary
60 |
--------------------------------------------------------------------------------
/.github/workflows/workflow-docker-manual.yml:
--------------------------------------------------------------------------------
1 | name: workflow-docker-manual
2 |
3 | on:
4 | workflow_dispatch:
5 | inputs:
6 | tags:
7 | description: 'Enter tag name for test/dev image'
8 | default: 'test'
9 |
10 | jobs:
11 | call-reusable-workflow:
12 | uses: binhex/workflow-templates/.github/workflows/workflow-docker-manual.yml@main
13 | with:
14 | tags: ${{ github.event.inputs.tags }}
15 | platforms: linux/amd64,linux/arm64
16 | secrets:
17 | CR_PAT: ${{ secrets.CR_PAT }}
18 | DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }}
19 | DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
20 | DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
21 | EMAIL_ADDRESS: ${{ secrets.EMAIL_ADDRESS }}
22 | EMAIL_PASSWORD: ${{ secrets.EMAIL_PASSWORD }}
23 | GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }}
24 | GITLAB_USERNAME: ${{ secrets.GITLAB_USERNAME }}
25 | IMMORTALITY_PAT: ${{ secrets.IMMORTALITY_PAT }}
26 | QUAY_TOKEN: ${{ secrets.QUAY_TOKEN }}
27 | QUAY_USERNAME: ${{ secrets.QUAY_USERNAME }}
28 | TDB_PAT: ${{ secrets.TDB_PAT }}
29 |
--------------------------------------------------------------------------------
/.github/workflows/workflow-docker-release.yml:
--------------------------------------------------------------------------------
1 | name: workflow-docker-release
2 |
3 | on:
4 | push:
5 | tags:
6 | - '*'
7 |
8 | jobs:
9 | call-reusable-workflow:
10 | uses: binhex/workflow-templates/.github/workflows/workflow-docker-release.yml@main
11 | with:
12 | tags: ${{ github.event.inputs.tags }}
13 | platforms: linux/amd64,linux/arm64
14 | secrets:
15 | CR_PAT: ${{ secrets.CR_PAT }}
16 | DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }}
17 | DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
18 | DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
19 | EMAIL_ADDRESS: ${{ secrets.EMAIL_ADDRESS }}
20 | EMAIL_PASSWORD: ${{ secrets.EMAIL_PASSWORD }}
21 | GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }}
22 | GITLAB_USERNAME: ${{ secrets.GITLAB_USERNAME }}
23 | IMMORTALITY_PAT: ${{ secrets.IMMORTALITY_PAT }}
24 | QUAY_TOKEN: ${{ secrets.QUAY_TOKEN }}
25 | QUAY_USERNAME: ${{ secrets.QUAY_USERNAME }}
26 | TDB_PAT: ${{ secrets.TDB_PAT }}
27 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # ignore comments files
2 | todo.txt
3 | notes.txt
4 |
5 | # ignore mac generated files
6 | .DS_Store
7 |
8 | # ignore pycharm ide
9 | .idea
10 |
11 | # ignore compiled python scripts
12 | *.pyc
13 |
14 | # ignore virtual env
15 | venv
16 |
--------------------------------------------------------------------------------
/.shellcheckrc:
--------------------------------------------------------------------------------
1 | external-sources=true
2 |
--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM binhex/arch-base:latest
2 | LABEL org.opencontainers.image.authors="binhex"
3 | LABEL org.opencontainers.image.source="https://github.com/binhex/arch-int-vpn"
4 |
5 | # release tag name from buildx arg
6 | ARG RELEASETAG
7 |
8 | # arch from buildx --platform, e.g. amd64
9 | ARG TARGETARCH
10 |
11 | # additional files
12 | ##################
13 |
14 | # add install bash script
15 | ADD build/root/*.sh /root/
16 |
17 | # add bash script for root user
18 | ADD run/root/*.sh /root/
19 |
20 | # add bash script for nobody user
21 | ADD run/nobody/*.sh /home/nobody/
22 |
23 | # add bash script for local user
24 | ADD run/local/*.sh /usr/local/bin/
25 |
26 | # install app
27 | #############
28 |
29 | # make executable and run bash scripts to install app
30 | RUN chmod +x /root/*.sh /home/nobody/*.sh /usr/local/bin/*.sh && \
31 | /bin/bash /root/install.sh "${RELEASETAG}" "${TARGETARCH}"
32 |
--------------------------------------------------------------------------------
/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 | {one line to give the program's name and a brief idea of what it does.}
635 | Copyright (C) {year} {name of author}
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 | {project} Copyright (C) {year} {fullname}
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 | .
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Application
2 |
3 |
4 |
5 | [Privoxy](http://www.privoxy.org/)
6 | [OpenVPN](https://openvpn.net/)
7 | [WireGuard](https://www.wireguard.com/)
8 |
9 | ## Description
10 |
11 | Privoxy is a non-caching web proxy with filtering capabilities for enhancing privacy, manipulating cookies and modifying web page data and HTTP headers before the page is rendered by the browser. Privoxy is a "privacy enhancing proxy", filtering Web pages and removing advertisements.
12 |
13 | OpenVPN is an open-source software application that implements virtual private network (VPN) techniques for creating secure point-to-point or site-to-site connections in routed or bridged configurations and remote access facilities. It uses a custom security protocol that utilizes SSL/TLS for key exchange.
14 |
15 | WireGuard is an extremely simple yet fast and modern VPN that utilizes state-of-the-art cryptography. It aims to be faster, simpler, leaner, and more useful than IPsec, while avoiding the massive headache. It intends to be considerably more performant than OpenVPN. WireGuard is designed as a general purpose VPN for running on embedded interfaces and super computers alike, fit for many different circumstances. Initially released for the Linux kernel, it is now cross-platform (Windows, macOS, BSD, iOS, Android) and widely deployable. It is currently under heavy development, but already it might be regarded as the most secure, easiest to use, and simplest VPN solution in the industry.
16 |
17 | ## Build notes
18 |
19 | This is an intermediate Docker image which is used as a base image for other Docker images which require the OpenVPN client.
20 |
21 | ## Usage
22 |
23 | N/A, intermediate image used as a base for *VPN Docker Images.
24 |
25 | ## Access application
26 |
27 | N/A
28 |
29 | ## Example
30 |
31 | N/A, intermediate image used as a base for *VPN Docker Images.
32 |
33 | ## Notes
34 |
35 | N/A
36 | ___
37 | If you appreciate my work, then please consider buying me a beer :D
38 |
39 | [](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=MM5E27UX6AUU4)
40 |
41 | [Documentation](https://github.com/binhex/documentation) | [Support forum](http://forums.unraid.net/index.php?topic=45811.0)
--------------------------------------------------------------------------------
/build/root/install.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # exit script if return code != 0
4 | set -e
5 |
6 | # release tag name from buildx arg, stripped of build ver using string manipulation
7 | RELEASETAG="${1}"
8 |
9 | # target arch from buildx arg
10 | TARGETARCH="${2}"
11 |
12 | if [[ -z "${RELEASETAG}" ]]; then
13 | echo "[warn] Release tag name from build arg is empty, exiting script..."
14 | exit 1
15 | fi
16 |
17 | if [[ -z "${TARGETARCH}" ]]; then
18 | echo "[warn] Target architecture name from build arg is empty, exiting script..."
19 | exit 1
20 | fi
21 |
22 | # write RELEASETAG to file to record the release tag used to build the image
23 | echo "INT_RELEASE_TAG=${RELEASETAG}" >> '/etc/image-release'
24 |
25 | # build scripts
26 | ####
27 |
28 | # download build scripts from github
29 | curl --connect-timeout 5 --max-time 600 --retry 5 --retry-delay 0 --retry-max-time 60 -o /tmp/scripts-master.zip -L https://github.com/binhex/scripts/archive/master.zip
30 |
31 | # unzip build scripts
32 | unzip /tmp/scripts-master.zip -d /tmp
33 |
34 | # move shell scripts to /root
35 | mv /tmp/scripts-master/shell/arch/docker/*.sh /usr/local/bin/
36 |
37 | # pacman packages
38 | ####
39 |
40 | # define pacman packages
41 | pacman_packages="base-devel cargo openssl-1.1 kmod openvpn privoxy bind-tools ipcalc wireguard-tools openresolv libnatpmp ldns"
42 |
43 | # install pre-reqs
44 | pacman -S --needed $pacman_packages --noconfirm
45 |
46 | # github release - microsocks
47 | ####
48 |
49 | # download and compile microsocks
50 | github.sh --install-path '/tmp/compile' --github-owner 'rofl0r' --github-repo 'microsocks' --query-type 'release' --compile-src 'make install'
51 |
52 | # cargo (rust) install - boringtun-cli
53 | ####
54 |
55 | # install boringtun-cli using rust tool 'cargo'
56 | cargo install boringtun-cli
57 |
58 | # move and chmod compiled binary to /usr/local/bin
59 | mv /home/nobody/.cargo/bin/boringtun-cli /usr/local/bin/
60 | chmod +x /usr/local/bin/boringtun-cli
61 |
62 | # env vars
63 | ####
64 |
65 | cat <<'EOF' > /tmp/envvars_common_heredoc
66 |
67 | # check for presence of network interface docker0
68 | check_network=$(ifconfig | grep docker0 || true)
69 |
70 | # if network interface docker0 is present then we are running in host mode and thus must exit
71 | if [[ ! -z "${check_network}" ]]; then
72 | echo "[crit] Network type detected as 'Host', this will cause major issues, please stop the container and switch back to 'Bridge' mode" | ts '%Y-%m-%d %H:%M:%.S' && exit 1
73 | fi
74 |
75 | export VPN_ENABLED=$(echo "${VPN_ENABLED,,}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
76 | if [[ ! -z "${VPN_ENABLED}" ]]; then
77 | if [ "${VPN_ENABLED}" != "no" ]; then
78 | export VPN_ENABLED="yes"
79 | echo "[info] VPN_ENABLED defined as '${VPN_ENABLED}'" | ts '%Y-%m-%d %H:%M:%.S'
80 | else
81 | export VPN_ENABLED="no"
82 | echo "[info] VPN_ENABLED defined as '${VPN_ENABLED}'" | ts '%Y-%m-%d %H:%M:%.S'
83 | echo "[warn] !!IMPORTANT!! VPN IS SET TO DISABLED', YOU WILL NOT BE SECURE" | ts '%Y-%m-%d %H:%M:%.S'
84 | fi
85 | else
86 | echo "[warn] VPN_ENABLED not defined,(via -e VPN_ENABLED), defaulting to 'yes'" | ts '%Y-%m-%d %H:%M:%.S'
87 | export VPN_ENABLED="yes"
88 | fi
89 |
90 | if [[ "${VPN_ENABLED}" == "yes" ]]; then
91 |
92 | # listen for incoming connections on port 1234 from other containers, this is used to trigger
93 | # the restart of the containers sharing the network if the vpn container is restarted.
94 | nohup nc -l -s 127.0.0.1 -p 1234 -k &>> '/tmp/nc_listen.log' &
95 |
96 | # get values from env vars as defined by user
97 | export VPN_CLIENT=$(echo "${VPN_CLIENT}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
98 | if [[ ! -z "${VPN_CLIENT}" ]]; then
99 | echo "[info] VPN_CLIENT defined as '${VPN_CLIENT}'" | ts '%Y-%m-%d %H:%M:%.S'
100 | else
101 | echo "[warn] VPN_CLIENT not defined (via -e VPN_CLIENT), defaulting to 'openvpn'" | ts '%Y-%m-%d %H:%M:%.S'
102 | export VPN_CLIENT="openvpn"
103 | fi
104 |
105 | # get values from env vars as defined by user
106 | export VPN_PROV=$(echo "${VPN_PROV}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
107 | if [[ ! -z "${VPN_PROV}" ]]; then
108 | echo "[info] VPN_PROV defined as '${VPN_PROV}'" | ts '%Y-%m-%d %H:%M:%.S'
109 | else
110 | echo "[crit] VPN_PROV not defined,(via -e VPN_PROV), exiting..." | ts '%Y-%m-%d %H:%M:%.S' && exit 1
111 | fi
112 |
113 | if [[ "${VPN_CLIENT}" == "wireguard" ]]; then
114 |
115 | # create directory to store wireguard config files
116 | mkdir -p /config/wireguard
117 |
118 | # set perms and owner for files in /config/wireguard directory
119 | set +e
120 | chown -R "${PUID}":"${PGID}" "/config/wireguard" &> /dev/null
121 | exit_code_chown=$?
122 | chmod -R 775 "/config/wireguard" &> /dev/null
123 | exit_code_chmod=$?
124 | set -e
125 |
126 | if (( ${exit_code_chown} != 0 || ${exit_code_chmod} != 0 )); then
127 | echo "[warn] Unable to chown/chmod /config/wireguard/, assuming SMB mountpoint" | ts '%Y-%m-%d %H:%M:%.S'
128 | fi
129 |
130 | # force removal of mac os resource fork files in wireguard folder
131 | rm -rf /config/wireguard/._*.conf
132 |
133 | # wildcard search for wireguard config files (match on first result)
134 | vpn_config_path=$(find /config/wireguard -maxdepth 1 -name "*.conf" -print -quit)
135 |
136 | if [[ -z "${vpn_config_path}" ]]; then
137 |
138 | if [[ "${VPN_PROV}" == "pia" ]]; then
139 |
140 | # if conf file not found in /config/wireguard then set defaults, wireguard config
141 | # file for pia will be dynamically generated and visible on next startup
142 |
143 | echo "[info] VPN_CONFIG not defined (wireguard config doesnt file exists), defaulting to '/config/wireguard/wg0.conf'" | ts '%Y-%m-%d %H:%M:%.S'
144 | export VPN_CONFIG="/config/wireguard/wg0.conf"
145 |
146 | echo "[info] VPN_REMOTE_SERVER not defined (wireguard config doesnt file exists), defaulting to 'nl-amsterdam.privacy.network'" | ts '%Y-%m-%d %H:%M:%.S'
147 | export VPN_REMOTE_SERVER="nl-amsterdam.privacy.network"
148 |
149 | echo "[info] VPN_REMOTE_PORT not defined (wireguard config file doesnt exists), defaulting to '1337'" | ts '%Y-%m-%d %H:%M:%.S'
150 | export VPN_REMOTE_PORT="1337"
151 |
152 | else
153 |
154 | # if conf file not found in /config/wireguard and provider is not pia then exit
155 | echo "[crit] No WireGuard config file located in /config/wireguard/ (conf extension), please download from your VPN provider and then restart this container, exiting..." | ts '%Y-%m-%d %H:%M:%.S' && exit 1
156 |
157 | fi
158 |
159 | else
160 |
161 | # rename wireguard config file to prevent issues with spaces and other illegal characters for device
162 | export VPN_CONFIG="/config/wireguard/wg0.conf"
163 | if [[ $(basename "${vpn_config_path}") != wg0.conf ]]; then
164 | mv "${vpn_config_path}" "${VPN_CONFIG}"
165 | fi
166 | echo "[info] WireGuard config file (conf extension) is located at ${VPN_CONFIG}" | ts '%Y-%m-%d %H:%M:%.S'
167 |
168 | # convert CRLF (windows) to LF (unix) for wireguard conf file
169 | /usr/local/bin/dos2unix.sh "${VPN_CONFIG}"
170 |
171 | # get endpoint line from wireguard config file
172 | # note \K removes everything up to \K flag thus allowing variable lookbehind
173 | export VPN_REMOTE_SERVER=$(cat "${VPN_CONFIG}" | grep -P -o '^Endpoint(\s+)?=(\s+)?\K[^:]+' || true)
174 | if [[ -z "${VPN_REMOTE_SERVER}" ]]; then
175 | echo "[crit] VPN configuration file ${VPN_CONFIG} does not contain 'Endpoint' line, showing contents of file before exit..." | ts '%Y-%m-%d %H:%M:%.S'
176 | cat "${VPN_CONFIG}" && exit 1
177 | else
178 | echo "[info] VPN_REMOTE_SERVER defined as '${VPN_REMOTE_SERVER}'" | ts '%Y-%m-%d %H:%M:%.S'
179 | fi
180 |
181 | if [[ "${VPN_PROV}" == "pia" ]]; then
182 |
183 | export VPN_REMOTE_PORT=$(cat "${VPN_CONFIG}" | grep -P -o '^Endpoint.*:\K\d+' || true)
184 | if [[ -z "${VPN_REMOTE_PORT}" ]]; then
185 | echo "[warn] VPN configuration file ${VPN_CONFIG} does not contain port on 'Endpoint' line, defaulting to '1337'" | ts '%Y-%m-%d %H:%M:%.S'
186 | export VPN_REMOTE_PORT="1337"
187 | fi
188 |
189 | else
190 |
191 | export VPN_REMOTE_PORT=$(cat "${VPN_CONFIG}" | grep -P -o '^Endpoint.*:\K\d+' || true)
192 | if [[ -z "${VPN_REMOTE_PORT}" ]]; then
193 | echo "[crit] VPN configuration file ${VPN_CONFIG} does not contain port on 'Endpoint' line, showing contents of file before exit..." | ts '%Y-%m-%d %H:%M:%.S'
194 | cat "${VPN_CONFIG}" && exit 1
195 | fi
196 |
197 | fi
198 | echo "[info] VPN_REMOTE_PORT defined as '${VPN_REMOTE_PORT}'" | ts '%Y-%m-%d %H:%M:%.S'
199 |
200 | fi
201 |
202 | # device type (derived from the wireguard config filename without the file extesion) will always be wg0
203 | # as we forceably rename the file
204 | echo "[info] VPN_DEVICE_TYPE defined as 'wg0'" | ts '%Y-%m-%d %H:%M:%.S'
205 | export VPN_DEVICE_TYPE="wg0"
206 |
207 | # protocol for wireguard is always udp
208 | echo "[info] VPN_REMOTE_PROTOCOL defined as 'udp'" | ts '%Y-%m-%d %H:%M:%.S'
209 | export VPN_REMOTE_PROTOCOL="udp"
210 |
211 | export USERSPACE_WIREGUARD=$(echo "${USERSPACE_WIREGUARD}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
212 | if [[ ! -z "${USERSPACE_WIREGUARD}" ]]; then
213 | echo "[info] USERSPACE_WIREGUARD defined as '${USERSPACE_WIREGUARD}'" | ts '%Y-%m-%d %H:%M:%.S'
214 | else
215 | echo "[info] USERSPACE_WIREGUARD not defined (via -e USERSPACE_WIREGUARD), defaulting to 'no'" | ts '%Y-%m-%d %H:%M:%.S'
216 | export USERSPACE_WIREGUARD="no"
217 | fi
218 |
219 | elif [[ "${VPN_CLIENT}" == "openvpn" ]]; then
220 |
221 | # create directory to store openvpn config files
222 | mkdir -p /config/openvpn
223 |
224 | # set perms and owner for files in /config/openvpn directory
225 | set +e
226 | chown -R "${PUID}":"${PGID}" "/config/openvpn" &> /dev/null
227 | exit_code_chown=$?
228 | chmod -R 775 "/config/openvpn" &> /dev/null
229 | exit_code_chmod=$?
230 | set -e
231 |
232 | if (( ${exit_code_chown} != 0 || ${exit_code_chmod} != 0 )); then
233 | echo "[warn] Unable to chown/chmod /config/openvpn/, assuming SMB mountpoint" | ts '%Y-%m-%d %H:%M:%.S'
234 | fi
235 |
236 | # force removal of mac os resource fork files in ovpn folder
237 | rm -rf /config/openvpn/._*.ovpn
238 |
239 | # wildcard search for openvpn config files (match on first result)
240 | export VPN_CONFIG=$(find /config/openvpn -maxdepth 1 -name "*.ovpn" -print -quit)
241 |
242 | # if ovpn file not found in /config/openvpn then exit
243 | if [[ -z "${VPN_CONFIG}" ]]; then
244 | echo "[crit] No OpenVPN config file located in /config/openvpn/ (ovpn extension), please download from your VPN provider and then restart this container." | ts '%Y-%m-%d %H:%M:%.S'
245 | echo "[info] Performing directory listing for '/config/openvpn/' before exiting..." | ts '%Y-%m-%d %H:%M:%.S'
246 | ls -al '/config/openvpn' ; exit 1
247 | fi
248 |
249 | echo "[info] OpenVPN config file (ovpn extension) is located at ${VPN_CONFIG}" | ts '%Y-%m-%d %H:%M:%.S'
250 |
251 | # convert CRLF (windows) to LF (unix) for ovpn
252 | /usr/local/bin/dos2unix.sh "${VPN_CONFIG}"
253 |
254 | # get all remote lines in ovpn file and save comma separated
255 | vpn_remote_line=$(cat "${VPN_CONFIG}" | grep -P -o '(?<=^remote\s).*' | paste -s -d, - || true)
256 |
257 | if [[ -n "${vpn_remote_line}" ]]; then
258 |
259 | # if remote servers are legacy then log issue and exit
260 | if [[ "${vpn_remote_line}" == *"privateinternetaccess.com"* ]]; then
261 | echo "[crit] VPN configuration file '${VPN_CONFIG}' 'remote' line is referencing PIA legacy network which is now shutdown, see Q19. from the following link on how to switch to PIA 'next-gen':- https://github.com/binhex/documentation/blob/master/docker/faq/vpn.md exiting script..." | ts '%Y-%m-%d %H:%M:%.S'
262 | exit 1
263 | fi
264 |
265 | # split comma separated string into list from vpn_remote_line variable
266 | IFS=',' read -ra vpn_remote_line_list <<< "${vpn_remote_line}"
267 |
268 | # process each remote line from ovpn file
269 | for vpn_remote_line_item in "${vpn_remote_line_list[@]}"; do
270 |
271 | # if remote line contains comments then remove
272 | vpn_remote_line_item=$(echo "${vpn_remote_line_item}" | sed -r 's~\s?+#.*$~~g')
273 |
274 | vpn_remote_server_cut=$(echo "${vpn_remote_line_item}" | cut -d " " -f1 || true)
275 |
276 | if [[ -z "${vpn_remote_server_cut}" ]]; then
277 | echo "[warn] VPN configuration file ${VPN_CONFIG} remote line is missing or malformed, skipping to next remote line..." | ts '%Y-%m-%d %H:%M:%.S'
278 | continue
279 | fi
280 |
281 | vpn_remote_port_cut=$(cat "${VPN_CONFIG}" | grep -P -o '(?<=^port\s).*' || true)
282 | if [[ -z "${vpn_remote_port_cut}" ]]; then
283 | vpn_remote_port_cut=$(echo "${vpn_remote_line_item}" | cut -d " " -f2 | grep -P -o '^[\d]{2,5}$' || true)
284 | if [[ -z "${vpn_remote_port_cut}" ]]; then
285 | echo "[warn] VPN configuration file ${VPN_CONFIG} remote port is missing or malformed, assuming port '1194'" | ts '%Y-%m-%d %H:%M:%.S'
286 | vpn_remote_port_cut="1194"
287 | fi
288 | fi
289 |
290 | vpn_remote_protocol_cut=$(cat "${VPN_CONFIG}" | grep -P -o '(?<=^proto\s).*' || true)
291 | if [[ -z "${vpn_remote_protocol_cut}" ]]; then
292 | vpn_remote_protocol_cut=$(echo "${vpn_remote_line_item}" | cut -d " " -f3 || true)
293 | if [[ -z "${vpn_remote_protocol_cut}" ]]; then
294 | echo "[warn] VPN configuration file ${VPN_CONFIG} remote protocol is missing or malformed, assuming protocol 'udp'" | ts '%Y-%m-%d %H:%M:%.S'
295 | vpn_remote_protocol_cut="udp"
296 | fi
297 | fi
298 |
299 | if [[ "${vpn_remote_protocol_cut}" == "tcp" ]]; then
300 | # if remote line contains old format 'tcp' then replace with newer 'tcp-client' format
301 | vpn_remote_protocol_cut="tcp-client"
302 | fi
303 |
304 | vpn_remote_server+="${vpn_remote_server_cut},"
305 | vpn_remote_port+="${vpn_remote_port_cut},"
306 | vpn_remote_protocol+="${vpn_remote_protocol_cut},"
307 |
308 | done
309 |
310 | echo "[info] VPN remote server(s) defined as '${vpn_remote_server}'" | ts '%Y-%m-%d %H:%M:%.S'
311 | echo "[info] VPN remote port(s) defined as '${vpn_remote_port}'" | ts '%Y-%m-%d %H:%M:%.S'
312 | echo "[info] VPN remote protcol(s) defined as '${vpn_remote_protocol}'" | ts '%Y-%m-%d %H:%M:%.S'
313 |
314 | export VPN_REMOTE_SERVER="${vpn_remote_server}"
315 | export VPN_REMOTE_PORT="${vpn_remote_port}"
316 | export VPN_REMOTE_PROTOCOL="${vpn_remote_protocol}"
317 |
318 | else
319 |
320 | echo "[crit] VPN configuration file ${VPN_CONFIG} does not contain 'remote' line, showing contents of file before exit..." | ts '%Y-%m-%d %H:%M:%.S'
321 | cat "${VPN_CONFIG}" && exit 1
322 |
323 | fi
324 |
325 | VPN_DEVICE_TYPE=$(cat "${VPN_CONFIG}" | grep -P -o -m 1 '(?<=^dev\s)[^\r\n\d]+' | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
326 | if [[ ! -z "${VPN_DEVICE_TYPE}" ]]; then
327 | export VPN_DEVICE_TYPE="${VPN_DEVICE_TYPE}0"
328 | echo "[info] VPN_DEVICE_TYPE defined as '${VPN_DEVICE_TYPE}'" | ts '%Y-%m-%d %H:%M:%.S'
329 | else
330 | echo "[crit] VPN_DEVICE_TYPE not found in ${VPN_CONFIG}, exiting..." | ts '%Y-%m-%d %H:%M:%.S' && exit 1
331 | fi
332 |
333 | export VPN_OPTIONS=$(echo "${VPN_OPTIONS}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
334 | if [[ ! -z "${VPN_OPTIONS}" ]]; then
335 | echo "[info] VPN_OPTIONS defined as '${VPN_OPTIONS}'" | ts '%Y-%m-%d %H:%M:%.S'
336 | else
337 | echo "[info] VPN_OPTIONS not defined (via -e VPN_OPTIONS)" | ts '%Y-%m-%d %H:%M:%.S'
338 | export VPN_OPTIONS=""
339 | fi
340 |
341 | fi
342 |
343 | export NAME_SERVERS=$(echo "${NAME_SERVERS}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
344 | if [[ ! -z "${NAME_SERVERS}" ]]; then
345 | echo "[info] NAME_SERVERS defined as '${NAME_SERVERS}'" | ts '%Y-%m-%d %H:%M:%.S'
346 | else
347 | echo "[warn] NAME_SERVERS not defined (via -e NAME_SERVERS), defaulting to name servers defined in readme.md" | ts '%Y-%m-%d %H:%M:%.S'
348 | export NAME_SERVERS="1.1.1.1,1.0.0.1"
349 | fi
350 |
351 | # resolve vpn endpoints, drop all, allow vpn endpoints, if client pia then also allow pia api and pia website
352 | source /root/iptable-init.sh
353 |
354 | export LAN_NETWORK=$(echo "${LAN_NETWORK}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
355 | if [[ ! -z "${LAN_NETWORK}" ]]; then
356 | echo "[info] LAN_NETWORK defined as '${LAN_NETWORK}'" | ts '%Y-%m-%d %H:%M:%.S'
357 | faq_vpn_url="https://github.com/binhex/documentation/blob/master/docker/faq/vpn.md"
358 |
359 | # split comma separated string into list from LAN_NETWORK env variable
360 | IFS=',' read -ra lan_network_list <<< "${LAN_NETWORK}"
361 | for i in "${lan_network_list[@]}"; do
362 | if echo "${i}" | grep -q -P -m 1 '\/8$'; then
363 | if echo "${i}" | grep -q -v -P -m 1 '\.0\.0\.0\/8$'; then
364 | echo "[warn] Network '${i}' incorrectly defined, see Q4. ${faq_vpn_url}" | ts '%Y-%m-%d %H:%M:%.S'
365 | first_octet=$(echo "${i}" | grep -P -o -m 1 '^\d{1,3}')
366 | i="${first_octet}.0.0.0/8"
367 | echo "[info] Network corrected to '${i}'" | ts '%Y-%m-%d %H:%M:%.S'
368 | fi
369 | elif echo "${i}" | grep -q -P -m 1 '\/16$'; then
370 | if echo "${i}" | grep -q -v -P -m 1 '\.0\.0\/16$'; then
371 | echo "[warn] Network '${i}' incorrectly defined, see Q4. ${faq_vpn_url}" | ts '%Y-%m-%d %H:%M:%.S'
372 | first_second_octet=$(echo "${i}" | grep -P -o -m 1 '^\d{1,3}\.\d{1,3}')
373 | i="${first_second_octet}.0.0/16"
374 | echo "[info] Network corrected to '${i}'" | ts '%Y-%m-%d %H:%M:%.S'
375 | fi
376 | elif echo "${i}" | grep -q -P -m 1 '\/24$'; then
377 | if echo "${i}" | grep -q -v -P -m 1 '\.0\/24$'; then
378 | echo "[warn] Network '${i}' incorrectly defined, see Q4. ${faq_vpn_url}" | ts '%Y-%m-%d %H:%M:%.S'
379 | first_second_third_octet=$(echo "${i}" | grep -P -o -m 1 '^\d{1,3}\.\d{1,3}\.\d{1,3}')
380 | i="${first_second_third_octet}.0/24"
381 | echo "[info] Network corrected to '${i}'" | ts '%Y-%m-%d %H:%M:%.S'
382 | fi
383 | fi
384 |
385 | # strip out spaces
386 | i=$(echo "${i}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
387 |
388 | # append to variable with comma
389 | NETWORK+="${i},"
390 | done
391 |
392 | # strip out trailing comma
393 | export LAN_NETWORK=${NETWORK%?}
394 |
395 | echo "[info] LAN_NETWORK exported as '${LAN_NETWORK}'" | ts '%Y-%m-%d %H:%M:%.S'
396 | else
397 | echo "[crit] LAN_NETWORK not defined (via -e LAN_NETWORK), exiting..." | ts '%Y-%m-%d %H:%M:%.S' && exit 1
398 | fi
399 |
400 | if [[ "${VPN_PROV}" != "airvpn" ]]; then
401 | export VPN_USER=$(echo "${VPN_USER}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
402 | if [[ ! -z "${VPN_USER}" ]]; then
403 | echo "[info] VPN_USER defined as '${VPN_USER}'" | ts '%Y-%m-%d %H:%M:%.S'
404 | else
405 | echo "[warn] VPN_USER not defined (via -e VPN_USER), assuming authentication via other method" | ts '%Y-%m-%d %H:%M:%.S'
406 | fi
407 |
408 | export VPN_PASS=$(echo "${VPN_PASS}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
409 | if [[ ! -z "${VPN_PASS}" ]]; then
410 | echo "[info] VPN_PASS defined as '${VPN_PASS}'" | ts '%Y-%m-%d %H:%M:%.S'
411 | else
412 | echo "[warn] VPN_PASS not defined (via -e VPN_PASS), assuming authentication via other method" | ts '%Y-%m-%d %H:%M:%.S'
413 | fi
414 | fi
415 |
416 | if [[ "${VPN_PROV}" == "pia" ]]; then
417 |
418 | export STRICT_PORT_FORWARD=$(echo "${STRICT_PORT_FORWARD}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
419 | if [[ ! -z "${STRICT_PORT_FORWARD}" ]]; then
420 | echo "[info] STRICT_PORT_FORWARD defined as '${STRICT_PORT_FORWARD}'" | ts '%Y-%m-%d %H:%M:%.S'
421 | else
422 | echo "[warn] STRICT_PORT_FORWARD not defined (via -e STRICT_PORT_FORWARD), defaulting to 'yes'" | ts '%Y-%m-%d %H:%M:%.S'
423 | export STRICT_PORT_FORWARD="yes"
424 | fi
425 |
426 | fi
427 |
428 | export ADDITIONAL_PORTS=$(echo "${ADDITIONAL_PORTS}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
429 | export VPN_INPUT_PORTS=$(echo "${VPN_INPUT_PORTS}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
430 | if [[ ! -z "${ADDITIONAL_PORTS}" ]]; then
431 | echo "[warn] ADDITIONAL_PORTS DEPRECATED, please rename env var to 'VPN_INPUT_PORTS'" | ts '%Y-%m-%d %H:%M:%.S'
432 | echo "[info] ADDITIONAL_PORTS defined as '${ADDITIONAL_PORTS}'" | ts '%Y-%m-%d %H:%M:%.S'
433 | export VPN_INPUT_PORTS="${ADDITIONAL_PORTS}"
434 | elif [[ ! -z "${VPN_INPUT_PORTS}" ]]; then
435 | echo "[info] VPN_INPUT_PORTS defined as '${VPN_INPUT_PORTS}'" | ts '%Y-%m-%d %H:%M:%.S'
436 | else
437 | echo "[info] VPN_INPUT_PORTS not defined (via -e VPN_INPUT_PORTS), skipping allow for custom incoming ports" | ts '%Y-%m-%d %H:%M:%.S'
438 | fi
439 |
440 | export VPN_OUTPUT_PORTS=$(echo "${VPN_OUTPUT_PORTS}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
441 | if [[ ! -z "${VPN_OUTPUT_PORTS}" ]]; then
442 | echo "[info] VPN_OUTPUT_PORTS defined as '${VPN_OUTPUT_PORTS}'" | ts '%Y-%m-%d %H:%M:%.S'
443 | else
444 | echo "[info] VPN_OUTPUT_PORTS not defined (via -e VPN_OUTPUT_PORTS), skipping allow for custom outgoing ports" | ts '%Y-%m-%d %H:%M:%.S'
445 | fi
446 |
447 | export ENABLE_STARTUP_SCRIPTS=$(echo "${ENABLE_STARTUP_SCRIPTS}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
448 | if [[ ! -z "${ENABLE_STARTUP_SCRIPTS}" ]]; then
449 | echo "[info] ENABLE_STARTUP_SCRIPTS defined as '${ENABLE_STARTUP_SCRIPTS}'" | ts '%Y-%m-%d %H:%M:%.S'
450 | else
451 | echo "[info] ENABLE_STARTUP_SCRIPTS not defined (via -e ENABLE_STARTUP_SCRIPTS), defaulting to 'no'" | ts '%Y-%m-%d %H:%M:%.S'
452 | export ENABLE_STARTUP_SCRIPTS="no"
453 | fi
454 |
455 | fi
456 |
457 | export ENABLE_SOCKS=$(echo "${ENABLE_SOCKS}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
458 | if [[ ! -z "${ENABLE_SOCKS}" ]]; then
459 | echo "[info] ENABLE_SOCKS defined as '${ENABLE_SOCKS}'" | ts '%Y-%m-%d %H:%M:%.S'
460 | else
461 | echo "[warn] ENABLE_SOCKS not defined (via -e ENABLE_SOCKS), defaulting to 'no'" | ts '%Y-%m-%d %H:%M:%.S'
462 | export ENABLE_SOCKS="no"
463 | fi
464 |
465 | export ENABLE_PRIVOXY=$(echo "${ENABLE_PRIVOXY}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
466 | if [[ ! -z "${ENABLE_PRIVOXY}" ]]; then
467 | echo "[info] ENABLE_PRIVOXY defined as '${ENABLE_PRIVOXY}'" | ts '%Y-%m-%d %H:%M:%.S'
468 | else
469 | echo "[warn] ENABLE_PRIVOXY not defined (via -e ENABLE_PRIVOXY), defaulting to 'no'" | ts '%Y-%m-%d %H:%M:%.S'
470 | export ENABLE_PRIVOXY="no"
471 | fi
472 |
473 | if [[ "${ENABLE_SOCKS}" == "yes" ]]; then
474 |
475 | export SOCKS_USER=$(echo "${SOCKS_USER}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
476 | if [[ ! -z "${SOCKS_USER}" ]]; then
477 | echo "[info] SOCKS_USER defined as '${SOCKS_USER}'" | ts '%Y-%m-%d %H:%M:%.S'
478 | else
479 | echo "[warn] SOCKS_USER not defined (via -e SOCKS_USER), disabling authentication for microsocks" | ts '%Y-%m-%d %H:%M:%.S'
480 | export SOCKS_USER=""
481 | fi
482 |
483 | if [[ -n "${SOCKS_USER}" ]]; then
484 |
485 | export SOCKS_PASS=$(echo "${SOCKS_PASS}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
486 | if [[ ! -z "${SOCKS_PASS}" ]]; then
487 | echo "[info] SOCKS_PASS defined as '${SOCKS_PASS}'" | ts '%Y-%m-%d %H:%M:%.S'
488 | else
489 | echo "[warn] SOCKS_PASS not defined (via -e SOCKS_PASS), defaulting to 'socks'" | ts '%Y-%m-%d %H:%M:%.S'
490 | export SOCKS_PASS="socks"
491 | fi
492 |
493 | fi
494 | fi
495 |
496 | EOF
497 |
498 | # replace env vars common placeholder string with contents of file (here doc)
499 | sed -i '/# ENVVARS_COMMON_PLACEHOLDER/{
500 | s/# ENVVARS_COMMON_PLACEHOLDER//g
501 | r /tmp/envvars_common_heredoc
502 | }' /usr/local/bin/init.sh
503 | rm /tmp/envvars_common_heredoc
504 |
505 | cat <<'EOF' > /tmp/config_heredoc
506 |
507 | if [[ "${ENABLE_STARTUP_SCRIPTS}" == "yes" ]]; then
508 |
509 | # define path to scripts
510 | base_path="/config"
511 | user_script_path="${base_path}/scripts"
512 |
513 | mkdir -p "${user_script_path}"
514 |
515 | # find any scripts located in "${user_script_path}"
516 | user_scripts=$(find "${user_script_path}" -maxdepth 1 -name '*sh' 2> '/dev/null' | xargs)
517 |
518 | # loop over scripts, make executable and source
519 | for i in ${user_scripts}; do
520 | chmod +x "${i}"
521 | echo "[info] Executing user script '${i}' in the foreground..." | ts '%Y-%m-%d %H:%M:%.S'
522 | source "${i}" | ts '%Y-%m-%d %H:%M:%.S [script]'
523 | done
524 |
525 | # change ownership as we are running as root
526 | chown -R nobody:users "${user_script_path}"
527 |
528 | fi
529 |
530 | EOF
531 |
532 | # replace config placeholder string with contents of file (here doc)
533 | sed -i '/# CONFIG_PLACEHOLDER/{
534 | s/# CONFIG_PLACEHOLDER//g
535 | r /tmp/config_heredoc
536 | }' /usr/local/bin/init.sh
537 | rm /tmp/config_heredoc
538 |
539 | # cleanup
540 | cleanup.sh
541 |
--------------------------------------------------------------------------------
/run/local/tools.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # this function must be run as root as it overwrites /etc/hosts
4 | function round_robin_endpoint_ip() {
5 |
6 | local endpoint_name="${1}"
7 |
8 | local ip_address_count_array
9 | local endpoint_ip_array
10 | local current_ip
11 | local current_ip_index_number
12 | local next_ip
13 | local index_number
14 |
15 | # convert space separated ip's to array
16 | IFS=" " read -r -a endpoint_ip_array <<< "$2"
17 |
18 | # calculate number of ip's in the array
19 | # note need to -1 number as array index starts at 0
20 | ip_address_count_array=$((${#endpoint_ip_array[@]}-1))
21 |
22 | # get current ip address from /etc/hosts for this named endpoint
23 | current_ip=$(grep -P -o -m 1 ".*${endpoint_name}" < '/etc/hosts' | cut -f1)
24 |
25 | # get index number in array of current ip (if it exists, else -1)
26 | current_ip_index_number=-1
27 | for i in "${!endpoint_ip_array[@]}"; do
28 | if [[ "${endpoint_ip_array[$i]}" == "${current_ip}" ]]; then
29 | current_ip_index_number="${i}"
30 | break
31 | fi
32 | done
33 |
34 | # if current_ip_index_number is equal to number of ip's in the array or current ip
35 | # index number not found then get first ip in array (0), else get next ip in array
36 | if (( "${current_ip_index_number}" == "${ip_address_count_array}" || "${current_ip_index_number}" == -1 )); then
37 | next_ip=${endpoint_ip_array[0]}
38 | else
39 | index_number=$((current_ip_index_number+1))
40 | next_ip=${endpoint_ip_array[${index_number}]}
41 | fi
42 |
43 | # write ip address to /etc/hosts
44 | # note due to /etc/hosts being mounted we need to copy, edit, then overwrite
45 | cp -f '/etc/hosts' '/etc/hosts2'
46 | sed -i -e "s~.*${endpoint_name}~${next_ip} ${endpoint_name}~g" '/etc/hosts2'
47 | cp -f '/etc/hosts2' '/etc/hosts'
48 | rm -f '/etc/hosts2'
49 |
50 | }
51 |
52 | # this function works out what docker network interfaces we have available and returns a
53 | # dictionary including gateway ip, gateway adapter, ip of adapter, subnet mask and cidr
54 | # format of net mask
55 | function get_docker_networking() {
56 |
57 | local docker_mask
58 | local docker_interfaces
59 | local docker_interface
60 | local docker_ip
61 | local docker_network_cidr
62 | local default_gateway_adapter
63 | local default_gateway_ip
64 |
65 | # get space seperated list of docker adapters, excluding loopback and vpn adapter
66 | docker_interfaces=$(ip link show | grep -v 'state DOWN' | cut -d ' ' -f 2 | grep -P -o '^[^@:]+' | grep -P -v "^(lo|${VPN_DEVICE_TYPE})$" | xargs)
67 |
68 | if [[ -z "${docker_interfaces}" ]]; then
69 | echo "[warn] Unable to identify Docker network interfaces, exiting script..."
70 | exit 1
71 | fi
72 |
73 | DOCKER_NETWORKING=""
74 |
75 | for docker_interface in ${docker_interfaces}; do
76 |
77 | # identify adapter for local gateway
78 | default_gateway_adapter=$(ip route show default | awk '/default/ {print $5}')
79 |
80 | # identify ip for local gateway
81 | default_gateway_ip=$(ip route show default | awk '/default/ {print $3}')
82 |
83 | # identify ip for docker interface
84 | docker_ip=$(ifconfig "${docker_interface}" | grep -P -o -m 1 '(?<=inet\s)[^\s]+')
85 |
86 | # identify netmask for docker interface
87 | docker_mask=$(ifconfig "${docker_interface}" | grep -P -o -m 1 '(?<=netmask\s)[^\s]+')
88 |
89 | # convert netmask into cidr format, strip leading spaces
90 | if [[ "${docker_mask}" == "255.255.255.255" ]]; then
91 | # edge case where ipcalc does not work for networks with a single host, so we specify the cidr mask manually
92 | docker_network_cidr="${docker_ip}/32"
93 | else
94 | docker_network_cidr=$(ipcalc "${docker_ip}" "${docker_mask}" | grep -P -o -m 1 "(?<=Network:)\s+[^\s]+" | sed -e 's/^[[:space:]]*//')
95 | fi
96 |
97 | # append docker interface, gateway adapter, gateway ip, ip, mask and cidr to string
98 | DOCKER_NETWORKING+="${docker_interface},${default_gateway_adapter},${default_gateway_ip},${docker_ip},${docker_mask},${docker_network_cidr} "
99 |
100 | done
101 |
102 | # remove trailing space
103 | DOCKER_NETWORKING="${DOCKER_NETWORKING%"${DOCKER_NETWORKING##*[![:space:]]}"}"
104 |
105 | if [[ "${DEBUG}" == "true" ]]; then
106 | echo "[debug] Docker interface name, Gateway interface name, Gateway IP, Docker interface IP, Subnet mask and CIDR are defined as '${DOCKER_NETWORKING}'" | ts '%Y-%m-%d %H:%M:%.S'
107 | fi
108 |
109 | }
110 |
111 | # this function resolves name to ip address and writes out to /etc/hosts, we do this as we block
112 | # all name lookups on the lan to prevent ip leakage and thus must be able to resolve all vpn
113 | # endpoints that we may connect to.
114 | # this function must be run as root as it overwrites /etc/hosts
115 | function resolve_vpn_endpoints() {
116 |
117 | local vpn_remote_server_list
118 | local vpn_remote_server
119 | local vpn_remote_item
120 | local vpn_remote_item_dns_answer
121 | local vpn_remote_ip_array
122 | local vpn_remote_array
123 |
124 | # split comma separated string into list from VPN_REMOTE_SERVER variable
125 | # shellcheck disable=SC2153
126 | IFS=',' read -ra vpn_remote_server_list <<< "${VPN_REMOTE_SERVER}"
127 |
128 | # initialise indexed array used to store remote ip addresses for all remote endpoints
129 | # note arrays are local to function unless -g flag is added
130 | declare -a vpn_remote_ip_array
131 |
132 | # initalise associative array used to store names and ip for remote endpoints
133 | # note arrays are local to function unless -g flag is added
134 | declare -A vpn_remote_array
135 |
136 | if [[ "${VPN_PROV}" == "pia" ]]; then
137 |
138 | # used to identify wireguard port for pia
139 | vpn_remote_server_list+=(www.privateinternetaccess.com)
140 |
141 | # used to identify wireguard port for pia (proxy alternative)
142 | vpn_remote_server_list+=(piaproxy.net)
143 |
144 | # used to retrieve list of port forward enabled endpoints for pia
145 | vpn_remote_server_list+=(serverlist.piaservers.net)
146 |
147 | fi
148 |
149 | # process remote servers in the array
150 | for vpn_remote_item in "${vpn_remote_server_list[@]}"; do
151 |
152 | vpn_remote_server=$(echo "${vpn_remote_item}" | tr -d ',')
153 |
154 | # if the vpn_remote_server is NOT an ip address (-v option) then resolve it
155 | # note -q prevents output to stdout
156 | if echo "${vpn_remote_server}" | grep -v -q -P -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'; then
157 |
158 | while true; do
159 |
160 | # resolve hostname to ip address(es)
161 | # note grep -m 8 is used to limit number of returned ip's per host to
162 | # 8 to reduce the change of hitting 64 remote options for openvpn
163 | vpn_remote_item_dns_answer=$(drill -a -4 "${vpn_remote_server}" | grep -v 'SERVER' | grep -m 8 -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | xargs)
164 |
165 | # check answer is not blank, if it is blank assume bad ns
166 | if [[ -n "${vpn_remote_item_dns_answer}" ]]; then
167 |
168 | if [[ "${DEBUG}" == "true" ]]; then
169 | echo "[debug] DNS operational, we can resolve name '${vpn_remote_server}' to address '${vpn_remote_item_dns_answer}'" | ts '%Y-%m-%d %H:%M:%.S'
170 | fi
171 |
172 | # append remote server ip addresses to the string using comma separators
173 | vpn_remote_ip_array+=(${vpn_remote_item_dns_answer})
174 |
175 | # filter out pia website (used for wireguard token) and serverlist (used to generate list of endpoints
176 | # with port forwarding enabled) as we do not need to rotate the ip for these and in fact rotating pia
177 | # website breaks the ability to get the token
178 | if [[ "${vpn_remote_item}" != "www.privateinternetaccess.com" && "${vpn_remote_item}" != "serverlist.piaservers.net" ]]; then
179 |
180 | # append endpoint name and ip addresses to associative array
181 | vpn_remote_array+=( ["${vpn_remote_server}"]="${vpn_remote_ip_array[@]}" )
182 |
183 | # dump associative array to file to be read back by tools.sh
184 | declare -p vpn_remote_array > '/tmp/endpoints'
185 | fi
186 |
187 | break
188 |
189 | else
190 |
191 | if [[ "${DEBUG}" == "true" ]]; then
192 | echo "[debug] Having issues resolving name '${vpn_remote_server}', sleeping before retry..." | ts '%Y-%m-%d %H:%M:%.S'
193 | fi
194 | sleep 5s
195 |
196 | fi
197 |
198 | done
199 |
200 | # get first ip from ${vpn_remote_item_dns_answer} and write to the hosts file
201 | # this is required as openvpn will use the remote entry in the ovpn file
202 | # even if you specify the --remote options on the command line, and thus we
203 | # must also be able to resolve the host name (assuming it is a name and not ip).
204 | remote_dns_answer_first=$(echo "${vpn_remote_item_dns_answer}" | cut -d ' ' -f 1)
205 |
206 | # if name not already in /etc/hosts file then write
207 | if ! grep -P -o -m 1 "${vpn_remote_server}" < '/etc/hosts'; then
208 |
209 | # if name resolution to ip is not blank then write to hosts file
210 | if [[ -n "${remote_dns_answer_first}" ]]; then
211 | echo "${remote_dns_answer_first} ${vpn_remote_server}" >> /etc/hosts
212 | fi
213 |
214 | fi
215 |
216 | else
217 |
218 | # append remote server ip addresses to the string using comma separators
219 | vpn_remote_ip_array+=(${vpn_remote_server})
220 |
221 | fi
222 |
223 | done
224 |
225 | # assign array to string (cannot export array in bash) and export for use with other scripts
226 | export VPN_REMOTE_IP_LIST="${vpn_remote_ip_array[*]}"
227 | }
228 |
229 | # wait for valid ip for vpn adapter - blocking
230 | function get_vpn_adapter_ip() {
231 |
232 | if [[ "${DEBUG}" == "true" ]]; then
233 | echo "[debug] Waiting for valid VPN adapter IP addresses from tunnel..."
234 | fi
235 |
236 | # loop and wait until tunnel adapter local ip is valid
237 | # TODO should be caps as its used by multiple scripts and change name to VPN_ADAPTER_IP, but this touches a lot of scripts.
238 | vpn_ip=""
239 | while ! check_valid_ip "${vpn_ip}"; do
240 |
241 | vpn_ip=$(ifconfig "${VPN_DEVICE_TYPE}" 2>/dev/null | grep 'inet' | grep -P -o -m 1 '(?<=inet\s)[^\s]+')
242 | sleep 1s
243 |
244 | done
245 |
246 | if [[ "${DEBUG}" == "true" ]]; then
247 | echo "[debug] Valid VPN adapter IP from tunnel acquired '${vpn_ip}'"
248 | fi
249 |
250 | # write ip address of vpn adapter to file, used in subsequent scripts
251 | echo "${vpn_ip}" > /tmp/getvpnip
252 |
253 | }
254 |
255 | # get vpn adapter gateway ip address - blocking
256 | function get_vpn_gateway_ip() {
257 |
258 | if [[ "${DEBUG}" == "true" ]]; then
259 | echo "[debug] Waiting for valid VPN gateway IP addresses from tunnel..."
260 | fi
261 |
262 | # wait for valid ip address for vpn adapter
263 | get_vpn_adapter_ip
264 |
265 | if [[ "${VPN_PROV}" == "protonvpn" ]]; then
266 |
267 | # get gateway ip, used for openvpn and wireguard to get port forwarding working via getvpnport.sh
268 | VPN_GATEWAY_IP=""
269 | while ! check_valid_ip "${VPN_GATEWAY_IP}"; do
270 |
271 | # use parameter expansion to convert last octet to 1 (gateway ip) from assigned vpn adapter ip
272 | VPN_GATEWAY_IP=${vpn_ip%.*}.1
273 |
274 | sleep 1s
275 |
276 | done
277 |
278 | fi
279 |
280 | if [[ "${VPN_PROV}" == "pia" ]]; then
281 |
282 | # if empty get gateway ip (openvpn clients), otherwise skip (defined in wireguard.sh)
283 | if [[ -z "${VPN_GATEWAY_IP}" ]]; then
284 |
285 | # get gateway ip, used for openvpn and wireguard to get port forwarding working via getvpnport.sh
286 | VPN_GATEWAY_IP=""
287 | while ! check_valid_ip "${VPN_GATEWAY_IP}"; do
288 |
289 | VPN_GATEWAY_IP=$(ip route s t all | grep -m 1 "0.0.0.0/1 via .* dev ${VPN_DEVICE_TYPE}" | cut -d ' ' -f3)
290 | sleep 1s
291 |
292 | done
293 |
294 | fi
295 |
296 | fi
297 |
298 | if [[ "${DEBUG}" == "true" ]]; then
299 | echo "[debug] Valid VPN gateway IP address from tunnel acquired '${VPN_GATEWAY_IP}'"
300 | fi
301 |
302 | }
303 |
304 | # this function checks dns is operational, a file is created if
305 | # dns is not operational and this is monitored and picked up
306 | # by the script /root/openvpn.sh and triggers a restart of the
307 | # openvpn process.
308 | function check_dns() {
309 |
310 | local hostname="${1}"
311 |
312 | local remote_dns_answer
313 | local retry_count=12
314 | local retry_wait=5
315 |
316 | if [[ "${VPN_ENABLED}" == "yes" ]]; then
317 |
318 | if [[ -z "${hostname}" ]]; then
319 |
320 | echo "[warn] No name argument passed, exiting script '${0}'..."
321 | exit 1
322 |
323 | fi
324 |
325 | if [[ "${DEBUG}" == "true" ]]; then
326 | echo "[debug] Checking we can resolve name '${hostname}' to address..."
327 | fi
328 |
329 | while true; do
330 |
331 | # if file exists to denote tunnel is down (from openvpndown.sh or wireguarddown.sh) then break out of function
332 | if [[ -f '/tmp/tunneldown' ]]; then
333 | if [[ "${DEBUG}" == "true" ]]; then
334 | echo "[debug] Tunnel marked as down via file '/tmp/tunneldown', exiting function '${FUNCNAME[0]}'..."
335 | fi
336 | break
337 | fi
338 |
339 | retry_count=$((retry_count-1))
340 |
341 | if [ "${retry_count}" -eq "0" ]; then
342 | echo "[info] DNS failure, creating file '/tmp/dnsfailure' to indicate failure..."
343 | touch "/tmp/dnsfailure"
344 | chmod +r "/tmp/dnsfailure"
345 | break
346 | fi
347 |
348 | # check we can resolve names before continuing (required for tools.sh/get_vpn_external_ip)
349 | # note -v 'SERVER' is to prevent name server ip being matched from stdout
350 | remote_dns_answer=$(drill -a -4 "${hostname}" 2> /dev/null | grep -v 'SERVER' | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | xargs)
351 |
352 | # check answer is not blank, if it is blank assume bad ns
353 | if [[ -n "${remote_dns_answer}" ]]; then
354 |
355 | if [[ "${DEBUG}" == "true" ]]; then
356 | echo "[debug] DNS operational, we can resolve name '${hostname}' to address '${remote_dns_answer}'"
357 | fi
358 | break
359 |
360 | else
361 |
362 | if [[ "${DEBUG}" == "true" ]]; then
363 | echo "[debug] Having issues resolving name '${hostname}'"
364 | echo "[debug] Retrying in ${retry_wait} secs..."
365 | echo "[debug] ${retry_count} retries left"
366 | fi
367 | sleep "${retry_wait}s"
368 |
369 | fi
370 |
371 | done
372 |
373 | fi
374 |
375 | }
376 |
377 | # function to check incoming port is open (webscrape)
378 | function check_incoming_port_webscrape() {
379 |
380 | local incoming_port_check_url="${1}"
381 | shift
382 | local post_data="${1}"
383 | shift
384 | local regex_open="${1}"
385 | shift
386 | local regex_closed="${1}"
387 |
388 | EXTERNAL_SITE_UP="false"
389 | INCOMING_PORT_OPEN="false"
390 |
391 | if [[ "${DEBUG}" == "true" ]]; then
392 | echo "[debug] Checking ${APPLICATION} incoming port '${!application_incoming_port}' is open, using external url '${incoming_port_check_url}'..."
393 | fi
394 |
395 | # use curl to check incoming port is open (web scrape)
396 | if curl --connect-timeout 30 --max-time 120 --silent --data "${post_data}" -X POST "${incoming_port_check_url}" | grep -i -P "${regex_open}" 1> /dev/null; then
397 |
398 | if [[ "${DEBUG}" == "true" ]]; then
399 | echo "[debug] ${APPLICATION} incoming port '${!application_incoming_port}' is open"
400 | fi
401 | EXTERNAL_SITE_UP="true"
402 | INCOMING_PORT_OPEN="true"
403 | return
404 |
405 | else
406 |
407 | # if port is not open then check we have a match for closed, if no match then suspect web scrape issue
408 | if curl --connect-timeout 30 --max-time 120 --silent --data "${post_data}" -X POST "${incoming_port_check_url}" | grep -i -P "${regex_closed}" 1> /dev/null; then
409 |
410 | echo "[info] ${APPLICATION} incoming port closed, marking for reconfigure"
411 | EXTERNAL_SITE_UP="true"
412 | return
413 |
414 | else
415 |
416 | echo "[warn] Incoming port site '${incoming_port_check_url}' failed to web scrape, marking as failed"
417 | return
418 |
419 | fi
420 |
421 | fi
422 |
423 | }
424 |
425 | # function to check incoming port is open (json)
426 | function check_incoming_port_json() {
427 |
428 | incoming_port_check_url="${1}"
429 | json_query="${2}"
430 |
431 | EXTERNAL_SITE_UP="false"
432 | INCOMING_PORT_OPEN="false"
433 |
434 | if [[ "${DEBUG}" == "true" ]]; then
435 | echo "[debug] Checking ${APPLICATION} incoming port '${!application_incoming_port}' is open, using external url '${incoming_port_check_url}'..."
436 | fi
437 |
438 | response=$(curl --connect-timeout 30 --max-time 120 --silent "${incoming_port_check_url}" | jq "${json_query}")
439 |
440 | if [[ "${response}" == "true" ]]; then
441 |
442 | if [[ "${DEBUG}" == "true" ]]; then
443 | echo "[debug] ${APPLICATION} incoming port '${!application_incoming_port}' is open"
444 | fi
445 | EXTERNAL_SITE_UP="true"
446 | INCOMING_PORT_OPEN="true"
447 | return
448 |
449 | elif [[ "${response}" == "false" ]]; then
450 |
451 | echo "[info] ${APPLICATION} incoming port '${!application_incoming_port}' is closed, marking for reconfigure"
452 | EXTERNAL_SITE_UP="true"
453 | return
454 |
455 | else
456 |
457 | echo "[warn] Incoming port site '${incoming_port_check_url}' failed json download, marking as failed"
458 | return
459 |
460 | fi
461 |
462 | }
463 |
464 | # function to call check_incoming_port_webscrape or check_incoming_port_json
465 | function check_incoming_port() {
466 |
467 | # variable used below with bash indirect expansion
468 | application_incoming_port="${APPLICATION}_port"
469 |
470 | if [[ -z "${!application_incoming_port}" ]]; then
471 | echo "[warn] ${APPLICATION} incoming port is not defined" ; return 1
472 | fi
473 |
474 | if [[ -z "${external_ip}" ]]; then
475 | echo "[warn] External IP address is not defined" ; return 2
476 | fi
477 |
478 | # run function for first site (web scrape)
479 | check_incoming_port_webscrape "https://canyouseeme.org/" "port=${!application_incoming_port}&submit=Check" "success.*?on port.*?${!application_incoming_port}" "error.*?on port.*?${!application_incoming_port}"
480 |
481 | # if web scrape error/site down then try second site (json)
482 | if [[ "${EXTERNAL_SITE_UP}" == "false" ]]; then
483 | check_incoming_port_json "https://ifconfig.co/port/${!application_incoming_port}" ".reachable"
484 | fi
485 |
486 | # if port down then mark as closed
487 | if [[ "${INCOMING_PORT_OPEN}" == "false" ]]; then
488 | touch "/tmp/portclosed"
489 | return
490 | fi
491 | }
492 |
493 | # this function reads in the contents of a temporary file which contains the current
494 | # iptables chain policies to check that they are in place before proceeding onto
495 | # check tunnel connectivity. this file is generated by a line in the iptables.sh
496 | # for the application container, and is removed at startup via the init.sh
497 | # script. we use the temporary file to read in iptables chain policies, as we
498 | # cannot perform any iptables instructions for non root users.
499 | function check_iptables_drop() {
500 |
501 | # check /tmp/getiptables file exists
502 | if [ ! -f /tmp/getiptables ]; then
503 | return 1
504 | fi
505 |
506 | # check all chain policies are set to drop
507 | grep -q '\-P INPUT DROP' < /tmp/getiptables || return 1
508 | grep -q '\-P FORWARD DROP' < /tmp/getiptables || return 1
509 | grep -q '\-P OUTPUT DROP' < /tmp/getiptables || return 1
510 |
511 | return 0
512 | }
513 |
514 | # function to call check_iptables_drop
515 | function check_iptables() {
516 | if [[ "${DEBUG}" == "true" ]]; then
517 | echo "[debug] Waiting for iptables chain policies to be in place..."
518 | fi
519 |
520 | # loop and wait until iptables chain policies are in place
521 | while ! check_iptables_drop
522 | do
523 | sleep 0.1
524 | done
525 |
526 | if [[ "${DEBUG}" == "true" ]]; then
527 | echo "[debug] iptables chain policies are in place"
528 | fi
529 | }
530 |
531 | # function to read the assigned vpn incoming port from the file
532 | # "/tmp/getvpnextip", created by function get_vpn_external_ip
533 | function check_vpn_external_ip() {
534 |
535 | while [ ! -f "/tmp/getvpnextip" ]
536 | do
537 | sleep 0.1s
538 | done
539 |
540 | # get vpn external ip address (file contents generated by tools.sh/get_vpn_external_ip)
541 | external_ip=$( /dev/null | grep -P -o -m 1 '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')"
603 |
604 | if ! check_valid_ip "${EXTERNAL_IP}"; then
605 | return 1
606 | fi
607 |
608 | # write external ip address to text file, this is then read by the downloader script
609 | echo "${EXTERNAL_IP}" > /tmp/getvpnextip
610 |
611 | # chmod file to prevent restrictive umask causing read issues for user nobody (owner is user root)
612 | chmod +r /tmp/getvpnextip
613 |
614 | }
615 |
616 | function get_vpn_external_ip() {
617 |
618 | local external_ip_urls_array=( "http://checkip.amazonaws.com" "http://whatismyip.akamai.com" "https://ifconfig.co/ip" "https://showextip.azurewebsites.net" )
619 |
620 | if [[ -z "${vpn_ip}" ]]; then
621 | echo "[warn] VPN IP address is not defined or is an empty string"
622 | return 1
623 | fi
624 |
625 | # get token json response, this is required for wireguard connection
626 | for external_ip_url in "${external_ip_urls_array[@]}"; do
627 |
628 | if ! get_external_ip_web "${external_ip_url}"; then
629 |
630 | echo "[warn] Cannot determine external IP address, trying next URL..."
631 | continue
632 | else
633 | echo "[info] Successfully retrieved external IP address ${EXTERNAL_IP} from URL '${external_ip_url}'"
634 | return 0
635 |
636 | fi
637 |
638 | done
639 |
640 | echo "[warn] Cannot determine external IP address, performing tests before setting to '127.0.0.1'..."
641 | echo "[info] Show name servers defined for container" ; cat /etc/resolv.conf
642 | echo "[info] Show contents of hosts file" ; cat /etc/hosts
643 |
644 | # write external ip address to text file, this is then read by the downloader script
645 | echo "127.0.0.1" > /tmp/getvpnextip
646 |
647 | # chmod file to prevent restrictive umask causing read issues for user nobody (owner is user root)
648 | chmod +r /tmp/getvpnextip
649 |
650 | return 1
651 |
652 | }
653 |
654 | function pia_port_forward_check() {
655 |
656 | local jq_query_details
657 | local jq_query_result
658 |
659 | echo "[info] Port forwarding is enabled"
660 | echo "[info] Checking endpoint '${VPN_REMOTE_SERVER}' is port forward enabled..."
661 |
662 | # run curl to grab api result
663 | jq_query_result=$(curl --silent --insecure "${PIA_VPNINFO_API}")
664 |
665 | if [[ -z "${jq_query_result}" ]]; then
666 | echo "[warn] PIA endpoint API '${PIA_VPNINFO_API}' currently down, skipping endpoint port forward check"
667 | return 1
668 | fi
669 |
670 | # run jq query to get endpoint name (dns) only, use xargs to turn into single line string
671 | jq_query_details=$(echo "${jq_query_result}" | jq -r "${JQ_QUERY_PORT_FORWARD_ENABLED}" 2> /dev/null | xargs)
672 |
673 | if [[ -z "${jq_query_details}" ]]; then
674 | echo "[warn] Json query '${JQ_QUERY_PORT_FORWARD_ENABLED}' returns empty result for port forward enabled servers, skipping endpoint port forward check"
675 | return 1
676 | fi
677 |
678 | # run grep to check that defined vpn remote is in the list of port forward enabled endpoints
679 | # grep -w = exact match (whole word), grep -q = quiet mode (no output)
680 | if echo "${jq_query_details}" | grep -qw "${VPN_REMOTE_SERVER}"; then
681 |
682 | echo "[info] PIA endpoint '${VPN_REMOTE_SERVER}' is in the list of endpoints that support port forwarding shown below:-"
683 | pia_port_forward_list
684 | return 0
685 |
686 | else
687 |
688 | echo "[info] PIA endpoint '${VPN_REMOTE_SERVER}' is NOT in the list of endpoints that support port forwarding shown below:-"
689 | pia_port_forward_list
690 | return 1
691 |
692 | fi
693 |
694 | }
695 |
696 | function pia_port_forward_list() {
697 |
698 | local jq_query_details
699 | local jq_query_result
700 |
701 | # run curl to grab api result
702 | jq_query_result=$(curl --silent --insecure "${PIA_VPNINFO_API}")
703 |
704 | # run jq query to get endpoint name (dns) only, use xargs to turn into single line string
705 | jq_query_details=$(echo "${jq_query_result}" | jq -r "${JQ_QUERY_PORT_FORWARD_ENABLED}" 2> /dev/null | xargs)
706 |
707 | # convert to list with separator being space
708 | IFS=' ' read -ra jq_query_details_list <<< "${jq_query_details}"
709 |
710 | echo "[info] List of PIA endpoints that support port forwarding:-"
711 |
712 | # loop over list of port forward enabled endpooints and echo out to console
713 | for i in "${jq_query_details_list[@]}"; do
714 | echo "[info] ${i}"
715 | done
716 |
717 | }
718 |
719 | function pia_generate_token() {
720 |
721 | local pia_generate_token_url_array=( "https://www.privateinternetaccess.com/gtoken/generateToken" "https://piaproxy.net/gtoken/generateToken" )
722 |
723 | local retry_count=12
724 | local retry_wait_secs=10
725 | local token_json_response
726 | local result='false'
727 |
728 | while true; do
729 |
730 | if [[ "${retry_count}" -eq "0" ]]; then
731 | return 1
732 | fi
733 |
734 | # get token json response, this is required for wireguard connection
735 | for pia_generate_token_url in "${pia_generate_token_url_array[@]}"; do
736 |
737 | token_json_response=$(curl --silent --insecure -u "${VPN_USER}:${VPN_PASS}" "${pia_generate_token_url}")
738 |
739 | if [ "$(echo "${token_json_response}" | jq -r '.status')" == "OK" ]; then
740 |
741 | echo "[info] Successfully downloaded PIA json to generate token for wireguard from URL '${pia_generate_token_url}'"
742 | PIA_GENERATE_TOKEN=$(echo "${token_json_response}" | jq -r '.token')
743 |
744 | if [[ -n "${PIA_GENERATE_TOKEN}" ]]; then
745 | echo "[info] Successfully generated PIA token for wireguard"
746 | result='true'
747 | break
748 | else
749 | echo "[warn] PIA token not generated successfully (empty)"
750 | return 1
751 | fi
752 |
753 | else
754 |
755 | echo "[warn] Failed to download PIA json to generate token for wireguard from URL '${pia_generate_token_url}'"
756 | return 1
757 |
758 | fi
759 |
760 | echo "[info] ${retry_count} retries left"
761 | echo "[info] Retrying in ${retry_wait_secs} secs..."
762 | retry_count=$((retry_count-1))
763 | sleep "${retry_wait_secs}"s & wait $!
764 |
765 | done
766 |
767 | if [[ "${result}" == "true" ]]; then
768 | break
769 | fi
770 |
771 | done
772 |
773 | if [[ "${DEBUG}" == "true" ]]; then
774 | echo "[debug] PIA generated 'token' for wireguard is '${PIA_GENERATE_TOKEN}'"
775 | fi
776 |
777 | }
778 |
779 | function pia_payload_and_signature() {
780 |
781 | local retry_count=12
782 | local retry_wait_secs=10
783 | local payload_and_sig
784 |
785 | while true; do
786 |
787 | if [ "${retry_count}" -eq "0" ]; then
788 |
789 | echo "[warn] Unable to download PIA json payload, creating file '/tmp/portfailure' to indicate failure..."
790 | touch "/tmp/portfailure" && chmod +r "/tmp/portfailure" ; return 1
791 |
792 | fi
793 |
794 | # get payload and signature
795 | # note use of urlencode, this is required, otherwise login failure can occur
796 | payload_and_sig=$(curl --interface "${VPN_DEVICE_TYPE}" --insecure --silent --max-time 5 --get --data-urlencode "token=${PIA_GENERATE_TOKEN}" "https://${VPN_GATEWAY_IP}:19999/getSignature")
797 |
798 | if [ "$(echo "${payload_and_sig}" | jq -r '.status')" != "OK" ]; then
799 |
800 | echo "[warn] Unable to successfully download PIA json payload from URL 'https://${VPN_GATEWAY_IP}:19999/getSignature' using token '${PIA_GENERATE_TOKEN}'"
801 | echo "[info] ${retry_count} retries left"
802 | echo "[info] Retrying in ${retry_wait_secs} secs..."
803 | retry_count=$((retry_count-1))
804 | sleep "${retry_wait_secs}"s & wait $!
805 |
806 | else
807 |
808 | # reset retry count on successful step
809 | retry_count=12
810 | break
811 |
812 | fi
813 |
814 | done
815 |
816 | PAYLOAD=$(echo "${payload_and_sig}" | jq -r '.payload')
817 | SIGNATURE=$(echo "${payload_and_sig}" | jq -r '.signature')
818 |
819 |
820 | }
821 | function pia_assign_incoming_port() {
822 |
823 | local retry_count=12
824 | local retry_wait_secs=10
825 | local payload_decoded
826 | local expires_at
827 | local pia_incoming_port
828 |
829 | # generate token, used to get payload and signature
830 | if ! pia_generate_token; then
831 | echo "[warn] Unable to generate token for port forwarding, creating file '/tmp/portfailure' to indicate failure..."
832 | touch "/tmp/portfailure" && chmod +r "/tmp/portfailure" ; return 1
833 | fi
834 |
835 | # get payload and signature
836 | pia_payload_and_signature
837 |
838 | # decode payload to get port, and expires date (2 months)
839 | payload_decoded=$(echo "${PAYLOAD}" | base64 -d | jq)
840 |
841 | if [[ -n "${payload_decoded}" ]]; then
842 |
843 | pia_incoming_port=$(echo "${payload_decoded}" | jq -r '.port')
844 | # note expires_at time in this format'2020-11-24T22:12:07.627551124Z'
845 | expires_at=$(echo "${payload_decoded}" | jq -r '.expires_at')
846 |
847 | if [[ "${DEBUG}" == "true" ]]; then
848 |
849 | echo "[debug] PIA generated 'token' for port forwarding is '${PIA_GENERATE_TOKEN}'"
850 | echo "[debug] PIA assigned incoming port is '${pia_incoming_port}'"
851 | echo "[debug] PIA port forward assigned expires on '${expires_at}'"
852 |
853 | fi
854 |
855 | else
856 |
857 | echo "[warn] Unable to decode payload, creating file '/tmp/portfailure' to indicate failure..."
858 | touch "/tmp/portfailure" && chmod +r "/tmp/portfailure" ; return 1
859 |
860 | fi
861 |
862 | if [[ "${pia_incoming_port}" =~ ^-?[0-9]+$ ]]; then
863 |
864 | # write port number to text file (read by downloader script)
865 | echo "${pia_incoming_port}" > /tmp/getvpnport
866 |
867 | else
868 |
869 | echo "[warn] Incoming port assigned is not a decimal value '${pia_incoming_port}', creating file '/tmp/portfailure' to indicate failure..."
870 | touch "/tmp/portfailure" && chmod +r "/tmp/portfailure" ; return 1
871 |
872 | fi
873 |
874 | # run function to bind port every 15 minutes
875 | pia_keep_incoming_port_alive
876 |
877 | }
878 |
879 | function pia_keep_incoming_port_alive() {
880 |
881 | local retry_count=12
882 | local retry_wait_secs=10
883 | local bind_port
884 |
885 | if [[ "${DEBUG}" == "true" ]]; then
886 | echo "[debug] Running infinite while loop to keep assigned incoming port for PIA live..."
887 | fi
888 |
889 | while true; do
890 |
891 | if [ "${retry_count}" -eq "0" ]; then
892 |
893 | echo "[warn] Unable to bind incoming port, creating file '/tmp/portfailure' to indicate failure..."
894 | touch "/tmp/portfailure" && chmod +r "/tmp/portfailure" ; return 1
895 |
896 | fi
897 |
898 | # note use of urlencode, this is required, otherwise login failure can occur
899 | bind_port=$(curl --interface "${VPN_DEVICE_TYPE}" --insecure --silent --max-time 5 --get --data-urlencode "payload=${PAYLOAD}" --data-urlencode "signature=${SIGNATURE}" "https://${VPN_GATEWAY_IP}:19999/bindPort")
900 |
901 | if [ "$(echo "${bind_port}" | jq -r '.status')" != "OK" ]; then
902 |
903 | echo "[warn] Unable to bind port using URL 'https://${VPN_GATEWAY_IP}:19999/bindPort'"
904 | retry_count=$((retry_count-1))
905 | echo "[info] ${retry_count} retries left"
906 | echo "[info] Retrying in ${retry_wait_secs} secs..."
907 | sleep "${retry_wait_secs}"s & wait $!
908 | continue
909 |
910 | else
911 |
912 | # reset retry count on successful step
913 | retry_count=12
914 |
915 | fi
916 |
917 | echo "[info] Successfully assigned and bound incoming port"
918 |
919 | # we need to poll AT LEAST every 15 minutes to keep the port open
920 | sleep 10m & wait $!
921 |
922 | done
923 |
924 | }
925 |
926 | function protonvpn_port_forward_check() {
927 |
928 | # run function from tools.sh
929 | get_vpn_gateway_ip
930 |
931 | # check if username has the required suffix of '+pmp'
932 | if [[ "${VPN_USER}" != *"+pmp"* ]]; then
933 | echo "[info] ProtonVPN username '${VPN_USER}' does not contain the suffix '+pmp' and therefore is not enabled for port forwarding, skipping port forward assignment..."
934 | return 1
935 | fi
936 |
937 | # check if endpoint is enabled for p2p
938 | if ! natpmpc -g "${VPN_GATEWAY_IP}"; then
939 | echo "[warn] ProtonVPN endpoint '${VPN_REMOTE_SERVER}' is not enabled for P2P port forwarding, skipping port forward assignment..."
940 | return 1
941 | fi
942 | return 0
943 |
944 | }
945 |
946 | function protonvpn_assign_incoming_port() {
947 |
948 | local protonvpn_incoming_port
949 | local protocol_list
950 |
951 | if [[ "${DEBUG}" == "true" ]]; then
952 | echo "[debug] Running infinite while loop to keep assigned incoming port for ProtonVPN live..."
953 | fi
954 |
955 | while true; do
956 |
957 | protocol_list="UDP TCP"
958 |
959 | for protocol in ${protocol_list}; do
960 |
961 | # assign incoming port for udp/tcp
962 | protonvpn_incoming_port=$(natpmpc -g "${VPN_GATEWAY_IP}" -a 1 0 "${protocol}" 60 | grep -P -o -m 1 '(?<=Mapped public port\s)\d+')
963 | if [ -z "${protonvpn_incoming_port}" ]; then
964 | echo "[warn] Unable to assign an incoming port for protocol ${protocol}, creating file '/tmp/portfailure' to indicate failure..."
965 | touch "/tmp/portfailure" && chmod +r "/tmp/portfailure" ; return 1
966 | fi
967 |
968 | done
969 |
970 | if [[ "${DEBUG}" == "true" ]]; then
971 | echo "[debug] ProtonVPN assigned incoming port is '${protonvpn_incoming_port}'"
972 | fi
973 |
974 | # write port number to text file (read by downloader script)
975 | echo "${protonvpn_incoming_port}" > /tmp/getvpnport
976 |
977 | # we need to poll AT LEAST every 60 seconds to keep the port open
978 | sleep 45s & wait $!
979 |
980 | done
981 | }
982 |
983 | function get_vpn_incoming_port() {
984 |
985 | if [[ "${VPN_PROV}" == "protonvpn" ]]; then
986 |
987 | if [[ "${STRICT_PORT_FORWARD}" == "no" ]]; then
988 |
989 | echo "[info] Port forwarding is not enabled"
990 |
991 | # create empty incoming port file (read by downloader script)
992 | touch /tmp/getvpnport
993 |
994 | else
995 |
996 | echo "[info] Script started to assign incoming port for '${VPN_PROV}'"
997 |
998 | # write pid of this script to file, this file is then used to kill this script if openvpn/wireguard restarted/killed
999 | echo "${BASHPID}" > '/tmp/getvpnport.pid'
1000 |
1001 | # check whether endpoint is enabled for port forwarding and username has correct suffix
1002 | if protonvpn_port_forward_check; then
1003 |
1004 | # assign incoming port - blocking as in infinite while loop
1005 | protonvpn_assign_incoming_port
1006 |
1007 | fi
1008 |
1009 | echo "[info] Script finished to assign incoming port"
1010 |
1011 | fi
1012 |
1013 | elif [[ "${VPN_PROV}" == "pia" ]]; then
1014 |
1015 | if [[ "${STRICT_PORT_FORWARD}" == "no" ]]; then
1016 |
1017 | echo "[info] Port forwarding is not enabled"
1018 |
1019 | # create empty incoming port file (read by downloader script)
1020 | touch /tmp/getvpnport
1021 |
1022 | else
1023 |
1024 | echo "[info] Script started to assign incoming port for '${VPN_PROV}'"
1025 |
1026 | # write pid of this script to file, this file is then used to kill this script if openvpn/wireguard restarted/killed
1027 | echo "${BASHPID}" > '/tmp/getvpnport.pid'
1028 |
1029 | # pia api url for endpoint status (port forwarding enabled true|false)
1030 | PIA_VPNINFO_API="https://serverlist.piaservers.net/vpninfo/servers/v4"
1031 |
1032 | # jq (json query tool) query to list port forward enabled servers by hostname (dns)
1033 | JQ_QUERY_PORT_FORWARD_ENABLED='.regions | .[] | select(.port_forward==true) | .dns'
1034 |
1035 | # check whether endpoint is enabled for port forwarding
1036 | if pia_port_forward_check; then
1037 |
1038 | # assign incoming port - blocking as in infinite while loop
1039 | pia_assign_incoming_port
1040 |
1041 | fi
1042 |
1043 | echo "[info] Script finished to assign incoming port"
1044 |
1045 | fi
1046 |
1047 | else
1048 |
1049 | echo "[info] VPN provider '${VPN_PROV}' not supported for automatic port forwarding, skipping incoming port assignment"
1050 |
1051 | fi
1052 |
1053 | }
1054 |
--------------------------------------------------------------------------------
/run/nobody/microsocks.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/dumb-init /bin/bash
2 |
3 | echo "[info] Attempting to start microsocks..."
4 |
5 | microsocks_cli="nohup /usr/local/bin/microsocks -i 0.0.0.0 -p 9118"
6 |
7 | if [[ -n "${SOCKS_USER}" ]]; then
8 | microsocks_cli="${microsocks_cli} -u ${SOCKS_USER} -P ${SOCKS_PASS}"
9 | fi
10 |
11 | if [[ "${VPN_ENABLED}" == "yes" ]]; then
12 | microsocks_cli="${microsocks_cli} -b ${vpn_ip}"
13 | fi
14 |
15 | if [[ "${DEBUG}" == "false" ]]; then
16 | microsocks_cli="${microsocks_cli} -q"
17 | fi
18 |
19 | ${microsocks_cli} &
20 |
21 | echo "[info] microsocks process started"
22 |
--------------------------------------------------------------------------------
/run/nobody/preruncheck.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # script to call multiple scripts in series to read in values written by script /root/prerunget.sh
4 |
5 | # source in various tools
6 | # shellcheck source=../local/tools.sh
7 | source tools.sh
8 |
9 | # blocking script, will wait for valid ip address assigned to tun0/tap0 (ip read in from file /tmp/getvpnip)
10 | # value read in is generated by script tools.sh
11 | check_vpn_tunnel_ip
12 |
13 | # blocking script, will wait for vpn incoming port to be assigned (port read in from file /tmp/getvpnport)
14 | # value read in is generated by script tools.sh
15 | check_vpn_incoming_port
16 |
17 | # blocking script, will wait for name resolution to be operational (will write to /tmp/dnsfailure if failure)
18 | check_dns www.google.com
19 |
20 | # blocking script, will wait for external ip address retrieval (external ip read in from file /tmp/getvpnextip)
21 | # value read in is generated by script tools.sh/get_vpn_external_ip
22 | check_vpn_external_ip
23 |
24 | # blocking script, will wait for iptables chain policy to be DROP (iptables listing read in from file /tmp/getiptables)
25 | # value read in is generated by script /root/iptable.sh
26 | check_iptables
27 |
--------------------------------------------------------------------------------
/run/nobody/privoxy.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/dumb-init /bin/bash
2 |
3 | if [[ "${ENABLE_PRIVOXY}" == "yes" ]]; then
4 |
5 | mkdir -p /config/privoxy
6 |
7 | if [[ ! -f "/config/privoxy/config" ]]; then
8 |
9 | echo "[info] Configuring Privoxy..."
10 | cp -R /etc/privoxy/ /config/
11 |
12 | sed -i -e "s~confdir /etc/privoxy~confdir /config/privoxy~g" /config/privoxy/config
13 | sed -i -e "s~logdir /var/log/privoxy~logdir /config/privoxy~g" /config/privoxy/config
14 | sed -i -e "s~listen-address.*~listen-address :8118~g" /config/privoxy/config
15 |
16 | fi
17 |
18 | if [[ "${privoxy_running}" == "false" ]]; then
19 |
20 | echo "[info] Attempting to start Privoxy..."
21 |
22 | # run Privoxy (daemonized, non-blocking)
23 | /usr/bin/privoxy /config/privoxy/config
24 |
25 | # make sure process privoxy DOES exist
26 | retry_count=12
27 | retry_wait=1
28 | while true; do
29 |
30 | if ! pgrep -x "privoxy" > /dev/null; then
31 |
32 | retry_count=$((retry_count-1))
33 | if [ "${retry_count}" -eq "0" ]; then
34 |
35 | echo "[warn] Wait for Privoxy process to start aborted, too many retries"
36 | echo "[info] Showing output from command before exit..."
37 | timeout 10 /usr/bin/privoxy /config/privoxy/config ; return 1
38 |
39 | else
40 |
41 | if [[ "${DEBUG}" == "true" ]]; then
42 | echo "[debug] Waiting for Privoxy process to start"
43 | echo "[debug] Re-check in ${retry_wait} secs..."
44 | echo "[debug] ${retry_count} retries left"
45 | fi
46 | sleep "${retry_wait}s"
47 |
48 | fi
49 |
50 | else
51 |
52 | echo "[info] Privoxy process started"
53 | break
54 |
55 | fi
56 |
57 | done
58 |
59 | echo "[info] Waiting for Privoxy process to start listening on port 8118..."
60 |
61 | while [[ $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".8118\"") == "" ]]; do
62 | sleep 0.1
63 | done
64 |
65 | echo "[info] Privoxy process listening on port 8118"
66 |
67 | fi
68 |
69 | else
70 |
71 | if [[ "${DEBUG}" == "true" ]]; then
72 | echo "[info] Privoxy set to disabled"
73 | fi
74 |
75 | fi
76 |
77 | # set privoxy ip to current vpn ip (used when checking for changes on next run)
78 | privoxy_ip="${vpn_ip}"
79 |
--------------------------------------------------------------------------------
/run/root/iptable-init.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | function accept_vpn_endpoints() {
4 |
5 | local direction="${1}"
6 |
7 | if [[ "${direction}" == "input" ]]; then
8 | io_flag="INPUT -i"
9 | srcdst_flag="-s"
10 | else
11 | io_flag="OUTPUT -o"
12 | srcdst_flag="-d"
13 | fi
14 |
15 | # convert list of ip's back into an array (cannot export arrays in bash)
16 | IFS=' ' read -ra vpn_remote_ip_array <<< "${VPN_REMOTE_IP_LIST}"
17 |
18 | for docker_network in ${DOCKER_NETWORKING}; do
19 |
20 | # read in DOCKER_NETWORKING from tools.sh
21 | docker_interface="$(echo "${docker_network}" | cut -d ',' -f 1 )"
22 |
23 | # iterate over remote ip address array and create accept rules
24 | for vpn_remote_ip_item in "${vpn_remote_ip_array[@]}"; do
25 |
26 | # note grep -e is required to indicate no flags follow to prevent -A from being incorrectly picked up
27 | rule_exists=$(iptables -S | grep -e "-A ${io_flag} ${docker_interface} ${srcdst_flag} ${vpn_remote_ip_item} -j ACCEPT" || true)
28 |
29 | if [[ -z "${rule_exists}" ]]; then
30 |
31 | # accept input/output to remote vpn endpoint
32 | iptables -A ${io_flag} "${docker_interface}" ${srcdst_flag} "${vpn_remote_ip_item}" -j ACCEPT
33 |
34 | fi
35 |
36 | done
37 |
38 | done
39 |
40 | }
41 |
42 | function drop_all_ipv4() {
43 |
44 | # check and set iptables drop
45 | if ! iptables -S | grep '^-P' > /dev/null 2>&1; then
46 |
47 | echo "[crit] iptables default policies not available, exiting script..." | ts '%Y-%m-%d %H:%M:%.S'
48 | exit 1
49 |
50 | else
51 |
52 | if [[ "${DEBUG}" == "true" ]]; then
53 | echo "[debug] iptables default policies available, setting policy to drop..." | ts '%Y-%m-%d %H:%M:%.S'
54 | fi
55 |
56 | # set policy to drop ipv4 for input
57 | iptables -P INPUT DROP > /dev/null
58 |
59 | # set policy to drop ipv4 for forward
60 | iptables -P FORWARD DROP > /dev/null
61 |
62 | # set policy to drop ipv4 for output
63 | iptables -P OUTPUT DROP > /dev/null
64 |
65 | fi
66 | }
67 |
68 | function drop_all_ipv6() {
69 |
70 | # check and set ip6tables drop
71 | if ! ip6tables -S | grep '^-P' > /dev/null 2>&1; then
72 |
73 | echo "[warn] ip6tables default policies not available, skipping ip6tables drops" | ts '%Y-%m-%d %H:%M:%.S'
74 |
75 | else
76 |
77 | if [[ "${DEBUG}" == "true" ]]; then
78 | echo "[debug] ip6tables default policies available, setting policy to drop..." | ts '%Y-%m-%d %H:%M:%.S'
79 | fi
80 |
81 | # set policy to drop ipv6 for input
82 | ip6tables -P INPUT DROP > /dev/null
83 |
84 | # set policy to drop ipv6 for forward
85 | ip6tables -P FORWARD DROP > /dev/null
86 |
87 | # set policy to drop ipv6 for output
88 | ip6tables -P OUTPUT DROP > /dev/null
89 |
90 | fi
91 | }
92 |
93 | function name_resolution() {
94 |
95 | rule_flag="${1}"
96 |
97 | # permit queries to docker internal name server via any port
98 | #
99 | # note no port specified as docker randomises the port, run command 'iptables -L -v -t nat' to view docker internal
100 | # chain showing randomised port
101 | #
102 | # decent article discussing docker dns https://alex-v.blog/2019/12/13/quirks-of-dns-traffic-with-docker-compose/
103 | #
104 | iptables "${rule_flag}" INPUT -s 127.0.0.11/32 -j ACCEPT
105 | iptables "${rule_flag}" INPUT -d 127.0.0.11/32 -j ACCEPT
106 |
107 | iptables "${rule_flag}" OUTPUT -s 127.0.0.11/32 -j ACCEPT
108 | iptables "${rule_flag}" OUTPUT -d 127.0.0.11/32 -j ACCEPT
109 |
110 | # permit name resolution on port 53 for any ip
111 | #
112 | # note no ip specified due to the fact that a user can specify the name servers via docker and the query will then be
113 | # sent to 127.0.0.11 and forwarded onto the defined name servers
114 | #
115 | iptables "${rule_flag}" INPUT -p tcp -m tcp --sport 53 -j ACCEPT
116 | iptables "${rule_flag}" INPUT -p udp -m udp --sport 53 -j ACCEPT
117 |
118 | iptables "${rule_flag}" OUTPUT -p tcp -m tcp --dport 53 -j ACCEPT
119 | iptables "${rule_flag}" OUTPUT -p udp -m udp --dport 53 -j ACCEPT
120 |
121 | }
122 |
123 | function add_name_servers() {
124 |
125 | # split comma separated string into list from NAME_SERVERS env variable
126 | IFS=',' read -ra name_server_list <<< "${NAME_SERVERS}"
127 |
128 | if [[ "${DEBUG}" == "true" ]]; then
129 | echo "[debug] Showing name servers in '/etc/resolv.conf' before overwrite from NAME_SERVERS..." | ts '%Y-%m-%d %H:%M:%.S'
130 | cat '/etc/resolv.conf' | ts '%Y-%m-%d %H:%M:%.S [debug]'
131 | fi
132 |
133 | # remove all existing name servers inherited from host
134 | > /etc/resolv.conf
135 |
136 | # process name servers in the list
137 | for name_server_item in "${name_server_list[@]}"; do
138 |
139 | # strip whitespace from start and end of name_server_item
140 | name_server_item=$(echo "${name_server_item}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
141 |
142 | # append name server to /etc/resolv.conf
143 | echo "nameserver ${name_server_item}" >> /etc/resolv.conf
144 |
145 | done
146 |
147 | if [[ "${DEBUG}" == "true" ]]; then
148 | echo "[debug] Showing name servers in '/etc/resolv.conf' after overwrite from NAME_SERVERS..." | ts '%Y-%m-%d %H:%M:%.S'
149 | cat '/etc/resolv.conf' | ts '%Y-%m-%d %H:%M:%.S [debug]'
150 | fi
151 |
152 | }
153 |
154 | function main() {
155 |
156 | # drop all for ipv4
157 | drop_all_ipv4
158 |
159 | # drop all for ipv6
160 | drop_all_ipv6
161 |
162 | # source in tools script
163 | # shellcheck source=../local/tools.sh
164 | source tools.sh
165 |
166 | # insert accept name resolution rules
167 | name_resolution '-I'
168 |
169 | # call function from tools.sh to resolve all vpn endpoints
170 | resolve_vpn_endpoints
171 |
172 | # delete accept name resolution rules
173 | name_resolution '-D'
174 |
175 | # overwrite name servers using value from env var 'NAME_SERVERS'
176 | # Note we do this AFTER resolving vpn endpoints to permit name resolution
177 | # of the vpn endpoints using whatever the host has defined, including
178 | # local name servers - useful for pihole
179 | add_name_servers
180 |
181 | # run function from tools.sh to create global var 'docker_networking' used below
182 | get_docker_networking
183 |
184 | # call function to add vpn remote endpoints to iptables input accept rule
185 | accept_vpn_endpoints "input"
186 |
187 | # call function to add vpn remote endpoints to iptables output accept rule
188 | accept_vpn_endpoints "output"
189 |
190 | }
191 |
192 | main
--------------------------------------------------------------------------------
/run/root/iptable.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # initialise arrays for incoming ports
4 | incoming_ports_ext_array=()
5 | incoming_ports_lan_array=()
6 |
7 | # append incoming ports for applications to arrays
8 | if [[ "${APPLICATION}" == "qbittorrent" ]]; then
9 | incoming_ports_ext_array+=(${WEBUI_PORT})
10 | elif [[ "${APPLICATION}" == "sabnzbd" ]]; then
11 | incoming_ports_ext_array+=(8080 8090)
12 | elif [[ "${APPLICATION}" == "deluge" ]]; then
13 | incoming_ports_ext_array+=(8112)
14 | incoming_ports_lan_array+=(58846)
15 | fi
16 |
17 | # if microsocks enabled then add port for microsocks to incoming ports lan array
18 | if [[ "${ENABLE_SOCKS}" == "yes" ]]; then
19 | incoming_ports_lan_array+=(9118)
20 | fi
21 |
22 | # if privoxy enabled then add port for privoxy to incoming ports lan array
23 | if [[ "${ENABLE_PRIVOXY}" == "yes" ]]; then
24 | incoming_ports_lan_array+=(8118)
25 | fi
26 |
27 | # source in tools script
28 | # shellcheck source=../local/tools.sh
29 | source tools.sh
30 |
31 | # run function from tools.sh, this creates global var 'docker_networking' used below
32 | get_docker_networking
33 |
34 | # if vpn input ports specified then add to incoming ports external array
35 | if [[ -n "${VPN_INPUT_PORTS}" ]]; then
36 |
37 | # split comma separated string into array from VPN_INPUT_PORTS env variable
38 | IFS=',' read -ra vpn_input_ports_array <<< "${VPN_INPUT_PORTS}"
39 |
40 | # merge both arrays
41 | incoming_ports_ext_array=("${incoming_ports_ext_array[@]}" "${vpn_input_ports_array[@]}")
42 |
43 | fi
44 |
45 | # convert list of ip's back into an array (cannot export arrays in bash)
46 | IFS=' ' read -ra vpn_remote_ip_array <<< "${VPN_REMOTE_IP_LIST}"
47 |
48 | # if vpn output ports specified then add to outbound ports lan array
49 | if [[ -n "${VPN_OUTPUT_PORTS}" ]]; then
50 | # split comma separated string into array from VPN_OUTPUT_PORTS env variable
51 | IFS=',' read -ra outgoing_ports_lan_array <<< "${VPN_OUTPUT_PORTS}"
52 | fi
53 |
54 | # array for both protocols
55 | multi_protocol_array=(tcp udp)
56 |
57 | # split comma separated string into array from LAN_NETWORK env variable
58 | IFS=',' read -ra lan_network_array <<< "${LAN_NETWORK}"
59 |
60 | # split comma separated string into array from VPN_REMOTE_PORT env var
61 | IFS=',' read -ra vpn_remote_port_array <<< "${VPN_REMOTE_PORT}"
62 |
63 | # ip route
64 | ###
65 |
66 | # process lan networks in the array
67 | for lan_network_item in "${lan_network_array[@]}"; do
68 |
69 | # strip whitespace from start and end of lan_network_item
70 | lan_network_item=$(echo "${lan_network_item}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
71 |
72 | # read in DOCKER_NETWORKING from tools.sh, get 2nd and third values in first list item
73 | default_gateway_adapter="$(echo "${DOCKER_NETWORKING}" | cut -d ',' -f 2 )"
74 | default_gateway_ip="$(echo "${DOCKER_NETWORKING}" | cut -d ',' -f 3 )"
75 |
76 | echo "[info] Adding ${lan_network_item} as route via adapter ${default_gateway_adapter}"
77 | ip route add "${lan_network_item}" via "${default_gateway_ip}" dev "${default_gateway_adapter}"
78 |
79 | done
80 |
81 | echo "[info] ip route defined as follows..."
82 | echo "--------------------"
83 | ip route s t all
84 | echo "--------------------"
85 |
86 | # iptables marks
87 | ###
88 |
89 | if [[ "${DEBUG}" == "true" ]]; then
90 | echo "[debug] Modules currently loaded for kernel" ; lsmod
91 | fi
92 |
93 | # check we have iptable_mangle, if so setup fwmark
94 | lsmod | grep iptable_mangle
95 | iptable_mangle_exit_code="${?}"
96 |
97 | if [[ "${iptable_mangle_exit_code}" == 0 ]]; then
98 |
99 | echo "[info] iptable_mangle support detected, adding fwmark for tables"
100 |
101 | mark=0
102 | # required as path did not exist in latest tarball (20/09/2023)
103 | mkdir -p '/etc/iproute2'
104 |
105 | # setup route for application using set-mark to route traffic to lan
106 | for incoming_ports_ext_item in "${incoming_ports_ext_array[@]}"; do
107 |
108 | mark=$((mark+1))
109 | echo "${incoming_ports_ext_item} ${incoming_ports_ext_item}_${APPLICATION}" >> '/etc/iproute2/rt_tables'
110 | ip rule add fwmark "${mark}" table "${incoming_ports_ext_item}_${APPLICATION}"
111 | ip route add default via "${default_gateway_ip}" table "${incoming_ports_ext_item}_${APPLICATION}"
112 |
113 | done
114 |
115 | fi
116 |
117 | # input iptable rules
118 | ###
119 |
120 | # loop over docker adapters
121 | for docker_network in ${DOCKER_NETWORKING}; do
122 |
123 | # read in DOCKER_NETWORKING from tools.sh
124 | docker_interface="$(echo "${docker_network}" | cut -d ',' -f 1 )"
125 | docker_network_cidr="$(echo "${docker_network}" | cut -d ',' -f 6 )"
126 |
127 | # accept input to/from docker containers (172.x range is internal dhcp)
128 | iptables -A INPUT -s "${docker_network_cidr}" -d "${docker_network_cidr}" -j ACCEPT
129 |
130 | for vpn_remote_ip_item in "${vpn_remote_ip_array[@]}"; do
131 |
132 | # note grep -e is required to indicate no flags follow to prevent -A from being incorrectly picked up
133 | rule_exists=$(iptables -S | grep -e "-A INPUT -i ${docker_interface} -s ${vpn_remote_ip_item} -j ACCEPT")
134 |
135 | if [[ -z "${rule_exists}" ]]; then
136 |
137 | # return rule
138 | iptables -A INPUT -i "${docker_interface}" -s "${vpn_remote_ip_item}" -j ACCEPT
139 |
140 | fi
141 |
142 | done
143 |
144 | done
145 |
146 | # loop over docker adapters
147 | for docker_network in ${DOCKER_NETWORKING}; do
148 |
149 | # read in DOCKER_NETWORKING from tools.sh
150 | docker_interface="$(echo "${docker_network}" | cut -d ',' -f 1 )"
151 |
152 | for incoming_ports_ext_item in "${incoming_ports_ext_array[@]}"; do
153 |
154 | for vpn_remote_protocol_item in "${multi_protocol_array[@]}"; do
155 |
156 | # allows communication from any ip (ext or lan) to containers running in vpn network on specific ports
157 | iptables -A INPUT -i "${docker_interface}" -p "${vpn_remote_protocol_item}" --dport "${incoming_ports_ext_item}" -j ACCEPT
158 |
159 | done
160 |
161 | done
162 |
163 | done
164 |
165 | # loop over docker adapters
166 | for docker_network in ${DOCKER_NETWORKING}; do
167 |
168 | # read in DOCKER_NETWORKING from tools.sh
169 | docker_interface="$(echo "${docker_network}" | cut -d ',' -f 1 )"
170 | docker_network_cidr="$(echo "${docker_network}" | cut -d ',' -f 6 )"
171 |
172 | # process lan networks in the array
173 | for lan_network_item in "${lan_network_array[@]}"; do
174 |
175 | # strip whitespace from start and end of lan_network_item
176 | lan_network_item=$(echo "${lan_network_item}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
177 |
178 | for incoming_ports_lan_item in "${incoming_ports_lan_array[@]}"; do
179 |
180 | # allows communication from lan ip to containers running in vpn network on specific ports
181 | iptables -A INPUT -i "${docker_interface}" -s "${lan_network_item}" -d "${docker_network_cidr}" -p tcp --dport "${incoming_ports_lan_item}" -j ACCEPT
182 |
183 | done
184 |
185 | for outgoing_ports_lan_item in "${outgoing_ports_lan_array[@]}"; do
186 |
187 | # return rule
188 | iptables -A INPUT -i "${docker_interface}" -s "${lan_network_item}" -d "${docker_network_cidr}" -p tcp --sport "${outgoing_ports_lan_item}" -j ACCEPT
189 |
190 | done
191 |
192 | done
193 |
194 | done
195 |
196 | # accept input icmp (ping)
197 | iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT
198 |
199 | # accept input to local loopback
200 | iptables -A INPUT -i lo -j ACCEPT
201 |
202 | # accept input to tunnel adapter
203 | iptables -A INPUT -i "${VPN_DEVICE_TYPE}" -j ACCEPT
204 |
205 | # output iptable rules
206 | ###
207 |
208 | # loop over docker adapters
209 | for docker_network in ${DOCKER_NETWORKING}; do
210 |
211 | # read in DOCKER_NETWORKING from tools.sh
212 | docker_network_cidr="$(echo "${docker_network}" | cut -d ',' -f 6 )"
213 |
214 | # accept output to/from docker containers (172.x range is internal dhcp)
215 | iptables -A OUTPUT -s "${docker_network_cidr}" -d "${docker_network_cidr}" -j ACCEPT
216 |
217 | done
218 |
219 | # loop over docker adapters
220 | for docker_network in ${DOCKER_NETWORKING}; do
221 |
222 | # read in DOCKER_NETWORKING from tools.sh
223 | docker_interface="$(echo "${docker_network}" | cut -d ',' -f 1 )"
224 |
225 | # iterate over remote ip address array (from start.sh) and create accept rules
226 | for vpn_remote_ip_item in "${vpn_remote_ip_array[@]}"; do
227 |
228 | # note grep -e is required to indicate no flags follow to prevent -A from being incorrectly picked up
229 | rule_exists=$(iptables -S | grep -e "-A OUTPUT -o ${docker_interface} -d ${vpn_remote_ip_item} -j ACCEPT")
230 |
231 | if [[ -z "${rule_exists}" ]]; then
232 |
233 | # accept output to remote vpn endpoint
234 | iptables -A OUTPUT -o "${docker_interface}" -d "${vpn_remote_ip_item}" -j ACCEPT
235 |
236 | fi
237 |
238 | done
239 |
240 | done
241 |
242 | # if iptable mangle is available (kernel module) then use mark
243 | if [[ "${iptable_mangle_exit_code}" == 0 ]]; then
244 |
245 | mark=0
246 |
247 | for incoming_ports_ext_item in "${incoming_ports_ext_array[@]}"; do
248 |
249 | mark=$((mark+1))
250 | # accept output from application - used for external access
251 | iptables -t mangle -A OUTPUT -p tcp --sport "${incoming_ports_ext_item}" -j MARK --set-mark "${mark}"
252 |
253 | done
254 |
255 | fi
256 |
257 | # loop over docker adapters
258 | for docker_network in ${DOCKER_NETWORKING}; do
259 |
260 | # read in DOCKER_NETWORKING from tools.sh
261 | docker_interface="$(echo "${docker_network}" | cut -d ',' -f 1 )"
262 |
263 | for incoming_ports_ext_item in "${incoming_ports_ext_array[@]}"; do
264 |
265 | for vpn_remote_protocol_item in "${multi_protocol_array[@]}"; do
266 |
267 | # return rule
268 | iptables -A OUTPUT -o "${docker_interface}" -p "${vpn_remote_protocol_item}" --sport "${incoming_ports_ext_item}" -j ACCEPT
269 |
270 | done
271 |
272 | done
273 |
274 | done
275 |
276 | # loop over docker adapters
277 | for docker_network in ${DOCKER_NETWORKING}; do
278 |
279 | # read in DOCKER_NETWORKING from tools.sh
280 | docker_interface="$(echo "${docker_network}" | cut -d ',' -f 1 )"
281 | docker_network_cidr="$(echo "${docker_network}" | cut -d ',' -f 6 )"
282 |
283 | # process lan networks in the array
284 | for lan_network_item in "${lan_network_array[@]}"; do
285 |
286 | # strip whitespace from start and end of lan_network_item
287 | lan_network_item=$(echo "${lan_network_item}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
288 |
289 | for incoming_ports_lan_item in "${incoming_ports_lan_array[@]}"; do
290 |
291 | # return rule
292 | iptables -A OUTPUT -o "${docker_interface}" -s "${docker_network_cidr}" -d "${lan_network_item}" -p tcp --sport "${incoming_ports_lan_item}" -j ACCEPT
293 |
294 | done
295 |
296 | for outgoing_ports_lan_item in "${outgoing_ports_lan_array[@]}"; do
297 |
298 | # allows communication from vpn network to containers running in lan network on specific ports
299 | iptables -A OUTPUT -o "${docker_interface}" -s "${docker_network_cidr}" -d "${lan_network_item}" -p tcp --dport "${outgoing_ports_lan_item}" -j ACCEPT
300 |
301 | done
302 |
303 | done
304 |
305 | done
306 |
307 | # accept output for icmp (ping)
308 | iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT
309 |
310 | # accept output from local loopback adapter
311 | iptables -A OUTPUT -o lo -j ACCEPT
312 |
313 | # accept output from tunnel adapter
314 | iptables -A OUTPUT -o "${VPN_DEVICE_TYPE}" -j ACCEPT
315 |
316 | echo "[info] iptables defined as follows..."
317 | echo "--------------------"
318 | iptables -S 2>&1 | tee /tmp/getiptables
319 | chmod +r /tmp/getiptables
320 | echo "--------------------"
321 |
--------------------------------------------------------------------------------
/run/root/openvpn.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | function create_openvpn_cli() {
4 |
5 | # define common command lne parameters for openvpn
6 | openvpn_cli="/usr/bin/openvpn --reneg-sec 0 --mute-replay-warnings --auth-nocache --setenv VPN_PROV '${VPN_PROV}' --setenv VPN_CLIENT '${VPN_CLIENT}' --setenv DEBUG '${DEBUG}' --setenv VPN_DEVICE_TYPE '${VPN_DEVICE_TYPE}' --setenv VPN_ENABLED '${VPN_ENABLED}' --setenv VPN_REMOTE_SERVER '${VPN_REMOTE_SERVER}' --setenv APPLICATION '${APPLICATION}' --script-security 2 --writepid /root/openvpn.pid --remap-usr1 SIGHUP --log-append /dev/stdout --pull-filter ignore 'up' --pull-filter ignore 'down' --pull-filter ignore 'route-ipv6' --pull-filter ignore 'ifconfig-ipv6' --pull-filter ignore 'tun-ipv6' --pull-filter ignore 'dhcp-option DNS6' --pull-filter ignore 'persist-tun' --pull-filter ignore 'reneg-sec' --up /root/openvpnup.sh --up-delay --up-restart"
7 |
8 | if [[ -z "${vpn_ping}" ]]; then
9 |
10 | # if no ping options in the ovpn file then specify keepalive option
11 | openvpn_cli="${openvpn_cli} --keepalive 10 60"
12 |
13 | fi
14 |
15 | if [[ "${VPN_PROV}" == "pia" || "${VPN_PROV}" == "protonvpn" ]]; then
16 |
17 | # add pia specific flags
18 | openvpn_cli="${openvpn_cli} --setenv STRICT_PORT_FORWARD '${STRICT_PORT_FORWARD}' --setenv VPN_USER '${VPN_USER}' --setenv VPN_PASS '${VPN_PASS}' --down /root/openvpndown.sh --disable-occ"
19 |
20 | fi
21 |
22 | if [[ ! -z "${VPN_USER}" && ! -z "${VPN_PASS}" ]]; then
23 |
24 | # add additional flags to pass credentials
25 | openvpn_cli="${openvpn_cli} --auth-user-pass credentials.conf"
26 |
27 | fi
28 |
29 | if [[ ! -z "${VPN_OPTIONS}" ]]; then
30 |
31 | # add additional flags to openvpn cli
32 | # note do not single/double quote the variable VPN_OPTIONS
33 | openvpn_cli="${openvpn_cli} ${VPN_OPTIONS}"
34 |
35 | fi
36 |
37 | # finally add options specified in ovpn file
38 | openvpn_cli="${openvpn_cli} --cd /config/openvpn --config '${VPN_CONFIG}'"
39 |
40 | }
41 |
42 | function add_remote_server_ip() {
43 |
44 | # check answer is not blank, generated in start.sh, if it is blank assume bad ns or vpn remote is an ip address
45 | if [[ ! -z "${VPN_REMOTE_IP}" ]]; then
46 |
47 | # iterate through list of ip addresses and add each ip as a --remote option to ${openvpn_cli}
48 | for vpn_remote_ip_item in "${vpn_remote_ip_array[@]}"; do
49 | openvpn_cli="${openvpn_cli} --remote ${vpn_remote_ip_item} ${VPN_REMOTE_PORT} ${VPN_REMOTE_PROTOCOL}"
50 | done
51 |
52 | # randomize the --remote option that openvpn will use to connect. this should help
53 | # prevent getting stuck on a particular server should it become unstable/unavailable
54 | openvpn_cli="${openvpn_cli} --remote-random"
55 |
56 | fi
57 |
58 | }
59 |
60 | function run_openvpn() {
61 |
62 | create_openvpn_cli
63 | add_remote_server_ip
64 |
65 | if [[ "${DEBUG}" == "true" ]]; then
66 | echo "[debug] OpenVPN command line:- ${openvpn_cli}"
67 | fi
68 |
69 | echo "[info] Starting OpenVPN (non daemonised)..."
70 | eval "${openvpn_cli}"
71 |
72 | }
73 |
74 | function watchdog() {
75 |
76 | # loop and watch out for files generated by user nobody scripts that indicate failure
77 | while true; do
78 |
79 | # reset flag, used to indicate connection status
80 | down="false"
81 |
82 | # if '/tmp/portclosed' file exists (generated by /home/nobody/watchdog.sh when incoming port
83 | # detected as closed) then terminate openvpn
84 | if [ -f "/tmp/portclosed" ];then
85 |
86 | echo "[info] Sending SIGTERM (-15) to 'openvpn' due to port closed..."
87 | down="true"
88 | rm -f "/tmp/portclosed"
89 |
90 | fi
91 |
92 | # if '/tmp/dnsfailure' file exists (generated by tools.sh when dns fails)
93 | # then terminate openvpn
94 | if [ -f "/tmp/dnsfailure" ];then
95 |
96 | echo "[info] Sending SIGTERM (-15) to 'openvpn' due to dns failure..."
97 | down="true"
98 | rm -f "/tmp/dnsfailure"
99 |
100 | fi
101 |
102 | # if '/tmp/portfailure' file exists (generated by tools.sh when incoming port
103 | # allocation fails) then terminate openvpn
104 | if [ -f "/tmp/portfailure" ];then
105 |
106 | echo "[info] Sending SIGTERM (-15) to 'openvpn' due to incoming port allocation failure..."
107 | down="true"
108 | rm -f "/tmp/portfailure"
109 |
110 | fi
111 |
112 | if [ "${down}" == "true" ]; then
113 |
114 | if [ -f '/tmp/endpoints' ]; then
115 |
116 | # read in associative array of endpint names and ip addresses from file created from function resolve_vpn_endpoints in tools.sh
117 | source '/tmp/endpoints'
118 |
119 | for i in "${!vpn_remote_array[@]}"; do
120 |
121 | endpoint_name="${i}"
122 | endpoint_ip_array=( "${vpn_remote_array[$i]}" )
123 |
124 | # run function to round robin the endpoint ip and write to /etc/hosts
125 | round_robin_endpoint_ip "${endpoint_name}" "${endpoint_ip_array[@]}"
126 |
127 | done
128 |
129 | fi
130 |
131 | fi
132 |
133 | # if flagged by above scripts then down vpn tunnel
134 | if [ "${down}" == "true" ]; then
135 | pkill -SIGTERM "openvpn"
136 | fi
137 |
138 | sleep 30s
139 |
140 | done
141 |
142 | }
143 |
144 | function start_openvpn() {
145 |
146 | # set sleep period for recheck (in secs)
147 | sleep_period_secs="30"
148 |
149 | # split comma separated string into array from VPN_REMOTE_SERVER env var
150 | IFS=',' read -ra vpn_remote_server_list <<< "${VPN_REMOTE_SERVER}"
151 |
152 | # split comma separated string into array from VPN_REMOTE_PORT env var
153 | IFS=',' read -ra vpn_remote_port_list <<< "${VPN_REMOTE_PORT}"
154 |
155 | # split comma separated string into array from VPN_REMOTE_PROTOCOL env var
156 | IFS=',' read -ra vpn_remote_protocol_list <<< "${VPN_REMOTE_PROTOCOL}"
157 |
158 | # convert list of ip's back into an array (cannot export arrays in bash)
159 | IFS=' ' read -ra vpn_remote_ip_array <<< "${VPN_REMOTE_IP_LIST}"
160 |
161 | # setup ip tables and routing for application
162 | source /root/iptable.sh
163 |
164 | # start background watchdog function
165 | watchdog &
166 |
167 | # loop back around to top if run out of vpn remote servers
168 | while true; do
169 |
170 | # iterate over arrays and send to start_openvpn_cli function (blocking until openvpn process dies)
171 | for index in "${!vpn_remote_port_list[@]}"; do
172 |
173 | # required as this is passed via openvpn setenv to tools.sh script
174 | # (checks endpoint is in list of port forward enabled endpoints)
175 | VPN_REMOTE_SERVER="${vpn_remote_server_list[$index]}"
176 |
177 | VPN_REMOTE_PORT="${vpn_remote_port_list[$index]}"
178 | VPN_REMOTE_PROTOCOL="${vpn_remote_protocol_list[$index]}"
179 | VPN_REMOTE_IP="${vpn_remote_ip_array[$index]}"
180 |
181 | if [[ "${DEBUG}" == "true" ]]; then
182 |
183 | echo "[debug] VPN remote configuration options as follows..."
184 | echo "[debug] VPN remote server is defined as '${VPN_REMOTE_SERVER}'"
185 | echo "[debug] VPN remote port is defined as '${VPN_REMOTE_PORT}'"
186 | echo "[debug] VPN remote protocol is defined as '${VPN_REMOTE_PROTOCOL}'"
187 | echo "[debug] VPN remote ip is defined as '${VPN_REMOTE_IP}'"
188 |
189 | fi
190 |
191 | run_openvpn
192 |
193 | done
194 |
195 | done
196 |
197 | }
198 |
199 | # source in resolve dns and round robin ip's from functions
200 | source tools.sh
201 |
202 | # start openvpn function
203 | start_openvpn
--------------------------------------------------------------------------------
/run/root/openvpndown.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | if [[ "${VPN_PROV}" == "pia" || "${VPN_PROV}" == "protonvpn" ]]; then
4 | if [ -f '/tmp/getvpnport.pid' ]; then
5 | # kill tools.sh/get_vpn_incoming_port on openvpn down, note use sig 15 not 2
6 | kill -15 $(cat '/tmp/getvpnport.pid') 2> /dev/null
7 | rm -f '/tmp/getvpnport.pid'
8 | fi
9 | fi
10 |
11 | # create file that denotes tunnel as down to prevent dns resolution check
12 | touch '/tmp/tunneldown' && chmod +r '/tmp/tunneldown'
13 |
--------------------------------------------------------------------------------
/run/root/openvpnup.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # remove file that denotes tunnel is down (from openvpndown.sh)
4 | rm -f '/tmp/tunneldown'
5 |
6 | # run scripts to get tunnel ip, check dns, get external ip, and get incoming port
7 | # note needs to be run in background, otherwise it blocks openvpn
8 | /root/prerunget.sh &
9 |
--------------------------------------------------------------------------------
/run/root/prerunget.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # script to call multiple scripts in series to read and then write out values
4 |
5 | # source in various tools
6 | source tools.sh
7 |
8 | # blocking function, will wait for valid ip address assigned to tun0/tap0 (port written to file /tmp/getvpnip)
9 | get_vpn_gateway_ip
10 |
11 | # blocking function, will wait for name resolution to be operational (will write to /tmp/dnsfailure if failure)
12 | check_dns www.google.com
13 |
14 | # blocking function, will wait for external ip address retrieval (external ip written to file /tmp/getvpnextip)
15 | get_vpn_external_ip
16 |
17 | # pia|protonvpn only - backgrounded function, will wait for vpn incoming port to be assigned (port written to file /tmp/getvpnport)
18 | # note backgrounded as running in infinite loop to check for port assignment
19 | get_vpn_incoming_port &
20 |
--------------------------------------------------------------------------------
/run/root/start.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # if vpn set to "no" then don't run openvpn
4 | if [[ "${VPN_ENABLED}" == "no" ]]; then
5 |
6 | echo "[info] VPN not enabled, skipping configuration of VPN"
7 |
8 | else
9 |
10 | echo "[info] VPN is enabled, beginning configuration of VPN"
11 |
12 | if [[ "${VPN_CLIENT}" == "openvpn" ]]; then
13 |
14 | # if vpn username and password specified then write credentials to file (authentication maybe via keypair)
15 | if [[ -n "${VPN_USER}" && -n "${VPN_PASS}" ]]; then
16 |
17 | # store credentials in separate file for authentication
18 | if ! grep -Fq "auth-user-pass credentials.conf" "${VPN_CONFIG}"; then
19 | sed -i -e 's/auth-user-pass.*/auth-user-pass credentials.conf/g' "${VPN_CONFIG}"
20 | fi
21 |
22 | echo "${VPN_USER}" > /config/openvpn/credentials.conf
23 |
24 | username_char_check=$(echo "${VPN_USER}" | grep -P -o -m 1 '[^a-zA-Z0-9@]+')
25 |
26 | if [[ -n "${username_char_check}" ]]; then
27 | echo "[warn] Username contains characters which could cause authentication issues, please consider changing this if possible"
28 | fi
29 |
30 | echo "${VPN_PASS}" >> /config/openvpn/credentials.conf
31 |
32 | password_char_check=$(echo "${VPN_PASS}" | grep -P -o -m 1 '[^a-zA-Z0-9@]+')
33 |
34 | if [[ -n "${password_char_check}" ]]; then
35 | echo "[warn] Password contains characters which could cause authentication issues, please consider changing this if possible"
36 | fi
37 |
38 | fi
39 |
40 | # note - do not remove redirection of gateway for ipv6 - required for certain vpn providers (airvpn)
41 |
42 | # remove keysize from ovpn file if present, deprecated and now removed option
43 | sed -i '/^keysize.*/d' "${VPN_CONFIG}"
44 |
45 | # remove ncp-disable from ovpn file if present, deprecated and now removed option
46 | sed -i '/^ncp-disable/d' "${VPN_CONFIG}"
47 |
48 | # remove persist-tun from ovpn file if present, this allows reconnection to tunnel on disconnect
49 | sed -i '/^persist-tun/d' "${VPN_CONFIG}"
50 |
51 | # remove reneg-sec from ovpn file if present, this is removed to prevent re-checks and dropouts
52 | sed -i '/^reneg-sec.*/d' "${VPN_CONFIG}"
53 |
54 | # remove up script from ovpn file if present, this is removed as we do not want any other up/down scripts to run
55 | sed -i '/^up\s.*/d' "${VPN_CONFIG}"
56 |
57 | # remove down script from ovpn file if present, this is removed as we do not want any other up/down scripts to run
58 | sed -i '/^down\s.*/d' "${VPN_CONFIG}"
59 |
60 | # remove ipv6 configuration from ovpn file if present (iptables not configured to support ipv6)
61 | sed -i '/^route-ipv6/d' "${VPN_CONFIG}"
62 |
63 | # remove ipv6 configuration from ovpn file if present (iptables not configured to support ipv6)
64 | sed -i '/^ifconfig-ipv6/d' "${VPN_CONFIG}"
65 |
66 | # remove ipv6 configuration from ovpn file if present (iptables not configured to support ipv6)
67 | sed -i '/^tun-ipv6/d' "${VPN_CONFIG}"
68 |
69 | # remove dhcp option for dns ipv6 configuration from ovpn file if present (dns defined via name_server env var value)
70 | sed -i '/^dhcp-option DNS6.*/d' "${VPN_CONFIG}"
71 |
72 | # remove windows specific openvpn options
73 | sed -i '/^route-method exe/d' "${VPN_CONFIG}"
74 | sed -i '/^service\s.*/d' "${VPN_CONFIG}"
75 | sed -i '/^block-outside-dns/d' "${VPN_CONFIG}"
76 |
77 | if [[ "${DEBUG}" == "true" ]]; then
78 | echo "[debug] Contents of ovpn file ${VPN_CONFIG} as follows..." ; cat "${VPN_CONFIG}"
79 | fi
80 |
81 | # assign any matching ping options in ovpn file to variable (used to decide whether to specify --keealive option in openvpn.sh)
82 | vpn_ping=$(grep -P -o -m 1 '^ping.*' < "${VPN_CONFIG}")
83 |
84 | # forcibly set virtual network device to 'tun0/tap0' (referenced in iptables)
85 | sed -i "s/^dev\s${VPN_DEVICE_TYPE}.*/dev ${VPN_DEVICE_TYPE}/g" "${VPN_CONFIG}"
86 |
87 | fi
88 |
89 | if [[ "${DEBUG}" == "true" ]]; then
90 |
91 | echo "[debug] Environment variables defined as follows" ; set
92 |
93 | if [[ "${VPN_CLIENT}" == "openvpn" ]]; then
94 |
95 | echo "[debug] Directory listing of files in /config/openvpn/ as follows" ; ls -al '/config/openvpn'
96 | echo "[debug] Contents of OpenVPN config file '${VPN_CONFIG}' as follows..." ; cat "${VPN_CONFIG}"
97 |
98 | else
99 |
100 | echo "[debug] Directory listing of files in /config/wireguard/ as follows" ; ls -al '/config/wireguard'
101 |
102 | if [ -f "${VPN_CONFIG}" ]; then
103 | echo "[debug] Contents of WireGuard config file '${VPN_CONFIG}' as follows..." ; cat "${VPN_CONFIG}"
104 | else
105 | echo "[debug] File path '${VPN_CONFIG}' does not exist, skipping displaying file content"
106 | fi
107 |
108 | fi
109 |
110 | fi
111 |
112 | # workaround for pia CRL issue
113 | if [[ "${VPN_CLIENT}" == "openvpn" ]]; then
114 |
115 | if [[ "${VPN_PROV}" == "pia" ]]; then
116 |
117 | # turn off compression, required to bypass pia crl-verify issue with pia
118 | # see https://github.com/binhex/arch-qbittorrentvpn/issues/233
119 | sed -i -e 's~^compress~comp-lzo no~g' "${VPN_CONFIG}"
120 |
121 | # remove crl-verify as pia verification has invalid date
122 | # see https://github.com/binhex/arch-qbittorrentvpn/issues/233
123 | sed -i '//,/<\/crl-verify>/d' "${VPN_CONFIG}"
124 |
125 | fi
126 |
127 | fi
128 |
129 | if [[ "${VPN_CLIENT}" == "openvpn" ]]; then
130 |
131 | # check if we have tun module available
132 | check_tun_available=$(lsmod | grep tun)
133 |
134 | # if tun module not available then try installing it
135 | if [[ -z "${check_tun_available}" ]]; then
136 | echo "[info] Attempting to load tun kernel module..."
137 | /sbin/modprobe tun
138 | tun_module_exit_code=$?
139 | if [[ $tun_module_exit_code != 0 ]]; then
140 | echo "[warn] Unable to load tun kernel module using modprobe, trying insmod..."
141 | insmod /lib/modules/tun.ko
142 | tun_module_exit_code=$?
143 | if [[ $tun_module_exit_code != 0 ]]; then
144 | echo "[warn] Unable to load tun kernel module, assuming its dynamically loaded"
145 | fi
146 | fi
147 | fi
148 |
149 | # create the tunnel device if not present (unraid users do not require this step)
150 | mkdir -p /dev/net
151 | [ -c "/dev/net/tun" ] || mknod "/dev/net/tun" c 10 200
152 | tun_create_exit_code=$?
153 | if [[ $tun_create_exit_code != 0 ]]; then
154 | echo "[crit] Unable to create tun device, try adding docker container option '--device=/dev/net/tun'" ; exit 1
155 | else
156 | chmod 600 /dev/net/tun
157 | fi
158 |
159 | fi
160 |
161 | # check if we have iptable_mangle module available
162 | check_mangle_available=$(lsmod | grep iptable_mangle)
163 |
164 | # if mangle module not available then try installing it
165 | if [[ -z "${check_mangle_available}" ]]; then
166 | echo "[info] Attempting to load iptable_mangle module..."
167 | /sbin/modprobe iptable_mangle
168 | mangle_module_exit_code=$?
169 | if [[ $mangle_module_exit_code != 0 ]]; then
170 | echo "[warn] Unable to load iptable_mangle module using modprobe, trying insmod..."
171 | insmod /lib/modules/iptable_mangle.ko
172 | mangle_module_exit_code=$?
173 | if [[ $mangle_module_exit_code != 0 ]]; then
174 | echo "[warn] Unable to load iptable_mangle module, you will not be able to connect to the applications Web UI or Privoxy outside of your LAN"
175 | echo "[info] unRAID/Ubuntu users: Please attempt to load the module by executing the following on your host: '/sbin/modprobe iptable_mangle'"
176 | echo "[info] Synology users: Please attempt to load the module by executing the following on your host: 'insmod /lib/modules/iptable_mangle.ko'"
177 | fi
178 | fi
179 | fi
180 |
181 | if [[ "${DEBUG}" == "true" ]]; then
182 | echo "[debug] Show name servers defined for container" ; cat /etc/resolv.conf
183 |
184 | # iterate over array of remote servers
185 | for index in "${!vpn_remote_server_list[@]}"; do
186 | echo "[debug] Show name resolution for VPN endpoint ${vpn_remote_server_list[$index]}" ; drill -a "${vpn_remote_server_list[$index]}"
187 | done
188 |
189 | echo "[debug] Show contents of hosts file" ; cat /etc/hosts
190 | fi
191 |
192 | if [[ "${VPN_CLIENT}" == "openvpn" ]]; then
193 |
194 | # start openvpn client
195 | # shellcheck source=./openvpn.sh
196 | source /root/openvpn.sh
197 |
198 | elif [[ "${VPN_CLIENT}" == "wireguard" ]]; then
199 |
200 | # start wireguard client
201 | # shellcheck source=./wireguard.sh
202 | source /root/wireguard.sh
203 |
204 | fi
205 |
206 | fi
207 |
--------------------------------------------------------------------------------
/run/root/wireguard.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | function pia_create_wireguard_keys() {
4 |
5 | # create ephemeral wireguard private and public keys
6 | wireguard_private_key=$(wg genkey)
7 | wireguard_public_key=$(echo "${wireguard_private_key}" | wg pubkey)
8 |
9 | }
10 |
11 | function pia_wireguard_authenticate() {
12 |
13 | # authenticate via the pia wireguard restful api
14 | # this will return json with data required for authentication.
15 | echo "[info] Trying to connect to the PIA WireGuard API on '${VPN_REMOTE_SERVER}'..."
16 | pia_wireguard_authentication_json=$(curl --silent --get --insecure --data-urlencode "pt=${PIA_GENERATE_TOKEN}" --data-urlencode "pubkey=${wireguard_public_key}" "https://${VPN_REMOTE_SERVER}:1337/addKey")
17 |
18 | }
19 |
20 | function pia_get_wireguard_config() {
21 |
22 | pia_wireguard_peer_ip=$(echo "${pia_wireguard_authentication_json}" | jq -r '.peer_ip')
23 | pia_wireguard_server_key=$(echo "$pia_wireguard_authentication_json" | jq -r '.server_key')
24 |
25 | # commented line below is legacy method for getting server port, now moved to init.sh,
26 | # but keeping the below line in case we need to switch to the previous method
27 | #pia_wireguard_server_port=$(echo "$pia_wireguard_authentication_json" | jq -r '.server_port')
28 |
29 | # this is the gateway ip for wireguard, this is required in tools.sh, which is called
30 | # as part of the wireguardup.sh.
31 | export VPN_GATEWAY_IP=$(echo "$pia_wireguard_authentication_json" | jq -r '.server_vip')
32 |
33 | if [[ "${DEBUG}" == "true" ]]; then
34 |
35 | echo "[debug] PIA WireGuard 'peer ip' is '${pia_wireguard_peer_ip}'"
36 | echo "[debug] PIA WireGuard 'server key' is '${pia_wireguard_server_key}'"
37 | echo "[debug] PIA WireGuard 'server vip' (gsteway) is '${VPN_GATEWAY_IP}'"
38 |
39 | fi
40 |
41 | }
42 |
43 | function pia_create_wireguard_config_file() {
44 |
45 | # get pia wireguard server ip address for hostname using hosts
46 | # file lookup (hosts file entry created in start.sh)
47 | #pia_wireguard_server_ip=$(getent hosts "${VPN_REMOTE_SERVER}" | awk '{ print $1 }')
48 |
49 | cat < "${VPN_CONFIG}"
50 |
51 | [Interface]
52 | Address = ${pia_wireguard_peer_ip}
53 | PrivateKey = ${wireguard_private_key}
54 | PostUp = '/root/wireguardup.sh'
55 | PostDown = '/root/wireguarddown.sh'
56 |
57 | [Peer]
58 | PublicKey = ${pia_wireguard_server_key}
59 | AllowedIPs = 0.0.0.0/0
60 | Endpoint = ${VPN_REMOTE_SERVER}:${VPN_REMOTE_PORT}
61 |
62 | EOF
63 |
64 | }
65 |
66 | function watchdog() {
67 |
68 | # loop and watch out for files generated by user nobody scripts that indicate failure
69 | while true; do
70 |
71 | # reset flag, used to indicate connection status
72 | down="false"
73 |
74 | # if '/tmp/portclosed' file exists (generated by /home/nobody/watchdog.sh when incoming port
75 | # detected as closed) then down wireguard
76 | if [ -f "/tmp/portclosed" ]; then
77 |
78 | echo "[info] Sending 'down' command to WireGuard due to port closed..."
79 | down="true"
80 | rm -f "/tmp/portclosed"
81 |
82 | fi
83 |
84 | # if '/tmp/dnsfailure' file exists (generated by tools.sh when dns fails)
85 | # then down wireguard
86 | if [ -f "/tmp/dnsfailure" ]; then
87 |
88 | echo "[info] Sending 'down' command to WireGuard due to dns failure..."
89 | down="true"
90 | rm -f "/tmp/dnsfailure"
91 |
92 | fi
93 |
94 | # if '/tmp/portfailure' file exists (generated by tools.sh/get_vpn_incoming_port when incoming port
95 | # allocation fails) then down wireguard
96 | if [ -f "/tmp/portfailure" ]; then
97 |
98 | echo "[info] Sending 'down' command to WireGuard due to incoming port allocation failure..."
99 | down="true"
100 | rm -f "/tmp/portfailure"
101 |
102 | fi
103 |
104 | if [ "${down}" == "true" ]; then
105 |
106 | if [ -f '/tmp/endpoints' ]; then
107 |
108 | # read in associative array of endpint names and ip addresses from file created from function resolve_vpn_endpoints in tools.sh
109 | source '/tmp/endpoints'
110 |
111 | for i in "${!vpn_remote_array[@]}"; do
112 |
113 | endpoint_name="${i}"
114 | endpoint_ip_array=( "${vpn_remote_array[$i]}" )
115 |
116 | # run function to round robin the endpoint ip and write to /etc/hosts
117 | round_robin_endpoint_ip "${endpoint_name}" "${endpoint_ip_array[@]}"
118 |
119 | done
120 |
121 | fi
122 |
123 | fi
124 |
125 | # check if wireguard 'peer' exists, if not assume wireguard connection is down and bring up
126 | if ! wg show | grep --quiet 'peer'; then
127 | echo "[info] WireGuard 'peer' not found, attempting to cycle WireGuard interface..."
128 | down="true"
129 | fi
130 |
131 | # if flagged by above scripts then cycle vpn tunnel
132 | if [ "${down}" == "true" ]; then
133 | run_wireguard 'down'
134 | run_wireguard 'up'
135 |
136 | fi
137 |
138 | sleep 30s
139 |
140 | done
141 |
142 | }
143 |
144 | function edit_wireguard() {
145 |
146 | # delete any existing PostUp/PostDown scripts (cannot easily edit and replace lines without insertion)
147 | sed -i -r '/.*PostUp = .*|.*PostDown = .*/d' "${VPN_CONFIG}"
148 |
149 | # insert PostUp/PostDown script lines after [Interface]
150 | sed -i -e "/\[Interface\]/a PostUp = '/root/wireguardup.sh'\nPostDown = '/root/wireguarddown.sh'" "${VPN_CONFIG}"
151 |
152 | # removes all ipv6 address and port from wireguard config
153 | sed -r -i -e 's/,?(\s+)?[a-f0-9]{4}::?[^,]+(\s+)?,?//g' "${VPN_CONFIG}"
154 |
155 | # removes all ipv6 port only from wireguard config
156 | sed -r -i -e 's/,?(\s+)?::[^,]+(\s+)?,?//g' "${VPN_CONFIG}"
157 |
158 | }
159 |
160 | function run_wireguard() {
161 |
162 | wireguard_action="${1}"
163 |
164 | if [[ "${wireguard_action}" == 'up' ]]; then
165 | configure_wireguard
166 | fi
167 |
168 | echo "[info] Attempting to bring WireGuard interface '${wireguard_action}'..."
169 |
170 | if [[ "${USERSPACE_WIREGUARD}" == 'yes' ]]; then
171 | if [[ "${DEBUG}" == "true" ]]; then
172 | echo "[debug] Running WireGuard userspace implementation 'boringtun-cli'..."
173 | fi
174 | if ! boringtun-cli WG_SUDO=1 wg-quick "${wireguard_action}" "${VPN_CONFIG}"; then
175 | echo "[warn] Failed to bring '${wireguard_action}' WireGuard userspace implementation 'boringtun-cli'"
176 | return 1
177 | fi
178 | else
179 | if [[ "${DEBUG}" == "true" ]]; then
180 | echo "[debug] Running WireGuard kernel implementation..."
181 | fi
182 | if ! wg-quick "${wireguard_action}" "${VPN_CONFIG}"; then
183 | echo "[warn] Failed to bring '${wireguard_action}' WireGuard kernel implementation"
184 | return 1
185 | fi
186 | fi
187 |
188 | echo "[info] Successfully brought Wireguard interface '${wireguard_action}'"
189 | }
190 |
191 | function configure_wireguard() {
192 |
193 | echo "[info] Configuring WireGuard..."
194 |
195 | # if vpn provider is pia then get required dynamic configuration and write to wireguard config file
196 | if [[ "${VPN_PROV}" == "pia" ]]; then
197 |
198 | pia_create_wireguard_keys
199 | pia_generate_token
200 | pia_wireguard_authenticate
201 | pia_get_wireguard_config
202 | pia_create_wireguard_config_file
203 |
204 | else
205 |
206 | # edit wireguard config to remove ipv6, required for mullvad and possibly other non pia
207 | # vpn providers
208 | edit_wireguard
209 |
210 | fi
211 |
212 |
213 | }
214 |
215 | # source in resolve dns and round robin ip's from functions
216 | # shellcheck source=../local/tools.sh
217 | source tools.sh
218 |
219 | # setup ip tables and routing for application
220 | # shellcheck source=./iptable.sh
221 | source '/root/iptable.sh'
222 |
223 | # start watchdog function
224 | watchdog
225 |
--------------------------------------------------------------------------------
/run/root/wireguarddown.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | if [[ "${VPN_PROV}" == "pia" || "${VPN_PROV}" == "protonvpn" ]]; then
4 | if [ -f '/tmp/getvpnport.pid' ]; then
5 | # kill tools.sh/get_vpn_incoming_port on wireguard down, note use sig 15 not 2
6 | kill -15 $(cat '/tmp/getvpnport.pid') 2> /dev/null
7 | rm -f '/tmp/getvpnport.pid'
8 | fi
9 | fi
10 |
11 | # create file that denotes tunnel as down to prevent dns resolution check
12 | touch '/tmp/tunneldown' && chmod +r '/tmp/tunneldown'
13 |
--------------------------------------------------------------------------------
/run/root/wireguardup.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # remove file that denotes tunnel is down (from wireguarddown.sh)
4 | rm -f '/tmp/tunneldown'
5 |
6 | # run scripts to get tunnel ip, check dns, get external ip, and get incoming port
7 | # note do not background this script, otherwise you cannot kill the backgrounded
8 | # tools.sh/get_vpn_incoming_port function
9 | /root/prerunget.sh
10 |
--------------------------------------------------------------------------------