Skip to content

Commit 6d4df9c

Browse files
committed
ovpn_client added detection for tls-crypt or tls-auth
Added detection to `ovpn_client` to detect if `tls-crypt` or `tls-auth` is present in server config so it adds `ta.key` to generated client config.
1 parent 6fb11f1 commit 6d4df9c

File tree

3 files changed

+103
-4
lines changed

3 files changed

+103
-4
lines changed

root/app/bin/ovpn_client

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ function build_ovpn() {
3131
cat $file
3232
done
3333

34+
# Check if server config is using tls-crypt or tls-auth
35+
local crypto=""
36+
for srv_file in $OVPN_ROOT/openvpn/server/*.conf
37+
do
38+
crypto="$(ovpn_findopt $srv_file tls-crypt tls-auth)"
39+
if [ -n "$crypto" ]; then
40+
break
41+
fi
42+
done
43+
3444
# CA
3545
echo "<ca>"
3646
cat $EASYRSA_PKI/ca.crt
@@ -47,10 +57,17 @@ function build_ovpn() {
4757
cat $EASYRSA_PKI/private/$1.key
4858
echo "</key>"
4959

50-
# tls-crypt
51-
echo "<tls-crypt>"
52-
cat $EASYRSA_PKI/ta.key
53-
echo "</tls-crypt>"
60+
if [ "$crypto" = "tls-crypt" ]; then
61+
# tls-crypt
62+
echo "<tls-crypt>"
63+
cat $EASYRSA_PKI/ta.key
64+
echo "</tls-crypt>"
65+
elif [ "$crypto" = "tls-auth" ]; then
66+
# tls-auth
67+
echo "<tls-auth>"
68+
cat $EASYRSA_PKI/ta.key
69+
echo "</tls-auth>"
70+
fi
5471
fi
5572
}
5673

root/app/bin/ovpn_findopt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/python
2+
3+
#
4+
# Finds first of specified options in config file
5+
# @author Martin Dagarin
6+
# @version 1
7+
# @since 19/03/2019
8+
#
9+
# Usage: ovpn_findopt <config_file> <opt1> <opt2> <opt3> ...
10+
#
11+
import sys
12+
13+
# Import libraries included in this docker
14+
sys.path.insert(0, '/app/lib')
15+
import libovpn
16+
17+
if len(sys.argv) < 3:
18+
# Invalid command
19+
print("")
20+
sys.exit(0)
21+
22+
config_file = sys.argv[1]
23+
found = libovpn.conf_optFindFirst(config_file, sys.argv[2:])
24+
25+
if found is not None:
26+
print(found)
27+
else:
28+
print("")

root/app/lib/libovpn.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/python
2+
3+
#
4+
# Functions to help with scripting
5+
# @author Martin Dagarin
6+
# @version 1
7+
# @since 19/03/2019
8+
#
9+
10+
#
11+
# Checks if OpenVPN file has configuration option enabled
12+
# @param file Path to OpenVPN config file
13+
# @param opt Option to check for
14+
# @return Returns True if found, else false
15+
#
16+
17+
def conf_hasOpt(file, opt):
18+
with open(file, "r") as f:
19+
for line in f:
20+
line = line.strip()
21+
if line.startswith(opt):
22+
return True
23+
return False
24+
25+
#
26+
# Finds option which is enabled in config
27+
# @param file Path to OpenVPN config file
28+
# @param opts Array of options, which to check
29+
# @return Returns first options which was found or None
30+
#
31+
def conf_optFindFirst(file, opts):
32+
with open(file, "r") as f:
33+
for line in f:
34+
line = line.strip()
35+
for opt in opts:
36+
if line.startswith(opt):
37+
return opt
38+
return None
39+
40+
#
41+
# Replaces $VARIABLES in file with values
42+
# @param file Path to file
43+
# @param var Array of tuples with key value ("$KEY","VALUE")
44+
#
45+
def conf_envsubst(file, vars):
46+
with open(file, "r") as f:
47+
lines = f.readlines()
48+
with open(file, "w") as f:
49+
for line in lines:
50+
# Dont proces comments
51+
if not line.strip().startswith("#"):
52+
for kv in vars:
53+
line = line.replace(kv[0],kv[1])
54+
f.write(line)

0 commit comments

Comments
 (0)