项目作者: EvilFreelancer

项目描述 :
OpenVPN config generator/importer written on PHP
高级语言: PHP
项目地址: git://github.com/EvilFreelancer/openvpn-php.git
创建时间: 2018-01-30T20:04:01Z
项目社区:https://github.com/EvilFreelancer/openvpn-php

开源协议:MIT License

下载


Latest Stable Version
Build Status
Total Downloads
License
Code Climate
Code Coverage
Scrutinizer CQ

OpenVPN config manager

OpenVPN configuration manager written on PHP.

  1. composer require evilfreelancer/openvpn-php

By the way, OpenVPN library support Laravel framework, details here.

How to use

It’s very simple, you need to set the required parameters, then
generate the config and voila, everything is done.

More examples here.

Write new config in OOP style

  1. require_once __DIR__ . '/../vendor/autoload.php';
  2. // Config object
  3. $config = new \OpenVPN\Config();
  4. // Set server options
  5. $config->dev = 'tun';
  6. $config->proto = 'tcp';
  7. $config->port = 1194;
  8. $config->resolvRetry = 'infinite';
  9. $config->cipher = 'AES-256-CBC';
  10. $config->redirectGateway = true;
  11. $config->server = '10.8.0.0 255.255.255.0';
  12. $config->keepalive = '10 120';
  13. $config->renegSec = 18000;
  14. $config->user = 'nobody';
  15. $config->group = 'nogroup';
  16. $config->persistKey = true;
  17. $config->persistTun = true;
  18. $config->compLzo = true;
  19. $config->verb = 3;
  20. $config->mute = 20;
  21. $config->status = '/var/log/openvpn/status.log';
  22. $config->logAppend = '/var/log/openvpn/openvpn.log';
  23. $config->clientConfigDir = 'ccd';
  24. $config->scriptSecurity = 3;
  25. $config->usernameAsCommonName = true;
  26. $config->verifyClientCert = 'none';
  27. // Set routes which will be used by server after starting
  28. $config->setRoutes([
  29. '10.1.1.0 255.255.255.0',
  30. '10.1.2.0 255.255.255.0',
  31. '10.1.3.0 255.255.255.0',
  32. ]);
  33. // Set additional certificates of server
  34. $config->setCerts([
  35. 'ca' => '/etc/openvpn/keys/ca.crt',
  36. 'cert' => '/etc/openvpn/keys/issued/server.crt',
  37. ]); // You can embed certificates into config by adding true as second parameter of setCerts method
  38. // Another way for adding certificates
  39. $config
  40. ->setCert('key', '/etc/openvpn/keys/private/server.key')
  41. ->setCert('dh', '/etc/openvpn/keys/dh.pem');
  42. // Set pushes which will be passed to client
  43. $config->setPushes([
  44. // Additional routes, which clients will see
  45. 'route 10.1.2.0 255.255.255.0',
  46. 'route 10.1.3.0 255.255.255.0',
  47. 'route 10.1.4.0 255.255.255.0',
  48. // Replace default gateway, all client's traffic will be routed via VPN
  49. 'redirect-gateway def1',
  50. // Prepend additional DNS addresses
  51. 'dhcp-option DNS 8.8.8.8',
  52. 'dhcp-option DNS 8.8.4.4',
  53. ]);
  54. // Generate config by options
  55. echo $config->generate();

Import existing OpenVPN config

For example, you have server.conf, to import this file you need create
\OpenVPN\Import object and specify a name of your config file.

  1. <?php
  2. require_once __DIR__ . '/../vendor/autoload.php';
  3. // Import OpenVPN config file
  4. $import = new \OpenVPN\Import('server.conf');
  5. // or (classic way)
  6. $import = new \OpenVPN\Import();
  7. $import->read('server.conf');
  8. // Parse configuration and return "\OpenVPN\Config" object
  9. $config = $import->parse();

In $config variable will be \OpenVPN\Config object.

Client config example

