Last active
July 18, 2024 17:25
-
-
Save ofirule/8cb5765ca964cf751a0b2e2dc50a341e to your computer and use it in GitHub Desktop.
converting openvpn .conf file to .ovpn file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, os | |
ALLOW_FILE_OPTIONS = ["ca", "cert", "dh", "extra-certs", "key", "pkcs12", "secret", "crl-verify", "http-proxy-user-pass", "tls-auth", "tls-crypt"] | |
filepath = sys.argv[1] | |
output_file = os.path.basename(filepath). replace(".conf", ".ovpn") | |
inline_tuples_to_add = [] | |
with open(output_file, 'w') as dst: | |
with open(filepath) as src: | |
for l in src: | |
option = l.split() | |
if len(option) >= 2 and option[0] in ALLOW_FILE_OPTIONS and os.path.isfile(option[1]): | |
inline_tuples_to_add.append((option[0], option[1])) | |
continue | |
dst.write(l) | |
dst.write("key-direction 1\n\n") # needed fot tls-auth | |
for t in inline_tuples_to_add : | |
tag_begining = "<{}>\n".format(t[0]) | |
dst.write(tag_begining) | |
with open(t[1]) as tag_cpntent_file: | |
dst.writelines(tag_cpntent_file.readlines()) | |
tag_ending = "</{}>\n\n".format(t[0]) | |
dst.write(tag_ending) |
This line is problematic if the extension is not .conf
:
output_file = os.path.basename(filepath). replace(".conf", ".ovpn")
I would recommend doing the following:
output_file = os.path.splitext(filepath)[0] + ".ovpn"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Running example:
python convert_openvpn_conf_file_to_ovpn_file.py /etc/openvpn/client.conf
would create a .ovpn file on current working directory