Crate ovpnfile [] [src]

A tiny library for parsing openvpn config files

openvpn config files have a very simple format. Arguments which would be passed to the openvpn command line as --<option> <arg1> <arg2> are placed on a line in the config file which looks like this option arg1 arg2. Additionally some options can use a pseudo XML syntax to include the contents of a file in the config file, for example

<cert>
-----BEGIN CERTIFICATE-----
[...]
-----END CERTIFICATE-----
</cert>

All of these options are documented here

This library represents each possible option as a separate variant of the ConfigDirective enum. Required arguments are are represented as Strings whilst optional arguments are represented as Option<String>. There are a few exceptions which I will mention shortly.

Usage

The entry point is the ovpnfile::parse function, which takes a Read containing the config file and returns a Result which if successful will be a ParsedConfigFile. Lines from the config file are represented as ConfigLine<T> entries, where T is the parse result for that line. For example

use std::io::{BufReader};
use ovpnfile::{ConfigDirective, ConfigLine, ParseWarning};
use ovpnfile;

let contents = r"
resolv-retry 10
remote somehost someport
unknown-command
".as_bytes();

let reader = BufReader::new(contents);
let result = ovpnfile::parse(reader).unwrap();
assert!(result.success_lines == vec![
    ConfigLine{number: 1, result: ConfigDirective::ResolvRetry{n: "10".to_string()}},
    ConfigLine{number: 2, result: ConfigDirective::Remote{
        host: "somehost".to_string(),
        port: Some("someport".to_string()),
        proto: None,
    }},
]);
assert!(result.warning_lines == vec![ConfigLine{number: 3, result: ParseWarning::NoMatchingCommand}]);

Lines which fail to parse either because the command is not recognized or there are missing required arguments for the command result in warning, as you can see from the above example.

Inline File Contents

As mentioned earlier some commands can include file contents inline in the config file. These commands are:

--ca
--ca
--cert
--extra-certs
--dh
--key
--pkcs12
--crl-verify
--http-proxy-user-pass
--tls-auth
--tls-crypt
--secret

The corresponding enum variants have a file record attribute which is an instance of File. File is either an InlineFileContents(String) or a FilePath(String). So for example

use std::io::{BufReader};
use ovpnfile::{ConfigDirective, File, ConfigLine};
use ovpnfile;

let contents = r"
tls-auth somefile somedirection
<tls-auth>
line1
line2
</tls-auth>
".as_bytes();

let reader = BufReader::new(contents);
let result = ovpnfile::parse(reader).unwrap();
assert!(result.success_lines == vec![
    ConfigLine{number: 1, result: ConfigDirective::TlsAuth{
        file: File::FilePath("somefile".to_string()),
        direction: Some("somedirection".to_string()),
    }},
    ConfigLine{number: 2, result: ConfigDirective::TlsAuth{
        file: File::InlineFileContents("line1\nline2".to_string()),
        direction: None,
    }},
]);

Server Bridge

The --server-bridge argument is special, it can take two forms

server-bridge gateway netmask pool-start-IP pool-end-IP
server-bridge nogw

This is represented in this library as the ServerBridgeArg enum variant, it can either be a NoGateway or GatewayConfig{gateway: String, netmask: String, pool_start_ip: String, pool_end_ip: String}.

Structs

ConfigLine

Represents a line of the config file, the type T will be either a ConfigDirective or a ParseWarning.

ParsedConfigFile

The result of the parse function

Enums

ConfigDirective

Represents one sucesfully parsed directive, there is a variant for each possible option.

File

Arguments for any option which can have inline file contents

ParseWarning

Possible warnings

ServerBridgeArg

Arguments of the --server-bridge option

Functions

parse

The entry point for this library. Pass a Read containing the config file and get back a ParsedConfigFile.