For making client configuration you need just add required parameters
and generate the config:

  1. <?php
  2. require_once __DIR__ . '/../vendor/autoload.php';
  3. // Config object
  4. $config = new \OpenVPN\Config();
  5. // Set client options
  6. $config->client();
  7. $config->dev = 'tun';
  8. $config->proto = 'tcp';
  9. $config->resolvRetry = 'infinite';
  10. $config->cipher = 'AES-256-CB';
  11. $config->redirectGateway = true;
  12. $config->keyDirection = 1;
  13. $config->remoteCertTls = 'server';
  14. $config->authUserPass = true;
  15. $config->authNocache = true;
  16. $config->nobind = true;
  17. $config->persistKey = true;
  18. $config->persistTun = true;
  19. $config->compLzo = true;
  20. $config->verb = 3;
  21. $config->httpProxy = 'proxy-http.example.com 3128';
  22. // Set multiple remote servers
  23. $config->setRemotes([
  24. 'vpn1.example.com 1194',
  25. 'vpn2.example.com 11194'
  26. ]);
  27. // Set single remote
  28. $config->setRemote('vpn1.example.com 1194');
  29. // Or set remote server as parameter of object
  30. $config->remote = 'vpn.example.com 1194';
  31. // Set additional certificates of client
  32. $config->setCerts([
  33. 'ca' => '/etc/openvpn/keys/ca.crt',
  34. 'cert' => '/etc/openvpn/keys/issued/client1.crt',
  35. 'key' => '/etc/openvpn/keys/private/client1.key',
  36. ], true); // true - mean embed certificates into config, false by default
  37. // Generate config by options
  38. echo $config->generate();

Downloadable config

Just a simple usage example:

  1. header('Content-Type:text/plain');
  2. header('Content-Disposition: attachment; filename=client.ovpn');
  3. header('Pragma: no-cache');
  4. header('Expires: 0');
  5. echo $config->generate();
  6. die();

Laravel framework support

This library is optimized for usage as normal Laravel package, all functional is available via \OpenVPN facade,
for access to (for example) client object you need:

  1. // Config og client object
  2. $config = \OpenVPN::client([
  3. 'dev' => 'tun',
  4. 'proto' => 'tcp',
  5. 'resolv-retry' => 'infinite',
  6. 'cipher' => 'AES-256-CB',
  7. 'redirect-gateway' => true,
  8. 'key-direction' => 1,
  9. 'remote-cert-tls' => 'server',
  10. 'auth-user-pass' => true,
  11. 'auth-nocache' => true,
  12. 'persist-key' => true,
  13. 'persist-tun' => true,
  14. 'comp-lzo' => true,
  15. 'verb' => 3,
  16. ]);
  17. // Another way for change values
  18. $config->set('verb', 3);
  19. $config->set('nobind');
  20. // Yet another way for change values via magic methods
  21. $config->remote = 'vpn.example.com 1194';
  22. $config->httpProxy = 'proxy-http.example.com 3128';
  23. // Set multiple remote servers
  24. $config->setRemotes([
  25. 'vpn1.example.com 1194',
  26. 'vpn2.example.com 11194'
  27. ]);
  28. // Set additional certificates of client
  29. $config->setCerts([
  30. 'ca' => '/etc/openvpn/keys/ca.crt',
  31. 'cert' => '/etc/openvpn/keys/issued/client1.crt',
  32. 'key' => '/etc/openvpn/keys/private/client1.key',
  33. ], true); // true mean embed certificates into config, false by default
  34. // Generate config by options
  35. echo $config->generate();

It will read openvpn-client.php configuration from config folder (if it was published of course), then merge your parameters to this
array and in results you will see the \OpenVPN\Config object.

List of available methods

  • \OpenVPN::server(array $parameters = []) - Will return \OpenVPN\Config object with settings loaded from openvpn-server.php
  • \OpenVPN::client(array $parameters = []) - Will return \OpenVPN\Config object with settings loaded from openvpn-client.php
  • \OpenVPN::importer(string $filename = null, bool $isContent = false) - Will return \OpenVPN\Import object, with help of this object
    you may read OpenVPN configuration of your server or client
  • \OpenVPN::generator(\OpenVPN\Config $config) - Will return \OpenVPN\Generator object with ->generate() method, which may used
    for render OpenVPN configuration by parameters from Config object

Installation

The package’s service provider will automatically register its service provider.

Publish the openvpn-server.php and openvpn-client.php configuration files:

  1. php artisan vendor:publish --provider="OpenVPN\Laravel\ServiceProvider"

Testing

Before you begin need to install dev dependencies

```shell script
composer install —dev

  1. Then run tests
  2. ```shell script
  3. composer test
  4. # which same as
  5. composer test:lint
  6. composer test:unit

or

shell script ./vendor/bin/phpunit