Created
February 22, 2016 17:28
-
-
Save ABalanuta/20f1257c267fd9c768db to your computer and use it in GitHub Desktop.
Super simple .ovpn file generator for OpenVPN servers without openvpn-as installed to generate it.
This file contains hidden or 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, getopt | |
help = "ovpn.py --host <host> --ca <capath> --crt <certpath> --key <keypath> [--port <port>, --proto <proto>, --options <optionspath>]" | |
template = """ | |
dev tun | |
port {{PORT}} | |
proto {{PROTO}} | |
remote {{HOST}} | |
nobind | |
comp-lzo | |
persist-key | |
persist-tun | |
verb 3 | |
{{OPTIONS}} | |
<ca> | |
{{CA}} | |
</ca> | |
<cert> | |
{{CERT}} | |
</cert> | |
<key> | |
{{KEY}} | |
</key> | |
""" | |
def main(argv): | |
"""Super simple .ovpn file generator for OpenVPN servers without openvpn-as installed to generate it.""" | |
host = None | |
port = "1194" | |
proto = "udp" | |
ca = None | |
crt = None | |
key = None | |
options = None | |
try: | |
opts, args = getopt.getopt(argv, "hs:o:p:", ["host=", "ca=", "crt=", "key=", "options=", "proto=", "port=", "help"]) | |
except getopt.GetoptError: | |
print(help) | |
sys.exit(2) | |
for opt, arg in opts: | |
if opt in ("-h", "--help"): | |
print(help) | |
elif opt in ("-s", "--host"): | |
host = arg | |
elif opt in ("-p", "--port"): | |
port = int(arg) | |
elif opt == "--proto": | |
proto = arg | |
elif opt == "--ca": | |
ca = open(arg, "r").read() | |
elif opt == "--crt": | |
crt = open(arg, "r").read() | |
elif opt == "--key": | |
key = open(arg, "r").read() | |
elif opt in ("-o", "--options"): | |
options = open(arg, "r").read() | |
if not host or not ca or not crt or not key: | |
print(help) | |
for auth in [ca, crt, key]: | |
if not check_authenticity(auth): | |
raise Exception("Invalid key or certificate") | |
map = {"{{HOST}}": host, | |
"{{PORT}}": port, | |
"{{PROTO}}": proto, | |
"{{CA}}": ca, | |
"{{CERT}}": strip_crt(crt), | |
"{{KEY}}": key, | |
"{{OPTIONS}}": options} | |
ovpn = template | |
for key, value in map.iteritems(): | |
ovpn = ovpn.replace(key, value or "") | |
print ovpn | |
def strip_crt(crt): | |
"""Strips the header from crt file.""" | |
index = crt.index("-----BEGIN CERTIFICATE-----") | |
return crt[index:] | |
def check_authenticity(file): | |
"""Checks if the provided file is either a key or certificate. Much security!""" | |
return ("-----BEGIN CERTIFICATE-----" in file or "-----BEGIN PRIVATE KEY-----" in file) | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment