evilfreelancer/openvpn-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

evilfreelancer/openvpn-php

Composer 安装命令:

composer require evilfreelancer/openvpn-php

包简介

OpenVPN config generator writen on PHP

README 文档

README

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

OpenVPN config manager

OpenVPN configuration manager written on PHP.

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

require_once __DIR__ . '/../vendor/autoload.php';

// Config object
$config = new \OpenVPN\Config();

// Set server options
$config->dev                  = 'tun';
$config->proto                = 'tcp';
$config->port                 = 1194;
$config->resolvRetry          = 'infinite';
$config->cipher               = 'AES-256-CBC';
$config->redirectGateway      = true;
$config->server               = '10.8.0.0 255.255.255.0';
$config->keepalive            = '10 120';
$config->renegSec             = 18000;
$config->user                 = 'nobody';
$config->group                = 'nogroup';
$config->persistKey           = true;
$config->persistTun           = true;
$config->compLzo              = true;
$config->verb                 = 3;
$config->mute                 = 20;
$config->status               = '/var/log/openvpn/status.log';
$config->logAppend            = '/var/log/openvpn/openvpn.log';
$config->clientConfigDir      = 'ccd';
$config->scriptSecurity       = 3;
$config->usernameAsCommonName = true;
$config->verifyClientCert     = 'none';

// Set routes which will be used by server after starting
$config->setRoutes([
    '10.1.1.0 255.255.255.0',
    '10.1.2.0 255.255.255.0',
    '10.1.3.0 255.255.255.0',
]);

// Set additional certificates of server
$config->setCerts([
    'ca'   => '/etc/openvpn/keys/ca.crt',
    'cert' => '/etc/openvpn/keys/issued/server.crt',
]); // You can embed certificates into config by adding true as second parameter of setCerts method

// Another way for adding certificates
$config
    ->setCert('key', '/etc/openvpn/keys/private/server.key')
    ->setCert('dh', '/etc/openvpn/keys/dh.pem');

// Set pushes which will be passed to client
$config->setPushes([
    // Additional routes, which clients will see
    'route 10.1.2.0 255.255.255.0',
    'route 10.1.3.0 255.255.255.0',
    'route 10.1.4.0 255.255.255.0',

    // Replace default gateway, all client's traffic will be routed via VPN
    'redirect-gateway def1',

    // Prepend additional DNS addresses    
    'dhcp-option DNS 8.8.8.8', 
    'dhcp-option DNS 8.8.4.4',
]);

// Generate config by options
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.

<?php
require_once __DIR__ . '/../vendor/autoload.php';

// Import OpenVPN config file
$import = new \OpenVPN\Import('server.conf');

// or (classic way)
$import = new \OpenVPN\Import();
$import->read('server.conf');

// Parse configuration and return "\OpenVPN\Config" object
$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:

<?php
require_once __DIR__ . '/../vendor/autoload.php';

// Config object
$config = new \OpenVPN\Config();

// Set client options
$config->client();
$config->dev             = 'tun';
$config->proto           = 'tcp';
$config->resolvRetry     = 'infinite';
$config->cipher          = 'AES-256-CB';
$config->redirectGateway = true;
$config->keyDirection    = 1;
$config->remoteCertTls   = 'server';
$config->authUserPass    = true;
$config->authNocache     = true;
$config->nobind          = true;
$config->persistKey      = true;
$config->persistTun      = true;
$config->compLzo         = true;
$config->verb            = 3;
$config->httpProxy       = 'proxy-http.example.com 3128';

// Set multiple remote servers
$config->setRemotes([
    'vpn1.example.com 1194',
    'vpn2.example.com 11194'
]);

// Set single remote
$config->setRemote('vpn1.example.com 1194');

// Or set remote server as parameter of object
$config->remote = 'vpn.example.com 1194';

// Set additional certificates of client
$config->setCerts([
    'ca'   => '/etc/openvpn/keys/ca.crt',
    'cert' => '/etc/openvpn/keys/issued/client1.crt',
    'key'  => '/etc/openvpn/keys/private/client1.key',
], true); // true - mean embed certificates into config, false by default

// Generate config by options
echo $config->generate();

Downloadable config

Just a simple usage example:

header('Content-Type:text/plain');
header('Content-Disposition: attachment; filename=client.ovpn');
header('Pragma: no-cache');
header('Expires: 0');

echo $config->generate();
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:

// Config og client object
$config = \OpenVPN::client([
    'dev'              => 'tun',
    'proto'            => 'tcp',
    'resolv-retry'     => 'infinite',
    'cipher'           => 'AES-256-CB',
    'redirect-gateway' => true,
    'key-direction'    => 1,
    'remote-cert-tls'  => 'server',
    'auth-user-pass'   => true,
    'auth-nocache'     => true,
    'persist-key'      => true,
    'persist-tun'      => true,
    'comp-lzo'         => true,
    'verb'             => 3,
]);

// Another way for change values
$config->set('verb', 3);
$config->set('nobind');

// Yet another way for change values via magic methods
$config->remote    = 'vpn.example.com 1194';
$config->httpProxy = 'proxy-http.example.com 3128';

// Set multiple remote servers
$config->setRemotes([
    'vpn1.example.com 1194',
    'vpn2.example.com 11194'
]);

// Set additional certificates of client
$config->setCerts([
    'ca'   => '/etc/openvpn/keys/ca.crt',
    'cert' => '/etc/openvpn/keys/issued/client1.crt',
    'key'  => '/etc/openvpn/keys/private/client1.key',
], true); // true mean embed certificates into config, false by default

// Generate config by options
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:

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

Testing

Before you begin need to install dev dependencies

composer install --dev

Then run tests

composer test

# which same as
composer test:lint
composer test:unit

or

./vendor/bin/phpunit

Links

evilfreelancer/openvpn-php 适用场景与选型建议

evilfreelancer/openvpn-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.94k 次下载、GitHub Stars 达 30, 最近一次更新时间为 2018 年 01 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「package」 「plugin」 「import」 「config」 「laravel」 「generate」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 evilfreelancer/openvpn-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 evilfreelancer/openvpn-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 evilfreelancer/openvpn-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4.94k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 31
  • 点击次数: 5
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 30
  • Watchers: 6
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-01-31